Skip to content

Commit

Permalink
added test for adduserstogroups
Browse files Browse the repository at this point in the history
  • Loading branch information
pputman-clabs committed Jan 7, 2023
1 parent 99deb02 commit 3e97904
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
33 changes: 30 additions & 3 deletions mockokta.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewClient() *MockClient {
c.Group = &GroupResource{
Client: c,
GroupRoles: make(map[string][]*okta.Role),
GroupUsers: make(map[string][]string),
}
c.User = &UserResource{
Client: c,
Expand Down Expand Up @@ -59,7 +60,7 @@ func (client *MockClient) ListUsers(ctx context.Context, qp *query.Params) ([]*o
}

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

func (g *GroupResource) CreateGroup(ctx context.Context, group okta.Group) (*okta.Group, *okta.Response, error) {
Expand Down Expand Up @@ -93,7 +94,15 @@ func NewGroup(groupName string) *okta.Group {
}

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

Expand Down Expand Up @@ -135,6 +144,15 @@ func (g *GroupResource) GroupContainsRole(group okta.Group, roleType string) boo
return false
}

func (g *GroupResource) GroupContainsUser(group okta.Group, userEmail string) bool {
for _, groupUser := range g.GroupUsers[group.Profile.Name] {
if groupUser == userEmail {
return true
}
}
return false
}

func (g *GroupResource) GetGroupById(groupId string) (*okta.Group, error) {
for _, group := range g.Groups {
if group.Id == groupId {
Expand Down Expand Up @@ -173,7 +191,7 @@ func (u *UserResource) ListUsers(ctx context.Context, qp *query.Params) ([]*okta
return u.Users, nil, nil
}

func (u *UserResource) GetUserByEmail(ctx context.Context, email string) (*okta.User, error) {
func (u *UserResource) GetUserByEmail(email string) (*okta.User, error) {
for _, user := range u.Users {
if (*user.Profile)["email"] == email {
return user, nil
Expand All @@ -182,6 +200,15 @@ func (u *UserResource) GetUserByEmail(ctx context.Context, email string) (*okta.
return nil, fmt.Errorf("user not found")
}

func (u *UserResource) GetUserById(userId string) (*okta.User, error) {
for _, user := range u.Users {
if user.Id == userId {
return user, nil
}
}
return nil, fmt.Errorf("user not found")
}

func NewRole(roleType string) okta.Role {
return okta.Role{
Type: roleType,
Expand Down
46 changes: 45 additions & 1 deletion mockokta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,58 @@ func TestUserResource_CreateUser(t *testing.T) {
client := NewClient()

want, _ := client.User.CreateUser(userEmail)
got, _ := client.User.GetUserByEmail(context.TODO(), userEmail)
got, _ := client.User.GetUserByEmail(userEmail)

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

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)

_, err := client.Group.AddUserToGroup(context.TODO(), groupNameArg, userEmailArg)

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)

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

t.Run("should add user to 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.AddUserToGroup(context.TODO(), group.Id, user.Id)

if !client.Group.GroupContainsUser(*group, userEmailArg) {
t.Errorf("expected group %v to contain user %v but it did not", groupNameArg, userEmailArg)
}
})
}

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

0 comments on commit 3e97904

Please sign in to comment.