Пример #1
0
// TestDigest tests the returning a form's digest
func TestDigest(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need aggregate across a form's submissions.")
	{
		url := "/v1/form/580627b42600e2035218509f/digest"
		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling aggregate endpoint: %s", url)
		{
			t.Log("\tWhen we user version v1 of the aggregate endpoint.")
			if w.Code != 200 {
				t.Fatalf("\t%s\tShould be able to get the aggregation keys : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to get the aggregation keys .", tests.Success)

			var fm handlers.FormDigest
			if err := json.Unmarshal(w.Body.Bytes(), &fm); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)

			if len(fm.Questions) != 5 {
				t.Fatalf("\t%s\tShould be able to return 1 question instead of %v.", tests.Failed, len(fm.Questions))
			}
			t.Logf("\t%s\tShould be able to return 1 question.", tests.Success)
		}
	}
}
Пример #2
0
func TestAggregateGroupSubmission(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need aggregate across a form's submissions.")
	{
		url := "/v1/form/580627b42600e2035218509f/aggregate/d452b94d-e650-41c6-80af-c56091315c90/submission"
		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling aggregate endpoint: %s", url)
		{
			t.Log("\tWhen we user version v1 of the aggregate endpoint.")
			if w.Code != 200 {
				t.Fatalf("\t%s\tShould be able to get the aggregation keys : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to get the aggregation keys .", tests.Success)

			var ta []form.TextAggregation
			if err := json.Unmarshal(w.Body.Bytes(), &ta); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)
		}
	}
}
Пример #3
0
// setupGraph initializes an in-memory Cayley graph and logging for an individual test.
func setupGraph(t *testing.T) (*db.DB, *cayley.Handle, []map[string]interface{}) {
	tests.ResetLog()

	items, _, _, _, err := wirefix.Get()
	if err != nil {
		t.Fatalf("%s\tShould load item records from the fixture file : %v", tests.Failed, err)
	}
	t.Logf("%s\tShould load item records from the fixture file.", tests.Success)

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}

	store, err := cayley.NewMemoryGraph()
	if err != nil {
		t.Fatalf("\t%s\tShould be able to create a new Cayley graph : %v", tests.Failed, err)
	}
	t.Logf("\t%s\tShould be able to create a new Cayley graph.", tests.Success)

	// Convert the items to maps.
	var itemMaps []map[string]interface{}
	for _, itm := range items {
		itemMap := map[string]interface{}{
			"type":    itm.Type,
			"item_id": itm.ID,
			"version": itm.Version,
			"data":    itm.Data,
		}
		itemMaps = append(itemMaps, itemMap)
	}

	return db, store, itemMaps
}
Пример #4
0
// TestMaskByName tests the retrieval of a specific mask.
func TestMaskByName(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to get a specific mask.")
	{
		url := "/v1/mask/" + mCollection + "/observation_time"
		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			if w.Code != 200 {
				t.Fatalf("\t%s\tShould be able to retrieve the mask : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the mask.", tests.Success)

			var msk mask.Mask
			if err := json.Unmarshal(w.Body.Bytes(), &msk); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)

			if msk.Collection != mCollection && msk.Field != "observation_time" {
				t.Fatalf("\t%s\tShould have the correct mask : %s - %s", tests.Failed, msk.Collection, msk.Field)
			}
			t.Logf("\t%s\tShould have the correct mask.", tests.Success)
		}
	}
}
Пример #5
0
func TestAggregateGroup(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need aggregate across a form's submissions.")
	{
		url := "/v1/form/580627b42600e2035218509f/aggregate/all"
		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling aggregate endpoint: %s", url)
		{
			t.Log("\tWhen we user version v1 of the aggregate endpoint.")
			if w.Code != 200 {
				t.Fatalf("\t%s\tShould be able to get the aggregation keys : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to get the aggregation keys .", tests.Success)

			var ag form.Aggregation
			if err := json.Unmarshal(w.Body.Bytes(), &ag); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)

			if ag.Count != 9 {
				t.Fatalf("\t%s\tShould have only one aggregation instead of %v.", tests.Failed, ag.Count)
			}
			t.Logf("\t%s\tShould have only one aggregation.", tests.Success)
		}
	}
}
Пример #6
0
// TestInvalidWebTokens tests create an invalid web token and tests it fails.
func TestInvalidWebTokens(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	tokens := []string{
		"",
		"6dcda2da-92c3-11e5-8994-feff819cdc9f",
		"OGY4OGI3YWQtZjc5Ny00ODI1LWI0MmUtMjIwZTY5ZDQxYjMzOmFKT2U1b0pFZlZ4cWUrR0JONEl0WlhmQTY0K3JsN2VGcmM2MVNQMkV1WVE9",
	}

	t.Log("Given the need to validate bad web tokens don't validate.")
	{
		for _, token := range tokens {
			t.Logf("\tWhen using token [%s]", token)
			{
				if _, err := auth.ValidateWebToken(tests.Context, db, token); err == nil {
					t.Errorf("\t%s\tShould Not be able to validate the web token : %v", tests.Failed, err)
				} else {
					t.Logf("\t%s\tShould Not be able to validate the web token.", tests.Success)
				}
			}
		}
	}
}
Пример #7
0
// Test udp.Addr works correctly.
func TestUDPAddr(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to listen on any port and know that bound UDP address.")
	{
		// Create a configuration.
		cfg := udp.Config{
			NetType: "udp4",
			Addr:    ":0", // Defer port assignment to OS.

			ConnHandler: udpConnHandler{},
			ReqHandler:  udpReqHandler{},
			RespHandler: udpRespHandler{},

			OptIntPool: udp.OptIntPool{
				RecvMinPoolSize: func() int { return 2 },
				RecvMaxPoolSize: func() int { return 1000 },
				SendMinPoolSize: func() int { return 2 },
				SendMaxPoolSize: func() int { return 1000 },
			},
		}

		// Create a new UDP value.
		u, err := udp.New("TEST", "TEST", cfg)
		if err != nil {
			t.Fatal("\tShould be able to create a new UDP listener.", tests.Failed, err)
		}
		t.Log("\tShould be able to create a new UDP listener.", tests.Success)

		// Addr should be nil before start.
		if addr := u.Addr(); addr != nil {
			t.Fatalf("\tAddr() should be nil before Start; Addr() = %q. %s", addr, tests.Failed)
		}
		t.Log("\tAddr() should be nil before Start.", tests.Success)

		// Start accepting client data.
		if err := u.Start("TEST"); err != nil {
			t.Fatal("\tShould be able to start the UDP listener.", tests.Failed, err)
		}
		defer u.Stop("TEST")

		// Addr should be non-nil after Start.
		addr := u.Addr()
		if addr == nil {
			t.Fatal("\tAddr() should be not be nil after Start.", tests.Failed)
		}
		t.Log("\tAddr() should be not be nil after Start.", tests.Success)

		// The OS should assign a random open port, which shouldn't be 0.
		_, port, err := net.SplitHostPort(addr.String())
		if err != nil {
			t.Fatalf("\tSplitHostPort should not fail. tests.Failed %v. %s", err, tests.Failed)
		}
		if port == "0" {
			t.Fatalf("\tAddr port should not be %q. %s", port, tests.Failed)
		}
		t.Logf("\tAddr() should be not be 0 after Start (port = %q). %s", port, tests.Success)
	}
}
Пример #8
0
// ExampleNew provides a basic example for using a pool.
func ExampleNew() {
	tests.ResetLog()
	defer tests.DisplayLog()

	// Create a configuration.
	cfg := pool.Config{
		MinRoutines: func() int { return 3 },
		MaxRoutines: func() int { return 4 },
	}

	// Create a new pool.
	p, err := pool.New("TEST", "TheWork", cfg)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Pass in some work to be performed.
	p.Do("TEST", &theWork{})
	p.Do("TEST", &theWork{})
	p.Do("TEST", &theWork{})

	// Wait to the work to be processed.
	time.Sleep(100 * time.Millisecond)

	// Shutdown the pool.
	p.Shutdown("TEST")
}
Пример #9
0
// TestActionsDELETE sample test for the DELETE call.
func TestActionsDELETE(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to test DELETE action call.")
	{
		action := "flagged_by"
		userID := "ITEST_a63af637-58af-472b-98c7-f5c00743bac6"
		itemID := "ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82"

		url := fmt.Sprintf("/v1/action/%s/user/%s/on/item/%s", action, userID, itemID)
		r := httptest.NewRequest("DELETE", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			t.Log("\tWhen we use version v1 of the actions endpoint.")

			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to remove the action : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to remove the action.", tests.Success)
		}
	}
}
Пример #10
0
// TestExec tests the execution of a specific query.
func TestExec(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to execute a specific query.")
	{
		url := "/v1/exec/" + qPrefix + "_basic?station_id=42021"
		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to retrieve the query : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the query.", tests.Success)

			recv := tests.IndentJSON(w.Body.String())
			resp := tests.IndentJSON(`{"results":[{"Name":"Basic","Docs":[{"name":"C14 - Pasco County Buoy, FL"}]}]}`)

			if resp != recv {
				t.Log(resp)
				t.Log(recv)
				t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
			}
			t.Logf("\t%s\tShould get the expected result.", tests.Success)
		}
	}
}
Пример #11
0
// TestPool tests the pool is functional.
func TestPool(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to validate the work pool functions.")
	{
		cfg := pool.Config{
			MinRoutines: func() int { return 100 },
			MaxRoutines: func() int { return 5000 },
		}

		p, err := pool.New("TestPool", "Pool1", cfg)
		if err != nil {
			t.Fatal("\tShould not get error creating pool.", tests.Failed, err)
		}
		t.Log("\tShould not get error creating pool.", tests.Success)

		for i := 0; i < 100; i++ {
			p.Do("TestPool", &theWork{privateID: i})
		}

		time.Sleep(100 * time.Millisecond)

		p.Shutdown("TestPool")
	}
}
Пример #12
0
// TestLoginUser validates a user can login and not after changes.
func TestLoginUser(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	var publicID string
	defer func() {
		if err := removeUser(db, publicID); err != nil {
			t.Fatalf("\t%s\tShould be able to remove the test user : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove the test user.", tests.Success)
	}()

	t.Log("Given the need to test user login.")
	{
		t.Log("\tWhen using a new user")
		{
			u1, err := auth.NewUser(auth.NUser{
				Status:   auth.StatusActive,
				FullName: "Test Kennedy",
				Email:    "*****@*****.**",
				Password: "******",
			})
			if err != nil {
				t.Fatalf("\t%s\tShould be able to build a new user : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to build a new user.", tests.Success)

			if err := auth.CreateUser(tests.Context, db, u1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a user : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a user.", tests.Success)

			// We need to do this so we can clean up after.
			publicID = u1.PublicID

			if _, err := auth.LoginUser(tests.Context, db, u1.Email, "_Password124"); err != nil {
				t.Errorf("\t%s\tShould be able to login the user : %v", tests.Failed, err)
			} else {
				t.Logf("\t%s\tShould be able to login the user.", tests.Success)
			}

			if err := auth.UpdateUserPassword(tests.Context, db, u1, "password890"); err != nil {
				t.Fatalf("\t%s\tShould be able to update the user password : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to update the user password.", tests.Success)

			if _, err := auth.LoginUser(tests.Context, db, u1.Email, "_Password124"); err == nil {
				t.Errorf("\t%s\tShould Not be able to login the user.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to login the user.", tests.Success)
			}
		}
	}
}
Пример #13
0
// TestNoSession tests when a nil session is used.
func TestNoSession(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to test calls with a bad session.")
	{
		t.Log("\tWhen using a nil session")
		{
			if _, err := session.Create(tests.Context, nil, publicID, 10*time.Second); err == nil {
				t.Errorf("\t%s\tShould Not be able to create a session.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to create a session.", tests.Success)
			}

			if _, err := session.GetBySessionID(tests.Context, nil, "NOT EXISTS"); err == nil {
				t.Errorf("\t%s\tShould Not be able to retrieve the session.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to retrieve the session.", tests.Success)
			}

			if _, err := session.GetByLatest(tests.Context, nil, publicID); err == nil {
				t.Errorf("\t%s\tShould Not be able to retrieve the session.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to retrieve the session.", tests.Success)
			}
		}
	}
}
Пример #14
0
// TestRetrievePattern tests the retrieval of a specific pattern.
func TestRetrievePattern(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to get a specific pattern.")
	{
		url := "/v1/pattern/" + patternPrefix + "comment"
		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			if w.Code != 200 {
				t.Fatalf("\t%s\tShould be able to retrieve the pattern : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the pattern.", tests.Success)

			var p pattern.Pattern
			if err := json.Unmarshal(w.Body.Bytes(), &p); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)

			if p.Type != patternPrefix+"comment" {
				t.Fatalf("\t%s\tShould have the correct pattern : %s", tests.Failed, p.Type)
			}
			t.Logf("\t%s\tShould have the correct pattern.", tests.Success)
		}
	}
}
Пример #15
0
// TestSignaled tests when jobs is requested to shutdown.
func TestSignaled(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to test a task that is requested to shutdown.")
	{
		t.Log("\tWhen using a task that should see the signal.")
		{
			var job task
			job.KillAfter(100 * time.Millisecond)

			// Need the job method to quit as soon as we are done.
			defer job.Kill()

			go func() {
				time.Sleep(50 * time.Millisecond)
				syscall.Kill(syscall.Getpid(), syscall.SIGINT)
			}()

			if err := runner.Run(tests.Context, 3*time.Second, &job); err != runner.ErrSignaled {
				t.Fatalf("\t%s\tShould receive a signaled error : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould receive a signaled error.", tests.Success)
		}
	}
}
Пример #16
0
// TestScriptByName tests the retrieval of a specific script.
func TestScriptByName(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to get a specific script.")
	{
		url := "/v1/script/" + sPrefix + "_basic_script_pre"
		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			if w.Code != 200 {
				t.Fatalf("\t%s\tShould be able to retrieve the script : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the script.", tests.Success)

			var scr script.Script
			if err := json.Unmarshal(w.Body.Bytes(), &scr); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)

			if scr.Name != sPrefix+"_basic_script_pre" {
				t.Fatalf("\t%s\tShould have the correct script : %s", tests.Failed, scr.Name)
			}
			t.Logf("\t%s\tShould have the correct script.", tests.Success)
		}
	}
}
Пример #17
0
// TestRetrieveRelationship tests the retrieval of a specific relationship.
func TestRetrieveRelationship(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to get a specific relationship.")
	{
		url := "/v1/relationship/" + relPrefix + "authored"
		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			if w.Code != 200 {
				t.Fatalf("\t%s\tShould be able to retrieve the relationship : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the relationship.", tests.Success)

			var rel relationship.Relationship
			if err := json.Unmarshal(w.Body.Bytes(), &rel); err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)

			if rel.Predicate != relPrefix+"authored" {
				t.Fatalf("\t%s\tShould have the correct relationship : %s", tests.Failed, rel.Predicate)
			}
			t.Logf("\t%s\tShould have the correct relationship.", tests.Success)
		}
	}
}
Пример #18
0
// TestNamedOnView tests the execution of a specific named query on a view.
func TestNamedOnView(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to execute a specific query.")
	{
		url := "/v1/exec/" + qPrefix + "_basic_view/view/VTEST_thread/ITEST_c1b2bbfe-af9f-4903-8777-bd47c4d5b20a?item_of_interest=ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82"

		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to retrieve the query : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the query.", tests.Success)

			recv := tests.IndentJSON(w.Body.String())
			resp := tests.IndentJSON(`{"results":[{"Name":"BasicView","Docs":[{"item_id":"ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82"}]}]}`)

			if resp != recv {
				t.Log(resp)
				t.Log(recv)
				t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
			}
			t.Logf("\t%s\tShould get the expected result.", tests.Success)
		}
	}
}
Пример #19
0
// TestIsExpired tests that we can properly identify when a
// session is expired or not.
func TestIsExpired(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to validate a session has expired.")
	{
		s := session.Session{
			DateExpires: time.Now().Add(-time.Hour),
		}

		t.Log("\tWhen using an expired session.")
		{
			if !s.IsExpired(tests.Context) {
				t.Fatalf("\t%s\tShould be expired.", tests.Failed)
			}
			t.Logf("\t%s\tShould be expired", tests.Success)
		}

		s = session.Session{
			DateExpires: time.Now().Add(time.Hour),
		}

		t.Log("\tWhen using an valid session")
		{
			if s.IsExpired(tests.Context) {
				t.Fatalf("\t%s\tShould Not be expired.", tests.Failed)
			}
			t.Logf("\t%s\tShould Not be expired", tests.Success)
		}
	}
}
Пример #20
0
// TestDisableUser test the disabling of a user.
func TestDisableUser(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	var publicID string
	defer func() {
		if err := removeUser(db, publicID); err != nil {
			t.Fatalf("\t%s\tShould be able to remove the test user : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove the test user.", tests.Success)
	}()

	t.Log("Given the need to update a user.")
	{
		t.Log("\tWhen using an existing user.")
		{
			u1, err := auth.NewUser(auth.NUser{
				Status:   auth.StatusActive,
				FullName: "Test Kennedy",
				Email:    "*****@*****.**",
				Password: "******",
			})
			if err != nil {
				t.Fatalf("\t%s\tShould be able to build a new user : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to build a new user.", tests.Success)

			if err := auth.CreateUser(tests.Context, db, u1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a user : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a user.", tests.Success)

			// We need to do this so we can clean up after.
			publicID = u1.PublicID

			u2, err := auth.GetUserByPublicID(tests.Context, db, u1.PublicID, true)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the user by PublicID : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the user by PublicID.", tests.Success)

			if err := auth.UpdateUserStatus(tests.Context, db, u2.PublicID, auth.StatusDisabled); err != nil {
				t.Fatalf("\t%s\tShould be able to disable the user : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to disable the user.", tests.Success)

			if _, err := auth.GetUserByPublicID(tests.Context, db, u1.PublicID, true); err == nil {
				t.Fatalf("\t%s\tShould Not be able to retrieve the user by PublicID.", tests.Failed)
			}
			t.Logf("\t%s\tShould Not be able to retrieve the user by PublicID.", tests.Success)
		}
	}
}
Пример #21
0
// TestModelInvalidation tests things that can fail with model validation.
func TestModelInvalidation(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to validate models will invalidate with bad data.")
	{
		t.Log("\tWhen using a test user.")
		{
			var nu auth.NUser

			if err := nu.Validate(); err == nil {
				t.Errorf("\t%s\tShould Not be able to validate NUser value.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to validate NUser value.", tests.Success)
			}

			if _, err := auth.NewUser(nu); err == nil {
				t.Errorf("\t%s\tShould Not be able to create a new user value.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to create a new user value.", tests.Success)
			}

			var u auth.User

			if _, err := u.Pwd(); err == nil {
				t.Errorf("\t%s\tShould Not be able to call Pwd is empty password.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to call Pwd is empty password.", tests.Success)
			}

			if _, err := u.Salt(); err == nil {
				t.Errorf("\t%s\tShould Not be able to call Salt with bad values.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to call Salt with bad values.", tests.Success)
			}

			if _, err := u.WebToken(""); err == nil {
				t.Errorf("\t%s\tShould Not be able to call WebToken with bad values.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to call WebToken with bad values.", tests.Success)
			}

			if ok := u.IsPasswordValid(""); ok {
				t.Errorf("\t%s\tShould Not be able to call IsPasswordValid with empty password.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to call IsPasswordValid with empty password.", tests.Success)
			}

			u.Password = "******"

			if ok := u.IsPasswordValid(""); ok {
				t.Errorf("\t%s\tShould Not be able to call IsPasswordValid with bad password.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to call IsPasswordValid with bad password.", tests.Success)
			}
		}
	}
}
Пример #22
0
// setup initializes for each indivdual test.
func setup(t *testing.T) *cayley.Handle {
	tests.ResetLog()

	store, err := cayleyshelf.New(cfg.MustURL("MONGO_URI").String(), nil)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to connect to the cayley graph : %s", tests.Failed, err)
	}

	return store
}
Пример #23
0
func setup(t *testing.T) *db.DB {
	tests.ResetLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("Should be able to get a Mongo session : %v", err)
	}

	return db
}
Пример #24
0
// TestExpiredWebToken tests create a web token and tests when it expires.
func TestExpiredWebToken(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	var publicID string
	defer func() {
		if err := removeUser(db, publicID); err != nil {
			t.Fatalf("\t%s\tShould be able to remove the test user : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove the test user.", tests.Success)
	}()

	t.Log("Given the need to validate web tokens expire.")
	{
		t.Log("\tWhen using a new user.")
		{
			u1, err := auth.NewUser(auth.NUser{
				Status:   auth.StatusActive,
				FullName: "Test Kennedy",
				Email:    "*****@*****.**",
				Password: "******",
			})
			if err != nil {
				t.Fatalf("\t%s\tShould be able to build a new user : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to build a new user.", tests.Success)

			if err := auth.CreateUser(tests.Context, db, u1); err != nil {
				t.Fatalf("\t%s\tShould be able to create a user : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a user.", tests.Success)

			// We need to do this so we can clean up after.
			publicID = u1.PublicID

			webTok, err := auth.CreateWebToken(tests.Context, db, u1, 1*time.Millisecond)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to create a web token : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a web token.", tests.Success)

			if _, err := auth.ValidateWebToken(tests.Context, db, webTok); err == nil {
				t.Fatalf("\t%s\tShould Not be able to validate the web token : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould Not be able to validate the web token.", tests.Success)
		}
	}
}
Пример #25
0
// TestGetLatest tests the retrieval of the latest session.
func TestGetLatest(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	defer func() {
		if err := removeSessions(db); err != nil {
			t.Errorf("\t%s\tShould be able to remove all sessions : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove all sessions.", tests.Success)
	}()

	t.Log("Given the need to get the latest sessions in the DB.")
	{
		t.Logf("\tWhen using PublicID %s", publicID)
		{
			if err := removeSessions(db); err != nil {
				t.Fatalf("\t%s\tShould be able to remove all sessions : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to remove all sessions.", tests.Success)

			if _, err := session.Create(tests.Context, db, publicID, 10*time.Second); err != nil {
				t.Fatalf("\t%s\tShould be able to create a session : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a session.", tests.Success)

			time.Sleep(time.Second)

			s2, err := session.Create(tests.Context, db, publicID, 10*time.Second)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to create another session : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create another session.", tests.Success)

			s3, err := session.GetByLatest(tests.Context, db, publicID)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the latest session : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the latest session.", tests.Success)

			if s2.SessionID != s3.SessionID {
				t.Errorf("\t%s\tShould be able to get back the latest session.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be able to get back the latest session.", tests.Success)
			}
		}
	}
}
Пример #26
0
// TestGetByID tests if we can get a single item from the db.
func TestGetByID(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	defer func() {
		if err := itemfix.Remove(tests.Context, db, prefix); err != nil {
			t.Fatalf("\t%s\tShould be able to remove the items : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove the items.", tests.Success)
	}()

	t.Log("Given the need to get an item in the database by ID.")
	{
		t.Log("\tWhen starting from an empty items collection")
		{
			items, err := itemfix.Get()
			if err != nil {
				t.Fatalf("\t%s\tShould be able retrieve item fixture : %s", tests.Failed, err)
			}

			var itemIDs []string
			for _, it := range items {
				if err := item.Upsert(tests.Context, db, &it); err != nil {
					t.Fatalf("\t%s\tShould be able to upsert items : %s", tests.Failed, err)
				}
				itemIDs = append(itemIDs, it.ID)
			}
			t.Logf("\t%s\tShould be able to upsert items.", tests.Success)

			itmBack, err := item.GetByID(tests.Context, db, itemIDs[0])
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get an item by ID : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get an item by ID.", tests.Success)

			// Check equality for all immutable fields: ID, Version, Data. Timestamps will change on Upsert.
			if !reflect.DeepEqual(items[0].Data, itmBack.Data) || (items[0].ID != itmBack.ID) || (items[0].Version != itmBack.Version) {
				t.Logf("\t%+v", items[0])
				t.Logf("\t%+v", itmBack)
				t.Fatalf("\t%s\tShould be able to get back the same item.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same item.", tests.Success)
		}
	}
}
Пример #27
0
// TestGetByIDs tests if we can get items from the db.
func TestGetByIDs(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db, err := db.NewMGO(tests.Context, tests.TestSession)
	if err != nil {
		t.Fatalf("\t%s\tShould be able to get a Mongo session : %v", tests.Failed, err)
	}
	defer db.CloseMGO(tests.Context)

	defer func() {
		if err := itemfix.Remove(tests.Context, db, prefix); err != nil {
			t.Fatalf("\t%s\tShould be able to remove the items : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove the items.", tests.Success)
	}()

	t.Log("Given the need to get items in the database by IDs.")
	{
		t.Log("\tWhen starting from an empty items collection")
		{
			items1, err := itemfix.Get()
			if err != nil {
				t.Fatalf("\t%s\tShould be able retrieve item fixture : %s", tests.Failed, err)
			}

			var itemIDs []string
			for _, it := range items1 {
				if err := item.Upsert(tests.Context, db, &it); err != nil {
					t.Fatalf("\t%s\tShould be able to upsert items : %s", tests.Failed, err)
				}
				itemIDs = append(itemIDs, it.ID)
			}
			t.Logf("\t%s\tShould be able to upsert items.", tests.Success)

			items2, err := item.GetByIDs(tests.Context, db, itemIDs)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to get items by IDs : %s", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to get items by IDs.", tests.Success)

			if len(items1) != len(items2) {
				t.Logf("\t%+v", items1)
				t.Logf("\t%+v", items2)
				t.Fatalf("\t%s\tShould be able to get back the same items.", tests.Failed)
			}
			t.Logf("\t%s\tShould be able to get back the same items.", tests.Success)
		}
	}
}
Пример #28
0
// TestFormsGET sample test for the GET call.
func TestFormsGET(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to test GET form call.")
	{
		url := "/v1/form"
		r := httptest.NewRequest("GET", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			t.Log("\tWhen we user version v1 of the forms endpoint.")
			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to retrieve the forms list : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to retrieve the forms list.", tests.Success)

			var forms []struct {
				ID string `json:"id"`
			}

			err := json.NewDecoder(w.Body).Decode(&forms)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to unmarshal the response : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to unmarshal the response.", tests.Success)

			total := 141
			if len(forms) != total {
				t.Log("GOT :", len(forms))
				t.Log("WANT:", total)
				t.Errorf("\t%s\tShould have the correct amount of forms.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould have the correct amount of forms.", tests.Success)
			}

			want := "5790f40c6413f60007228586"
			if forms[0].ID != want {
				t.Log("GOT :", forms[0].ID)
				t.Log("WANT:", want)
				t.Errorf("\t%s\tShould have the correct id.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould have the correct id.", tests.Success)
			}
		}
	}
}
Пример #29
0
// TestCreate tests the creation of sessions.
func TestCreate(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	db := db.NewMGO()
	defer db.CloseMGO()

	defer func() {
		if err := removeSessions(db); err != nil {
			t.Errorf("\t%s\tShould be able to remove all sessions : %v", tests.Failed, err)
		}
		t.Logf("\t%s\tShould be able to remove all sessions.", tests.Success)
	}()

	t.Log("Given the need to create sessions in the DB.")
	{
		t.Logf("\tWhen using PublicID %s", publicID)
		{
			if err := removeSessions(db); err != nil {
				t.Fatalf("\t%s\tShould be able to remove all sessions : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to remove all sessions.", tests.Success)

			s1, err := session.Create(tests.Context, db, publicID, 10*time.Second)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to create a session : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to create a session.", tests.Success)

			s2, err := session.GetBySessionID(tests.Context, db, s1.SessionID)
			if err != nil {
				t.Fatalf("\t%s\tShould be able to retrieve the session : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to retrieve the session.", tests.Success)

			if s1.SessionID != s2.SessionID {
				t.Fatalf("\t%s\tShould be able to get back the same session.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be able to get back the same session.", tests.Success)
			}

			if s1.PublicID != s2.PublicID {
				t.Fatalf("\t%s\tShould be able to get back the same user.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be able to get back the same user.", tests.Success)
			}
		}
	}
}
Пример #30
0
// TestActionsPOST sample test for the POST call.
func TestActionsPOST(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to test POST action call.")
	{
		action := "flagged_by"
		userID := "ITEST_80aa936a-f618-4234-a7be-df59a14cf8de"
		itemID := "ITEST_d16790f8-13e9-4cb4-b9ef-d82835589660"
		url := fmt.Sprintf("/v1/action/%s/user/%s/on/item/%s", action, userID, itemID)
		r := httptest.NewRequest("POST", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			t.Log("\tWhen we use version v1 of the actions endpoint.")

			if w.Code != http.StatusOK {
				t.Fatalf("\t%s\tShould be able to add the action : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould be able to add the action.", tests.Success)
		}
	}

	t.Log("Given the need to test a wrong POST action call.")
	{
		action := "flagged_by"
		userID := "ITEST_80aa936a-f618-4234-a7be-df59a14cf8de"
		itemID := "wrongitem"
		url := fmt.Sprintf("/v1/action/%s/user/%s/on/item/%s", action, userID, itemID)
		r := httptest.NewRequest("POST", url, nil)
		w := httptest.NewRecorder()

		a.ServeHTTP(w, r)

		t.Logf("\tWhen calling url : %s", url)
		{
			t.Log("\tWhen we use version v1 of the actions endpoint.")

			if w.Code != http.StatusInternalServerError {
				t.Fatalf("\t%s\tShould fail on finding the target : %v", tests.Failed, w.Code)
			}
			t.Logf("\t%s\tShould fail on finding the target.", tests.Success)
		}
	}

}