Example #1
0
// TestDeleteRegex validates the removal of a regex from the database.
func TestDeleteRegex(t *testing.T) {
	const fixture = "basic.json"
	rgx1, db := setup(t, fixture)
	defer teardown(t, db)

	rgxName := prefix + "_basic"
	rgxBadName := prefix + "_basic_advice"

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

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

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

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

	rgxName := prefix + "_unknown"

	t.Log("Given the need to validate failure of API with bad session.")
	{
		t.Log("When giving a nil session")
		{
			err := regex.Upsert(tests.Context, nil, rgx1)
			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 = regex.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 = regex.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 = regex.GetByName(tests.Context, nil, rgxName)
			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 = regex.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 = regex.GetLastHistoryByName(tests.Context, nil, rgxName)
			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 = regex.Delete(tests.Context, nil, rgxName)
			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)
		}
	}
}
Example #3
0
// Delete removes the specified Regex from the system.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (regexHandle) Delete(c *web.Context) error {
	if err := regex.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["name"]); err != nil {
		if err == regex.ErrNotFound {
			err = web.ErrNotFound
		}
		return err
	}

	c.Respond(nil, http.StatusNoContent)
	return nil
}