Search in sources :

Example 26 with NameValuePair

use of org.apache.commons.httpclient.NameValuePair in project sling by apache.

the class AccessPrivilegesInfoTest method testGrantedWriteForGroup.

/*
	 * group testuser granted read / granted write
	 */
@Test
public void testGrantedWriteForGroup() throws IOException, JsonException {
    testGroupId = H.createTestGroup();
    testUserId = H.createTestUser();
    testFolderUrl = H.createTestFolder();
    Credentials adminCreds = new UsernamePasswordCredentials("admin", "admin");
    //add testUserId to testGroup
    String groupPostUrl = HttpTest.HTTP_BASE_URL + "/system/userManager/group/" + testGroupId + ".update.html";
    List<NameValuePair> groupPostParams = new ArrayList<NameValuePair>();
    groupPostParams.add(new NameValuePair(":member", testUserId));
    H.assertAuthenticatedPostStatus(adminCreds, groupPostUrl, HttpServletResponse.SC_OK, groupPostParams, null);
    //assign some privileges
    String postUrl = testFolderUrl + ".modifyAce.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testGroupId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:write", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:readAccessControl", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:modifyAccessControl", "granted"));
    H.assertAuthenticatedPostStatus(adminCreds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    String getUrl = testFolderUrl + ".privileges-info.json";
    //fetch the JSON for the test page to verify the settings.
    Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");
    String json = H.getAuthenticatedContent(testUserCreds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObj = JsonUtil.parseObject(json);
    assertEquals(true, jsonObj.getBoolean("canAddChildren"));
    assertEquals(true, jsonObj.getBoolean("canDeleteChildren"));
    //the parent node must also have jcr:removeChildren granted for 'canDelete' to be true
    assertEquals(false, jsonObj.getBoolean("canDelete"));
    assertEquals(true, jsonObj.getBoolean("canModifyProperties"));
    assertEquals(true, jsonObj.getBoolean("canReadAccessControl"));
    assertEquals(true, jsonObj.getBoolean("canModifyAccessControl"));
    //add a child node to verify the 'canDelete' use case
    String childFolderUrl = H.getTestClient().createNode(testFolderUrl + "/testFolder" + random.nextInt() + SlingPostConstants.DEFAULT_CREATE_SUFFIX, null);
    String childPostUrl = childFolderUrl + ".modifyAce.html";
    postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testGroupId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:removeNode", "granted"));
    H.assertAuthenticatedPostStatus(adminCreds, childPostUrl, HttpServletResponse.SC_OK, postParams, null);
    String childGetUrl = childFolderUrl + ".privileges-info.json";
    String childJson = H.getAuthenticatedContent(testUserCreds, childGetUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(childJson);
    JsonObject childJsonObj = JsonUtil.parseObject(childJson);
    assertEquals(true, childJsonObj.getBoolean("canDelete"));
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 27 with NameValuePair

use of org.apache.commons.httpclient.NameValuePair in project sling by apache.

the class AuthenticatedTestUtil method createTestUser.

public String createTestUser() throws IOException {
    String postUrl = HTTP_BASE_URL + "/system/userManager/user.create.html";
    String testUserId = "testUser" + getNextInt();
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair(":name", testUserId));
    postParams.add(new NameValuePair("pwd", "testPwd"));
    postParams.add(new NameValuePair("pwdConfirm", "testPwd"));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    final String msg = "Unexpected status while attempting to create test user at " + postUrl;
    assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, msg);
    final String sessionInfoUrl = HTTP_BASE_URL + "/system/sling/info.sessionInfo.json";
    assertAuthenticatedHttpStatus(creds, sessionInfoUrl, HttpServletResponse.SC_OK, "session info failed for user " + testUserId);
    return testUserId;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 28 with NameValuePair

use of org.apache.commons.httpclient.NameValuePair in project sling by apache.

the class AuthenticatedTestUtil method assertAuthenticatedPostStatus.

/** Execute a POST request and check status */
public void assertAuthenticatedPostStatus(Credentials creds, String url, int expectedStatusCode, List<NameValuePair> postParams, String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    post.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);
        if (postParams != null) {
            final NameValuePair[] nvp = {};
            post.setRequestBody(postParams.toArray(nvp));
        }
        final int status = httpClient.executeMethod(post);
        if (assertMessage == null) {
            assertEquals(expectedStatusCode, status);
        } else {
            assertEquals(assertMessage, expectedStatusCode, status);
        }
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) PostMethod(org.apache.commons.httpclient.methods.PostMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 29 with NameValuePair

use of org.apache.commons.httpclient.NameValuePair in project sling by apache.

the class PostServletMoveTest method testMoveNodeMultipleSourceInValid.

public void testMoveNodeMultipleSourceInValid() throws IOException {
    final String testPath = TEST_BASE_PATH + "/mvmult/" + System.currentTimeMillis();
    final String testRoot = testClient.createNode(HTTP_BASE_URL + testPath, null);
    // create multiple source nodes
    Map<String, String> props = new HashMap<String, String>();
    props.put("text", "Hello");
    testClient.createNode(HTTP_BASE_URL + testPath + "/src1", props);
    testClient.createNode(HTTP_BASE_URL + testPath + "/src2", props);
    testClient.createNode(HTTP_BASE_URL + testPath + "/src3", props);
    testClient.createNode(HTTP_BASE_URL + testPath + "/src4", props);
    // move the src? nodes
    List<NameValuePair> nvPairs = new ArrayList<NameValuePair>();
    nvPairs.add(new NameValuePair(SlingPostConstants.RP_OPERATION, SlingPostConstants.OPERATION_MOVE));
    nvPairs.add(new NameValuePair(SlingPostConstants.RP_DEST, testPath + "/dest"));
    nvPairs.add(new NameValuePair(SlingPostConstants.RP_APPLY_TO, testPath + "/src1"));
    nvPairs.add(new NameValuePair(SlingPostConstants.RP_APPLY_TO, testPath + "/src2"));
    nvPairs.add(new NameValuePair(SlingPostConstants.RP_APPLY_TO, testPath + "/src3"));
    nvPairs.add(new NameValuePair(SlingPostConstants.RP_APPLY_TO, testPath + "/src4"));
    assertPostStatus(testRoot, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, nvPairs, "Expecting Move Failure (dest must have trailing slash)");
    // create destination parent
    testClient.createNode(HTTP_BASE_URL + testPath + "/dest", props);
    // retest after creating test
    assertPostStatus(testRoot, HttpServletResponse.SC_PRECONDITION_FAILED, nvPairs, "Expecting Move Failure (dest already exists)");
    // assert non-existence of the src?/text properties
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src1/text", HttpServletResponse.SC_NOT_FOUND);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src2/text", HttpServletResponse.SC_NOT_FOUND);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src3/text", HttpServletResponse.SC_NOT_FOUND);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src4/text", HttpServletResponse.SC_NOT_FOUND);
    // assert non-existence of src?
    assertHttpStatus(HTTP_BASE_URL + testPath + "/src1.html", HttpServletResponse.SC_OK);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/src2.html", HttpServletResponse.SC_OK);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/src3.html", HttpServletResponse.SC_OK);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/src4.html", HttpServletResponse.SC_OK);
    testClient.delete(testRoot);
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 30 with NameValuePair

