Ejemplo n.º 1
0
// TestUpsertCreateScript tests if we can create a script record in the db.
func TestUpsertCreateScript(t *testing.T) {
	const fixture = "basic.json"
	scr1, db := setup(t, fixture)
	defer teardown(t, db)

	t.Log("Given the need to save a script into the database.")
	{
		t.Log("\tWhen using script", scr1)
		{
			if err := script.Upsert(tests.Context, db, scr1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a script : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a script.", tests.Success)

			if _, err := script.GetLastHistoryByName(tests.Context, db, scr1.Name); err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the script from history: %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the script from history.", tests.Success)

			scr2, err := script.GetByName(tests.Context, db, scr1.Name)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the script : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the script.", tests.Success)

			if !reflect.DeepEqual(scr1, scr2) {
				t.Logf("\t%+v", scr1)
				t.Logf("\t%+v", scr2)
				t.Errorf("\t%s\tShould be able to get back the same script values.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be able to get back the same script values.", tests.Success)
			}
		}
	}
}
Ejemplo n.º 2
0
// TestDeleteScript validates the removal of a script from the database.
func TestDeleteScript(t *testing.T) {
	const fixture = "basic.json"
	scr1, db := setup(t, fixture)
	defer teardown(t, db)

	scrName := prefix + "_basic"
	scrBadName := prefix + "_basic_advice"

	t.Log("Given the need to delete a script in the database.")
	{
		t.Log("\tWhen using script", scr1)
		{
			if err := script.Upsert(tests.Context, db, scr1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a script : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a script.", tests.Success)

			if err := script.Delete(tests.Context, db, scrName); err != nil {
				t.Fatalf("\t%s\tShould be able to delete a script using its name[%s]: %s", tests.Failed, scrName, err)
			}
			t.Logf("\t%s\tShould be able to delete a script using its name[%s]:", tests.Success, scrName)

			if err := script.Delete(tests.Context, db, scrBadName); err == nil {
				t.Fatalf("\t%s\tShould not be able to delete a script using wrong name name[%s]", tests.Failed, scrBadName)
			}
			t.Logf("\t%s\tShould not be able to delete a script using wrong name name[%s]", tests.Success, scrBadName)

			if _, err := script.GetByName(tests.Context, db, scrName); err == nil {
				t.Fatalf("\t%s\tShould be able to validate script with Name[%s] does not exists: %s", tests.Failed, scrName, errors.New("Record Exists"))
			}
			t.Logf("\t%s\tShould be able to validate script  with Name[%s] does not exists:", tests.Success, scrName)
		}
	}
}
Ejemplo n.º 3
0
// TestAPIFailureScripts validates the failure of the api using a nil session.
func TestAPIFailureScripts(t *testing.T) {
	const fixture = "basic.json"
	scr1, db := setup(t, fixture)
	defer teardown(t, db)

	scrName := prefix + "_unknown"

	t.Log("Given the need to validate failure of API with bad session.")
	{
		t.Log("When giving a nil session")
		{
			err := script.Upsert(tests.Context, nil, scr1)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused create by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused create by api with bad session: %s", tests.Success, err)

			_, err = script.GetNames(tests.Context, nil)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err)

			_, err = script.GetAll(tests.Context, nil, nil)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err)

			_, err = script.GetByName(tests.Context, nil, scrName)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err)

			_, err = script.GetByNames(tests.Context, nil, nil)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err)

			_, err = script.GetLastHistoryByName(tests.Context, nil, scrName)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err)

			err = script.Delete(tests.Context, nil, scrName)
			if err == nil {
				t.Fatalf("\t%s\tShould be refused delete by api with bad session", tests.Failed)
			}
			t.Logf("\t%s\tShould be refused delete by api with bad session: %s", tests.Success, err)
		}
	}
}
Ejemplo n.º 4
0
// TestUpsertUpdateScript validates update operation of a given Script.
func TestUpsertUpdateScript(t *testing.T) {
	const fixture = "basic.json"
	scr1, db := setup(t, fixture)
	defer teardown(t, db)

	t.Log("Given the need to update a script into the database.")
	{
		t.Log("\tWhen using two scripts")
		{
			if err := script.Upsert(tests.Context, db, scr1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a script : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a script.", tests.Success)

			scr2 := scr1
			scr2.Commands = append(scr2.Commands, map[string]interface{}{"command": 4})

			if err := script.Upsert(tests.Context, db, scr2); err != nil {
				t.Fatalf("\t%s\tShould be able to update a script record: %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to update a script record.", tests.Success)

			if _, err := script.GetLastHistoryByName(tests.Context, db, scr1.Name); err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the script from history: %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the script from history.", tests.Success)

			updScr, err := script.GetByName(tests.Context, db, scr2.Name)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve a script record: %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve a script record.", tests.Success)

			if updScr.Name != scr1.Name {
				t.Errorf("\t%s\tShould be able to get back the same script name.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be able to get back the same script name.", tests.Success)
			}

			if lendiff := len(updScr.Commands) - len(scr1.Commands); lendiff != 1 {
				t.Errorf("\t%s\tShould have one more parameter in script record: %d", tests.Failed, lendiff)
			} else {
				t.Logf("\t%s\tShould have one more parameter in script record.", tests.Success)
			}

			if !reflect.DeepEqual(scr2.Commands[0], updScr.Commands[0]) {
				t.Logf("\t%+v", scr2.Commands[0])
				t.Logf("\t%+v", updScr.Commands[0])
				t.Errorf("\t%s\tShould be abe to validate the script param values in db.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be abe to validate the script param values in db.", tests.Success)
			}
		}
	}
}
Ejemplo n.º 5
0
// Retrieve returns the specified script from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (scriptHandle) Retrieve(c *web.Context) error {
	scr, err := script.GetByName(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["name"])
	if err != nil {
		if err == script.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(scr, http.StatusOK)
	return nil
}