// TestDeleteMask validates the removal of a mask from the database. func TestDeleteMask(t *testing.T) { const fixture = "basic.json" masks, db := setup(t, fixture) defer teardown(t, db) t.Log("Given the need to delete a mask in the database.") { t.Log("\tWhen using mask", masks[0]) { if err := mask.Upsert(tests.Context, db, masks[0]); err != nil { t.Fatalf("\t%s\tShould be able to create a mask : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a mask.", tests.Success) if err := mask.Delete(tests.Context, db, masks[0].Collection, masks[0].Field); err != nil { t.Fatalf("\t%s\tShould be able to delete a mask : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to delete a mask.", tests.Success) if err := mask.Delete(tests.Context, db, "collection", "field"); err == nil { t.Fatalf("\t%s\tShould not be able to delete a mask that does not exist.", tests.Failed) } t.Logf("\t%s\tShould not be able to delete a mask that does not exist.", tests.Success) if _, err := mask.GetByName(tests.Context, db, masks[0].Collection, masks[0].Field); err == nil { t.Fatalf("\t%s\tShould be able to validate mask does not exists: %s", tests.Failed, errors.New("Record Exists")) } t.Logf("\t%s\tShould be able to validate mask does not exists.", tests.Success) } } }
// Delete removes the specified mask from the system. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (maskHandle) Delete(c *web.Context) error { if err := mask.Delete(c.SessionID, c.Ctx["DB"].(*db.DB), c.Params["collection"], c.Params["field"]); err != nil { if err == mask.ErrNotFound { err = web.ErrNotFound } return err } c.Respond(nil, http.StatusNoContent) return nil }
// TestAPIFailureMasks validates the failure of the api using a nil session. func TestAPIFailureMasks(t *testing.T) { const fixture = "basic.json" masks, db := setup(t, fixture) defer teardown(t, db) t.Log("Given the need to validate failure of API with bad session.") { t.Log("When giving a nil session") { err := mask.Upsert(tests.Context, nil, masks[0]) 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 = mask.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 = mask.GetByCollection(tests.Context, nil, masks[0].Collection) 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 = mask.GetByName(tests.Context, nil, masks[0].Collection, masks[0].Field) 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 = mask.GetLastHistoryByName(tests.Context, nil, masks[0].Collection, masks[0].Field) 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 = mask.Delete(tests.Context, nil, masks[0].Collection, masks[0].Field) 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) } } }