Skip to content

Commit

Permalink
[SDK-4668] Add Roles field to OrganizationMember struct (#293)
Browse files Browse the repository at this point in the history
Co-authored-by: Ewan Harris <ewan.harris@okta.com>
  • Loading branch information
renxinhe and ewanharris committed Oct 20, 2023
1 parent a8b8fb6 commit 6aa68cc
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 7 deletions.
21 changes: 21 additions & 0 deletions management/management.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions management/management.gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions management/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ type OrganizationMember struct {
Picture *string `json:"picture,omitempty"`
Name *string `json:"name,omitempty"`
Email *string `json:"email,omitempty"`
// Roles is only included when the field is requested using the `IncludeFields` RequestOption.
// All fields that are required must also be included in the `IncludeFields` call so to requests
// all fields use `IncludeFields("user_id", "picture", "name", "email", "roles")`.
Roles []*OrganizationMemberListRole `json:"roles,omitempty"`
}

// OrganizationMemberListRole holds member role information.
type OrganizationMemberListRole struct {
// ID for this role.
ID *string `json:"id,omitempty"`

// Name of the role.
Name *string `json:"name,omitempty"`
}

// OrganizationConnection holds connection information for an Organization.
Expand Down
13 changes: 12 additions & 1 deletion management/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,25 @@ func TestOrganizationManager_Members(t *testing.T) {

org := givenAnOrganization(t)
user := givenAUser(t)
role := givenARole(t)

err := api.Organization.AddMembers(context.Background(), org.GetID(), []string{user.GetID()})
assert.NoError(t, err)

members, err := api.Organization.Members(context.Background(), org.GetID())
err = api.Organization.AssignMemberRoles(context.Background(), org.GetID(), user.GetID(), []string{role.GetID()})
assert.NoError(t, err)

members, err := api.Organization.Members(
context.Background(), org.GetID(),
IncludeFields("user_id", "roles"),
)

assert.NoError(t, err)
assert.Len(t, members.Members, 1)
assert.Equal(t, user.GetID(), members.Members[0].GetUserID())

assert.Len(t, members.Members[0].Roles, 1)
assert.Equal(t, role.GetID(), members.Members[0].Roles[0].GetID())
}

func TestOrganizationManager_MembersCheckpointPagination(t *testing.T) {
Expand Down
7 changes: 6 additions & 1 deletion management/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ func cleanupRole(t *testing.T, roleID string) {

err := api.Role.Delete(context.Background(), roleID)
if err != nil {
if err.(Error).Status() != http.StatusNotFound {
managementErr, ok := err.(Error)

// Some test (e.g. TestRoleManager_Delete) expects a 404 error during
// clean up, therefore we only raise non-404 errors.
// If `err` doesn't cast to management.Error, we raise it immediately.
if !ok || ok && managementErr.Status() != http.StatusNotFound {
t.Error(err)
}
}
Expand Down
118 changes: 113 additions & 5 deletions test/data/recordings/TestOrganizationManager_Members.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,78 @@ interactions:
code: 201
duration: 224.211208ms
- id: 2
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 50
transfer_encoding: []
trailer: {}
host: go-auth0-dev.eu.auth0.com
remote_addr: ""
request_uri: ""
body: |
{"name":"test-role403","description":"Test Role"}
form: {}
headers:
Content-Type:
- application/json
User-Agent:
- Go-Auth0-SDK/latest
url: https://go-auth0-dev.eu.auth0.com/api/v2/roles
method: POST
response:
proto: HTTP/2.0
proto_major: 2
proto_minor: 0
transfer_encoding: []
trailer: {}
content_length: -1
uncompressed: true
body: '{"id":"rol_lEh5V8omwZrnZd9o","name":"test-role403","description":"Test Role"}'
headers:
Content-Type:
- application/json; charset=utf-8
status: 200 OK
code: 200
duration: 111.664917ms
- id: 3
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 35
transfer_encoding: []
trailer: {}
host: go-auth0-dev.eu.auth0.com
remote_addr: ""
request_uri: ""
body: |
{"roles":["rol_lEh5V8omwZrnZd9o"]}
form: {}
headers:
Content-Type:
- application/json
User-Agent:
- Go-Auth0-SDK/latest
url: https://go-auth0-dev.eu.auth0.com/api/v2/organizations/org_SunNyTSHhG4ib6E9/members/auth0%7C63d16bb11e39d8a43b8ed0f5/roles
method: POST
response:
proto: HTTP/2.0
proto_major: 2
proto_minor: 0
transfer_encoding: []
trailer: {}
content_length: 0
uncompressed: false
body: ""
headers:
Content-Type:
- application/json; charset=utf-8
status: 204 No Content
code: 204
duration: 123.835375ms
- id: 4
request:
proto: HTTP/1.1
proto_major: 1
Expand Down Expand Up @@ -109,7 +181,7 @@ interactions:
status: 204 No Content
code: 204
duration: 102.516083ms
- id: 3
- id: 5
request:
proto: HTTP/1.1
proto_major: 1
Expand All @@ -128,7 +200,7 @@ interactions:
- application/json
User-Agent:
- Go-Auth0-SDK/latest
url: https://go-auth0-dev.eu.auth0.com/api/v2/organizations/org_SunNyTSHhG4ib6E9/members?include_totals=true&per_page=50
url: https://go-auth0-dev.eu.auth0.com/api/v2/organizations/org_SunNyTSHhG4ib6E9/members?fields=user_id%2Croles&include_fields=true&include_totals=true&per_page=50
method: GET
response:
proto: HTTP/2.0
Expand All @@ -138,14 +210,50 @@ interactions:
trailer: {}
content_length: -1
uncompressed: true
body: '{"members":[{"user_id":"auth0|63d16bb11e39d8a43b8ed0f5","email":"chuck801@example.com","picture":"https://example-picture-url.jpg","name":"chuck801@example.com"}],"start":0,"limit":50,"total":1}'
body: '{"members":[{"user_id":"auth0|63d16bb11e39d8a43b8ed0f5","roles":[{"id":"rol_lEh5V8omwZrnZd9o","name":"test-role403"}]}],"start":0,"limit":50,"total":1}'
headers:
Content-Type:
- application/json; charset=utf-8
status: 200 OK
code: 200
duration: 116.271458ms
- id: 4
- id: 6
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 3
transfer_encoding: []
trailer: {}
host: go-auth0-dev.eu.auth0.com
remote_addr: ""
request_uri: ""
body: |
{}
form: {}
headers:
Content-Type:
- application/json
User-Agent:
- Go-Auth0-SDK/latest
url: https://go-auth0-dev.eu.auth0.com/api/v2/roles/rol_lEh5V8omwZrnZd9o
method: DELETE
response:
proto: HTTP/2.0
proto_major: 2
proto_minor: 0
transfer_encoding: []
trailer: {}
content_length: 2
uncompressed: false
body: '{}'
headers:
Content-Type:
- application/json; charset=utf-8
status: 200 OK
code: 200
duration: 107.122166ms
- id: 7
request:
proto: HTTP/1.1
proto_major: 1
Expand Down Expand Up @@ -180,7 +288,7 @@ interactions:
status: 204 No Content
code: 204
duration: 151.131042ms
- id: 5
- id: 8
request:
proto: HTTP/1.1
proto_major: 1
Expand Down

0 comments on commit 6aa68cc

Please sign in to comment.