Skip to content

Commit

Permalink
adding test for list group users
Browse files Browse the repository at this point in the history
  • Loading branch information
pputman-clabs committed Jan 8, 2023
1 parent 2576f1c commit 9a56483
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 42 deletions.
50 changes: 24 additions & 26 deletions mockokta.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewClient() *MockClient {
c.Group = &GroupResource{
Client: c,
GroupRoles: make(map[string][]*okta.Role),
GroupUsers: make(map[string][]string),
GroupUsers: make(map[string][]string),
}
c.User = &UserResource{
Client: c,
Expand Down Expand Up @@ -111,34 +111,33 @@ func (g *GroupResource) AddUserToGroup(ctx context.Context, groupId string, user
return nil, err
}

g.GroupUsers[group.Profile.Name] = append(g.GroupUsers[group.Profile.Name], (*user.Profile)["email"].(string))
g.GroupUsers[group.Profile.Name] = append(g.GroupUsers[group.Profile.Name], (*user.Profile)["email"].(string))

return nil, nil
}

func (g *GroupResource) RemoveUserFromGroup(ctx context.Context, groupId string, userId string) (*okta.Response, error) {
group, err := g.GetGroupById(groupId)
group, err := g.GetGroupById(groupId)
if err != nil {
return nil, err
}
groupName := group.Profile.Name
groupName := group.Profile.Name
user, err := g.Client.User.GetUserById(userId)
if err != nil {
return nil, err
}
userEmail := (*user.Profile)["email"].(string)
userEmail := (*user.Profile)["email"].(string)

for idx, u := range g.GroupUsers[groupName] {
if u == userEmail {
g.GroupUsers[groupName][idx] = g.GroupUsers[groupName][len(g.GroupUsers[groupName])-1]
g.GroupUsers[groupName][len(g.GroupUsers[groupName])-1] = ""
g.GroupUsers[groupName] = g.GroupUsers[groupName][:len(g.GroupUsers[groupName])-1]
}
}
return nil, nil
for idx, u := range g.GroupUsers[groupName] {
if u == userEmail {
g.GroupUsers[groupName][idx] = g.GroupUsers[groupName][len(g.GroupUsers[groupName])-1]
g.GroupUsers[groupName][len(g.GroupUsers[groupName])-1] = ""
g.GroupUsers[groupName] = g.GroupUsers[groupName][:len(g.GroupUsers[groupName])-1]
}
}
return nil, nil
}


func (g *GroupResource) AssignRoleToGroup(ctx context.Context, groupId string, assignRoleRequest okta.AssignRoleRequest, qp *query.Params) (*okta.Role, *okta.Response, error) {
if !SliceContainsString(ADMIN_ROLES, assignRoleRequest.Type) {
return nil, nil, fmt.Errorf("invalid role")
Expand Down Expand Up @@ -178,21 +177,21 @@ func (g *GroupResource) GroupContainsRole(group okta.Group, roleType string) boo
}

func (g *GroupResource) ListGroupUsers(ctx context.Context, groupId string, qp *query.Params) ([]*okta.User, *okta.Response, error) {
group, err := g.GetGroupById(groupId)
if err != nil {
return nil, nil, err
}
var users []*okta.User
for _, user := range g.GroupUsers[group.Profile.Name] {
user, _ := g.Client.User.GetUserByEmail(user)
users = append(users, user)
}
return users, nil, nil
group, err := g.GetGroupById(groupId)
if err != nil {
return nil, nil, err
}
var users []*okta.User
for _, user := range g.GroupUsers[group.Profile.Name] {
user, _ := g.Client.User.GetUserByEmail(user)
users = append(users, user)
}
return users, nil, nil
}

func (g *GroupResource) GroupContainsUser(group okta.Group, userEmail string) bool {
for _, groupUser := range g.GroupUsers[group.Profile.Name] {
if groupUser == userEmail {
if groupUser == userEmail {
return true
}
}
Expand All @@ -217,7 +216,6 @@ func (g *GroupResource) GetGroupByName(groupName string) (*okta.Group, error) {
return nil, fmt.Errorf("group not found")
}


type UserResource struct {
Client *MockClient
Users []*okta.User
Expand Down
56 changes: 40 additions & 16 deletions mockokta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math/rand"
"reflect"
"testing"
"log"
"time"

"github.com/okta/okta-sdk-golang/v2/okta"
Expand Down Expand Up @@ -232,16 +231,16 @@ func TestGroupResource_AddUserToGroup(t *testing.T) {
})

t.Run("should add user to group", func(t *testing.T) {
userEmailArg := "[email protected]"
userEmailArg := "[email protected]"
groupNameArg := "TestGroup"

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

client.Group.AddUserToGroup(context.TODO(), group.Id, user.Id)

if !client.Group.GroupContainsUser(*group, userEmailArg) {
if !client.Group.GroupContainsUser(*group, userEmailArg) {
t.Errorf("expected group %v to contain user %v but it did not", groupNameArg, userEmailArg)
}
})
Expand Down Expand Up @@ -273,48 +272,73 @@ func TestGroupResource_RemoveUserFromGroup(t *testing.T) {
})

t.Run("should remove user from group", func(t *testing.T) {
userEmailArg := "[email protected]"
userEmailArg := "[email protected]"
groupNameArg := "TestGroup"

client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
user, _ := client.User.CreateUser(userEmailArg)
user, _ := client.User.CreateUser(userEmailArg)
client.Group.AddUserToGroup(context.TODO(), group.Id, user.Id)

client.Group.RemoveUserFromGroup(context.TODO(), group.Id, user.Id)

if client.Group.GroupContainsUser(*group, userEmailArg) {
if client.Group.GroupContainsUser(*group, userEmailArg) {
t.Errorf("expected group %v to not contain user %v", groupNameArg, userEmailArg)
}
})
t.Run("should not remove other users", func(t *testing.T) {
userEmailArg1 := "[email protected]"
userEmailArg2 := "[email protected]"
userEmailArg3 := "[email protected]"
userEmailArg1 := "[email protected]"
userEmailArg2 := "[email protected]"
userEmailArg3 := "[email protected]"

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)
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.RemoveUserFromGroup(context.TODO(), group.Id, user2.Id)

want := 2
got := len(client.Group.GroupUsers[group.Profile.Name])
want := 2
got := len(client.Group.GroupUsers[group.Profile.Name])

if got != want {
if got != want {
t.Errorf("expected group %v to have %d users but found %d", groupNameArg, want, got)
}
})
}

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

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.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)

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

func TestUserResource_ListUsers(t *testing.T) {
client := NewClient()
userEmailArg1 := "TestUser1"
Expand Down

0 comments on commit 9a56483

Please sign in to comment.