Search in sources :

Example 31 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project alfresco-remote-api by Alfresco.

the class SiteServiceTest method testGetMemberInfo.

public void testGetMemberInfo() throws Exception {
    String testGroup = "SiteServiceTestGroupA";
    String testGroupName = "GROUP_" + testGroup;
    if (!authorityService.authorityExists(testGroupName)) {
        this.authenticationComponent.setSystemUserAsCurrentUser();
        testGroupName = authorityService.createAuthority(AuthorityType.GROUP, testGroup, testGroup, authorityService.getDefaultZones());
    }
    if (!authorityService.getContainedAuthorities(AuthorityType.USER, testGroupName, true).contains(USER_TWO)) {
        this.authenticationComponent.setSystemUserAsCurrentUser();
        this.authorityService.addAuthority(testGroupName, USER_TWO);
    }
    this.authenticationComponent.setCurrentUser(USER_ONE);
    // CRUD a membership group for a web site
    // Create a site
    String shortName = GUID.generate();
    createSite("myPreset", shortName, "myTitle", "myDescription", SiteVisibility.PUBLIC, 200);
    // Build the JSON membership object
    JSONObject membership = new JSONObject();
    membership.put("role", SiteModel.SITE_CONSUMER);
    JSONObject group = new JSONObject();
    group.put("fullName", testGroupName);
    membership.put("group", group);
    // Create a new group membership
    {
        Response response = sendRequest(new PostRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, membership.toString(), "application/json"), 200);
        JSONObject newMember = new JSONObject(response.getContentAsString());
        // Validate the return value
        assertEquals("role not correct", SiteModel.SITE_CONSUMER, newMember.getString("role"));
        JSONObject newGroup = newMember.getJSONObject("authority");
        assertNotNull(newGroup);
        assertEquals("full name not correct", testGroupName, newGroup.getString("fullName"));
        assertEquals("authorityType not correct", "GROUP", newGroup.getString("authorityType"));
    }
    // Now List memberships
    {
        Response response = sendRequest(new GetRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "?authorityType=USER"), 200);
        JSONArray listResult = new JSONArray(response.getContentAsString());
        assertNotNull(listResult);
        assertEquals(2, listResult.length());
        for (int i = 0; i < listResult.length(); i++) {
            JSONObject json = listResult.getJSONObject(i);
            if (USER_ONE.equals(json.getJSONObject("authority").get("fullName"))) {
                assertEquals("user one is Not member of any group", false, json.get("isMemberOfGroup"));
            } else {
                assertEquals("full name not correct", USER_TWO, json.getJSONObject("authority").get("fullName"));
                assertEquals("user two is member of a SiteServiceTestGroupA group", true, json.get("isMemberOfGroup"));
            }
        }
    }
    // cleanup
    if (authorityService.authorityExists(testGroupName)) {
        this.authenticationComponent.setSystemUserAsCurrentUser();
        this.authorityService.deleteAuthority(testGroupName);
    }
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray)

Example 32 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project alfresco-remote-api by Alfresco.

the class SiteServiceTest method testDeleteMembership.

public void testDeleteMembership() throws Exception {
    // Create a site
    String shortName = GUID.generate();
    createSite("myPreset", shortName, "myTitle", "myDescription", SiteVisibility.PUBLIC, 200);
    // Build the JSON membership object
    JSONObject membership = new JSONObject();
    membership.put("role", SiteModel.SITE_CONSUMER);
    JSONObject person = new JSONObject();
    person.put("userName", USER_TWO);
    membership.put("person", person);
    // Post the membership
    sendRequest(new PostRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, membership.toString(), "application/json"), 200);
    // Delete the membership
    sendRequest(new DeleteRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_TWO), 200);
    // Check that the membership has been deleted
    sendRequest(new GetRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_TWO), 404);
}
Also used : PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) DeleteRequest(org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)

Example 33 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project alfresco-remote-api by Alfresco.

the class CustomModelImportTest method testValidUpload_ExtModuleOnly.

public void testValidUpload_ExtModuleOnly() throws Exception {
    File zipFile = getResourceFile("validExtModule.zip");
    PostRequest postRequest = buildMultipartPostRequest(zipFile);
    AuthenticationUtil.setFullyAuthenticatedUser(NON_ADMIN_USER);
    Response response = sendRequest(postRequest, 403);
    AuthenticationUtil.setFullyAuthenticatedUser(CUSTOM_MODEL_ADMIN);
    response = sendRequest(postRequest, 200);
    JSONObject json = new JSONObject(new JSONTokener(response.getContentAsString()));
    assertFalse(json.has("modelName"));
    String extModule = json.getString("shareExtModule");
    Document document = XMLUtil.parse(extModule);
    NodeList nodes = document.getElementsByTagName("id");
    assertEquals(1, nodes.getLength());
    assertNotNull(nodes.item(0).getTextContent());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 34 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project alfresco-remote-api by Alfresco.

the class CustomModelImportTest method testInvalidZipUpload.

public void testInvalidZipUpload() throws Exception {
    String content = "<note>" + "<from>Jane</from>" + "<to>John</to>" + "<heading>Upload test</heading>" + "<body>This is an invalid model or a Share extension module</body>" + "</note>";
    ZipEntryContext context = new ZipEntryContext("invalidFormat.xml", content.getBytes());
    File zipFile = createZip(context);
    PostRequest postRequest = buildMultipartPostRequest(zipFile);
    // Invalid. Neither a model nor a Share extension module file
    sendRequest(postRequest, 400);
}
Also used : PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 35 with PostRequest

use of org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest in project alfresco-remote-api by Alfresco.

the class CustomModelImportTest method buildMultipartPostRequest.

public PostRequest buildMultipartPostRequest(File file) throws IOException {
    Part[] parts = { new FilePart("filedata", file.getName(), file, "application/zip", null) };
    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new HttpMethodParams());
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    multipartRequestEntity.writeRequest(os);
    PostRequest postReq = new PostRequest(UPLOAD_URL, os.toByteArray(), multipartRequestEntity.getContentType());
    return postReq;
}
Also used : PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart)

Aggregations

PostRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest)110 JSONObject (org.json.JSONObject)90 Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)84 JSONArray (org.json.JSONArray)28 GetRequest (org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)25 JSONTokener (org.json.JSONTokener)23 NodeRef (org.alfresco.service.cmr.repository.NodeRef)22 DeleteRequest (org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)14 PutRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)10 JSONStringer (org.json.JSONStringer)9 File (java.io.File)8 ZipFile (java.util.zip.ZipFile)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 JSONObject (org.json.simple.JSONObject)5 IOException (java.io.IOException)4 UserTransaction (javax.transaction.UserTransaction)4 FileInfo (org.alfresco.service.cmr.model.FileInfo)4 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)4 JSONException (org.json.JSONException)4 Serializable (java.io.Serializable)3