use of org.camunda.bpm.engine.identity.UserQuery in project camunda-bpm-platform by camunda.
the class TaskRestServiceInteractionTest method testGetSingleTaskHal.
@Test
@SuppressWarnings("unchecked")
public void testGetSingleTaskHal() {
// setup user query mock
List<User> mockUsers = Arrays.asList(MockProvider.mockUser().id(EXAMPLE_TASK_ASSIGNEE_NAME).build(), MockProvider.mockUser().id(EXAMPLE_TASK_OWNER).build());
UserQuery sampleUserQuery = mock(UserQuery.class);
when(sampleUserQuery.userIdIn(eq(EXAMPLE_TASK_ASSIGNEE_NAME), eq(EXAMPLE_TASK_OWNER))).thenReturn(sampleUserQuery);
when(sampleUserQuery.userIdIn(eq(EXAMPLE_TASK_OWNER), eq(EXAMPLE_TASK_ASSIGNEE_NAME))).thenReturn(sampleUserQuery);
when(sampleUserQuery.listPage(eq(0), eq(2))).thenReturn(mockUsers);
when(sampleUserQuery.count()).thenReturn((long) mockUsers.size());
when(processEngine.getIdentityService().createUserQuery()).thenReturn(sampleUserQuery);
// setup group query mock
List<Group> mockGroups = Arrays.asList(MockProvider.mockGroup().id(mockCandidateGroupIdentityLink.getGroupId()).build(), MockProvider.mockGroup().id(mockCandidateGroup2IdentityLink.getGroupId()).build());
GroupQuery sampleGroupQuery = mock(GroupQuery.class);
when(sampleGroupQuery.groupIdIn(eq(EXAMPLE_GROUP_ID), eq(EXAMPLE_GROUP_ID2))).thenReturn(sampleGroupQuery);
when(sampleGroupQuery.groupIdIn(eq(EXAMPLE_GROUP_ID2), eq(EXAMPLE_GROUP_ID))).thenReturn(sampleGroupQuery);
when(sampleGroupQuery.listPage(eq(0), eq(2))).thenReturn(mockGroups);
when(sampleGroupQuery.count()).thenReturn((long) mockGroups.size());
when(processEngine.getIdentityService().createGroupQuery()).thenReturn(sampleGroupQuery);
// setup process definition query mock
List<ProcessDefinition> mockDefinitions = MockProvider.createMockDefinitions();
ProcessDefinitionQuery sampleProcessDefinitionQuery = mock(ProcessDefinitionQuery.class);
when(sampleProcessDefinitionQuery.listPage(0, 1)).thenReturn(mockDefinitions);
when(sampleProcessDefinitionQuery.processDefinitionIdIn(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID)).thenReturn(sampleProcessDefinitionQuery);
when(sampleProcessDefinitionQuery.count()).thenReturn(1l);
when(processEngine.getRepositoryService().createProcessDefinitionQuery()).thenReturn(sampleProcessDefinitionQuery);
// setup case definition query mock
List<CaseDefinition> mockCaseDefinitions = MockProvider.createMockCaseDefinitions();
CaseDefinitionQuery sampleCaseDefinitionQuery = mock(CaseDefinitionQuery.class);
when(sampleCaseDefinitionQuery.listPage(0, 1)).thenReturn(mockCaseDefinitions);
when(sampleCaseDefinitionQuery.caseDefinitionIdIn(MockProvider.EXAMPLE_CASE_DEFINITION_ID)).thenReturn(sampleCaseDefinitionQuery);
when(sampleCaseDefinitionQuery.count()).thenReturn(1l);
when(processEngine.getRepositoryService().createCaseDefinitionQuery()).thenReturn(sampleCaseDefinitionQuery);
Response response = given().header("accept", Hal.APPLICATION_HAL_JSON).pathParam("id", EXAMPLE_TASK_ID).then().expect().statusCode(Status.OK.getStatusCode()).body("id", equalTo(EXAMPLE_TASK_ID)).body("name", equalTo(MockProvider.EXAMPLE_TASK_NAME)).body("assignee", equalTo(MockProvider.EXAMPLE_TASK_ASSIGNEE_NAME)).body("created", equalTo(MockProvider.EXAMPLE_TASK_CREATE_TIME)).body("due", equalTo(MockProvider.EXAMPLE_TASK_DUE_DATE)).body("delegationState", equalTo(MockProvider.EXAMPLE_TASK_DELEGATION_STATE.toString())).body("description", equalTo(MockProvider.EXAMPLE_TASK_DESCRIPTION)).body("executionId", equalTo(MockProvider.EXAMPLE_TASK_EXECUTION_ID)).body("owner", equalTo(MockProvider.EXAMPLE_TASK_OWNER)).body("parentTaskId", equalTo(MockProvider.EXAMPLE_TASK_PARENT_TASK_ID)).body("priority", equalTo(MockProvider.EXAMPLE_TASK_PRIORITY)).body("processDefinitionId", equalTo(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID)).body("processInstanceId", equalTo(MockProvider.EXAMPLE_PROCESS_INSTANCE_ID)).body("taskDefinitionKey", equalTo(MockProvider.EXAMPLE_TASK_DEFINITION_KEY)).body("suspended", equalTo(MockProvider.EXAMPLE_TASK_SUSPENSION_STATE)).body("caseExecutionId", equalTo(MockProvider.EXAMPLE_CASE_EXECUTION_ID)).body("caseInstanceId", equalTo(MockProvider.EXAMPLE_CASE_INSTANCE_ID)).body("caseDefinitionId", equalTo(MockProvider.EXAMPLE_CASE_DEFINITION_ID)).body("tenantId", equalTo(MockProvider.EXAMPLE_TENANT_ID)).body("_links.assignee.href", endsWith(EXAMPLE_TASK_ASSIGNEE_NAME)).body("_links.caseDefinition.href", endsWith(EXAMPLE_CASE_DEFINITION_ID)).body("_links.caseExecution.href", endsWith(EXAMPLE_CASE_EXECUTION_ID)).body("_links.caseInstance.href", endsWith(EXAMPLE_CASE_INSTANCE_ID)).body("_links.execution.href", endsWith(EXAMPLE_TASK_EXECUTION_ID)).body("_links.owner.href", endsWith(EXAMPLE_TASK_OWNER)).body("_links.parentTask.href", endsWith(EXAMPLE_TASK_PARENT_TASK_ID)).body("_links.processDefinition.href", endsWith(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID)).body("_links.processInstance.href", endsWith(MockProvider.EXAMPLE_PROCESS_INSTANCE_ID)).body("_links.identityLink.href", endsWith("/task/" + EXAMPLE_TASK_ID + "/identity-links")).body("_links.self.href", endsWith(EXAMPLE_TASK_ID)).when().get(SINGLE_TASK_URL);
String content = response.asString();
// validate embedded users:
List<Map<String, Object>> embeddedUsers = from(content).getList("_embedded.user");
Assert.assertEquals("There should be two users returned.", 2, embeddedUsers.size());
Map<String, Object> embeddedUser = embeddedUsers.get(0);
assertNotNull("The returned user should not be null.", embeddedUser);
assertEquals(MockProvider.EXAMPLE_TASK_ASSIGNEE_NAME, embeddedUser.get("id"));
assertEquals(MockProvider.EXAMPLE_USER_FIRST_NAME, embeddedUser.get("firstName"));
assertEquals(MockProvider.EXAMPLE_USER_LAST_NAME, embeddedUser.get("lastName"));
assertEquals(MockProvider.EXAMPLE_USER_EMAIL, embeddedUser.get("email"));
assertNull(embeddedUser.get("_embedded"));
Map<String, Object> links = (Map<String, Object>) embeddedUser.get("_links");
assertEquals(1, links.size());
assertHalLink(links, "self", UserRestService.PATH + "/" + MockProvider.EXAMPLE_TASK_ASSIGNEE_NAME);
embeddedUser = embeddedUsers.get(1);
assertNotNull("The returned user should not be null.", embeddedUser);
assertEquals(MockProvider.EXAMPLE_TASK_OWNER, embeddedUser.get("id"));
assertEquals(MockProvider.EXAMPLE_USER_FIRST_NAME, embeddedUser.get("firstName"));
assertEquals(MockProvider.EXAMPLE_USER_LAST_NAME, embeddedUser.get("lastName"));
assertEquals(MockProvider.EXAMPLE_USER_EMAIL, embeddedUser.get("email"));
assertNull(embeddedUser.get("_embedded"));
links = (Map<String, Object>) embeddedUser.get("_links");
assertEquals(1, links.size());
assertHalLink(links, "self", UserRestService.PATH + "/" + MockProvider.EXAMPLE_TASK_OWNER);
// validate embedded groups:
List<Map<String, Object>> embeddedGroups = from(content).getList("_embedded.group");
Assert.assertEquals("There should be two groups returned.", 2, embeddedGroups.size());
Map<String, Object> embeddedGroup = embeddedGroups.get(0);
assertNotNull("The returned group should not be null.", embeddedGroup);
assertEquals(MockProvider.EXAMPLE_GROUP_ID, embeddedGroup.get("id"));
assertEquals(MockProvider.EXAMPLE_GROUP_NAME, embeddedGroup.get("name"));
assertEquals(MockProvider.EXAMPLE_GROUP_TYPE, embeddedGroup.get("type"));
assertNull(embeddedGroup.get("_embedded"));
links = (Map<String, Object>) embeddedGroup.get("_links");
assertEquals(1, links.size());
assertHalLink(links, "self", GroupRestService.PATH + "/" + MockProvider.EXAMPLE_GROUP_ID);
embeddedGroup = embeddedGroups.get(1);
assertNotNull("The returned group should not be null.", embeddedGroup);
assertEquals(MockProvider.EXAMPLE_GROUP_ID2, embeddedGroup.get("id"));
assertEquals(MockProvider.EXAMPLE_GROUP_NAME, embeddedGroup.get("name"));
assertEquals(MockProvider.EXAMPLE_GROUP_TYPE, embeddedGroup.get("type"));
assertNull(embeddedGroup.get("_embedded"));
links = (Map<String, Object>) embeddedGroup.get("_links");
assertEquals(1, links.size());
assertHalLink(links, "self", GroupRestService.PATH + "/" + MockProvider.EXAMPLE_GROUP_ID2);
// validate embedded processDefinitions:
List<Map<String, Object>> embeddedDefinitions = from(content).getList("_embedded.processDefinition");
Assert.assertEquals("There should be one processDefinition returned.", 1, embeddedDefinitions.size());
Map<String, Object> embeddedProcessDefinition = embeddedDefinitions.get(0);
Assert.assertNotNull("The returned processDefinition should not be null.", embeddedProcessDefinition);
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID, embeddedProcessDefinition.get("id"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_DEFINITION_KEY, embeddedProcessDefinition.get("key"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_DEFINITION_CATEGORY, embeddedProcessDefinition.get("category"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_DEFINITION_NAME, embeddedProcessDefinition.get("name"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_DEFINITION_DESCRIPTION, embeddedProcessDefinition.get("description"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_DEFINITION_VERSION, embeddedProcessDefinition.get("version"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_DEFINITION_RESOURCE_NAME, embeddedProcessDefinition.get("resource"));
Assert.assertEquals(MockProvider.EXAMPLE_DEPLOYMENT_ID, embeddedProcessDefinition.get("deploymentId"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_DEFINITION_DIAGRAM_RESOURCE_NAME, embeddedProcessDefinition.get("diagram"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_DEFINITION_IS_SUSPENDED, embeddedProcessDefinition.get("suspended"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_APPLICATION_CONTEXT_PATH, embeddedProcessDefinition.get("contextPath"));
links = (Map<String, Object>) embeddedProcessDefinition.get("_links");
Assert.assertEquals(3, links.size());
assertHalLink(links, "self", "/process-definition/" + MockProvider.EXAMPLE_PROCESS_DEFINITION_ID);
assertHalLink(links, "deployment", "/deployment/" + MockProvider.EXAMPLE_DEPLOYMENT_ID);
assertHalLink(links, "resource", "/deployment/" + MockProvider.EXAMPLE_DEPLOYMENT_ID + "/resources/" + MockProvider.EXAMPLE_PROCESS_DEFINITION_RESOURCE_NAME);
// validate embedded caseDefinitions:
List<Map<String, Object>> embeddedCaseDefinitions = from(content).getList("_embedded.caseDefinition");
Assert.assertEquals("There should be one caseDefinition returned.", 1, embeddedCaseDefinitions.size());
Map<String, Object> embeddedCaseDefinition = embeddedCaseDefinitions.get(0);
Assert.assertNotNull("The returned caseDefinition should not be null.", embeddedCaseDefinition);
Assert.assertEquals(MockProvider.EXAMPLE_CASE_DEFINITION_ID, embeddedCaseDefinition.get("id"));
Assert.assertEquals(MockProvider.EXAMPLE_CASE_DEFINITION_KEY, embeddedCaseDefinition.get("key"));
Assert.assertEquals(MockProvider.EXAMPLE_CASE_DEFINITION_CATEGORY, embeddedCaseDefinition.get("category"));
Assert.assertEquals(MockProvider.EXAMPLE_CASE_DEFINITION_NAME, embeddedCaseDefinition.get("name"));
Assert.assertEquals(MockProvider.EXAMPLE_CASE_DEFINITION_VERSION, embeddedCaseDefinition.get("version"));
Assert.assertEquals(MockProvider.EXAMPLE_CASE_DEFINITION_RESOURCE_NAME, embeddedCaseDefinition.get("resource"));
Assert.assertEquals(MockProvider.EXAMPLE_DEPLOYMENT_ID, embeddedCaseDefinition.get("deploymentId"));
Assert.assertEquals(MockProvider.EXAMPLE_PROCESS_APPLICATION_CONTEXT_PATH, embeddedCaseDefinition.get("contextPath"));
links = (Map<String, Object>) embeddedCaseDefinition.get("_links");
Assert.assertEquals(3, links.size());
assertHalLink(links, "self", "/case-definition/" + MockProvider.EXAMPLE_CASE_DEFINITION_ID);
assertHalLink(links, "deployment", "/deployment/" + MockProvider.EXAMPLE_DEPLOYMENT_ID);
assertHalLink(links, "resource", "/deployment/" + MockProvider.EXAMPLE_DEPLOYMENT_ID + "/resources/" + MockProvider.EXAMPLE_CASE_DEFINITION_RESOURCE_NAME);
// validate embedded identity links
List<Map<String, Object>> embeddedIdentityLinks = from(content).getList("_embedded.identityLink");
assertEquals("There should be three identityLink returned", 4, embeddedIdentityLinks.size());
assertEmbeddedIdentityLink(mockAssigneeIdentityLink, embeddedIdentityLinks.get(0));
assertEmbeddedIdentityLink(mockOwnerIdentityLink, embeddedIdentityLinks.get(1));
assertEmbeddedIdentityLink(mockCandidateGroupIdentityLink, embeddedIdentityLinks.get(2));
assertEmbeddedIdentityLink(mockCandidateGroup2IdentityLink, embeddedIdentityLinks.get(3));
}
use of org.camunda.bpm.engine.identity.UserQuery in project camunda-bpm-platform by camunda.
the class UserRestServiceInteractionTest method testChangeCredentialsWithWrongAuthenticatedUserPassword.
@Test
public void testChangeCredentialsWithWrongAuthenticatedUserPassword() {
User initialUser = MockProvider.createMockUser();
UserQuery sampleUserQuery = mock(UserQuery.class);
when(identityServiceMock.createUserQuery()).thenReturn(sampleUserQuery);
when(sampleUserQuery.userId(MockProvider.EXAMPLE_USER_ID)).thenReturn(sampleUserQuery);
when(sampleUserQuery.singleResult()).thenReturn(initialUser);
Authentication authentication = MockProvider.createMockAuthentication();
when(identityServiceMock.getCurrentAuthentication()).thenReturn(authentication);
when(identityServiceMock.checkPassword(MockProvider.EXAMPLE_USER_ID, MockProvider.EXAMPLE_USER_PASSWORD)).thenReturn(false);
UserCredentialsDto dto = new UserCredentialsDto();
dto.setPassword("new-password");
dto.setAuthenticatedUserPassword(MockProvider.EXAMPLE_USER_PASSWORD);
given().pathParam("id", MockProvider.EXAMPLE_USER_ID).contentType(ContentType.JSON).body(dto).then().statusCode(Status.BAD_REQUEST.getStatusCode()).contentType(ContentType.JSON).body("type", equalTo("InvalidRequestException")).body("message", equalTo("The given authenticated user password is not valid.")).when().put(USER_CREDENTIALS_URL);
}
use of org.camunda.bpm.engine.identity.UserQuery in project camunda-bpm-platform by camunda.
the class UserRestServiceInteractionTest method testPutCredentialsNonExistingUserFails.
@Test
public void testPutCredentialsNonExistingUserFails() {
UserQuery sampleUserQuery = mock(UserQuery.class);
when(identityServiceMock.createUserQuery()).thenReturn(sampleUserQuery);
when(sampleUserQuery.userId("aNonExistingUser")).thenReturn(sampleUserQuery);
when(sampleUserQuery.singleResult()).thenReturn(null);
UserCredentialsDto dto = new UserCredentialsDto();
dto.setPassword("new-password");
given().pathParam("id", "aNonExistingUser").body(dto).contentType(ContentType.JSON).then().then().expect().statusCode(Status.NOT_FOUND.getStatusCode()).contentType(ContentType.JSON).body("type", equalTo(InvalidRequestException.class.getSimpleName())).body("message", equalTo("User with id aNonExistingUser does not exist")).when().put(USER_CREDENTIALS_URL);
// user was not updated
verify(identityServiceMock, never()).saveUser(any(User.class));
}
use of org.camunda.bpm.engine.identity.UserQuery in project camunda-bpm-platform by camunda.
the class UserRestServiceInteractionTest method testUserResourceOptionsDeleteAuthorized.
@Test
public void testUserResourceOptionsDeleteAuthorized() {
String fullUserUrl = "http://localhost:" + PORT + TEST_RESOURCE_ROOT_PATH + "/user/" + MockProvider.EXAMPLE_USER_ID;
User sampleUser = MockProvider.createMockUser();
UserQuery sampleUserQuery = mock(UserQuery.class);
when(identityServiceMock.createUserQuery()).thenReturn(sampleUserQuery);
when(sampleUserQuery.userId(MockProvider.EXAMPLE_USER_ID)).thenReturn(sampleUserQuery);
when(sampleUserQuery.singleResult()).thenReturn(sampleUser);
Authentication authentication = new Authentication(MockProvider.EXAMPLE_USER_ID, null);
when(identityServiceMock.getCurrentAuthentication()).thenReturn(authentication);
when(authorizationServiceMock.isUserAuthorized(MockProvider.EXAMPLE_USER_ID, null, DELETE, USER, MockProvider.EXAMPLE_USER_ID)).thenReturn(true);
when(authorizationServiceMock.isUserAuthorized(MockProvider.EXAMPLE_USER_ID, null, UPDATE, USER, MockProvider.EXAMPLE_USER_ID)).thenReturn(false);
when(processEngineConfigurationMock.isAuthorizationEnabled()).thenReturn(true);
given().pathParam("id", MockProvider.EXAMPLE_USER_ID).then().statusCode(Status.OK.getStatusCode()).body("links[0].href", equalTo(fullUserUrl + "/profile")).body("links[0].method", equalTo(HttpMethod.GET)).body("links[0].rel", equalTo("self")).body("links[1].href", equalTo(fullUserUrl)).body("links[1].method", equalTo(HttpMethod.DELETE)).body("links[1].rel", equalTo("delete")).body("links[2]", nullValue()).when().options(USER_URL);
verify(identityServiceMock, times(2)).getCurrentAuthentication();
verify(authorizationServiceMock, times(1)).isUserAuthorized(MockProvider.EXAMPLE_USER_ID, null, DELETE, USER, MockProvider.EXAMPLE_USER_ID);
verify(authorizationServiceMock, times(1)).isUserAuthorized(MockProvider.EXAMPLE_USER_ID, null, UPDATE, USER, MockProvider.EXAMPLE_USER_ID);
}
use of org.camunda.bpm.engine.identity.UserQuery in project camunda-bpm-platform by camunda.
the class UserRestServiceInteractionTest method testPutProfileThrowsAuthorizationException.
@Test
public void testPutProfileThrowsAuthorizationException() {
User initialUser = MockProvider.createMockUser();
User userUpdate = MockProvider.createMockUserUpdate();
UserQuery sampleUserQuery = mock(UserQuery.class);
when(identityServiceMock.createUserQuery()).thenReturn(sampleUserQuery);
when(sampleUserQuery.userId(MockProvider.EXAMPLE_USER_ID)).thenReturn(sampleUserQuery);
when(sampleUserQuery.singleResult()).thenReturn(initialUser);
String message = "exception expected";
doThrow(new AuthorizationException(message)).when(identityServiceMock).saveUser(any(User.class));
UserProfileDto updateDto = UserProfileDto.fromUser(userUpdate);
given().pathParam("id", MockProvider.EXAMPLE_USER_ID).body(updateDto).contentType(ContentType.JSON).then().statusCode(Status.FORBIDDEN.getStatusCode()).contentType(ContentType.JSON).body("type", equalTo(AuthorizationException.class.getSimpleName())).body("message", equalTo(message)).when().put(USER_PROFILE_URL);
}
Aggregations