// TestGetLastMaskHistoryByName validates retrieval of Mask from the history // collection. func TestGetLastMaskHistoryByName(t *testing.T) { const fixture = "basic.json" masks, db := setup(t, fixture) defer teardown(t, db) t.Log("Given the need to retrieve a mask from history.") { 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.Upsert(tests.Context, db, masks[1]); 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) msk, err := mask.GetLastHistoryByName(tests.Context, db, masks[1].Collection, masks[1].Field) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the last mask from history : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the last mask from history.", tests.Success) if !reflect.DeepEqual(masks[1], msk) { t.Logf("\t%+v", masks[1]) t.Logf("\t%+v", msk) t.Errorf("\t%s\tShould be able to get back the same mask values.", tests.Failed) } else { t.Logf("\t%s\tShould be able to get back the same mask values.", tests.Success) } } } }
// 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) } } }
// TestUpsertUpdateMask validates update operation of a given mask. func TestUpsertUpdateMask(t *testing.T) { const fixture = "basic.json" masks, db := setup(t, fixture) defer teardown(t, db) t.Log("Given the need to update a mask into the database.") { t.Log("\tWhen using two masks") { 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) masks[0].Type = mask.MaskAll if err := mask.Upsert(tests.Context, db, masks[0]); err != nil { t.Fatalf("\t%s\tShould be able to update a mask record: %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to update a mask record.", tests.Success) if _, err := mask.GetLastHistoryByName(tests.Context, db, masks[0].Collection, masks[0].Field); err != nil { t.Fatalf("\t%s\tShould be able to retrieve the mask from history: %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the mask from history.", tests.Success) updMsk, err := mask.GetByName(tests.Context, db, masks[0].Collection, masks[0].Field) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve a mask record: %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve a mask record.", tests.Success) if updMsk.Type != masks[0].Type { t.Logf("\t%+v", updMsk.Type) t.Logf("\t%+v", masks[0].Type) t.Errorf("\t%s\tShould have an updated mask record.", tests.Failed) } else { t.Logf("\t%s\tShould have an updated mask record.", tests.Success) } } } }
// Upsert inserts or updates the posted mask document into the database. // 204 SuccessNoContent, 400 Bad Request, 404 Not Found, 500 Internal func (maskHandle) Upsert(c *web.Context) error { var msk mask.Mask if err := json.NewDecoder(c.Request.Body).Decode(&msk); err != nil { return err } if err := mask.Upsert(c.SessionID, c.Ctx["DB"].(*db.DB), msk); err != nil { 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) } } }
// TestGetMaskByCollection validates retrieval of all query mask records by collection. func TestGetMaskByCollection(t *testing.T) { const fixture = "basic.json" masks, db := setup(t, fixture) defer teardown(t, db) t.Log("Given the need to retrieve a list of query masks by collection.") { t.Log("\tWhen using fixture", fixture) { for _, msk := range masks { if err := mask.Upsert(tests.Context, db, msk); err != nil { t.Fatalf("\t%s\tShould be able to create a query mask : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a query mask.", tests.Success) } msks, err := mask.GetByCollection(tests.Context, db, masks[0].Collection) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the query masks : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the query masks", tests.Success) var count int for _, msk := range msks { if msk.Collection == collection || (msk.Collection == "*" && msk.Field == "test") { count++ } } if count != len(masks) { t.Fatalf("\t%s\tShould have two query masks : %d", tests.Failed, count) } t.Logf("\t%s\tShould have two query masks.", tests.Success) } } }