Search in sources :

Example 81 with PostRequest

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

the class CustomModelImportTest method testNotZipFileUpload.

public void testNotZipFileUpload() throws Exception {
    File file = getResourceFile("validModel.zip");
    ZipFile zipFile = new ZipFile(file);
    ZipEntry zipEntry = zipFile.entries().nextElement();
    File unzippedModelFile = TempFileProvider.createTempFile(zipFile.getInputStream(zipEntry), "validModel", ".xml");
    tempFiles.add(unzippedModelFile);
    zipFile.close();
    PostRequest postRequest = buildMultipartPostRequest(unzippedModelFile);
    // CMM upload supports only zip file.
    sendRequest(postRequest, 400);
}
Also used : PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 82 with PostRequest

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

the class CustomModelImportTest method testValidUpload_ModelOnly.

public void testValidUpload_ModelOnly() throws Exception {
    File zipFile = getResourceFile("validModel.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()));
    String importedModelName = json.getString("modelName");
    importedModels.add(importedModelName);
    assertFalse(json.has("shareExtModule"));
    // Import the same model again
    // name conflict
    sendRequest(postRequest, 409);
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 83 with PostRequest

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

the class DiscussionRestApiTest method createReply.

private JSONObject createReply(NodeRef nodeRef, String title, String content, int expectedStatus) throws Exception {
    JSONObject reply = new JSONObject();
    reply.put("title", title);
    reply.put("content", content);
    Response response = sendRequest(new PostRequest(getRepliesUrl(nodeRef), reply.toString(), "application/json"), expectedStatus);
    if (expectedStatus != 200) {
        return null;
    }
    JSONObject result = new JSONObject(response.getContentAsString());
    return result.getJSONObject("item");
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject)

Example 84 with PostRequest

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

the class FacetRestApiTest method testUpdateSingleValue.

public void testUpdateSingleValue() throws Exception {
    // Build the Filter object
    final JSONObject filter = new JSONObject();
    final String filterName = "filter" + System.currentTimeMillis();
    filters.add(filterName);
    filter.put("filterID", filterName);
    filter.put("facetQName", "cm:test");
    filter.put("displayName", "facet-menu.facet.test1");
    filter.put("displayControl", "alfresco/search/FacetFilters/test");
    filter.put("maxFilters", 5);
    filter.put("hitThreshold", 1);
    filter.put("minFilterValueLength", 4);
    filter.put("sortBy", "ALPHABETICALLY");
    filter.put("isEnabled", true);
    JSONObject customProp = new JSONObject();
    // 1st custom prop
    JSONObject blockIncludeRequest = new JSONObject();
    blockIncludeRequest.put("name", "blockIncludeFacetRequest");
    blockIncludeRequest.put("value", "true");
    customProp.put("blockIncludeFacetRequest", blockIncludeRequest);
    filter.put("customProperties", customProp);
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            // Post the filter
            sendRequest(new PostRequest(POST_FACETS_URL, filter.toString(), "application/json"), 200);
            return null;
        }
    }, SEARCH_ADMIN_USER);
    // Admin updates displayName and facetQName in 2 put requests
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            // Retrieve the created filter
            Response response = sendRequest(new GetRequest(GET_FACETS_URL + "/" + filterName), 200);
            JSONObject jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString()));
            assertEquals(filterName, jsonRsp.getString("filterID"));
            assertEquals("facet-menu.facet.test1", jsonRsp.getString("displayName"));
            assertEquals("{http://www.alfresco.org/model/content/1.0}test", jsonRsp.getString("facetQName"));
            assertTrue(jsonRsp.getBoolean("isEnabled"));
            // Just supply the filterID and the required value
            JSONObject singleValueJson = new JSONObject();
            singleValueJson.put("filterID", filterName);
            // Change the displayName value and update
            singleValueJson.put("displayName", "facet-menu.facet.modifiedValue");
            sendRequest(new PutRequest(PUT_FACETS_URL, singleValueJson.toString(), "application/json"), 200);
            // Change the isEnabled value and update
            // We simulate two PUT requests without refreshing the page in
            // between updates
            singleValueJson = new JSONObject();
            singleValueJson.put("filterID", filterName);
            singleValueJson.put("isEnabled", false);
            sendRequest(new PutRequest(PUT_FACETS_URL, singleValueJson.toString(), "application/json"), 200);
            response = sendRequest(new GetRequest(GET_FACETS_URL + "/" + filterName), 200);
            jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString()));
            // Now see if the two changes have been persisted
            assertEquals("facet-menu.facet.modifiedValue", jsonRsp.getString("displayName"));
            assertFalse(jsonRsp.getBoolean("isEnabled"));
            // Make sure the rest of values haven't been changed
            assertEquals(filterName, jsonRsp.getString("filterID"));
            assertEquals("{http://www.alfresco.org/model/content/1.0}test", jsonRsp.getString("facetQName"));
            assertEquals("alfresco/search/FacetFilters/test", jsonRsp.getString("displayControl"));
            assertEquals(5, jsonRsp.getInt("maxFilters"));
            assertEquals(1, jsonRsp.getInt("hitThreshold"));
            assertEquals(4, jsonRsp.getInt("minFilterValueLength"));
            assertEquals("ALPHABETICALLY", jsonRsp.getString("sortBy"));
            assertEquals("ALL", jsonRsp.getString("scope"));
            assertFalse(jsonRsp.getBoolean("isDefault"));
            // Make sure custom properties haven't been deleted
            JSONObject retrievedCustomProp = jsonRsp.getJSONObject("customProperties");
            JSONObject retrievedBlockIncludeRequest = retrievedCustomProp.getJSONObject("blockIncludeFacetRequest");
            assertEquals("{http://www.alfresco.org/model/solrfacetcustomproperty/1.0}blockIncludeFacetRequest", retrievedBlockIncludeRequest.get("name"));
            assertEquals("true", retrievedBlockIncludeRequest.get("value"));
            // Change the facetQName value and update
            singleValueJson = new JSONObject();
            singleValueJson.put("filterID", filterName);
            singleValueJson.put("facetQName", "cm:testModifiedValue");
            // We simulate that 'testModifiedValue' QName doesn't have custom properties
            singleValueJson.put("customProperties", new JSONObject());
            sendRequest(new PutRequest(PUT_FACETS_URL, singleValueJson.toString(), "application/json"), 200);
            response = sendRequest(new GetRequest(GET_FACETS_URL + "/" + filterName), 200);
            jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString()));
            // Now see if the facetQName and its side-effect have been persisted
            assertEquals("{http://www.alfresco.org/model/content/1.0}testModifiedValue", jsonRsp.getString("facetQName"));
            assertNull("Custom properties should have been deleted.", jsonRsp.opt("customProperties"));
            // Make sure the rest of values haven't been changed
            assertEquals(filterName, jsonRsp.getString("filterID"));
            assertEquals("facet-menu.facet.modifiedValue", jsonRsp.getString("displayName"));
            assertEquals("alfresco/search/FacetFilters/test", jsonRsp.getString("displayControl"));
            assertEquals(5, jsonRsp.getInt("maxFilters"));
            assertEquals(1, jsonRsp.getInt("hitThreshold"));
            assertEquals(4, jsonRsp.getInt("minFilterValueLength"));
            assertEquals("ALPHABETICALLY", jsonRsp.getString("sortBy"));
            assertFalse(jsonRsp.getBoolean("isDefault"));
            assertEquals("ALL", jsonRsp.getString("scope"));
            assertFalse(jsonRsp.getBoolean("isEnabled"));
            return null;
        }
    }, SEARCH_ADMIN_USER);
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 85 with PostRequest

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

