Example #1
0
// AddFlag adds a new flag to a given FormSubmission based on the provided route
// params.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formSubmissionHandle) AddFlag(c *web.Context) error {
	id := c.Params["id"]
	flag := c.Params["flag"]

	s, err := submission.AddFlag(c.SessionID, c.Ctx["DB"].(*db.DB), id, flag)
	if err != nil {
		return err
	}

	c.Respond(s, http.StatusOK)

	return nil
}
Example #2
0
func Test_Flags(t *testing.T) {
	subs, db := setup(t, "submission")
	defer teardown(t, db)

	t.Log("Given the need to add and remove flags.")
	{
		t.Log("\tWhen starting from an empty submissions collection")
		{

			//----------------------------------------------------------------------
			// Create the submission.

			if err := submission.Create(tests.Context, db, subs[0].FormID.Hex(), &subs[0]); err != nil {
				t.Fatalf("\t%s\tShould be able to create a submission : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a submission.", tests.Success)

			//----------------------------------------------------------------------
			// Ensure the submission has been added to the database.

			sub, err := submission.Retrieve(tests.Context, db, subs[0].ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the submission : %s", tests.Failed, err.Error())
			}
			t.Logf("\t%s\tShould be able to retrieve the submission", tests.Success)

			newFlag := time.Now().String()

			//----------------------------------------------------------------------
			// Ensure that the new flag is not already on the submission.

			// Ensure that the new flag is not in the current flags.
			for _, flag := range sub.Flags {
				if flag == newFlag {
					t.Fatalf("\t%s\tShould not have test flag already : Flag already exists", tests.Failed)
				}
			}
			t.Logf("\t%s\tShould not have test flag already.", tests.Success)

			//----------------------------------------------------------------------
			// Add the flag to the submission.

			nsub, err := submission.AddFlag(tests.Context, db, sub.ID.Hex(), newFlag)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to add the flag : %s", tests.Failed, err.Error())
			}
			t.Logf("\t%s\tShould be able to add the flag", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the new flag was added to the submission returned.

			var found bool
			for _, flag := range nsub.Flags {
				if flag == newFlag {
					found = true
					break
				}
			}
			if !found {
				t.Fatalf("\t%s\tShould have test flag on returned object : Flag does not exist", tests.Failed)
			}
			t.Logf("\t%s\tShould have test flag on returned object.", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the new flag was added to the store's submission.

			rsub, err := submission.Retrieve(tests.Context, db, sub.ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the submission : %s", tests.Failed, err.Error())
			}
			t.Logf("\t%s\tShould be able to retrieve the submission", tests.Success)

			found = false
			for _, flag := range rsub.Flags {
				if flag == newFlag {
					found = true
					break
				}
			}
			if !found {
				t.Fatalf("\t%s\tShould have test flag on database object : Flag does not exist", tests.Failed)
			}
			t.Logf("\t%s\tShould have test flag on database object.", tests.Success)

			//----------------------------------------------------------------------
			// Remove the new flag from the submission.

			nsub, err = submission.RemoveFlag(tests.Context, db, sub.ID.Hex(), newFlag)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to remove the flag : %s", tests.Failed, err.Error())
			}
			t.Logf("\t%s\tShould be able to remove the flag", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the new flag was removed from the submission returned.

			found = false
			for _, flag := range nsub.Flags {
				if flag == newFlag {
					found = true
					break
				}
			}
			if found {
				t.Fatalf("\t%s\tShould not have test flag on returned object : Flag found", tests.Failed)
			}
			t.Logf("\t%s\tShould not have test flag on returned object.", tests.Success)

			//----------------------------------------------------------------------
			// Ensure that the new flag was removed from the submission in the store.

			rsub, err = submission.Retrieve(tests.Context, db, sub.ID.Hex())
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the submission : %s", tests.Failed, err.Error())
			}
			t.Logf("\t%s\tShould be able to retrieve the submission", tests.Success)

			found = false
			for _, flag := range rsub.Flags {
				if flag == newFlag {
					found = true
					break
				}
			}
			if found {
				t.Fatalf("\t%s\tShould not have test flag on database object : Flag found", tests.Failed)
			}
			t.Logf("\t%s\tShould not have test flag on database object.", tests.Success)
		}
	}
}