Skip to content

Commit

Permalink
Merge pull request #199 from jakbutler/issue/198-adding-roles-to-user
Browse files Browse the repository at this point in the history
Fix for issue #198: Core Authorization fails when adding Roles to User
  • Loading branch information
lbalmaceda authored Apr 25, 2019
2 parents 8374d8a + b444ec2 commit 3e6f295
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
20 changes: 10 additions & 10 deletions src/main/java/com/auth0/client/mgmt/UsersEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,15 @@ public Request<RolesPage> listRoles(String userId, PageFilter filter) {
* See https://auth0.com/docs/api/management/v2#!/Users/delete_user_roles
*
* @param userId the user id
* @param roles a list of role objects to remove from the user
* @param roleIds a list of role ids to remove from the user
* @return a Request to execute
*/
public Request removeRoles(String userId, List<Role> roles) {
public Request removeRoles(String userId, List<String> roleIds) {
Asserts.assertNotNull(userId, "user id");
Asserts.assertNotEmpty(roles, "roles");
Asserts.assertNotEmpty(roleIds, "role ids");

Map<String, List<Role>> body = new HashMap<>();
body.put("roles", roles);
Map<String, List<String>> body = new HashMap<>();
body.put("roles", roleIds);

final String url = baseUrl
.newBuilder()
Expand All @@ -520,15 +520,15 @@ public Request removeRoles(String userId, List<Role> roles) {
* See https://auth0.com/docs/api/management/v2#!/Users/post_user_roles
*
* @param userId the user id
* @param roles a list of role objects to assign to the user
* @param roleIds a list of role ids to assign to the user
* @return a Request to execute
*/
public Request addRoles(String userId, List<Role> roles) {
public Request addRoles(String userId, List<String> roleIds) {
Asserts.assertNotNull(userId, "user id");
Asserts.assertNotEmpty(roles, "roles");
Asserts.assertNotEmpty(roleIds, "role ids");

Map<String, List<Role>> body = new HashMap<>();
body.put("roles", roles);
Map<String, List<String>> body = new HashMap<>();
body.put("roles", roleIds);

final String url = baseUrl
.newBuilder()
Expand Down
18 changes: 6 additions & 12 deletions src/test/java/com/auth0/client/mgmt/UsersEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -739,23 +739,20 @@ public void shouldThrowOnAddRolesWithNullId() throws Exception {
@Test
public void shouldThrowOnAddRolesWithNullList() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'roles' cannot be null!");
exception.expectMessage("'role ids' cannot be null!");
api.users().addRoles("1", null);
}

@Test
public void shouldThrowOnAddRolesWithEmptyList() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'roles' cannot be empty!");
exception.expectMessage("'role ids' cannot be empty!");
api.users().addRoles("1", Collections.emptyList());
}

@Test
public void shouldAddRoles() throws Exception {
Role role = new Role();
role.setName("roleId");

Request request = api.users().addRoles("1", Collections.singletonList(role));
Request request = api.users().addRoles("1", Collections.singletonList("roleId"));
assertThat(request, is(notNullValue()));

server.emptyResponse(200);
Expand Down Expand Up @@ -783,23 +780,20 @@ public void shouldThrowOnRemoveRolesWithNullId() throws Exception {
@Test
public void shouldThrowOnRemoveRolesWithNullList() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'roles' cannot be null!");
exception.expectMessage("'role ids' cannot be null!");
api.users().removeRoles("1", null);
}

@Test
public void shouldThrowOnRemoveRolesWithEmptyList() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'roles' cannot be empty!");
exception.expectMessage("'role ids' cannot be empty!");
api.users().removeRoles("1", Collections.emptyList());
}

@Test
public void shouldRemoveRoles() throws Exception {
Role role = new Role();
role.setName("roleId");

Request request = api.users().removeRoles("1", Collections.singletonList(role));
Request request = api.users().removeRoles("1", Collections.singletonList("roleId"));
assertThat(request, is(notNullValue()));

server.emptyResponse( 200);
Expand Down

0 comments on commit 3e6f295

Please sign in to comment.