Skip to content

Commit

Permalink
Refactor and fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: bcmmbaga <[email protected]>
  • Loading branch information
bcmmbaga committed Dec 23, 2024
1 parent aba637b commit 36654d6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 62 deletions.
36 changes: 14 additions & 22 deletions management/server/idp/okta.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ func (om *OktaManager) GetUserDataByID(ctx context.Context, userID string, appMe
return nil, fmt.Errorf("unable to get user %s, statusCode %d", userID, resp.StatusCode)
}

userData, err := parseOktaUser(user)
if err != nil {
return nil, err
}
userData := parseOktaUser(user)
userData.AppMetadata = appMetadata

return userData, nil
Expand Down Expand Up @@ -150,11 +147,7 @@ func (om *OktaManager) GetUserByEmail(_ context.Context, email string) ([]*UserD

usersData := make([]*UserData, 0, len(users))
for _, user := range users {
userData, err := parseOktaUser(&user)
if err != nil {
return nil, err
}
usersData = append(usersData, userData)
usersData = append(usersData, parseOktaUser(&user))
}

return usersData, nil
Expand Down Expand Up @@ -230,12 +223,7 @@ func (om *OktaManager) getAllUsers() ([]*UserData, error) {

users := make([]*UserData, 0, len(userList))
for _, user := range userList {
userData, err := parseOktaUser(&user)
if err != nil {
return nil, err
}

users = append(users, userData)
users = append(users, parseOktaUser(&user))
}

return users, nil
Expand Down Expand Up @@ -280,16 +268,20 @@ type oktaUser interface {
}

// parseOktaUser parse okta user to UserData.
func parseOktaUser(user oktaUser) (*UserData, error) {
if user == nil {
return nil, fmt.Errorf("invalid okta user")
}

func parseOktaUser(user oktaUser) *UserData {
profile := user.GetProfile()

var names []string
if firstName := profile.GetFirstName(); firstName != "" {
names = append(names, firstName)
}
if lastName := profile.GetLastName(); lastName != "" {
names = append(names, lastName)
}

return &UserData{
Email: profile.GetEmail(),
Name: strings.Join([]string{profile.GetFirstName(), profile.GetLastName()}, " "),
Name: strings.Join(names, " "),
ID: user.GetId(),
}, nil
}
}
69 changes: 29 additions & 40 deletions management/server/idp/okta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,52 @@ package idp
import (
"testing"

"github.com/okta/okta-sdk-golang/v2/okta"
"github.com/okta/okta-sdk-golang/v5/okta"
"github.com/stretchr/testify/assert"
)

func TestParseOktaUser(t *testing.T) {
type parseOktaUserTest struct {
name string
inputProfile *okta.User
inputUser *okta.User
expectedUserData *UserData
assertErrFunc assert.ErrorAssertionFunc
}

parseOktaTestCase1 := parseOktaUserTest{
name: "Good Request",
inputProfile: &okta.User{
Id: "123",
Profile: &okta.UserProfile{
"email": "[email protected]",
"firstName": "John",
"lastName": "Doe",
testCases := []parseOktaUserTest{
{
name: "valid okta user",
inputUser: &okta.User{
Id: okta.PtrString("123"),
Profile: &okta.UserProfile{
Email: okta.PtrString("[email protected]"),
FirstName: *okta.NewNullableString(okta.PtrString("John")),
LastName: *okta.NewNullableString(okta.PtrString("Doe")),
},
},
},
expectedUserData: &UserData{
Email: "[email protected]",
Name: "John Doe",
ID: "123",
AppMetadata: AppMetadata{
WTAccountID: "456",
expectedUserData: &UserData{
Email: "[email protected]",
Name: "John Doe",
ID: "123",
AppMetadata: AppMetadata{
WTAccountID: "456",
},
},
assertErrFunc: assert.NoError,
},
{
name: "invalid okta user",
inputUser: nil,
expectedUserData: &UserData{},
},
assertErrFunc: assert.NoError,
}

parseOktaTestCase2 := parseOktaUserTest{
name: "Invalid okta user",
inputProfile: nil,
expectedUserData: nil,
assertErrFunc: assert.Error,
}

for _, testCase := range []parseOktaUserTest{parseOktaTestCase1, parseOktaTestCase2} {
t.Run(testCase.name, func(t *testing.T) {
userData, err := parseOktaUser(testCase.inputProfile)
testCase.assertErrFunc(t, err, testCase.assertErrFunc)

if err == nil {
assert.True(t, userDataEqual(testCase.expectedUserData, userData), "user data should match")
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
userData := parseOktaUser(tt.inputUser)
assert.Equal(t, tt.expectedUserData, userData, "user data should match")
})

}
}

// userDataEqual helper function to compare UserData structs for equality.
func userDataEqual(a, b *UserData) bool {
if a.Email != b.Email || a.Name != b.Name || a.ID != b.ID {
return false
}
return true
}

0 comments on commit 36654d6

Please sign in to comment.