Skip to content

Commit

Permalink
Merge pull request #17 from CACI-MIlMOVE/Misc-Test-Fixes
Browse files Browse the repository at this point in the history
Okta Feature Branch Misc Test Fixes
  • Loading branch information
danieljordan-caci authored Aug 30, 2023
2 parents 582b2d2 + 7e8312b commit 97bb88c
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 30 deletions.
1 change: 1 addition & 0 deletions migrations/app/migrations_manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@
20230808155723_add_ns_mayport.up.sql
20230822201206_add_fields_for_sit_to_mto_service_items.up.sql
20230823194524_copy_loginGov_oktaEmail.up.sql
20230829133429_add_unique_constaint_okta_id.up.sql
20230808155723_add_ns_mayport.up.sql
20230824224954_ghc_domestic_transit_times_update_data_migration.up.sql
20230828180000_update_mayport_zip.up.sql
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD CONSTRAINT users_okta_un UNIQUE (okta_id);
2 changes: 1 addition & 1 deletion pkg/factory/admin_user_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func BuildDefaultAdminUser(db *pop.Connection) models.AdminUser {
// GetTraitAdminUserEmail helps comply with the uniqueness constraint on emails
func GetTraitAdminUserEmail() []Customization {
// There's a uniqueness constraint on admin user emails so add some randomness
email := strings.ToLower(fmt.Sprintf("leo_spaceman_admin_%[email protected]", makeRandomString(5)))
email := strings.ToLower(fmt.Sprintf("leo_spaceman_admin_%[email protected]", MakeRandomString(5)))
return []Customization{
{
Model: models.User{
Expand Down
2 changes: 1 addition & 1 deletion pkg/factory/duty_location_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func buildDutyLocationWithBuildType(db *pop.Connection, customs []Customization,
affiliation := internalmessages.AffiliationAIRFORCE

location := models.DutyLocation{
Name: makeRandomString(10),
Name: MakeRandomString(10),
Affiliation: &affiliation,
AddressID: dlAddress.ID,
Address: dlAddress,
Expand Down
2 changes: 1 addition & 1 deletion pkg/factory/office_user_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func BuildOfficeUserWithRoles(db *pop.Connection, customs []Customization, roleT
// GetTraitOfficeUserEmail helps comply with the uniqueness constraint on emails
func GetTraitOfficeUserEmail() []Customization {
// There's a uniqueness constraint on office user emails so add some randomness
email := strings.ToLower(fmt.Sprintf("leo_spaceman_office_%[email protected]", makeRandomString(5)))
email := strings.ToLower(fmt.Sprintf("leo_spaceman_office_%[email protected]", MakeRandomString(5)))
return []Customization{
{
Model: models.User{
Expand Down
2 changes: 1 addition & 1 deletion pkg/factory/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func RandomEdipi() string {
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

// Returns a random alphanumeric string of specified length
func makeRandomString(n int) string {
func MakeRandomString(n int) string {
b := make([]byte, n)
for i := range b {
randInt, err := random.GetRandomInt(len(letterBytes))
Expand Down
2 changes: 1 addition & 1 deletion pkg/factory/user_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func BuildUser(db *pop.Connection, customs []Customization, traits []Trait) mode
}

// create user
OktaID := makeRandomString(20)
OktaID := MakeRandomString(20)
user := models.User{
OktaID: OktaID,
OktaEmail: "[email protected]",
Expand Down
6 changes: 3 additions & 3 deletions pkg/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type UserIdentity struct {
}

// FetchUserIdentity queries the database for information about the logged in user
func FetchUserIdentity(db *pop.Connection, loginGovID string) (*UserIdentity, error) {
func FetchUserIdentity(db *pop.Connection, oktaID string) (*UserIdentity, error) {
var identities []UserIdentity
query := `SELECT users.id,
users.okta_email AS email,
Expand All @@ -147,7 +147,7 @@ func FetchUserIdentity(db *pop.Connection, loginGovID string) (*UserIdentity, er
LEFT OUTER JOIN office_users AS ou on ou.user_id = users.id
LEFT OUTER JOIN admin_users AS au on au.user_id = users.id
WHERE users.okta_id = $1`
err := db.RawQuery(query, loginGovID).All(&identities)
err := db.RawQuery(query, oktaID).All(&identities)
if err != nil {
return nil, err
} else if len(identities) == 0 {
Expand Down Expand Up @@ -202,7 +202,7 @@ func FetchAppUserIdentities(db *pop.Connection, appname auth.Application, limit
sm.middle_name AS sm_middle
FROM service_members as sm
JOIN users on sm.user_id = users.id
WHERE users.okta_email != 'first.last@login.gov.test'
WHERE users.okta_email != 'first.last@okta.mil'
ORDER BY users.created_at DESC LIMIT $1`
}

Expand Down
37 changes: 15 additions & 22 deletions pkg/models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
)

func (suite *ModelSuite) TestUserValidation() {
fakeUUID, _ := uuid.FromString("39b28c92-0506-4bef-8b57-e39519f42dc1")
oktaID := "abcdefghijklmnopqrst"
userEmail := "[email protected]"

newUser := User{
OktaID: fakeUUID.String(),
OktaID: oktaID,
OktaEmail: userEmail,
}

Expand All @@ -27,7 +27,7 @@ func (suite *ModelSuite) TestUserValidation() {
suite.NoError(err)
suite.False(verrs.HasAny(), "Error validating model")
suite.Equal(userEmail, newUser.OktaEmail)
suite.Equal(fakeUUID, newUser.OktaID)
suite.Equal(oktaID, newUser.OktaID)
}

func (suite *ModelSuite) TestUserCreationWithoutValues() {
Expand All @@ -40,17 +40,17 @@ func (suite *ModelSuite) TestUserCreationWithoutValues() {
suite.verifyValidationErrors(newUser, expErrors)
}

func (suite *ModelSuite) TestUserCreationDuplicateUUID() {
fakeUUID, _ := uuid.FromString("39b28c92-0506-4bef-8b57-e39519f42dc2")
func (suite *ModelSuite) TestUserCreationDuplicateOktaID() {
oktaID := "abcdefghijklmnopqrst"
userEmail := "[email protected]"

newUser := User{
OktaID: fakeUUID.String(),
OktaID: oktaID,
OktaEmail: userEmail,
}

sameUser := User{
OktaID: fakeUUID.String(),
OktaID: oktaID,
OktaEmail: userEmail,
}

Expand All @@ -65,30 +65,24 @@ func (suite *ModelSuite) TestUserCreationDuplicateUUID() {
// nolint:errcheck
suite.DB().Create(&newUser)
err := suite.DB().Create(&sameUser)

suite.True(dberr.IsDBErrorForConstraint(err, pgerrcode.UniqueViolation, "constraint_name"), "Db should have errored on unique constraint for UUID")
suite.True(dberr.IsDBErrorForConstraint(err, pgerrcode.UniqueViolation, "users_okta_un"), "Db should have errored on unique constraint for UUID")
}

func (suite *ModelSuite) TestCreateUser() {
const testEmail = "[email protected]"
const expectedEmail = "[email protected]"
const goodUUID = "39b28c92-0506-4bef-8b57-e39519f42dc2"
const badUUID = "39xnfc92-0506-4bef-8b57-e39519f42dc2"
const oktaID = "00u3ckm7yEoUJuI1i0k6"

sally, err := CreateUser(suite.DB(), goodUUID, testEmail)
sally, err := CreateUser(suite.DB(), oktaID, testEmail)
suite.Nil(err, "No error for good create")
suite.Equal(expectedEmail, sally.OktaEmail, "should convert email to lower case")
suite.NotEqual(sally.ID, uuid.Nil)

fail, err := CreateUser(suite.DB(), expectedEmail, badUUID)
suite.NotNil(err, "should get and error from bad uuid")
suite.Nil(fail, "no user with bad uuid")
}

func (suite *ModelSuite) TestFetchUserIdentity() {
const goodUUID = "39b28c92-0506-4bef-8b57-e39519f42dc2"
oktaID := factory.MakeRandomString(20)
// First check that it all works with no record
identity, err := FetchUserIdentity(suite.DB(), goodUUID)
identity, err := FetchUserIdentity(suite.DB(), oktaID)
suite.Equal(ErrFetchNotFound, err, "Expected not to find missing Identity")
suite.Nil(identity)

Expand Down Expand Up @@ -139,7 +133,6 @@ func (suite *ModelSuite) TestFetchUserIdentity() {
suite.Equal(systemAdmin.User.OktaEmail, identity.Email)
suite.Nil(identity.ServiceMemberID)
suite.Nil(identity.OfficeUserID)

rs := []roles.Role{{
ID: uuid.FromStringOrNil("ed2d2cd7-d427-412a-98bb-a9b391d98d32"),
RoleType: roles.RoleTypeCustomer,
Expand All @@ -150,11 +143,11 @@ func (suite *ModelSuite) TestFetchUserIdentity() {
}
suite.NoError(suite.DB().Create(&rs))
customerRole := rs[0]
patUUID := uuid.Must(uuid.NewV4())
patOktaID := factory.MakeRandomString(20)
pat := factory.BuildUser(suite.DB(), []factory.Customization{
{
Model: User{
OktaID: patUUID.String(),
OktaID: patOktaID,
Active: true,
Roles: []roles.Role{customerRole},
},
Expand Down Expand Up @@ -261,7 +254,7 @@ func (suite *ModelSuite) TestFetchAppUserIdentities() {
suite.Run("service member", func() {

// Create a user email that won't be filtered out of the devlocal user query w/ a default value of
// first.last@login.gov.test
// first.last@okta.mil
user := factory.BuildUser(suite.DB(), []factory.Customization{
{
Model: User{
Expand Down

0 comments on commit 97bb88c

Please sign in to comment.