Skip to content

Commit

Permalink
converted returns of empty slices to be nonnil slices
Browse files Browse the repository at this point in the history
  • Loading branch information
pputman-clabs committed Jan 12, 2023
1 parent db0c6b6 commit 85e18d2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 54 deletions.
19 changes: 12 additions & 7 deletions mockokta.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ type GroupResource struct {

// Wrapper methods for Okta API Calls


// Initialize is an empty method so we can match the okta client interface with tests
func (client *MockClient) Initialize(ctx context.Context, conf ...okta.ConfigSetter) error {
return nil
return nil
}

// ListGroups is a wrapper to call client.Group.ListGroups to make it easier to match an interface for the okta client
Expand All @@ -70,7 +69,7 @@ func (client *MockClient) CreateGroup(ctx context.Context, group okta.Group) (*o
}

// DeleteGroup is a wrapper to call client.Group.DeleteGroup to make it easier to match an interface for the okta client
func (client *MockClient) DeleteGroup(ctx context.Context, groupID string) (*okta.Response, error) {
func (client *MockClient) DeleteGroup(ctx context.Context, groupID string) (*okta.Response, error) {
return client.Group.DeleteGroup(ctx, groupID)
}

Expand Down Expand Up @@ -197,8 +196,12 @@ func (g *GroupResource) AssignRoleToGroup(ctx context.Context, groupID string, a

// ListGroupAssignedRoles will list all the roles for a specified groupID
func (g *GroupResource) ListGroupAssignedRoles(ctx context.Context, groupID string, qp *query.Params) ([]*okta.Role, *okta.Response, error) {

group, _ := g.GetGroupByID(groupID)
return g.GroupRoles[group.Profile.Name], nil, nil
roles := make([]*okta.Role, 0, 0)

roles = append(roles, g.GroupRoles[group.Profile.Name]...)
return roles, nil, nil
}

// GroupContainsRole will search a group for a certain role and return a boolean of it found it
Expand All @@ -215,15 +218,15 @@ func (g *GroupResource) GroupContainsRole(group okta.Group, roleType string) boo
// ListGroupUsers will return a slice of all users in the specified group
func (g *GroupResource) ListGroupUsers(ctx context.Context, groupID string, qp *query.Params) ([]*okta.User, *okta.Response, error) {
group, _ := g.GetGroupByID(groupID)
users := make([]*okta.User, 0)
users := make([]*okta.User, 0, 0)
for _, user := range g.GroupUsers[group.Profile.Name] {
user, _ := g.Client.User.GetUserByEmail(user)
users = append(users, user)
}
return users, nil, nil
}

// GroupContainsUser will search a group for a user by email and return a boolean indicating
// GroupContainsUser will search a group for a user by email and return a boolean indicating
// if it found the user or not
func (g *GroupResource) GroupContainsUser(group okta.Group, userEmail string) bool {
for _, groupUser := range g.GroupUsers[group.Profile.Name] {
Expand Down Expand Up @@ -280,7 +283,9 @@ func (u *UserResource) CreateUser(userEmail string) (*okta.User, error) {

// ListUsers returns a list of all okta Users
func (u *UserResource) ListUsers(ctx context.Context, qp *query.Params) ([]*okta.User, *okta.Response, error) {
return u.Users, nil, nil
users := make([]*okta.User, 0, 0)
users = append(users, u.Users...)
return users, nil, nil
}

// GetUserByEmail searches for a user with the email and returns it
Expand Down
128 changes: 81 additions & 47 deletions mockokta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,22 @@ func TestGroupResource_AssignRoleToGroup(t *testing.T) {
})
}

// This is because of a bug where we were returning []*okta.Role(nil) instead of []*okta.Role
// so the solution was to create the map with make, and append to it. Otherwise production
// code could have a bug if checking a variable against nil, which would pass here since
// []*okta.Role(nil) will == nil but []*okta.Role{} won't
func TestGroupResource_ListGroupAssignedRoles(t *testing.T) {
t.Run("should return error if the group doesn't exist", func(t *testing.T) {
groupIDArg := "NonexistentGroup"
roleRequest := RandAdminRoleRequest()
t.Run("should return empty slice instead of slice(nil)", func(t *testing.T) {
groupNameArg := "NonExistentGroup"

client := NewClient()
_, _, err := client.Group.AssignRoleToGroup(context.TODO(), groupIDArg, roleRequest, nil)
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))

if err == nil {
t.Errorf("Expected err, but didn't get one")
want := []*okta.Role{}
got, _, _ := client.ListGroupAssignedRoles(context.TODO(), group.Id, nil)

if reflect.TypeOf(got) != reflect.TypeOf(want) || got == nil {
t.Errorf("got %#v want %#v", got, want)
}

})
Expand Down Expand Up @@ -334,18 +340,18 @@ func TestGroupResource_DeleteGroup(t *testing.T) {

client.Group.DeleteGroup(context.TODO(), group.Id)

want := []*okta.Group{}
got, _, _ := client.Group.ListGroups(context.TODO(), nil)
want := []*okta.Group{}
got, _, _ := client.Group.ListGroups(context.TODO(), nil)

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}

})
t.Run("should not delete extra groups", func(t *testing.T) {
groupNameArg1 := "TestGroup1"
groupNameArg2 := "TestGroup2"
groupNameArg3 := "TestGroup3"
groupNameArg2 := "TestGroup2"
groupNameArg3 := "TestGroup3"

client := NewClient()
group1, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg1))
Expand All @@ -354,56 +360,84 @@ func TestGroupResource_DeleteGroup(t *testing.T) {

client.Group.DeleteGroup(context.TODO(), group2.Id)

want := []*okta.Group{group1, group3}
got, _, _ := client.Group.ListGroups(context.TODO(), nil)
want := []*okta.Group{group1, group3}
got, _, _ := client.Group.ListGroups(context.TODO(), nil)

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}

})

}

func TestGroupResource_ListGroupUsers(t *testing.T) {
userEmailArg1 := "[email protected]"
userEmailArg2 := "[email protected]"
userEmailArg3 := "[email protected]"
t.Run("list existing users", func(t *testing.T) {
userEmailArg1 := "[email protected]"
userEmailArg2 := "[email protected]"
userEmailArg3 := "[email protected]"

groupNameArg := "TestGroup"
groupNameArg := "TestGroup"

client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
user1, _ := client.User.CreateUser(userEmailArg1)
user2, _ := client.User.CreateUser(userEmailArg2)
user3, _ := client.User.CreateUser(userEmailArg3)
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
user1, _ := client.User.CreateUser(userEmailArg1)
user2, _ := client.User.CreateUser(userEmailArg2)
user3, _ := client.User.CreateUser(userEmailArg3)

client.Group.AddUserToGroup(context.TODO(), group.Id, user1.Id)
client.Group.AddUserToGroup(context.TODO(), group.Id, user2.Id)
client.Group.AddUserToGroup(context.TODO(), group.Id, user3.Id)
client.Group.AddUserToGroup(context.TODO(), group.Id, user1.Id)
client.Group.AddUserToGroup(context.TODO(), group.Id, user2.Id)
client.Group.AddUserToGroup(context.TODO(), group.Id, user3.Id)

want := []*okta.User{user1, user2, user3}
got, _, _ := client.Group.ListGroupUsers(context.TODO(), group.Id, nil)
want := []*okta.User{user1, user2, user3}
got, _, _ := client.Group.ListGroupUsers(context.TODO(), group.Id, nil)

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
t.Run("empty users should return empty slice and not nil", func(t *testing.T) {
groupNameArg := "TestGroup"

client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))

want := []*okta.User{}
got, _, _ := client.Group.ListGroupUsers(context.TODO(), group.Id, nil)

if !reflect.DeepEqual(got, want) {
t.Errorf("got %#v want %#v", got, want)
}
})

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}

func TestUserResource_ListUsers(t *testing.T) {
client := NewClient()
userEmailArg1 := "TestUser1"
userEmailArg2 := "TestUser2"
t.Run("should list users", func(t *testing.T) {
client := NewClient()
userEmailArg1 := "TestUser1"
userEmailArg2 := "TestUser2"

user1, _ := client.User.CreateUser(userEmailArg1)
user2, _ := client.User.CreateUser(userEmailArg2)
user1, _ := client.User.CreateUser(userEmailArg1)
user2, _ := client.User.CreateUser(userEmailArg2)

want := []*okta.User{user1, user2}
got, _, _ := client.User.ListUsers(context.TODO(), nil)
want := []*okta.User{user1, user2}
got, _, _ := client.User.ListUsers(context.TODO(), nil)

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
t.Run("empty slice should not be nil", func(t *testing.T) {
client := NewClient()

got, _, _ := client.User.ListUsers(context.TODO(), nil)

if got == nil {
t.Errorf("got %#v want non nil slice", got)
}
})

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
Expand Down

0 comments on commit 85e18d2

Please sign in to comment.