the class FacetRestApiTest method testCreateUpdateFacetWithInvalidFilterId.

public void testCreateUpdateFacetWithInvalidFilterId() throws Exception {
    // Build the Filter object
    final JSONObject filter = new JSONObject();
    final String filterName = "filter" + System.currentTimeMillis();
    filters.add(filterName);
    filter.put("filterID", filterName);
    filter.put("facetQName", "cm:test1");
    filter.put("displayName", "facet-menu.facet.test1");
    filter.put("displayControl", "alfresco/search/FacetFilters/test1");
    filter.put("maxFilters", 5);
    filter.put("hitThreshold", 1);
    filter.put("minFilterValueLength", 4);
    filter.put("sortBy", "ALPHABETICALLY");
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            // Post the filter
            sendRequest(new PostRequest(POST_FACETS_URL, filter.toString(), "application/json"), 200);
            return null;
        }
    }, SEARCH_ADMIN_USER);
    // Admin tries to change the FilterID value
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            // Retrieve the created filter
            Response response = sendRequest(new GetRequest(GET_FACETS_URL + "/" + filterName), 200);
            JSONObject jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString()));
            assertEquals(filterName, jsonRsp.getString("filterID"));
            // Now change the filterID value and try to update
            jsonRsp.put("filterID", filterName + "Modified");
            sendRequest(new PutRequest(PUT_FACETS_URL, jsonRsp.toString(), "application/json"), 400);
            return null;
        }
    }, SEARCH_ADMIN_USER);
    // Admin tries to create a filter with a duplicate FilterID
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            // Post the filter
            sendRequest(new PostRequest(POST_FACETS_URL, filter.toString(), "application/json"), 400);
            return null;
        }
    }, SEARCH_ADMIN_USER);
    // Admin tries to create a filter with a malicious FilterID
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            Response response = sendRequest(new GetRequest(GET_FACETS_URL), 200);
            JSONObject jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString()));
            JSONArray facetsArray = (JSONArray) jsonRsp.get(FACETS);
            assertNotNull("JSON 'facets' array was null", facetsArray);
            final List<String> facets = getListFromJsonArray(facetsArray);
            filter.put("filterID", "<script>alert('Maliciouse-FilterID')</script>");
            // Post the filter
            sendRequest(new PostRequest(POST_FACETS_URL, filter.toString(), "application/json"), 400);
            // Retrieve all filters
            response = sendRequest(new GetRequest(GET_FACETS_URL), 200);
            jsonRsp = new JSONObject(new JSONTokener(response.getContentAsString()));
            facetsArray = (JSONArray) jsonRsp.get(FACETS);
            assertNotNull("JSON 'facets' array was null", facetsArray);
            final List<String> newFacets = getListFromJsonArray(facetsArray);
            assertEquals(facets, newFacets);
            return null;
        }
    }, SEARCH_ADMIN_USER);
}
Also used : JSONArray(org.json.JSONArray) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest) JSONException(org.json.JSONException) IOException(java.io.IOException) Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONTokener(org.json.JSONTokener) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) ArrayList(java.util.ArrayList) List(java.util.List)

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