Skip to content

Commit

Permalink
[SDK-4654] support organization get member roles (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyjames authored Oct 25, 2023
1 parent c930419 commit a1b7ffc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/main/java/com/auth0/client/mgmt/OrganizationsEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ public Request<Void> delete(String orgId) {
* @see <a href="https://auth0.com/docs/api/management/v2#!/Organizations/get_members">https://auth0.com/docs/api/management/v2#!/Organizations/get_members</a>
*/
public Request<MembersPage> 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.
* <br/>
* 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 <a href="https://auth0.com/docs/api/management/v2#!/Organizations/get_members">https://auth0.com/docs/api/management/v2#!/Organizations/get_members</a>
*/
public Request<MembersPage> getMembers(String orgId, PageFilter pageFilter, FieldsFilter fieldsFilter) {
Asserts.assertNotNull(orgId, "organization ID");

HttpUrl.Builder builder = baseUrl
Expand All @@ -192,7 +208,8 @@ public Request<MembersPage> 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<MembersPage>() {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/auth0/json/mgmt/organizations/Member.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +23,8 @@ public class Member {
private String picture;
@JsonProperty("name")
private String name;
@JsonProperty("roles")
private List<Role> roles;

/**
* @return the user ID of this Member.
Expand Down Expand Up @@ -75,4 +80,8 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

public List<Role> getRoles() {
return roles;
}
}
45 changes: 45 additions & 0 deletions src/test/java/com/auth0/client/mgmt/OrganizationEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<MembersPage> 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);
Expand All @@ -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<MembersPage> 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);
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/com/auth0/json/mgmt/organizations/MembersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ public void shouldDeserialize() throws Exception {
" \"user_id\": \"user_123\",\n" +
" \"email\": \"[email protected]\",\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);
Expand All @@ -39,6 +45,8 @@ public void shouldDeserialize() throws Exception {
assertThat(member.getEmail(), is("[email protected]"));
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"));
}
}

0 comments on commit a1b7ffc

Please sign in to comment.