Search in sources :

Example 36 with HttpResponse

use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testUploadToMyFiles.

/**
 * Tests Multipart upload to user's home (a.k.a My Files).
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/children}
 */
@Test
public void testUploadToMyFiles() throws Exception {
    setRequestContext(user1);
    // create folder f0
    String folder0Name = "f0-testUploadToMyFiles-" + RUNID;
    Folder folderResp = createFolder(Nodes.PATH_MY, folder0Name);
    String f0Id = folderResp.getId();
    final String fileName = "quick.pdf";
    final File file = getResourceFile(fileName);
    Paging paging = getPaging(0, Integer.MAX_VALUE);
    HttpResponse response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
    PublicApiClient.ExpectedPaging pagingResult = parsePaging(response.getJsonResponse());
    assertNotNull(paging);
    final int numOfNodes = pagingResult.getCount();
    MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
    MultiPartRequest reqBody = multiPartBuilder.build();
    // Try to upload into a non-existent folder
    post(getNodeChildrenUrl(UUID.randomUUID().toString()), reqBody.getBody(), null, reqBody.getContentType(), 404);
    // Upload
    response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals(fileName, document.getName());
    ContentInfo contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    // Default encoding
    assertEquals("UTF-8", contentInfo.getEncoding());
    // Check there is no path info returned.
    // The path info should only be returned when it is requested via a include statement.
    assertNull(document.getPath());
    // Retrieve the uploaded file
    response = getSingle(NodesEntityResource.class, document.getId(), null, 200);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(fileName, document.getName());
    contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
    // Check 'get children' is confirming the upload
    response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
    pagingResult = parsePaging(response.getJsonResponse());
    assertNotNull(paging);
    assertEquals(numOfNodes + 1, pagingResult.getCount().intValue());
    // Upload the same file again to check the name conflicts handling
    post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 409);
    response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
    pagingResult = parsePaging(response.getJsonResponse());
    assertNotNull(paging);
    assertEquals("Duplicate file name. The file shouldn't have been uploaded.", numOfNodes + 1, pagingResult.getCount().intValue());
    // Set autoRename=true and upload the same file again
    reqBody = MultiPartBuilder.copy(multiPartBuilder).setAutoRename(true).build();
    response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals("quick-1.pdf", document.getName());
    // upload the same file again, and request the path info to be present in the response
    response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), "?include=path", reqBody.getContentType(), 201);
    document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals("quick-2.pdf", document.getName());
    assertNotNull(document.getPath());
    response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
    pagingResult = parsePaging(response.getJsonResponse());
    assertNotNull(paging);
    assertEquals(numOfNodes + 3, pagingResult.getCount().intValue());
    // upload without specifying content type or without overriding filename - hence guess mimetype and use file's name
    final String fileName1 = "quick-1.txt";
    final File file1 = getResourceFile(fileName1);
    reqBody = MultiPartBuilder.create().setFileData(new FileData(null, file1)).build();
    response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals(fileName1, document.getName());
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, document.getContent().getMimeType());
    // upload with "default" binary content type and override filename - hence guess mimetype & use overridden name
    final String fileName2 = "quick-2.txt";
    final String fileName2b = "quick-2b.txt";
    final File file2 = getResourceFile(fileName2);
    reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2b, file2)).build();
    response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals(fileName2b, document.getName());
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, document.getContent().getMimeType());
    response = getSingle(NodesEntityResource.class, Nodes.PATH_MY, null, 200);
    Folder user1Home = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
    // User2 tries to upload a new file into the user1's home folder.
    setRequestContext(user2);
    final File file3 = getResourceFile(fileName2);
    reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file3)).build();
    post(getNodeChildrenUrl(user1Home.getId()), reqBody.getBody(), null, reqBody.getContentType(), 403);
    post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 403);
    setRequestContext(user1);
    response = getAll(getNodeChildrenUrl(f0Id), paging, 200);
    pagingResult = parsePaging(response.getJsonResponse());
    assertNotNull(paging);
    assertEquals("Access Denied. The file shouldn't have been uploaded.", numOfNodes + 5, pagingResult.getCount().intValue());
    // User1 tries to upload a file into a document rather than a folder!
    post(getNodeChildrenUrl(document.getId()), reqBody.getBody(), null, reqBody.getContentType(), 400);
    // Try to upload a file without defining the required formData
    reqBody = MultiPartBuilder.create().setAutoRename(true).build();
    post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 400);
    // Test unsupported node type
    reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).setAutoRename(true).setNodeType("cm:link").build();
    post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 400);
    // User1 uploads a new file
    reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName2, file2)).build();
    response = post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 201);
    document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    // Check the upload response
    assertEquals(fileName2, document.getName());
    contentInfo = document.getContent();
    assertNotNull(contentInfo);
    assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
    assertEquals("ISO-8859-1", contentInfo.getEncoding());
    // Test content size limit
    final SimpleFixedLimitProvider limitProvider = applicationContext.getBean("defaultContentLimitProvider", SimpleFixedLimitProvider.class);
    final long defaultSizeLimit = limitProvider.getSizeLimit();
    // 20 KB
    limitProvider.setSizeLimitString("20000");
    try {
        // quick.pdf size is about 23 KB
        reqBody = MultiPartBuilder.create().setFileData(new FileData(fileName, file)).setAutoRename(true).build();
        // Try to upload a file larger than the configured size limit
        post(getNodeChildrenUrl(f0Id), reqBody.getBody(), null, reqBody.getContentType(), 413);
    } finally {
        limitProvider.setSizeLimitString(Long.toString(defaultSizeLimit));
    }
}
Also used : ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) RestApiUtil.parsePaging(org.alfresco.rest.api.tests.util.RestApiUtil.parsePaging) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) ExpectedPaging(org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) NodesEntityResource(org.alfresco.rest.api.nodes.NodesEntityResource) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) SimpleFixedLimitProvider(org.alfresco.repo.content.ContentLimitProvider.SimpleFixedLimitProvider) MultiPartBuilder(org.alfresco.rest.api.tests.util.MultiPartBuilder) ContentInfo(org.alfresco.rest.api.tests.client.data.ContentInfo) PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 37 with HttpResponse

