From 323dab2e221c15b9b9094c472915fedde97d3150 Mon Sep 17 00:00:00 2001 From: pputman-clabs Date: Sat, 7 Jan 2023 08:41:46 -0600 Subject: [PATCH] adding test for add user and remove user --- mockokta.go | 31 +++++++++++++++++++- mockokta_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 101 insertions(+), 6 deletions(-) diff --git a/mockokta.go b/mockokta.go index 2ce8aa7..f309c04 100644 --- a/mockokta.go +++ b/mockokta.go @@ -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) @@ -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") diff --git a/mockokta_test.go b/mockokta_test.go index 3dda9e5..1b2267b 100644 --- a/mockokta_test.go +++ b/mockokta_test.go @@ -207,12 +207,11 @@ 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 := "TestUser@test.com" - 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") @@ -220,12 +219,11 @@ func TestGroupResource_AddUserToGroup(t *testing.T) { }) 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) @@ -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 := "TestUser@test.com" + + 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 := "TestUser@test.com" + 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 := "TestUser1@test.com" + userEmailArg2 := "TestUser2@test.com" + userEmailArg3 := "TestUser3@test.com" + + 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()