Skip to content

Commit e69c5e1

Browse files
Add roles to user management API request/responses (#459)
* Add roles to user management API responses * Update pkg/usermanagement/client.go Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent bb98743 commit e69c5e1

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

pkg/usermanagement/client.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@ type OrganizationMembership struct {
125125
// The ID of the Organization.
126126
OrganizationID string `json:"organization_id"`
127127

128-
// The role given to this Organization Membership
128+
// The role given to this Organization Membership or priority role if multiple roles are assigned.
129129
Role common.RoleResponse `json:"role"`
130130

131+
// All roles for the Organization Membership
132+
Roles []common.RoleResponse `json:"roles"`
133+
131134
// The Status of the Organization.
132135
Status OrganizationMembershipStatus `json:"status"`
133136

@@ -491,15 +494,23 @@ type CreateOrganizationMembershipOpts struct {
491494
// The ID of the Organization in which to add the User as a member.
492495
OrganizationID string `json:"organization_id"`
493496

494-
// The slug of the Role in which to grant this membership. If no RoleSlug is given, the default role will be granted.
497+
// The slug of the Role in which to grant this membership. If no RoleSlug is given, the default role will be granted. Mutually exclusive with RoleSlugs.
495498
// OPTIONAL
496499
RoleSlug string `json:"role_slug,omitempty"`
500+
501+
// The slugs of the Roles in which to grant this membership. Mutually exclusive with `role_slug`. Limited to one role when Multiple Roles is disabled.
502+
// OPTIONAL
503+
RoleSlugs []string `json:"role_slugs,omitempty"`
497504
}
498505

499506
type UpdateOrganizationMembershipOpts struct {
500507
// The slug of the Role to update to for this membership.
501508
// OPTIONAL
502509
RoleSlug string `json:"role_slug,omitempty"`
510+
511+
// The slugs of the Roles to update this membership to. Mutually exclusive with `role_slug`. Limited to one role when Multiple Roles is disabled.
512+
// OPTIONAL
513+
RoleSlugs []string `json:"role_slugs,omitempty"`
503514
}
504515

505516
type DeleteOrganizationMembershipOpts struct {

pkg/usermanagement/client_test.go

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,6 +2605,31 @@ func TestCreateOrganizationMembership(t *testing.T) {
26052605
Role: common.RoleResponse{
26062606
Slug: "member",
26072607
},
2608+
Roles: nil,
2609+
CreatedAt: "2021-06-25T19:07:33.155Z",
2610+
UpdatedAt: "2021-06-25T19:07:33.155Z",
2611+
},
2612+
},
2613+
{
2614+
scenario: "Request returns OrganizationMembership with multiple roles",
2615+
client: NewClient("test"),
2616+
options: CreateOrganizationMembershipOpts{
2617+
UserID: "user_01JPQN38A88C7HC0AXDHG09EE7",
2618+
OrganizationID: "org_01K2DJ6322T1HVVVFRESKAH1GW",
2619+
RoleSlugs: []string{"admin", "member"},
2620+
},
2621+
expected: OrganizationMembership{
2622+
ID: "om_01K2DJ6322T1HVVVFRESKAH1GW",
2623+
UserID: "user_01JPQN38A88C7HC0AXDHG09EE7",
2624+
OrganizationID: "org_01K2DJ6322T1HVVVFRESKAH1GW",
2625+
Status: Active,
2626+
Role: common.RoleResponse{
2627+
Slug: "admin",
2628+
},
2629+
Roles: []common.RoleResponse{
2630+
{Slug: "admin"},
2631+
{Slug: "member"},
2632+
},
26082633
CreatedAt: "2021-06-25T19:07:33.155Z",
26092634
UpdatedAt: "2021-06-25T19:07:33.155Z",
26102635
},
@@ -2641,7 +2666,16 @@ func createOrganizationMembershipTestHandler(w http.ResponseWriter, r *http.Requ
26412666
var body []byte
26422667
var err error
26432668

2644-
if r.URL.Path == "/user_management/organization_memberships" {
2669+
// Decode JSON body into a generic map
2670+
var data map[string]interface{}
2671+
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
2672+
http.Error(w, "Invalid JSON", http.StatusBadRequest)
2673+
return
2674+
}
2675+
2676+
orgId := data["organization_id"]
2677+
switch {
2678+
case r.URL.Path == "/user_management/organization_memberships" && orgId == "org_01E4ZCR3C56J083X43JQXF3JK5":
26452679
body, err = json.Marshal(OrganizationMembership{
26462680
ID: "om_01E4ZCR3C56J083X43JQXF3JK5",
26472681
UserID: "user_01E4ZCR3C5A4QZ2Z2JQXGKZJ9E",
@@ -2653,6 +2687,22 @@ func createOrganizationMembershipTestHandler(w http.ResponseWriter, r *http.Requ
26532687
CreatedAt: "2021-06-25T19:07:33.155Z",
26542688
UpdatedAt: "2021-06-25T19:07:33.155Z",
26552689
})
2690+
case r.URL.Path == "/user_management/organization_memberships" && orgId == "org_01K2DJ6322T1HVVVFRESKAH1GW":
2691+
body, err = json.Marshal(OrganizationMembership{
2692+
ID: "om_01K2DJ6322T1HVVVFRESKAH1GW",
2693+
UserID: "user_01JPQN38A88C7HC0AXDHG09EE7",
2694+
OrganizationID: "org_01K2DJ6322T1HVVVFRESKAH1GW",
2695+
Status: Active,
2696+
Role: common.RoleResponse{
2697+
Slug: "admin",
2698+
},
2699+
Roles: []common.RoleResponse{
2700+
{Slug: "admin"},
2701+
{Slug: "member"},
2702+
},
2703+
CreatedAt: "2021-06-25T19:07:33.155Z",
2704+
UpdatedAt: "2021-06-25T19:07:33.155Z",
2705+
})
26562706
}
26572707

26582708
if err != nil {
@@ -2693,6 +2743,30 @@ func TestUpdateOrganizationMembership(t *testing.T) {
26932743
Role: common.RoleResponse{
26942744
Slug: "member",
26952745
},
2746+
Roles: nil,
2747+
CreatedAt: "2021-06-25T19:07:33.155Z",
2748+
UpdatedAt: "2021-06-25T19:07:33.155Z",
2749+
},
2750+
},
2751+
{
2752+
scenario: "Request returns OrganizationMembership with multiple roles",
2753+
client: NewClient("test"),
2754+
organizationMembershipId: "om_01K2DJ6322T1HVVVFRESKAH1GW",
2755+
options: UpdateOrganizationMembershipOpts{
2756+
RoleSlug: "member",
2757+
},
2758+
expected: OrganizationMembership{
2759+
ID: "om_01K2DJ6322T1HVVVFRESKAH1GW",
2760+
UserID: "user_01JPQN38A88C7HC0AXDHG09EE7",
2761+
OrganizationID: "org_01K2DJ6322T1HVVVFRESKAH1GW",
2762+
Status: Active,
2763+
Role: common.RoleResponse{
2764+
Slug: "admin",
2765+
},
2766+
Roles: []common.RoleResponse{
2767+
{Slug: "admin"},
2768+
{Slug: "member"},
2769+
},
26962770
CreatedAt: "2021-06-25T19:07:33.155Z",
26972771
UpdatedAt: "2021-06-25T19:07:33.155Z",
26982772
},
@@ -2729,7 +2803,8 @@ func updateOrganizationMembershipTestHandler(w http.ResponseWriter, r *http.Requ
27292803
var body []byte
27302804
var err error
27312805

2732-
if r.URL.Path == "/user_management/organization_memberships/om_01E4ZCR3C56J083X43JQXF3JK5" {
2806+
switch {
2807+
case r.URL.Path == "/user_management/organization_memberships/om_01E4ZCR3C56J083X43JQXF3JK5":
27332808
body, err = json.Marshal(OrganizationMembership{
27342809
ID: "om_01E4ZCR3C56J083X43JQXF3JK5",
27352810
UserID: "user_01E4ZCR3C5A4QZ2Z2JQXGKZJ9E",
@@ -2741,6 +2816,22 @@ func updateOrganizationMembershipTestHandler(w http.ResponseWriter, r *http.Requ
27412816
CreatedAt: "2021-06-25T19:07:33.155Z",
27422817
UpdatedAt: "2021-06-25T19:07:33.155Z",
27432818
})
2819+
case r.URL.Path == "/user_management/organization_memberships/om_01K2DJ6322T1HVVVFRESKAH1GW":
2820+
body, err = json.Marshal(OrganizationMembership{
2821+
ID: "om_01K2DJ6322T1HVVVFRESKAH1GW",
2822+
UserID: "user_01JPQN38A88C7HC0AXDHG09EE7",
2823+
OrganizationID: "org_01K2DJ6322T1HVVVFRESKAH1GW",
2824+
Status: Active,
2825+
Role: common.RoleResponse{
2826+
Slug: "admin",
2827+
},
2828+
Roles: []common.RoleResponse{
2829+
{Slug: "admin"},
2830+
{Slug: "member"},
2831+
},
2832+
CreatedAt: "2021-06-25T19:07:33.155Z",
2833+
UpdatedAt: "2021-06-25T19:07:33.155Z",
2834+
})
27442835
}
27452836

27462837
if err != nil {

0 commit comments

Comments
 (0)