use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testDelete.
/**
* Tests delete (file or folder)
* <p>DELETE:</p>
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>}
*/
@Test
public void testDelete() throws Exception {
setRequestContext(user1);
// create folder f0
String folder0Name = "f0-testDelete-" + RUNID;
String f0Id = createFolder(Nodes.PATH_MY, folder0Name).getId();
String content1Id = createTextFile(f0Id, "content" + RUNID + "_1", "The quick brown fox jumps over the lazy dog.").getId();
// delete file
deleteNode(content1Id);
assertTrue(existsArchiveNode(content1Id));
// -ve test
deleteNode(content1Id, 404);
String folder1Id = createFolder(f0Id, "folder " + RUNID + "_1").getId();
String folder2Id = createFolder(folder1Id, "folder " + RUNID + "_2").getId();
String content2Id = createTextFile(folder2Id, "content" + RUNID + "_2", "The quick brown fox jumps over the lazy dog.").getId();
// cascade delete folder
deleteNode(folder1Id);
assertTrue(existsArchiveNode(folder1Id));
assertTrue(existsArchiveNode(folder2Id));
assertTrue(existsArchiveNode(content2Id));
// -ve test
deleteNode(folder2Id, 404);
deleteNode(content2Id, 404);
// -ve test
String rootNodeId = getRootNodeId();
deleteNode(rootNodeId, 403);
//
// permanently delete - ie. bypass trashcan (archive store)
//
String folder3Id = createFolder(f0Id, "folder " + RUNID + "_3").getId();
String folder4Id = createFolder(folder3Id, "folder " + RUNID + "_4").getId();
deleteNode(folder3Id, true, 204);
assertFalse(existsArchiveNode(folder3Id));
assertFalse(existsArchiveNode(folder4Id));
String sharedNodeId = getSharedNodeId();
final String folder5Id = createFolder(sharedNodeId, "folder " + RUNID + "_5").getId();
// -ve test - another user cannot delete
setRequestContext(user2);
deleteNode(folder5Id, 403);
setRequestContext(user1);
Map<String, Object> props = new HashMap<>();
props.put(PROP_OWNER, user2);
Node nUpdate = new Node();
nUpdate.setProperties(props);
HttpResponse response = put(URL_NODES, folder5Id, toJsonAsStringNonNull(nUpdate), null, 200);
Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertEquals(user2, ((Map) nodeResp.getProperties().get(PROP_OWNER)).get("id"));
// TODO see REPO-907. Apparently returns 204 here in tenant context ?? (eg. if useDefaultNetwork=false)
if (useDefaultNetwork) {
// -ve test - user1 can no longer delete
deleteNode(folder5Id, 403);
}
// TODO refactor with remote permission api calls (maybe use v0 until we have v1 ?)
final String tenantDomain = (networkOne != null ? networkOne.getId() : TenantService.DEFAULT_DOMAIN);
AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
return TenantUtil.runAsTenant(new TenantUtil.TenantRunAsWork<Void>() {
public Void doWork() throws Exception {
permissionService.setPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, folder5Id), user1, PermissionService.DELETE, true);
return null;
}
}, tenantDomain);
}
}, user1);
// -ve test - non-owner cannot bypass trashcan
deleteNode(folder5Id, true, 403);
// user1 has permission to delete (via trashcan)
deleteNode(folder5Id);
// admin can permanently delete
String folder6Id = createFolder(sharedNodeId, "folder " + RUNID + "_6").getId();
setRequestContext(networkAdmin);
deleteNode(folder6Id, true, 204);
// -ve - cannot delete Company Home root node
deleteNode(rootNodeId, true, 403);
Map<String, String> params = new HashMap<>();
params.put(Nodes.PARAM_RELATIVE_PATH, "/Sites");
response = getSingle(NodesEntityResource.class, rootNodeId, params, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
String sitesNodeId = nodeResp.getId();
// -ve - cannot delete Sites node
deleteNode(sitesNodeId, true, 403);
params = new HashMap<>();
params.put(Nodes.PARAM_RELATIVE_PATH, "/Data Dictionary");
response = getSingle(NodesEntityResource.class, rootNodeId, params, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
String ddNodeId = nodeResp.getId();
// -ve - cannot delete Data Dictionary node
deleteNode(ddNodeId, true, 403);
}
use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class NodeApiTest method testUpdatePermissionsOnNode.
/**
* Test update permission on a node
*
* @throws Exception
*/
private void testUpdatePermissionsOnNode() throws Exception {
// create folder with an empty document
String postUrl = createFolder();
String dId = createDocument(postUrl);
// update permissions
Document dUpdate = new Document();
NodePermissions nodePermissions = new NodePermissions();
List<NodePermissions.NodePermission> locallySetPermissions = new ArrayList<>();
locallySetPermissions.add(new NodePermissions.NodePermission(groupA, PermissionService.CONSUMER, AccessStatus.ALLOWED.toString()));
nodePermissions.setLocallySet(locallySetPermissions);
dUpdate.setPermissions(nodePermissions);
// update node
HttpResponse response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
validatePermissionsAfterUpdate(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), locallySetPermissions);
// Check permissions on node for user2 (part of groupB)
AuthenticationUtil.setRunAsUser(user2);
assertTrue(permissionService.hasPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), PermissionService.CONSUMER) == AccessStatus.DENIED);
// Check permissions on node for user1 (part of groupA)
AuthenticationUtil.setRunAsUser(user1);
assertTrue(permissionService.hasPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), PermissionService.CONSUMER) == AccessStatus.ALLOWED);
// add two groups with different permissions for each
locallySetPermissions.clear();
locallySetPermissions.add(new NodePermissions.NodePermission(groupA, PermissionService.EDITOR, AccessStatus.ALLOWED.toString()));
locallySetPermissions.add(new NodePermissions.NodePermission(groupB, PermissionService.CONSUMER, AccessStatus.ALLOWED.toString()));
nodePermissions.setLocallySet(locallySetPermissions);
dUpdate.setPermissions(nodePermissions);
// update node
response = put(URL_NODES, dId, toJsonAsStringNonNull(dUpdate), null, 200);
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
validatePermissionsAfterUpdate(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), locallySetPermissions);
// Check permissions on node for user2 (part of groupB)
AuthenticationUtil.setRunAsUser(user2);
assertTrue(permissionService.hasPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), PermissionService.CONSUMER) == AccessStatus.ALLOWED);
assertTrue(permissionService.hasPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), PermissionService.EDITOR) == AccessStatus.DENIED);
assertTrue(permissionService.hasPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), PermissionService.WRITE) == AccessStatus.DENIED);
assertTrue(permissionService.hasPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), PermissionService.READ) == AccessStatus.ALLOWED);
// Check permissions on node for user1 (part of groupA)
AuthenticationUtil.setRunAsUser(user1);
assertTrue(permissionService.hasPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), PermissionService.EDITOR) == AccessStatus.ALLOWED);
assertTrue(permissionService.hasPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), PermissionService.WRITE) == AccessStatus.ALLOWED);
assertTrue(permissionService.hasPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, documentResp.getId()), PermissionService.READ) == AccessStatus.ALLOWED);
}
use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class NodeAssociationsApiTest method testNodePeerAssocsPermissions.
/**
* Tests base permissions for managing (adding, listing and removing) peer associations.
*
* <p>POST:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<sourceNodeId>/targets}
*
* <p>DELETE:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<sourceNodeId>/targets/<targetNodeId>}
*
* <p>GET:</p>
* {@literal <host>:<port>/alfresco/api/<networkId>/public/alfresco/versions/1/nodes/<sourceNodeId>/targets}
* {
*/
@Test
public void testNodePeerAssocsPermissions() throws Exception {
setRequestContext(user1);
// as user 1 - create folder in "Shared Files" area and content within the folder
String sharedFolderNodeId = getSharedNodeId();
String sf1Id = createFolder(sharedFolderNodeId, "shared folder " + RUNID).getId();
Node n = new Node();
n.setName("shared content " + RUNID);
n.setNodeType(TYPE_CM_CONTENT);
n.setAspectNames(Arrays.asList(ASPECT_CM_REFERENCING, ASPECT_CM_PARTABLE));
HttpResponse response = post(getNodeChildrenUrl(sf1Id), toJsonAsStringNonNull(n), 201);
String so1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
// as user 1 - create folder in user's home (My Files) area and content within the folder
String u1myNodeId = getMyNodeId();
String u1f1Id = createFolder(u1myNodeId, "f1").getId();
n = new Node();
n.setName("o1");
n.setNodeType(TYPE_CM_CONTENT);
n.setAspectNames(Arrays.asList(ASPECT_CM_REFERENCING, ASPECT_CM_PARTABLE));
response = post(getNodeChildrenUrl(u1f1Id), toJsonAsStringNonNull(n), 201);
String u1o1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
// as user 2 - create folder in user's home (My Files) area and content within the folder
setRequestContext(user2);
String u2myNodeId = getMyNodeId();
String u2f1Id = createFolder(u2myNodeId, "f1").getId();
n = new Node();
n.setName("o1");
n.setNodeType(TYPE_CM_CONTENT);
n.setAspectNames(Arrays.asList(ASPECT_CM_REFERENCING, ASPECT_CM_PARTABLE));
response = post(getNodeChildrenUrl(u2f1Id), toJsonAsStringNonNull(n), 201);
String u2o1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
try {
Paging paging = getPaging(0, 100);
// empty lists - before
setRequestContext(user1);
response = getAll(getNodeTargetsUrl(u1f1Id), paging, null, 200);
List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
setRequestContext(user2);
response = getAll(getNodeTargetsUrl(u2f1Id), paging, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
// Create some assocs
setRequestContext(user1);
AssocTarget tgt = new AssocTarget(u1o1Id, ASSOC_TYPE_CM_REFERENCES);
post(getNodeTargetsUrl(u1f1Id), toJsonAsStringNonNull(tgt), 201);
setRequestContext(user2);
tgt = new AssocTarget(u2o1Id, ASSOC_TYPE_CM_REFERENCES);
post(getNodeTargetsUrl(u2f1Id), toJsonAsStringNonNull(tgt), 201);
setRequestContext(user1);
response = getAll(getNodeTargetsUrl(u1f1Id), paging, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
setRequestContext(user2);
response = getAll(getNodeTargetsUrl(u2f1Id), paging, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
// -ve tests
{
// source/target not readable
setRequestContext(user2);
// list
getAll(getNodeTargetsUrl(u1f1Id), paging, null, 403);
getAll(getNodeSourcesUrl(u1o1Id), paging, null, 403);
setRequestContext(user1);
// create
tgt = new AssocTarget(u2o1Id, ASSOC_TYPE_CM_REFERENCES);
post(getNodeTargetsUrl(u1f1Id), toJsonAsStringNonNull(tgt), 403);
tgt = new AssocTarget(u1o1Id, ASSOC_TYPE_CM_REFERENCES);
post(getNodeTargetsUrl(u2f1Id), toJsonAsStringNonNull(tgt), 403);
setRequestContext(user2);
// remove
delete(getNodeTargetsUrl(u1f1Id), u2o1Id, null, 403);
delete(getNodeTargetsUrl(u2f1Id), u1o1Id, null, 404);
}
setRequestContext(user1);
// Test listing targets (with permissions applied)
// update permission
// TODO refactor with remote permission api calls (use v0 until we have v1 ?) (RA-1085)
AuthenticationUtil.setFullyAuthenticatedUser(user1);
permissionService.setPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, sf1Id), user2, PermissionService.EDITOR, true);
setRequestContext(networkAdmin);
response = publicApiClient.get(getScope(), "nodes/" + sf1Id + "/targets", null, null, null, createParams(paging, null));
checkStatus(200, response.getStatusCode());
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
// user 1
setRequestContext(user1);
tgt = new AssocTarget(u1o1Id, ASSOC_TYPE_CM_REFERENCES);
post(getNodeTargetsUrl(sf1Id), toJsonAsStringNonNull(tgt), 201);
// user 2
setRequestContext(user2);
tgt = new AssocTarget(u2o1Id, ASSOC_TYPE_CM_REFERENCES);
post(getNodeTargetsUrl(sf1Id), toJsonAsStringNonNull(tgt), 201);
setRequestContext(networkAdmin);
response = publicApiClient.get(getScope(), "nodes/" + sf1Id + "/targets", null, null, null, createParams(paging, null));
checkStatus(200, response.getStatusCode());
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(2, nodes.size());
setRequestContext(user1);
response = getAll(getNodeTargetsUrl(sf1Id), paging, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(u1o1Id, nodes.get(0).getId());
setRequestContext(user2);
response = getAll(getNodeTargetsUrl(sf1Id), paging, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(u2o1Id, nodes.get(0).getId());
// Test listing sources (with permissions applied)
// update permission
// TODO refactor with remote permission api calls (use v0 until we have v1 ?)
AuthenticationUtil.setFullyAuthenticatedUser(user1);
permissionService.setPermission(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, sf1Id), user2, PermissionService.EDITOR, true);
setRequestContext(networkAdmin);
response = publicApiClient.get(getScope(), "nodes/" + so1Id + "/sources", null, null, null, createParams(paging, null));
checkStatus(200, response.getStatusCode());
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
// user 1
setRequestContext(user1);
tgt = new AssocTarget(so1Id, ASSOC_TYPE_CM_REFERENCES);
post(getNodeTargetsUrl(u1f1Id), toJsonAsStringNonNull(tgt), 201);
// user 2
setRequestContext(user2);
tgt = new AssocTarget(so1Id, ASSOC_TYPE_CM_REFERENCES);
post(getNodeTargetsUrl(u2f1Id), toJsonAsStringNonNull(tgt), 201);
setRequestContext(networkAdmin);
response = publicApiClient.get(getScope(), "nodes/" + so1Id + "/sources", null, null, null, createParams(paging, null));
checkStatus(200, response.getStatusCode());
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(2, nodes.size());
setRequestContext(user1);
response = getAll(getNodeSourcesUrl(so1Id), paging, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(u1f1Id, nodes.get(0).getId());
setRequestContext(user2);
response = getAll(getNodeSourcesUrl(so1Id), paging, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(u2f1Id, nodes.get(0).getId());
} finally {
// some cleanup
setRequestContext(user1);
deleteNode(u1f1Id, true, 204);
deleteNode(sf1Id, true, 204);
setRequestContext(user2);
deleteNode(u2f1Id, true, 204);
}
}
use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class QueriesNodesApiTest method checkApiCall.
private List<Node> checkApiCall(String pathRegex, String queryForm, String term, String nodeType, String rootNodeId, String include, String orderBy, Paging paging, int expectedStatus, Boolean checkNodeOrderAsc, Boolean propertyNullCheck, List<String> ids) throws Exception {
Map<String, String> params = new HashMap<>(1);
params.put(Queries.PARAM_TERM, term);
if (include != null) {
params.put(Queries.PARAM_INCLUDE, include);
}
if (nodeType != null) {
params.put(Queries.PARAM_NODE_TYPE, nodeType);
}
if (rootNodeId != null) {
params.put(Queries.PARAM_ROOT_NODE_ID, rootNodeId);
}
if (orderBy != null) {
params.put(Queries.PARAM_ORDERBY, orderBy);
}
// Create the list of NodeRefs returned from the dummy search
dummySearchServiceQueryNodeRefs.clear();
for (String id : ids) {
NodeRef nodeRef = getNodeRef(id);
dummySearchServiceQueryNodeRefs.add(nodeRef);
}
// Mix up the NodeRefs returned from the dummy search as the client side code is going to be doing the sorting.
if (orderBy != null) {
Collections.shuffle(dummySearchServiceQueryNodeRefs);
}
HttpResponse response = getAll(URL_QUERIES_LSN, paging, params, 200);
List<Node> nodes = null;
if (expectedStatus == 200) {
String termWithEscapedAsterisks = term.replaceAll("\\*", "\\\\*").replaceAll("\"", "\\\\\"");
String expectedQuery = DEAFULT_QUERY.equals(queryForm) ? String.format(DEAFULT_QUERY, termWithEscapedAsterisks) : NODE_TYPE_QUERY.equals(queryForm) ? String.format(NODE_TYPE_QUERY, termWithEscapedAsterisks, nodeType) : ROOT_NODE_QUERY_SUFFIX.equals(queryForm) ? String.format(ROOT_NODE_QUERY_SUFFIX, termWithEscapedAsterisks) : "TODO";
ArgumentCaptor<SearchParameters> searchParametersCaptor = ArgumentCaptor.forClass(SearchParameters.class);
verify(mockSearchService, times(++callCountToMockSearchService)).query(searchParametersCaptor.capture());
SearchParameters parameters = searchParametersCaptor.getValue();
String query = parameters.getQuery();
if (ROOT_NODE_QUERY_SUFFIX.equals(queryForm)) {
assertNotNull(query);
assertTrue("Query should have started with " + ROOT_NODE_QUERY_PREFIX + " but was " + query, query.startsWith(ROOT_NODE_QUERY_PREFIX));
assertTrue("Query should have ended with " + expectedQuery + " but was " + query, query.endsWith(expectedQuery));
String path = query.substring(ROOT_NODE_QUERY_PREFIX.length(), query.length() - expectedQuery.length());
assertTrue("Query path should match " + pathRegex + " but was " + path, Pattern.matches(pathRegex, path));
} else {
assertEquals("Query", expectedQuery, query);
}
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
checkNodeIds(nodes, ids, checkNodeOrderAsc);
if (propertyNullCheck != null) {
for (Node node : nodes) {
if (propertyNullCheck) {
assertNull(node.getAspectNames());
assertNull(node.getProperties());
assertNull(node.getPath());
assertNull(node.getIsLink());
} else {
assertNotNull(node.getAspectNames());
assertNotNull(node.getProperties());
assertNotNull(node.getPath());
assertNotNull(node.getIsLink());
}
}
}
}
return nodes;
}
use of org.alfresco.service.cmr.repository.NodeRef in project alfresco-remote-api by Alfresco.
the class QueriesPeopleApiTest method createTestUsers.
// Helper method to create users. These are deleted on tearDown.
private void createTestUsers() throws IllegalArgumentException, SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException {
AuthenticationUtil.setFullyAuthenticatedUser(user1);
for (String[] properties : userProperties) {
int l = properties.length;
if (l > 0) {
PersonInfo personInfo = newPersonInfo(properties);
String originalUsername = personInfo.getUsername();
String id = createUser(personInfo, networkOne);
Person person = new Person(id, // Not set to originalUsername, as the returned JSON does not set it
null, // enabled
true, personInfo.getFirstName(), personInfo.getLastName(), personInfo.getCompany(), personInfo.getSkype(), personInfo.getLocation(), personInfo.getTel(), personInfo.getMob(), personInfo.getInstantmsg(), personInfo.getGoogle(), // description
null);
testUsernames.add(originalUsername);
testPersons.add(person);
// The following call to personService.getPerson(id) returns a NodeRef like:
// workspace://SpacesStore/9db76769-96de-4de4-bdb4-a127130af362
// We call tenantService.getName(nodeRef) to get a fully qualified NodeRef as Solr returns this.
// They look like:
// workspace://@org.alfresco.rest.api.tests.queriespeopleapitest@SpacesStore/9db76769-96de-4de4-bdb4-a127130af362
NodeRef nodeRef = personService.getPerson(id);
nodeRef = tenantService.getName(nodeRef);
testPersonNodeRefs.add(nodeRef);
}
}
}
Aggregations