Example #1
0
// TestInvalidWebTokenUpdateEmail tests a token becomes invalid after an update.
func TestInvalidWebTokenUpdateEmail(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 don't work after user update.")
	{
		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, 5*time.Second)
			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 be able to validate the web token : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould be able to validate the web token.", tests.Success)

			uu := auth.UpdUser{
				PublicID: publicID,
				Status:   auth.StatusActive,
				FullName: "Update Kennedy",
				Email:    "*****@*****.**",
			}

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

			if _, err := auth.ValidateWebToken(tests.Context, db, webTok); err == nil {
				t.Fatalf("\t%s\tShould Not be able to validate the org web token.", tests.Failed)
			}
			t.Logf("\t%s\tShould Not be able to validate the org web token.", tests.Success)
		}
	}
}
Example #2
0
// TestUpdateUser tests we can update user information.
func TestUpdateUser(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

			uu := auth.UpdUser{
				PublicID: publicID,
				Status:   auth.StatusActive,
				FullName: "Update Kennedy",
				Email:    "*****@*****.**",
			}

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

			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)

			// Remove the objectid to be able to compare the values.
			u2.ID = ""

			// Need to remove the nanoseconds to be able to compare the values.
			u1.DateModified = u1.DateModified.Add(-time.Duration(u1.DateModified.Nanosecond()))
			u1.DateCreated = u1.DateCreated.Add(-time.Duration(u1.DateCreated.Nanosecond()))
			u2.DateModified = u2.DateModified.Add(-time.Duration(u2.DateModified.Nanosecond()))
			u2.DateCreated = u2.DateCreated.Add(-time.Duration(u2.DateCreated.Nanosecond()))

			// Update the fields that changed
			u1.Status = u2.Status
			u1.FullName = u2.FullName
			u1.Email = u2.Email

			if !reflect.DeepEqual(*u1, *u2) {
				t.Logf("\t%+v", *u1)
				t.Logf("\t%+v", *u2)
				t.Errorf("\t%s\tShould be able to get back the same user with changes.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould be able to get back the same user with changes.", tests.Success)
			}
		}
	}
}
Example #3
0
// TestUpdateUserValidation tests the update of a user that is not valid.
func TestUpdateUserValidation(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 Not be able to remove the test user", tests.Failed)
		}
		t.Logf("\t%s\tShould Not be able to remove the test user.", tests.Success)
	}()

	t.Log("Given the need to make sure only valid users are created in the DB.")
	{
		t.Log("\tWhen using a test user.")
		{
			uu := auth.UpdUser{
				PublicID: "asdasdasd",
				Status:   auth.StatusActive,
				FullName: "Test Kennedy",
				Email:    "*****@*****.**",
			}

			if err := auth.UpdateUser(tests.Context, db, uu); err == nil {
				t.Errorf("\t%s\tShould Not be able to update a user with invalid PublicID", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to update a user with invalid PublicID.", tests.Success)
			}

			uu = auth.UpdUser{
				PublicID: "6dcda2da-92c3-11e5-8994-feff819cdc9f",
				Status:   0,
				FullName: "Test Kennedy",
				Email:    "*****@*****.**",
			}

			if err := auth.UpdateUser(tests.Context, db, uu); err == nil {
				t.Errorf("\t%s\tShould Not be able to update a user with invalid Status", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to update a user with invalid Status.", tests.Success)
			}

			uu = auth.UpdUser{
				PublicID: "6dcda2da-92c3-11e5-8994-feff819cdc9f",
				Status:   auth.StatusActive,
				FullName: "1234567",
				Email:    "*****@*****.**",
			}

			if err := auth.UpdateUser(tests.Context, db, uu); err == nil {
				t.Errorf("\t%s\tShould Not be able to update a user with invalid FullName", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to update a user with invalid FullName.", tests.Success)
			}

			uu = auth.UpdUser{
				PublicID: "6dcda2da-92c3-11e5-8994-feff819cdc9f",
				Status:   auth.StatusActive,
				FullName: "Test Kennedy",
				Email:    "ardanlabs.com",
			}

			if err := auth.UpdateUser(tests.Context, db, uu); err == nil {
				t.Errorf("\t%s\tShould Not be able to update a user with invalid Email", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to update a user with invalid Email.", tests.Success)
			}

			uu = auth.UpdUser{
				PublicID: "6dcda2da-92c3-11e5-8994-feff819cdc9f",
				Status:   auth.StatusActive,
				FullName: "Test Kennedy",
				Email:    "*****@*****.**",
			}
		}
	}
}
Example #4
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")
		{
			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, nil, u1); err == nil {
				t.Errorf("\t%s\tShould Not be able to create a user.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to create a user.", tests.Success)
			}

			if _, err := auth.CreateWebToken(tests.Context, nil, u1, time.Second); err == nil {
				t.Errorf("\t%s\tShould Not be able to create a web token.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to create a web token.", tests.Success)
			}

			webTok := "OGY4OGI3YWQtZjc5Ny00ODI1LWI0MmUtMjIwZTY5ZDQxYjMzOmFKT2U1b0pFZlZ4cWUrR0JONEl0WlhmQTY0K3JsN2VGcmM2MVNQMkV1WVE9"

			if _, err := auth.ValidateWebToken(tests.Context, nil, webTok); err == nil {
				t.Errorf("\t%s\tShould Not be able to validate a web token.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to validate a web token.", tests.Success)
			}

			if _, err := auth.GetUserByPublicID(tests.Context, nil, "6dcda2da-92c3-11e5-8994-feff819cdc9f", true); err == nil {
				t.Errorf("\t%s\tShould Not be able to get a user by PublicID.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to get a user by PublicID.", tests.Success)
			}

			if _, err := auth.GetUserByEmail(tests.Context, nil, "*****@*****.**", true); err == nil {
				t.Errorf("\t%s\tShould Not be able to get a user by Email.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to get a user by Email.", tests.Success)
			}

			uu := auth.UpdUser{
				PublicID: "6dcda2da-92c3-11e5-8994-feff819cdc9f",
				Status:   auth.StatusActive,
				FullName: "Update Kennedy",
				Email:    "*****@*****.**",
			}

			if err := auth.UpdateUser(tests.Context, nil, uu); err == nil {
				t.Errorf("\t%s\tShould Not be able to update a user.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to update a user.", tests.Success)
			}

			if err := auth.UpdateUserPassword(tests.Context, nil, u1, "password890"); err == nil {
				t.Errorf("\t%s\tShould Not be able to update a user pasword.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to update a user password.", tests.Success)
			}

			if err := auth.UpdateUserStatus(tests.Context, nil, "6dcda2da-92c3-11e5-8994-feff819cdc9f", auth.StatusDisabled); err == nil {
				t.Errorf("\t%s\tShould Not be able to disable a user.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to disable a user.", tests.Success)
			}

			if _, err := auth.LoginUser(tests.Context, nil, "*****@*****.**", "_pass"); err == nil {
				t.Errorf("\t%s\tShould Not be able to login a user.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to login a user.", tests.Success)
			}

			if _, err := auth.GetUserWebToken(tests.Context, nil, "6dcda2da-92c3-11e5-8994-feff819cdc9f"); err == nil {
				t.Errorf("\t%s\tShould Not be able to get user web token.", tests.Failed)
			} else {
				t.Logf("\t%s\tShould Not be able to get user web token.", tests.Success)
			}
		}
	}
}