// TestUpsertCreateMask tests if we can create a query mask record in the db. func TestUpsertCreateMask(t *testing.T) { const fixture = "basic.json" masks, db := setup(t, fixture) defer teardown(t, db) t.Log("Given the need to save a query mask into the database.") { t.Log("\tWhen using fixture", fixture) { if err := mask.Upsert(tests.Context, db, masks[0]); 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) 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 query mask from history: %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the query mask from history.", tests.Success) msk, err := mask.GetByName(tests.Context, db, masks[0].Collection, masks[0].Field) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the query mask : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the query mask.", tests.Success) if !reflect.DeepEqual(masks[0], msk) { t.Logf("\t%+v", masks[0]) t.Logf("\t%+v", msk) t.Errorf("\t%s\tShould be able to get back the same query mask values.", tests.Failed) } else { t.Logf("\t%s\tShould be able to get back the same query 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) } } }
// Retrieve returns the specified mask from the system. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (maskHandle) Retrieve(c *web.Context) error { collection := c.Params["collection"] field := c.Params["field"] if collection == "" { collection = "*" } if field == "" { masks, err := mask.GetByCollection(c.SessionID, c.Ctx["DB"].(*db.DB), collection) if err != nil { if err == mask.ErrNotFound { err = web.ErrNotFound } return err } c.Respond(masks, http.StatusOK) return nil } msk, err := mask.GetByName(c.SessionID, c.Ctx["DB"].(*db.DB), collection, field) if err != nil { if err == mask.ErrNotFound { err = web.ErrNotFound } return err } c.Respond(msk, http.StatusOK) 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) } } }
// 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) } } } }