Skip to content

Commit

Permalink
adding test for add user and remove user
Browse files Browse the repository at this point in the history
  • Loading branch information
pputman-clabs committed Jan 7, 2023
1 parent 546494b commit 323dab2
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
31 changes: 30 additions & 1 deletion mockokta.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (client *MockClient) AddUserToGroup(ctx context.Context, groupId string, us
return client.Group.AddUserToGroup(ctx, groupId, userId)
}

func (client *MockClient) RemoveUserFromGroup(ctx context.Context, groupId string, userId string) (*okta.Response, error) {
return client.Group.RemoveUserFromGroup(ctx, groupId, userId)
}

func (g *GroupResource) CreateGroup(ctx context.Context, group okta.Group) (*okta.Group, *okta.Response, error) {

group.Id = fmt.Sprint(len(g.Groups) + 1)
Expand Down Expand Up @@ -102,10 +106,35 @@ func (g *GroupResource) AddUserToGroup(ctx context.Context, groupId string, user
if err != nil {
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)
if err != nil {
return nil, err
}
groupName := group.Profile.Name
user, err := g.Client.User.GetUserById(userId)
if err != nil {
return nil, err
}
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
}


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
76 changes: 71 additions & 5 deletions mockokta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,23 @@ func TestUserResource_CreateUser(t *testing.T) {
func TestGroupResource_AddUserToGroup(t *testing.T) {
t.Run("should err if group doesn't exist", func(t *testing.T) {
userEmailArg := "[email protected]"
groupNameArg := "NonexistentUser"

client := NewClient()
client.User.CreateUser(userEmailArg)
user, _ := client.User.CreateUser(userEmailArg)

_, err := client.Group.AddUserToGroup(context.TODO(), groupNameArg, userEmailArg)
_, err := client.Group.AddUserToGroup(context.TODO(), "1", user.Id)

if err == nil {
t.Errorf("expected error but didn't get one")
}
})

t.Run("should err if user doesn't exist", func(t *testing.T) {
userEmailArg := "NonexistentUser"
groupNameArg := "TestGroup"

client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
_, err := client.Group.AddUserToGroup(context.TODO(), group.Id, userEmailArg)
_, err := client.Group.AddUserToGroup(context.TODO(), group.Id, "1")

if err == nil {
t.Errorf("expected error but didn't get one %v", err)
Expand All @@ -247,6 +245,74 @@ func TestGroupResource_AddUserToGroup(t *testing.T) {
}
})
}
func TestGroupResource_RemoveUserFromGroup(t *testing.T) {
t.Run("should err if group doesn't exist", func(t *testing.T) {
userEmailArg := "[email protected]"

client := NewClient()
user, _ := client.User.CreateUser(userEmailArg)

_, err := client.Group.RemoveUserFromGroup(context.TODO(), "1", user.Id)

if err == nil {
t.Errorf("expected error but didn't get one")
}
})

t.Run("should err if user doesn't exist", func(t *testing.T) {
groupNameArg := "TestGroup"

client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
_, err := client.Group.RemoveUserFromGroup(context.TODO(), group.Id, "1")

if err == nil {
t.Errorf("expected error but didn't get one %v", err)
}
})

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

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

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

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]"

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)

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

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

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

}

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

0 comments on commit 323dab2

Please sign in to comment.