Search in sources :

Example 56 with UsernamePasswordCredentials

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

the class ModifyAceTest method createAceOrderTestFolderWithOneAce.

/**
	 * Helper to create a test folder with a single ACE pre-created
	 */
private void createAceOrderTestFolderWithOneAce() throws IOException, JsonException {
    testUserId = H.createTestUser();
    testFolderUrl = H.createTestFolder();
    addOrUpdateAce(testFolderUrl, testUserId, true, null);
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    assertEquals(1, jsonObject.size());
    JsonObject user = jsonObject.getJsonObject(testUserId);
    assertNotNull(user);
    assertEquals(testUserId, user.getString("principal"));
    assertEquals(0, user.getInt("order"));
}
Also used : JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 57 with UsernamePasswordCredentials

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

the class RemoveAcesTest method testRemoveAces.

//test removing multiple aces
public void testRemoveAces() throws IOException, JsonException {
    String folderUrl = createFolderWithAces(true);
    //remove the ace for the testUser principal
    String postUrl = folderUrl + ".deleteAce.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair(":applyTo", testUserId));
    postParams.add(new NameValuePair(":applyTo", testGroupId));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    //fetch the JSON for the acl to verify the settings.
    String getUrl = folderUrl + ".acl.json";
    String json = getAuthenticatedContent(creds, getUrl, CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    assertNotNull(jsonObject);
    assertEquals(0, jsonObject.size());
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 58 with UsernamePasswordCredentials

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

the class AnonymousAccessTest method testAnonymousContent.

public void testAnonymousContent() throws Exception {
    // disable credentials -> anonymous session
    final URL url = new URL(HTTP_BASE_URL);
    final AuthScope scope = new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM);
    httpClient.getParams().setAuthenticationPreemptive(false);
    httpClient.getState().setCredentials(scope, null);
    try {
        assertContent();
    } finally {
        // re-enable credentials -> admin session
        httpClient.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials("admin", "admin");
        httpClient.getState().setCredentials(scope, defaultcreds);
    }
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope) URL(java.net.URL) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 59 with UsernamePasswordCredentials

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

the class RemoveAcesTest method testRemoveAce.

//test removing a single ace
public void testRemoveAce() throws IOException, JsonException {
    String folderUrl = createFolderWithAces(false);
    //remove the ace for the testUser principal
    String postUrl = folderUrl + ".deleteAce.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair(":applyTo", testUserId));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    //fetch the JSON for the acl to verify the settings.
    String getUrl = folderUrl + ".acl.json";
    String json = getAuthenticatedContent(creds, getUrl, CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    assertNotNull(jsonObject);
    assertEquals(0, jsonObject.size());
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 60 with UsernamePasswordCredentials

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

the class RemoveAcesTest method createFolderWithAces.

private String createFolderWithAces(boolean addGroupAce) throws IOException, JsonException {
    testUserId = createTestUser();
    testFolderUrl = createTestFolder();
    String postUrl = testFolderUrl + ".modifyAce.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:write", "denied"));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    if (addGroupAce) {
        testGroupId = createTestGroup();
        postParams = new ArrayList<NameValuePair>();
        postParams.add(new NameValuePair("principalId", testGroupId));
        postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
        assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    }
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    String json = getAuthenticatedContent(creds, getUrl, CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    if (addGroupAce) {
        assertEquals(2, jsonObject.size());
    } else {
        assertEquals(1, jsonObject.size());
    }
    JsonObject aceObject = jsonObject.getJsonObject(testUserId);
    assertNotNull(aceObject);
    assertEquals(0, aceObject.getInt("order"));
    String principalString = aceObject.getString("principal");
    assertEquals(testUserId, principalString);
    JsonArray grantedArray = aceObject.getJsonArray("granted");
    assertNotNull(grantedArray);
    assertEquals("jcr:read", grantedArray.getString(0));
    JsonArray deniedArray = aceObject.getJsonArray("denied");
    assertNotNull(deniedArray);
    assertEquals("jcr:write", deniedArray.getString(0));
    if (addGroupAce) {
        aceObject = jsonObject.getJsonObject(testGroupId);
        assertNotNull(aceObject);
        principalString = aceObject.getString("principal");
        assertEquals(testGroupId, principalString);
        assertEquals(1, aceObject.getInt("order"));
        grantedArray = aceObject.getJsonArray("granted");
        assertNotNull(grantedArray);
        assertEquals("jcr:read", grantedArray.getString(0));
    }
    return testFolderUrl;
}
Also used : JsonArray(javax.json.JsonArray) NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)113 Credentials (org.apache.commons.httpclient.Credentials)97 ArrayList (java.util.ArrayList)65 NameValuePair (org.apache.commons.httpclient.NameValuePair)61 JsonObject (javax.json.JsonObject)52 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)51 Test (org.junit.Test)51 JsonArray (javax.json.JsonArray)19 HttpClient (org.apache.commons.httpclient.HttpClient)19 AuthScope (org.apache.commons.httpclient.auth.AuthScope)17 HashSet (java.util.HashSet)14 GetMethod (org.apache.commons.httpclient.methods.GetMethod)14 HttpException (org.apache.commons.httpclient.HttpException)9 IOException (java.io.IOException)8 HttpMethod (org.apache.commons.httpclient.HttpMethod)8 HttpState (org.apache.commons.httpclient.HttpState)6 PostMethod (org.apache.commons.httpclient.methods.PostMethod)6 URL (java.net.URL)5 RepositoryException (org.apache.sling.ide.transport.RepositoryException)5 Header (org.apache.commons.httpclient.Header)4