use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testCopySite.

@Test
public void testCopySite() throws Exception {
    setRequestContext(user1);
    // create folder
    Folder folderResp = createFolder(Nodes.PATH_MY, "siteCopytarget");
    String targetId = folderResp.getId();
    Map<String, String> body = new HashMap<>();
    body.put("targetParentId", targetId);
    // test that you can't copy a site
    HttpResponse response = getSingle("sites", tSiteId, null, null, 200);
    Site siteResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Site.class);
    String siteNodeId = siteResp.getGuid();
    post("nodes/" + siteNodeId + "/copy", toJsonAsStringNonNull(body), null, 422);
    // test that you can't copy a site doclib
    post("nodes/" + tDocLibNodeId + "/copy", toJsonAsStringNonNull(body), null, 422);
}
Also used : Site(org.alfresco.rest.api.model.Site) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Folder(org.alfresco.rest.api.tests.client.data.Folder) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Example 38 with HttpResponse

use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.

the class NodeApiTest method getDataDictionaryNodeId.

private String getDataDictionaryNodeId() throws Exception {
    Map params = new HashMap<>();
    params.put(Nodes.PARAM_RELATIVE_PATH, "/Data Dictionary");
    HttpResponse response = getSingle(NodesEntityResource.class, getRootNodeId(), params, 200);
    Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
    return nodeResp.getId();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.alfresco.rest.api.tests.client.data.Node) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 39 with HttpResponse

use of org.alfresco.rest.api.tests.client.HttpResponse 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);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) NodePermissions(org.alfresco.rest.api.model.NodePermissions) ArrayList(java.util.ArrayList) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Document(org.alfresco.rest.api.tests.client.data.Document)

Example 40 with HttpResponse

use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.

the class NodeApiTest method testUnlock.

/**
 * Tests unlock of a node
 * <p>POST:</p>
 * {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/unlock}
 */
@Test
public void testUnlock() throws Exception {
    setRequestContext(user1);
    // create folder
    Folder folderResp = createFolder(Nodes.PATH_MY, "folder" + RUNID);
    String folderId = folderResp.getId();
    // create doc d1
    String d1Name = "content" + RUNID + "_1l";
    Document d1 = createTextFile(folderId, d1Name, "The quick brown fox jumps over the lazy dog 1.");
    String d1Id = d1.getId();
    lock(d1Id, EMPTY_BODY);
    HttpResponse response = post(getNodeOperationUrl(d1Id, "unlock"), null, null, 200);
    Document documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
    assertEquals(d1Name, documentResp.getName());
    assertEquals(d1Id, documentResp.getId());
    assertNull(documentResp.getProperties().get("cm:lockType"));
    assertNull(documentResp.getProperties().get("cm:lockOwner"));
    lock(d1Id, EMPTY_BODY);
    // Users with admin rights can unlock nodes locked by other users.
    setRequestContext(networkAdmin);
    post(getNodeOperationUrl(d1Id, "unlock"), null, null, 200);
    // -ve
    // Missing target node
    post(getNodeOperationUrl("fakeId", "unlock"), null, null, 404);
    // Unlock by a user without permission
    lock(d1Id, EMPTY_BODY);
    setRequestContext(user2);
    post(getNodeOperationUrl(d1Id, "unlock"), null, null, 403);
    setRequestContext(user1);
    // Unlock on a not locked node
    post(getNodeOperationUrl(folderId, "unlock"), null, null, 422);
    // clean up
    // all locks were made by user1
    setRequestContext(user1);
    unlock(d1Id);
    deleteNode(folderId);
}
Also used : HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Folder(org.alfresco.rest.api.tests.client.data.Folder) Document(org.alfresco.rest.api.tests.client.data.Document) Test(org.junit.Test) AbstractSingleNetworkSiteTest(org.alfresco.rest.AbstractSingleNetworkSiteTest)

Aggregations

HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)162 Test (org.junit.Test)114 HashMap (java.util.HashMap)59 AbstractSingleNetworkSiteTest (org.alfresco.rest.AbstractSingleNetworkSiteTest)50 Document (org.alfresco.rest.api.tests.client.data.Document)49 Node (org.alfresco.rest.api.tests.client.data.Node)49 LinkedHashMap (java.util.LinkedHashMap)31 ArrayList (java.util.ArrayList)29 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)27 Folder (org.alfresco.rest.api.tests.client.data.Folder)26 File (java.io.File)25 NodesEntityResource (org.alfresco.rest.api.nodes.NodesEntityResource)25 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)22 CustomModel (org.alfresco.rest.api.model.CustomModel)16 ContentInfo (org.alfresco.rest.api.tests.client.data.ContentInfo)15 MultiPartBuilder (org.alfresco.rest.api.tests.util.MultiPartBuilder)15 JSONObject (org.json.simple.JSONObject)15 NodeRef (org.alfresco.service.cmr.repository.NodeRef)14 Map (java.util.Map)13 CustomAspect (org.alfresco.rest.api.model.CustomAspect)13