use of org.alfresco.rest.api.tests.RepoService.TestNetwork in project alfresco-remote-api by Alfresco.
the class TestUserPreferences method testUserPreferences.
@Test
public void testUserPreferences() throws Exception {
Iterator<TestNetwork> networksIt = getTestFixture().getNetworksIt();
assertTrue(networksIt.hasNext());
final TestNetwork network1 = networksIt.next();
assertTrue(networksIt.hasNext());
final TestNetwork network2 = networksIt.next();
final List<TestPerson> people = new ArrayList<TestPerson>(3);
// create users and some preferences
TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
TestPerson person = network1.createUser();
people.add(person);
person = network1.createUser();
people.add(person);
return null;
}
}, network1.getId());
TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
TestPerson person = network2.createUser();
people.add(person);
return null;
}
}, network2.getId());
final TestPerson person1 = people.get(0);
final TestPerson person2 = people.get(1);
final TestPerson person3 = people.get(2);
final List<Preference> expectedPreferences = new ArrayList<Preference>();
expectedPreferences.add(new Preference("org.alfresco.share.documentList.testPreference2", String.valueOf(true)));
expectedPreferences.add(new Preference("org.alfresco.share.documentList.testPreference1", String.valueOf(true)));
expectedPreferences.add(new Preference("org.alfresco.share.documentList.sortAscending", String.valueOf(true)));
expectedPreferences.add(new Preference("org.alfresco.share.documentList.testPreference3", String.valueOf(true)));
// new preference name for issue REPO-855
expectedPreferences.add(new Preference("org.alfresco.ext.folders.favourites.workspace://SpacesStore/4e3d0779-388a-4b94-91e1-eab588a7da3d.createdAt", String.valueOf(true)));
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
for (Preference pref : expectedPreferences) {
// TODO add preferences thru api
repoService.addPreference(person1.getId(), pref.getId(), pref.getValue());
}
return null;
}
}, person1.getId(), network1.getId());
Collections.sort(expectedPreferences);
People peopleProxy = publicApiClient.people();
// unknown user
try {
int skipCount = 0;
int maxItems = 2;
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
Paging paging = getPaging(skipCount, maxItems, expectedPreferences.size(), expectedPreferences.size());
peopleProxy.getPreferences(GUID.generate(), createParams(paging, null));
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
// test paging
{
int skipCount = 0;
int maxItems = 2;
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
Paging paging = getPaging(skipCount, maxItems, expectedPreferences.size(), expectedPreferences.size());
ListResponse<Preference> resp = peopleProxy.getPreferences(person1.getId(), createParams(paging, null));
checkList(expectedPreferences.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
}
{
int skipCount = 2;
int maxItems = 10;
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
Paging paging = getPaging(skipCount, maxItems, expectedPreferences.size(), expectedPreferences.size());
ListResponse<Preference> resp = peopleProxy.getPreferences(person1.getId(), createParams(paging, null));
checkList(expectedPreferences.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
}
// "-me-" user
{
int skipCount = 0;
int maxItems = 2;
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
Paging paging = getPaging(skipCount, maxItems, expectedPreferences.size(), expectedPreferences.size());
ListResponse<Preference> resp = peopleProxy.getPreferences(org.alfresco.rest.api.People.DEFAULT_USER, createParams(paging, null));
checkList(expectedPreferences.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
}
// invalid user - 404
try {
int skipCount = 2;
int maxItems = 10;
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
Paging paging = getPaging(skipCount, maxItems, expectedPreferences.size(), expectedPreferences.size());
peopleProxy.getPreferences("invalid.user", createParams(paging, null));
fail("");
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
// user from another account - 401
try {
int skipCount = 0;
int maxItems = 2;
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person3.getId()));
Paging paging = getPaging(skipCount, maxItems, expectedPreferences.size(), expectedPreferences.size());
// ListResponse<Preference> resp = peopleProxy.getPreferences(person1.getId(), createParams(paging, null));
// checkList(expectedPreferences.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
peopleProxy.getPreferences(person1.getId(), createParams(paging, null));
fail();
} catch (PublicApiException e) {
assertEquals(e.getHttpResponse().getStatusCode(), HttpStatus.SC_UNAUTHORIZED);
}
// another user from the same account - 403
try {
int skipCount = 0;
int maxItems = 2;
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
Paging paging = getPaging(skipCount, maxItems, expectedPreferences.size(), expectedPreferences.size());
peopleProxy.getPreferences(person1.getId(), createParams(paging, null));
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
}
// get a single preference
// Test Case: cloud-1493
{
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
Preference pref = expectedPreferences.get(0);
Preference ret = peopleProxy.getPreference(person1.getId(), pref.getId());
pref.expected(ret);
}
// unknown person id
try {
Preference pref = expectedPreferences.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
peopleProxy.getPreference(GUID.generate(), pref.getId());
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
// unknown preference id
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.getPreference(person1.getId(), GUID.generate());
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
// Test case: cloud-1968
try {
Preference pref = expectedPreferences.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.create("people", person1.getId(), "preferences", pref.getId(), pref.toJSON().toString(), "Unable to POST to a preference");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
Preference pref = expectedPreferences.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.update("people", person1.getId(), "preferences", pref.getId(), pref.toJSON().toString(), "Unable to PUT a preference");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
Preference pref = expectedPreferences.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.remove("people", person1.getId(), "preferences", pref.getId(), "Unable to DELETE a preference");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
Preference pref = expectedPreferences.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.create("people", person1.getId(), "preferences", null, pref.toJSON().toString(), "Unable to POST to preferences");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
Preference pref = expectedPreferences.get(0);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.update("people", person1.getId(), "preferences", null, pref.toJSON().toString(), "Unable to PUT preferences");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.remove("people", person1.getId(), "preferences", null, "Unable to DELETE preferences");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
{
// REPO-1061, REPO-890
try {
String skipCount = "a";
String maxItems = "hi";
HashMap<String, String> params = new HashMap<String, String>();
params.put("skipCount", skipCount);
params.put("maxItems", maxItems);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.getPreferences(person1.getId(), params);
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
}
try {
String skipCount = "a";
String maxItems = "null";
HashMap<String, String> params = new HashMap<String, String>();
params.put("skipCount", skipCount);
params.put("maxItems", maxItems);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.getPreferences(person1.getId(), params);
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
}
try {
String maxItems = "Red";
HashMap<String, String> params = new HashMap<String, String>();
params.put("maxItems", maxItems);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.getPreferences(person1.getId(), params);
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
}
try {
String skipCount = "yuck";
HashMap<String, String> params = new HashMap<String, String>();
params.put("skipCount", skipCount);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.getPreferences(person1.getId(), params);
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
}
try {
String skipCount = "-1";
HashMap<String, String> params = new HashMap<String, String>();
params.put("skipCount", skipCount);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.getPreferences(person1.getId(), params);
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
}
try {
String maxItems = "0";
HashMap<String, String> params = new HashMap<String, String>();
params.put("maxItems", maxItems);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
peopleProxy.getPreferences(person1.getId(), params);
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
}
}
}
use of org.alfresco.rest.api.tests.RepoService.TestNetwork in project alfresco-remote-api by Alfresco.
the class EnterpriseWorkflowTestApi method getOtherPersonInNetwork.
protected TestPerson getOtherPersonInNetwork(String usedPerson, String networkId) throws Exception {
TestNetwork usedNetwork = getTestFixture().getNetwork(networkId);
for (TestPerson person : usedNetwork.getPeople()) {
if (!person.getId().equals(usedPerson)) {
return person;
}
}
fail("Network doesn't have additonal users, cannot perform test");
return null;
}
use of org.alfresco.rest.api.tests.RepoService.TestNetwork in project alfresco-remote-api by Alfresco.
the class ProcessWorkflowApiTest method testDeleteProcessInstanceById.
@Test
public void testDeleteProcessInstanceById() throws Exception {
final RequestContext requestContext = initApiClientWithTestUser();
String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
final RequestContext adminContext = new RequestContext(requestContext.getNetworkId(), tenantAdmin);
TestNetwork anotherNetwork = getOtherNetwork(requestContext.getNetworkId());
tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + anotherNetwork.getId();
final RequestContext otherContext = new RequestContext(anotherNetwork.getId(), tenantAdmin);
ProcessesClient processesClient = publicApiClient.processesClient();
// delete with user starting the process instance
ProcessInfo process = startAdhocProcess(requestContext, null);
try {
processesClient.deleteProcessById(process.getId());
// Check if the process was actually deleted
assertNull(activitiProcessEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(process.getId()).singleResult());
HistoricProcessInstance deletedInstance = activitiProcessEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(process.getId()).singleResult();
assertNotNull(deletedInstance);
assertNotNull(deletedInstance.getEndTime());
assertEquals("deleted through REST API call", deletedInstance.getDeleteReason());
try {
processesClient.deleteProcessById(process.getId());
fail("expected exeception");
} catch (PublicApiException e) {
assertEquals(HttpStatus.NOT_FOUND.value(), e.getHttpResponse().getStatusCode());
}
} finally {
cleanupProcessInstance(process.getId());
}
// delete with admin in same network as the user starting the process instance
process = startAdhocProcess(requestContext, null);
try {
publicApiClient.setRequestContext(adminContext);
processesClient.deleteProcessById(process.getId());
// Check if the process was actually deleted
assertNull(activitiProcessEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(process.getId()).singleResult());
HistoricProcessInstance deletedInstance = activitiProcessEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(process.getId()).singleResult();
assertNotNull(deletedInstance);
assertNotNull(deletedInstance.getEndTime());
assertEquals("deleted through REST API call", deletedInstance.getDeleteReason());
} finally {
cleanupProcessInstance(process.getId());
}
// delete with admin from other network as the user starting the process instance
process = startAdhocProcess(requestContext, null);
try {
publicApiClient.setRequestContext(otherContext);
processesClient.deleteProcessById(process.getId());
fail("Expect permission exception");
} catch (PublicApiException e) {
assertEquals(HttpStatus.FORBIDDEN.value(), e.getHttpResponse().getStatusCode());
} finally {
cleanupProcessInstance(process.getId());
}
}
use of org.alfresco.rest.api.tests.RepoService.TestNetwork in project alfresco-remote-api by Alfresco.
the class ProcessWorkflowApiTest method testCreateProcessInstanceFromOtherNetwork.
@Test
@SuppressWarnings("unchecked")
public void testCreateProcessInstanceFromOtherNetwork() throws Exception {
final RequestContext requestContext = initApiClientWithTestUser();
org.activiti.engine.repository.ProcessDefinition processDefinition = activitiProcessEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey("@" + requestContext.getNetworkId() + "@activitiAdhoc").singleResult();
TestNetwork anotherNetwork = getOtherNetwork(requestContext.getNetworkId());
String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + anotherNetwork.getId();
RequestContext otherContext = new RequestContext(anotherNetwork.getId(), tenantAdmin);
publicApiClient.setRequestContext(otherContext);
ProcessesClient processesClient = publicApiClient.processesClient();
JSONObject createProcessObject = new JSONObject();
createProcessObject.put("processDefinitionId", processDefinition.getId());
final JSONObject variablesObject = new JSONObject();
variablesObject.put("bpm_dueDate", ISO8601DateFormat.format(new Date()));
variablesObject.put("bpm_priority", 1);
variablesObject.put("bpm_description", "test description");
TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
variablesObject.put("bpm_assignee", requestContext.getRunAsUser());
return null;
}
}, requestContext.getRunAsUser(), requestContext.getNetworkId());
createProcessObject.put("variables", variablesObject);
try {
processesClient.createProcess(createProcessObject.toJSONString());
} catch (PublicApiException e) {
assertEquals(HttpStatus.BAD_REQUEST.value(), e.getHttpResponse().getStatusCode());
}
}
use of org.alfresco.rest.api.tests.RepoService.TestNetwork in project alfresco-remote-api by Alfresco.
the class ProcessWorkflowApiTest method testGetProcessActivities.
@Test
public void testGetProcessActivities() throws Exception {
final RequestContext requestContext = initApiClientWithTestUser();
String tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + requestContext.getNetworkId();
final RequestContext adminContext = new RequestContext(requestContext.getNetworkId(), tenantAdmin);
TestNetwork anotherNetwork = getOtherNetwork(requestContext.getNetworkId());
tenantAdmin = AuthenticationUtil.getAdminUserName() + "@" + anotherNetwork.getId();
final RequestContext otherContext = new RequestContext(anotherNetwork.getId(), tenantAdmin);
final ProcessInfo process1 = startAdhocProcess(requestContext, null);
try {
ProcessesClient processesClient = publicApiClient.processesClient();
Map<String, String> paramMap = new HashMap<String, String>();
JSONObject activitiesJSON = processesClient.getActivities(process1.getId(), paramMap);
assertNotNull(activitiesJSON);
JSONArray entriesJSON = (JSONArray) activitiesJSON.get("entries");
assertNotNull(entriesJSON);
assertTrue(entriesJSON.size() == 2);
Map<String, JSONObject> activitiesMap = new HashMap<String, JSONObject>();
for (Object entry : entriesJSON) {
JSONObject jsonEntry = (JSONObject) entry;
JSONObject activityJSONObject = (JSONObject) jsonEntry.get("entry");
activitiesMap.put((String) activityJSONObject.get("activityDefinitionId"), activityJSONObject);
}
JSONObject activityJSONObject = activitiesMap.get("start");
assertNotNull(activityJSONObject);
assertNotNull(activityJSONObject.get("id"));
assertEquals("start", activityJSONObject.get("activityDefinitionId"));
assertNull(activityJSONObject.get("activityDefinitionName"));
assertEquals("startEvent", activityJSONObject.get("activityDefinitionType"));
assertNotNull(activityJSONObject.get("startedAt"));
assertNotNull(activityJSONObject.get("endedAt"));
assertNotNull(activityJSONObject.get("durationInMs"));
activityJSONObject = activitiesMap.get("adhocTask");
assertNotNull(activityJSONObject);
assertNotNull(activityJSONObject.get("id"));
assertEquals("adhocTask", activityJSONObject.get("activityDefinitionId"));
assertEquals("Adhoc Task", activityJSONObject.get("activityDefinitionName"));
assertEquals("userTask", activityJSONObject.get("activityDefinitionType"));
assertNotNull(activityJSONObject.get("startedAt"));
assertNull(activityJSONObject.get("endedAt"));
assertNull(activityJSONObject.get("durationInMs"));
paramMap = new HashMap<String, String>();
paramMap.put("status", "active");
activitiesJSON = processesClient.getActivities(process1.getId(), paramMap);
assertNotNull(activitiesJSON);
entriesJSON = (JSONArray) activitiesJSON.get("entries");
assertNotNull(entriesJSON);
assertTrue(entriesJSON.size() == 1);
paramMap = new HashMap<String, String>();
paramMap.put("status", "completed");
activitiesJSON = processesClient.getActivities(process1.getId(), paramMap);
assertNotNull(activitiesJSON);
entriesJSON = (JSONArray) activitiesJSON.get("entries");
assertNotNull(entriesJSON);
assertTrue(entriesJSON.size() == 1);
paramMap = new HashMap<String, String>();
try {
processesClient.getActivities("fakeid", paramMap);
fail("Exception expected");
} catch (PublicApiException expected) {
assertEquals(HttpStatus.NOT_FOUND.value(), expected.getHttpResponse().getStatusCode());
assertErrorSummary("The entity with id: fakeid was not found", expected.getHttpResponse());
}
// get activities with admin from the same tenant as the process initiator
publicApiClient.setRequestContext(adminContext);
paramMap = new HashMap<String, String>();
activitiesJSON = processesClient.getActivities(process1.getId(), paramMap);
assertNotNull(activitiesJSON);
entriesJSON = (JSONArray) activitiesJSON.get("entries");
assertNotNull(entriesJSON);
assertTrue(entriesJSON.size() == 2);
// get tasks with admin from another tenant as the process initiator
publicApiClient.setRequestContext(otherContext);
paramMap = new HashMap<String, String>();
try {
processesClient.getActivities(process1.getId(), paramMap);
fail("forbidden expected");
} catch (PublicApiException e) {
assertEquals(HttpStatus.FORBIDDEN.value(), e.getHttpResponse().getStatusCode());
}
} finally {
cleanupProcessInstance(process1.getId());
}
}
Aggregations