Search in sources :

Example 51 with RequestContext

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

the class TestPeople method testUpdatePersonNonexistentPerson.

@Test
public void testUpdatePersonNonexistentPerson() throws PublicApiException {
    final String personId = "non-existent";
    publicApiClient.setRequestContext(new RequestContext(account3.getId(), account3Admin, "admin"));
    people.update("people", personId, null, null, "{\n" + "  \"firstName\": \"Updated firstName\"\n" + "}", null, "Expected 404 response when updating " + personId, 404);
}
Also used : RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Test(org.junit.Test)

Example 52 with RequestContext

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

the class TestPeople method updateAvatar.

@Test
public void updateAvatar() throws PublicApiException, IOException {
    final String person1 = account1PersonIt.next();
    final String person2 = account1PersonIt.next();
    publicApiClient.setRequestContext(new RequestContext(account1.getId(), person2));
    AuthenticationUtil.setFullyAuthenticatedUser(person2);
    // Update allowed when no existing avatar
    {
        // Pre-condition: no avatar exists
        NodeRef personRef = personService.getPerson(person2, false);
        deleteAvatarDirect(personRef);
        people.getAvatar(person2, false, 404);
        // TODO: What do we expect the 200 response body to be? Currently it's the person JSON - doesn't seem right.
        ClassPathResource avatar = new ClassPathResource("publicapi/upload/quick.jpg");
        HttpResponse response = people.updateAvatar(person2, avatar.getFile(), 200);
        // TODO: ideally, this should be a "direct" retrieval to isolate update from get
        people.getAvatar(person2, false, 200);
    }
    // Update existing avatar
    {
        // Pre-condition: avatar exists
        people.getAvatar(person2, false, 200);
        ClassPathResource avatar = new ClassPathResource("test.jpg");
        HttpResponse response = people.updateAvatar(person2, avatar.getFile(), 200);
        people.getAvatar(person2, false, 200);
        // -me- alias
        people.updateAvatar(person2, avatar.getFile(), 200);
        people.getAvatar("-me-", false, 200);
    }
    // 400: invalid user ID
    {
        ClassPathResource avatar = new ClassPathResource("publicapi/upload/quick.jpg");
        people.updateAvatar("joe@@bloggs.example.com", avatar.getFile(), 404);
    }
    // 401: authentication failure
    {
        publicApiClient.setRequestContext(new RequestContext(account1.getId(), account1Admin, "Wr0ngP4ssw0rd!"));
        ClassPathResource avatar = new ClassPathResource("publicapi/upload/quick.jpg");
        people.updateAvatar(account1Admin, avatar.getFile(), 401);
    }
    // 403: permission denied
    {
        publicApiClient.setRequestContext(new RequestContext(account1.getId(), person1));
        ClassPathResource avatar = new ClassPathResource("publicapi/upload/quick.jpg");
        people.updateAvatar(person2, avatar.getFile(), 403);
        // Person can update themself
        people.updateAvatar(person1, avatar.getFile(), 200);
        // Admin can update someone else
        publicApiClient.setRequestContext(new RequestContext(account1.getId(), account1Admin, "admin"));
        people.updateAvatar(person1, avatar.getFile(), 200);
    }
    // 404: non-existent person
    {
        publicApiClient.setRequestContext(new RequestContext(account1.getId(), person1));
        // Pre-condition: non-existent person
        String nonPerson = "joebloggs@" + account1.getId();
        people.getPerson(nonPerson, 404);
        ClassPathResource avatar = new ClassPathResource("publicapi/upload/quick.jpg");
        people.updateAvatar(nonPerson, avatar.getFile(), 404);
    }
    // 413: content exceeds individual file size limit
    {
        // Test content size limit
        final ContentLimitProvider.SimpleFixedLimitProvider limitProvider = applicationContext.getBean("defaultContentLimitProvider", ContentLimitProvider.SimpleFixedLimitProvider.class);
        final long defaultSizeLimit = limitProvider.getSizeLimit();
        // 20 KB
        limitProvider.setSizeLimitString("20000");
        try {
            // ~26K
            ClassPathResource avatar = new ClassPathResource("publicapi/upload/quick.jpg");
            people.updateAvatar(person1, avatar.getFile(), 413);
        } finally {
            limitProvider.setSizeLimitString(Long.toString(defaultSizeLimit));
        }
    }
    // 501: thumbnails disabled
    {
        ThumbnailService thumbnailService = applicationContext.getBean("thumbnailService", ThumbnailService.class);
        // Disable thumbnail generation
        thumbnailService.setThumbnailsEnabled(false);
        try {
            ClassPathResource avatar = new ClassPathResource("publicapi/upload/quick.jpg");
            people.updateAvatar(person1, avatar.getFile(), 501);
        } finally {
            thumbnailService.setThumbnailsEnabled(true);
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ThumbnailService(org.alfresco.service.cmr.thumbnail.ThumbnailService) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 53 with RequestContext

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

the class TestPeople method testPagingAndSortingByLastName.

/**
 * Tests the capability to sort and paginate the list of people orderBy =
 * lastName ASC skip = 2, count = 3
 *
 * @throws Exception
 */
@Test
public void testPagingAndSortingByLastName() throws Exception {
    publicApiClient.setRequestContext(new RequestContext(account4.getId(), account4Admin, "admin"));
    // paging
    int skipCount = 2;
    int maxItems = 3;
    int totalResults = 5;
    PublicApiClient.Paging paging = getPaging(skipCount, maxItems, totalResults, totalResults);
    // orderBy=lastName ASC
    PublicApiClient.ListResponse<Person> resp = listPeople(paging, "lastName", true, 200);
    List<Person> expectedList = new LinkedList<>();
    expectedList.add((Person) personBen);
    expectedList.add((Person) personAliceD);
    expectedList.add((Person) personAlice);
    checkList(expectedList, paging.getExpectedPaging(), resp);
}
Also used : PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Person(org.alfresco.rest.api.tests.client.data.Person) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 54 with RequestContext

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

the class TestPeople method retrieveAvatar.

@Test
public void retrieveAvatar() throws Exception {
    final String person1 = account1PersonIt.next();
    publicApiClient.setRequestContext(new RequestContext(account1.getId(), person1));
    AuthenticationUtil.setFullyAuthenticatedUser(person1);
    NodeRef person1Ref = personService.getPerson(person1, false);
    // No avatar, but valid person
    {
        deleteAvatarDirect(person1Ref);
        // Pre-condition of test case
        assertNotNull(people.getPerson(person1));
        people.getAvatar(person1, false, 404);
    }
    // No avatar, but person exists and placeholder requested
    {
        // Pre-condition of test case
        assertNotNull(people.getPerson(person1));
        people.getAvatar(person1, true, 200);
    }
    // Non-existent person
    {
        String nonPerson = "i-do-not-exist";
        // Pre-condition of test case
        people.getPerson(nonPerson, 404);
        people.getAvatar(nonPerson, false, 404);
    }
    // Placeholder requested, but non-existent person
    {
        String nonPerson = "i-do-not-exist";
        // Pre-condition of test case
        people.getPerson(nonPerson, 404);
        people.getAvatar(nonPerson, true, 404);
    }
    // Avatar exists
    {
        // Create avatar - direct (i.e. not using the API, so that tests for get avatar can be separated from upload)
        // There's no significance to the image being used here, it was the most suitable I could find.
        ClassPathResource thumbRes = new ClassPathResource("test.jpg");
        deleteAvatarDirect(person1Ref);
        createAvatarDirect(person1Ref, thumbRes.getFile());
        // Get avatar - API call
        people.getAvatar(person1, false, 200);
    }
    // -me- alias
    {
        people.getAvatar("-me-", false, 200);
    }
    // If-Modified-Since behaviour
    {
        HttpResponse response = people.getAvatar(person1, false, 200);
        Map<String, String> responseHeaders = response.getHeaders();
        // Test 304 response
        String lastModified = responseHeaders.get(LAST_MODIFIED_HEADER);
        assertNotNull(lastModified);
        // Has it been modified since the time it was last modified - no!
        people.getAvatar(person1, lastModified, 304);
        // Create an updated avatar
        // ensure time has passed between updates
        waitMillis(2000);
        ClassPathResource thumbRes = new ClassPathResource("publicapi/upload/quick.jpg");
        deleteAvatarDirect(person1Ref);
        createAvatarDirect(person1Ref, thumbRes.getFile());
        people.getAvatar(person1, lastModified, 200);
    }
    // Attachment param
    {
        // No attachment parameter (default true)
        Boolean attachmentParam = null;
        HttpResponse response = people.getAvatar(person1, attachmentParam, false, null, 200);
        Map<String, String> responseHeaders = response.getHeaders();
        String contentDisposition = responseHeaders.get("Content-Disposition");
        assertNotNull(contentDisposition);
        assertTrue(contentDisposition.startsWith("attachment;"));
        // attachment=true
        attachmentParam = true;
        response = people.getAvatar(person1, attachmentParam, false, null, 200);
        responseHeaders = response.getHeaders();
        contentDisposition = responseHeaders.get("Content-Disposition");
        assertNotNull(contentDisposition);
        assertTrue(contentDisposition.startsWith("attachment;"));
        // attachment=false
        attachmentParam = false;
        response = people.getAvatar(person1, attachmentParam, false, null, 200);
        responseHeaders = response.getHeaders();
        contentDisposition = responseHeaders.get("Content-Disposition");
        assertNull(contentDisposition);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Map(java.util.Map) HashMap(java.util.HashMap) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 55 with RequestContext

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

the class TestPeople method testUpdatePersonPasswordByThemself.

@Test
public void testUpdatePersonPasswordByThemself() throws PublicApiException {
    publicApiClient.setRequestContext(new RequestContext(account1.getId(), account1Admin, "admin"));
    Person me = new Person();
    me.setId(UUID.randomUUID().toString() + "@" + account1.getId());
    me.setUserName(me.getId());
    me.setFirstName("Jo");
    me.setEmail(me.getId());
    me.setEnabled(true);
    me.setPassword("password123");
    me = people.create(me);
    publicApiClient.setRequestContext(new RequestContext(account1.getId(), me.getId(), "password123"));
    // update with correct oldPassword
    people.update(me.getId(), qjson("{ `oldPassword`:`password123`, `password`:`newpassword456` }"), 200);
    // The old password should no longer work - therefore they are "unauthorized".
    publicApiClient.setRequestContext(new RequestContext(account1.getId(), me.getId(), "password123"));
    people.getPerson(me.getId(), 401);
    // The new password should work.
    publicApiClient.setRequestContext(new RequestContext(account1.getId(), me.getId(), "newpassword456"));
    people.getPerson(me.getId());
    // update with wrong oldPassword
    people.update(me.getId(), qjson("{ `oldPassword`:`password123`, `password`:`newpassword456` }"), 403);
    // update with no oldPassword
    people.update(me.getId(), qjson("{ `password`:`newpassword456` }"), 400);
    people.update(me.getId(), qjson("{ `oldPassword`:``, `password`:`newpassword456` }"), 400);
    people.update(me.getId(), qjson("{ `oldPassword`:null, `password`:`newpassword456` }"), 400);
    // update with no new password
    people.update(me.getId(), qjson("{ `oldPassword`:`newpassword456` }"), 400);
    people.update(me.getId(), qjson("{ `oldPassword`:`newpassword456`, `password`:`` }"), 400);
    people.update(me.getId(), qjson("{ `oldPassword`:`newpassword456`, `password`:null }"), 400);
}
Also used : RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Person(org.alfresco.rest.api.tests.client.data.Person) Test(org.junit.Test)

Aggregations

RequestContext (org.alfresco.rest.api.tests.client.RequestContext)185 Test (org.junit.Test)174 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)102 JSONObject (org.json.simple.JSONObject)67 HashMap (java.util.HashMap)58 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)52 ArrayList (java.util.ArrayList)46 NodeRef (org.alfresco.service.cmr.repository.NodeRef)45 Task (org.activiti.engine.task.Task)44 TasksClient (org.alfresco.rest.workflow.api.tests.WorkflowApiClient.TasksClient)39 TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)38 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)36 ProcessInfo (org.alfresco.rest.workflow.api.model.ProcessInfo)36 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)33 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)26 Person (org.alfresco.rest.api.tests.client.data.Person)25 JSONArray (org.json.simple.JSONArray)25 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)24 Paging (org.alfresco.rest.api.tests.client.PublicApiClient.Paging)24 List (java.util.List)23