Search in sources :

Example 1 with PersonNetwork

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

the class TestNetworks method testPersonNetworks.

@Test
public void testPersonNetworks() throws Exception {
    People peopleProxy = publicApiClient.people();
    {
        /**
         * Test http://<host>:<port>/alfresco/a i.e. tenant servlet root - should return user's networks
         */
        final TestNetwork testAccount = getTestFixture().getRandomNetwork();
        Iterator<TestPerson> personIt = testAccount.getPeople().iterator();
        final TestPerson person = personIt.next();
        RequestContext rc = new RequestContext(null, person.getId());
        publicApiClient.setRequestContext(rc);
        HttpResponse response = publicApiClient.delete(null, null, null, null, null);
        // url /null/alfresco/versions/1 does not map to a Web Script
        assertEquals(404, response.getStatusCode());
        PublicApiClient.ExpectedErrorResponse errorResponse = RestApiUtil.parseErrorResponse(response.getJsonResponse());
        assertNotNull(errorResponse);
        assertNotNull(errorResponse.getErrorKey());
        assertNotNull(errorResponse.getBriefSummary());
        response = publicApiClient.put(null, null, null, null, null, null, null);
        assertEquals(404, response.getStatusCode());
        errorResponse = RestApiUtil.parseErrorResponse(response.getJsonResponse());
        assertNotNull(errorResponse);
        assertNotNull(errorResponse.getErrorKey());
        assertNotNull(errorResponse.getBriefSummary());
        response = publicApiClient.post(null, null, null, null, null, null);
        assertEquals(404, response.getStatusCode());
        errorResponse = RestApiUtil.parseErrorResponse(response.getJsonResponse());
        assertNotNull(errorResponse);
        assertNotNull(errorResponse.getErrorKey());
        assertNotNull(errorResponse.getBriefSummary());
        List<PersonNetwork> expectedNetworkMembers = person.getNetworkMemberships();
        int expectedTotal = expectedNetworkMembers.size();
        {
            // GET / - users networks
            Paging paging = getPaging(0, Integer.MAX_VALUE, expectedTotal, expectedTotal);
            publicApiClient.setRequestContext(new RequestContext("-default-", person.getId()));
            response = publicApiClient.index(createParams(paging, null));
            ListResponse<PersonNetwork> resp = PersonNetwork.parseNetworkMembers(response.getJsonResponse());
            assertEquals(200, response.getStatusCode());
            checkList(new ArrayList<PersonNetwork>(expectedNetworkMembers), paging.getExpectedPaging(), resp);
        }
    }
    // user from another network
    {
        publicApiClient.setRequestContext(new RequestContext("-default-", person21.getId()));
        List<PersonNetwork> networksMemberships = Collections.emptyList();
        try {
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, networksMemberships.size(), networksMemberships.size());
            peopleProxy.getNetworkMemberships(person11.getId(), createParams(paging, null));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
    }
    // user from the same network
    try {
        List<PersonNetwork> networksMemberships = person12.getNetworkMemberships();
        publicApiClient.setRequestContext(new RequestContext("-default-", person12.getId()));
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, networksMemberships.size(), networksMemberships.size());
        peopleProxy.getNetworkMemberships(person11.getId(), createParams(paging, null));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    List<PersonNetwork> networksMemberships = person11.getNetworkMemberships();
    // Test Case cloud-2203
    // Test Case cloud-1498
    // test paging
    {
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, networksMemberships.size(), networksMemberships.size());
        ListResponse<PersonNetwork> resp = peopleProxy.getNetworkMemberships(person11.getId(), createParams(paging, null));
        checkList(networksMemberships.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
    }
    // "-me-" user
    {
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        int skipCount = 0;
        int maxItems = Integer.MAX_VALUE;
        Paging paging = getPaging(skipCount, maxItems, networksMemberships.size(), networksMemberships.size());
        ListResponse<PersonNetwork> resp = peopleProxy.getNetworkMemberships(org.alfresco.rest.api.People.DEFAULT_USER, createParams(paging, null));
        checkList(networksMemberships.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
    }
    // unknown person id
    try {
        List<PersonNetwork> networkMemberships = person11.getNetworkMemberships();
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        int skipCount = 0;
        int maxItems = 2;
        Paging expectedPaging = getPaging(skipCount, maxItems, networkMemberships.size(), networkMemberships.size());
        peopleProxy.getNetworkMemberships("invalidUser", createParams(expectedPaging, null));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // invalid caller authentication
    try {
        List<PersonNetwork> networkMemberships = person11.getNetworkMemberships();
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId(), GUID.generate()));
        int skipCount = 0;
        int maxItems = 2;
        Paging expectedPaging = getPaging(skipCount, maxItems, networkMemberships.size(), networkMemberships.size());
        peopleProxy.getNetworkMemberships(person11.getId(), createParams(expectedPaging, null));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
    }
    // unknown person id
    try {
        List<PersonNetwork> networkMemberships = person11.getNetworkMemberships();
        assertTrue(networkMemberships.size() > 0);
        PersonNetwork network = networkMemberships.get(0);
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        peopleProxy.getNetworkMembership("invalidUser", network.getId());
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // invalid caller authentication
    try {
        List<PersonNetwork> networkMemberships = person11.getNetworkMemberships();
        assertTrue(networkMemberships.size() > 0);
        PersonNetwork network = networkMemberships.get(0);
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId(), GUID.generate()));
        peopleProxy.getNetworkMembership(person11.getId(), network.getId());
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
    }
    // incorrect network id
    try {
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        peopleProxy.getNetworkMembership(person11.getId(), GUID.generate());
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }
    // POST, POST networkId, PUT, PUT networkId, DELETE, DELETE networkId
    try {
        PersonNetwork pn = new PersonNetwork(GUID.generate());
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        peopleProxy.create("people", person11.getId(), "networks", null, pn.toJSON().toString(), "Unable to POST to person networks");
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    try {
        PersonNetwork pn = networksMemberships.get(0);
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        peopleProxy.create("people", person11.getId(), "networks", pn.getId(), pn.toJSON().toString(), "Unable to POST to a person network");
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    try {
        PersonNetwork pn = new PersonNetwork(GUID.generate());
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        peopleProxy.update("people", person11.getId(), "networks", null, pn.toJSON().toString(), "Unable to PUT person networks");
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    try {
        PersonNetwork pn = networksMemberships.get(0);
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        peopleProxy.update("people", person11.getId(), "networks", pn.getId(), pn.toJSON().toString(), "Unable to PUT a person network");
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    try {
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        peopleProxy.remove("people", person11.getId(), "networks", null, "Unable to DELETE person networks");
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    try {
        PersonNetwork pn = networksMemberships.get(0);
        publicApiClient.setRequestContext(new RequestContext("-default-", person11.getId()));
        peopleProxy.remove("people", person11.getId(), "networks", pn.getId(), "Unable to DELETE a person network");
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }
    // user not a member of the network
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId()));
        int skipCount = 0;
        int maxItems = 2;
        Paging expectedPaging = getPaging(skipCount, maxItems);
        peopleProxy.getNetworkMemberships(person11.getId(), createParams(expectedPaging, null));
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
    }
}
Also used : ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) People(org.alfresco.rest.api.tests.client.PublicApiClient.People) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) PersonNetwork(org.alfresco.rest.api.tests.client.data.PersonNetwork) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)1 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)1 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)1 ListResponse (org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse)1 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)1 People (org.alfresco.rest.api.tests.client.PublicApiClient.People)1 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)1 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)1 PersonNetwork (org.alfresco.rest.api.tests.client.data.PersonNetwork)1 Test (org.junit.Test)1