Beispiel #1
0
// 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
}
Beispiel #2
0
// processMasks reviews the document for fields that are defined to have
// their values masked.
func processMasks(context interface{}, db *db.DB, collection string, results []bson.M) error {
	masks, err := mask.GetByCollection(context, db, collection)
	if err != nil {

		// If there are no masks to process then great.
		return nil
	}

	for _, doc := range results {
		if err := matchMaskField(context, masks, doc); err != nil {
			return err
		}
	}

	return nil
}
Beispiel #3
0
// 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)
		}
	}
}
Beispiel #4
0
// 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)
		}
	}
}