use of io.gravitee.am.model.Role in project gravitee-access-management by gravitee-io.
the class UserInfoEndpointHandlerTest method shouldInvokeUserEndpoint_scopesRequest_roles_groups_full_profile.
@Test
public void shouldInvokeUserEndpoint_scopesRequest_roles_groups_full_profile() throws Exception {
JWT jwt = new JWT();
jwt.setJti("id-token");
jwt.setAud("client-id");
jwt.setSub("id-subject");
jwt.setScope("openid full_profile roles groups");
Client client = new Client();
client.setId("client-id");
client.setClientId("client-id");
Role role1 = new Role();
role1.setId("role1");
role1.setName("role-1");
Role role2 = new Role();
role2.setId("role2");
role2.setName("role-2");
router.route().order(-1).handler(createOAuth2AuthHandler(oAuth2AuthProvider(jwt, client)));
User user = createUser();
user.setRolesPermissions(new HashSet<>(Arrays.asList(role1, role2)));
user.setGroups(Arrays.asList("group-1", "group-2"));
when(userService.findById(anyString())).thenReturn(Maybe.just(user));
when(userService.enhance(user)).thenReturn(Single.just(user));
testRequest(HttpMethod.GET, "/userinfo", req -> req.putHeader(HttpHeaders.AUTHORIZATION, "Bearer test-token"), resp -> resp.bodyHandler(body -> {
final Map<String, Object> claims = Json.decodeValue(body.toString(), Map.class);
assertNotNull(claims);
assertEquals(23, claims.size());
assertTrue(claims.containsKey(CustomClaims.ROLES));
assertTrue(((List) claims.get(CustomClaims.ROLES)).containsAll(Arrays.asList("role-1", "role-2")));
assertTrue(claims.containsKey(CustomClaims.GROUPS));
assertTrue(((List) claims.get(CustomClaims.GROUPS)).containsAll(Arrays.asList("group-1", "group-2")));
assertTrue(claims.containsKey("custom-claim"));
assertTrue("gravitee".equals(claims.get("custom-claim")));
assertTrue("user-id".equals(claims.get("sub")));
}), HttpStatusCode.OK_200, "OK", null);
}
use of io.gravitee.am.model.Role in project gravitee-access-management by gravitee-io.
the class RolesResourceTest method shouldSearchRoles.
@Test
public void shouldSearchRoles() {
final String domainId = "domain-1";
final Domain mockDomain = new Domain();
mockDomain.setId(domainId);
final Role mockRole = new Role();
mockRole.setId("role-1-id");
mockRole.setName("role-1-name");
mockRole.setReferenceId(domainId);
final Role mockRole2 = new Role();
mockRole2.setId("role-2-id");
mockRole2.setName("role-2-name");
mockRole2.setReferenceId(domainId);
final Set<Role> roles = new HashSet<>(Arrays.asList(mockRole, mockRole2));
final Page<Role> pagedRoles = new Page<>(roles, 0, 2);
doReturn(Maybe.just(mockDomain)).when(domainService).findById(domainId);
doReturn(Single.just(roles)).when(roleService).findByDomain(domainId);
doReturn(Single.just(pagedRoles)).when(roleService).searchByDomain(domainId, "*role-2-name*", 0, 50);
final Response response = target("domains").path(domainId).path("roles").queryParam("q", "*role-2-name*").request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
JsonArray pageArray = new JsonObject(readEntity(response, String.class)).getJsonArray("data");
assertTrue(pageArray.size() == 2);
}
use of io.gravitee.am.model.Role in project gravitee-access-management by gravitee-io.
the class RolesResourceTest method shouldGetRoles.
@Test
public void shouldGetRoles() {
final String domainId = "domain-1";
final Domain mockDomain = new Domain();
mockDomain.setId(domainId);
final Role mockRole = new Role();
mockRole.setId("role-1-id");
mockRole.setName("role-1-name");
mockRole.setReferenceId(domainId);
final Role mockRole2 = new Role();
mockRole2.setId("role-2-id");
mockRole2.setName("role-2-name");
mockRole2.setReferenceId(domainId);
final Set<Role> roles = new HashSet<>(Arrays.asList(mockRole, mockRole2));
final Page<Role> pagedRoles = new Page<>(roles, 0, 2);
doReturn(Maybe.just(mockDomain)).when(domainService).findById(domainId);
doReturn(Single.just(pagedRoles)).when(roleService).findByDomain(domainId, 0, 50);
final Response response = target("domains").path(domainId).path("roles").request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
JsonArray pageArray = new JsonObject(readEntity(response, String.class)).getJsonArray("data");
assertTrue(pageArray.size() == 2);
}
use of io.gravitee.am.model.Role in project gravitee-access-management by gravitee-io.
the class RolesResourceTest method shouldCreate.
@Test
public void shouldCreate() {
final String domainId = "domain-1";
final Domain mockDomain = new Domain();
mockDomain.setId(domainId);
NewRole newRole = new NewRole();
newRole.setName("role-name");
Role role = new Role();
role.setId("role-id");
role.setName("role-name");
doReturn(Maybe.just(mockDomain)).when(domainService).findById(domainId);
doReturn(Single.just(role)).when(roleService).create(eq(domainId), any(), any());
final Response response = target("domains").path(domainId).path("roles").request().post(Entity.json(newRole));
assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
}
use of io.gravitee.am.model.Role in project gravitee-access-management by gravitee-io.
the class JdbcRoleRepository method search.
@Override
public Single<Page<Role>> search(ReferenceType referenceType, String referenceId, String query, int page, int size) {
LOGGER.debug("search({}, {}, {}, {}, {})", referenceType, referenceId, query, page, size);
boolean wildcardSearch = query.contains("*");
String wildcardValue = query.replaceAll("\\*+", "%");
String search = this.databaseDialectHelper.buildSearchRoleQuery(wildcardSearch, page, size);
String count = this.databaseDialectHelper.buildCountRoleQuery(wildcardSearch);
return fluxToFlowable(template.getDatabaseClient().sql(search).bind("value", wildcardSearch ? wildcardValue : query).bind("refId", referenceId).bind("refType", referenceType.name()).map(row -> rowMapper.read(JdbcRole.class, row)).all()).map(this::toEntity).flatMap(role -> completeWithScopes(Maybe.just(role), role.getId()).toFlowable()).toList().flatMap(data -> monoToSingle(template.getDatabaseClient().sql(count).bind("value", wildcardSearch ? wildcardValue : query).bind("refId", referenceId).bind("refType", referenceType.name()).map(row -> row.get(0, Long.class)).first()).map(total -> new Page<Role>(data, page, total)));
}
Aggregations