use of org.apache.commons.httpclient.NameValuePair in project sling by apache.

the class PostServletMoveTest method testMoveAllChildren.

/**
     * Test for SLING-2415 Ability to move all child nodes, without the parent node
     * Using :applyTo value of "*"
     */
public void testMoveAllChildren() throws IOException {
    final String testPath = TEST_BASE_PATH + "/mvmultwc/" + System.currentTimeMillis();
    final String testRoot = testClient.createNode(HTTP_BASE_URL + testPath, null);
    // create multiple source nodes
    Map<String, String> props = new HashMap<String, String>();
    props.put("text", "Hello");
    testClient.createNode(HTTP_BASE_URL + testPath + "/test/src1", props);
    testClient.createNode(HTTP_BASE_URL + testPath + "/test/src2", props);
    testClient.createNode(HTTP_BASE_URL + testPath + "/test/src3", props);
    testClient.createNode(HTTP_BASE_URL + testPath + "/test/src4", props);
    // create destination parent
    testClient.createNode(HTTP_BASE_URL + testPath + "/dest", props);
    // move the src? nodes
    List<NameValuePair> nvPairs = new ArrayList<NameValuePair>();
    nvPairs.add(new NameValuePair(SlingPostConstants.RP_OPERATION, SlingPostConstants.OPERATION_MOVE));
    nvPairs.add(new NameValuePair(SlingPostConstants.RP_DEST, testPath + "/dest/"));
    nvPairs.add(new NameValuePair(SlingPostConstants.RP_APPLY_TO, "*"));
    // we expect success
    assertPostStatus(testRoot + "/test", HttpServletResponse.SC_OK, nvPairs, "Expecting Move Success");
    // assert existence of the src?/text properties
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src1/text", HttpServletResponse.SC_OK);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src2/text", HttpServletResponse.SC_OK);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src3/text", HttpServletResponse.SC_OK);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src4/text", HttpServletResponse.SC_OK);
    // assert non-existence of src?
    assertHttpStatus(HTTP_BASE_URL + testPath + "/test/src1.html", HttpServletResponse.SC_NOT_FOUND);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/test/src2.html", HttpServletResponse.SC_NOT_FOUND);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/test/src3.html", HttpServletResponse.SC_NOT_FOUND);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/test/src4.html", HttpServletResponse.SC_NOT_FOUND);
    testClient.delete(testRoot);
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Aggregations

NameValuePair (org.apache.commons.httpclient.NameValuePair)162 ArrayList (java.util.ArrayList)109 Credentials (org.apache.commons.httpclient.Credentials)64 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)64 Test (org.junit.Test)55 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)49 JsonObject (javax.json.JsonObject)43 HashMap (java.util.HashMap)24 PostMethod (org.apache.commons.httpclient.methods.PostMethod)24 JsonArray (javax.json.JsonArray)20 Header (org.apache.commons.httpclient.Header)16 HashSet (java.util.HashSet)14 HttpClient (org.apache.commons.httpclient.HttpClient)13 HttpMethod (org.apache.commons.httpclient.HttpMethod)13 GetMethod (org.apache.commons.httpclient.methods.GetMethod)12 IOException (java.io.IOException)8 LinkedList (java.util.LinkedList)8 InputStream (java.io.InputStream)7 URL (java.net.URL)6 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)5