From 693d8dfab6ce21b6f15b86539f7e0b3f39857cfb Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Tue, 24 Oct 2023 15:58:41 -0500 Subject: [PATCH] [SDK-4654] support organization get member roles --- .../client/mgmt/OrganizationsEntity.java | 19 +++++++- .../auth0/json/mgmt/organizations/Member.java | 9 ++++ .../client/mgmt/OrganizationEntityTest.java | 45 +++++++++++++++++++ .../json/mgmt/organizations/MembersTest.java | 12 ++++- 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/auth0/client/mgmt/OrganizationsEntity.java b/src/main/java/com/auth0/client/mgmt/OrganizationsEntity.java index 1a1d5060..94f20d2b 100644 --- a/src/main/java/com/auth0/client/mgmt/OrganizationsEntity.java +++ b/src/main/java/com/auth0/client/mgmt/OrganizationsEntity.java @@ -184,6 +184,22 @@ public Request delete(String orgId) { * @see https://auth0.com/docs/api/management/v2#!/Organizations/get_members */ public Request getMembers(String orgId, PageFilter filter) { + return getMembers(orgId, filter, null); + } + + /** + * Get the members of an organization. A token with {@code read:organization_members} scope is required. + *
+ * Member roles are not sent by default. Supply a {@linkplain FieldsFilter} that includes "roles" (and {@code includeFields = true} to retrieve the roles assigned to each listed member. To include the roles in the response, you must include the {@code read:organization_member_roles} scope in the token. + * + * @param orgId the ID of the organization + * @param pageFilter an optional pagination filter + * @param fieldsFilter an optional fields filter. If null, all fields (except roles) are returned. + * @return a Request to execute + * + * @see https://auth0.com/docs/api/management/v2#!/Organizations/get_members + */ + public Request getMembers(String orgId, PageFilter pageFilter, FieldsFilter fieldsFilter) { Asserts.assertNotNull(orgId, "organization ID"); HttpUrl.Builder builder = baseUrl @@ -192,7 +208,8 @@ public Request getMembers(String orgId, PageFilter filter) { .addPathSegment(orgId) .addPathSegment("members"); - applyFilter(filter, builder); + applyFilter(pageFilter, builder); + applyFilter(fieldsFilter, builder); String url = builder.build().toString(); return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference() { diff --git a/src/main/java/com/auth0/json/mgmt/organizations/Member.java b/src/main/java/com/auth0/json/mgmt/organizations/Member.java index 74a4fbbe..5c1f073f 100644 --- a/src/main/java/com/auth0/json/mgmt/organizations/Member.java +++ b/src/main/java/com/auth0/json/mgmt/organizations/Member.java @@ -1,9 +1,12 @@ package com.auth0.json.mgmt.organizations; +import com.auth0.json.mgmt.roles.Role; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.List; + /** * Represents the member of an organization. * @see com.auth0.client.mgmt.OrganizationsEntity @@ -20,6 +23,8 @@ public class Member { private String picture; @JsonProperty("name") private String name; + @JsonProperty("roles") + private List roles; /** * @return the user ID of this Member. @@ -75,4 +80,8 @@ public String getName() { public void setName(String name) { this.name = name; } + + public List getRoles() { + return roles; + } } diff --git a/src/test/java/com/auth0/client/mgmt/OrganizationEntityTest.java b/src/test/java/com/auth0/client/mgmt/OrganizationEntityTest.java index a66f46f3..6eb1d8ec 100644 --- a/src/test/java/com/auth0/client/mgmt/OrganizationEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/OrganizationEntityTest.java @@ -297,6 +297,27 @@ public void shouldListOrgMembersWithoutFilter() throws Exception { assertThat(response.getItems(), hasSize(3)); } + @Test + public void shouldListOrgMembersWithFieldsFilter() throws Exception { + FieldsFilter fieldsFilter = new FieldsFilter().withFields("name,email,user_id,roles", true); + + Request request = api.organizations().getMembers("org_abc", null, fieldsFilter); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MockServer.ORGANIZATION_MEMBERS_LIST, 200); + MembersPage response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/organizations/org_abc/members")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + assertThat(recordedRequest, hasQueryParameter("fields", "name,email,user_id,roles")); + assertThat(recordedRequest, hasQueryParameter("include_fields", "true")); + + assertThat(response, is(notNullValue())); + assertThat(response.getItems(), hasSize(3)); + } + @Test public void shouldListOrgMembersWithPage() throws Exception { PageFilter filter = new PageFilter().withPage(0, 20); @@ -317,6 +338,30 @@ public void shouldListOrgMembersWithPage() throws Exception { assertThat(response.getItems(), hasSize(3)); } + @Test + public void shouldListOrgMembersWithFieldsFilterAndPageFilter() throws Exception { + PageFilter pageFilter = new PageFilter().withPage(0, 20); + FieldsFilter fieldsFilter = new FieldsFilter().withFields("name,email,user_id,roles", true); + + Request request = api.organizations().getMembers("org_abc", pageFilter, fieldsFilter); + assertThat(request, is(notNullValue())); + + server.jsonResponse(MockServer.ORGANIZATION_MEMBERS_LIST, 200); + MembersPage response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/organizations/org_abc/members")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + assertThat(recordedRequest, hasQueryParameter("fields", "name,email,user_id,roles")); + assertThat(recordedRequest, hasQueryParameter("include_fields", "true")); + assertThat(recordedRequest, hasQueryParameter("page", "0")); + assertThat(recordedRequest, hasQueryParameter("per_page", "20")); + + assertThat(response, is(notNullValue())); + assertThat(response.getItems(), hasSize(3)); + } + @Test public void shouldListOrgMembersWithTotals() throws Exception { PageFilter filter = new PageFilter().withTotals(true); diff --git a/src/test/java/com/auth0/json/mgmt/organizations/MembersTest.java b/src/test/java/com/auth0/json/mgmt/organizations/MembersTest.java index 37c05995..a70c7e35 100644 --- a/src/test/java/com/auth0/json/mgmt/organizations/MembersTest.java +++ b/src/test/java/com/auth0/json/mgmt/organizations/MembersTest.java @@ -30,7 +30,13 @@ public void shouldDeserialize() throws Exception { " \"user_id\": \"user_123\",\n" + " \"email\": \"fred@domain.com\",\n" + " \"picture\": \"https://profilepic.com/mypic.png\",\n" + - " \"name\": \"fred\"\n" + + " \"name\": \"fred\",\n" + + " \"roles\": [\n" + + " {\n" + + " \"id\": \"rol_abc\",\n" + + " \"name\": \"test role\"\n" + + " }\n" + + " ]" + "}"; Member member = fromJSON(memberJson, Member.class); @@ -39,6 +45,8 @@ public void shouldDeserialize() throws Exception { assertThat(member.getEmail(), is("fred@domain.com")); assertThat(member.getPicture(), is("https://profilepic.com/mypic.png")); assertThat(member.getName(), is("fred")); - + assertThat(member.getRoles().size(), is(1)); + assertThat(member.getRoles().get(0).getName(), is("test role")); + assertThat(member.getRoles().get(0).getId(), is("rol_abc")); } }