Search in sources :

Example 16 with TestSite

use of org.alfresco.rest.api.tests.RepoService.TestSite in project alfresco-remote-api by Alfresco.

the class TestNodeComments method setup.

@Override
@Before
public void setup() throws Exception {
    // init networks
    super.setup();
    Iterator<TestNetwork> accountsIt = getTestFixture().getNetworksIt();
    this.network1 = accountsIt.next();
    this.network2 = accountsIt.next();
    // Create users in different networks and a site
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            TestPerson person = network1.createUser();
            people.add(person);
            person = network1.createUser();
            people.add(person);
            person = network1.createUser();
            people.add(person);
            person = network1.createUser();
            people.add(person);
            return null;
        }
    }, network1.getId());
    this.person11 = people.get(0);
    this.person12 = people.get(1);
    this.person13 = people.get(2);
    this.person14 = people.get(3);
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            TestPerson person = network2.createUser();
            people.add(person);
            person = network2.createUser();
            people.add(person);
            return null;
        }
    }, network2.getId());
    this.person21 = people.get(4);
    this.person22 = people.get(5);
    TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            TestSite site = network1.createSite(SiteVisibility.PRIVATE);
            sites.add(site);
            site.updateMember(person13.getId(), SiteRole.SiteCollaborator);
            site.updateMember(person14.getId(), SiteRole.SiteCollaborator);
            return null;
        }
    }, person11.getId(), network1.getId());
    final TestSite site1 = sites.get(0);
    final List<NodeRef> nodes = new ArrayList<NodeRef>();
    TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            NodeRef nodeRef = repoService.createDocument(site1.getContainerNodeRef("documentLibrary"), "Test Doc", "Test Content");
            nodes.add(nodeRef);
            NodeRef folderNodeRef = repoService.createFolder(site1.getContainerNodeRef("documentLibrary"), "Test Folder");
            nodeRef = folderNodeRef;
            nodes.add(nodeRef);
            nodeRef = repoService.createDocument(site1.getContainerNodeRef("documentLibrary"), "Test Doc 1", "Test Content 1");
            nodes.add(nodeRef);
            nodeRef = repoService.createDocument(site1.getContainerNodeRef("documentLibrary"), "Test Doc 2", "Test Content 2");
            nodes.add(nodeRef);
            nodeRef = repoService.createDocument(site1.getContainerNodeRef("documentLibrary"), "Test Doc 3", "Test Content 3");
            nodes.add(nodeRef);
            nodeRef = repoService.createCmObject(site1.getContainerNodeRef("documentLibrary"), "CM Object");
            nodes.add(nodeRef);
            nodeRef = repoService.createObjectOfCustomType(site1.getContainerNodeRef("documentLibrary"), "Custom type object", "{custom.model}sop");
            nodes.add(nodeRef);
            nodeRef = repoService.createDocument(folderNodeRef, "Test Doc 4", "Test Content 4 - in Test Folder");
            nodes.add(nodeRef);
            return null;
        }
    }, person11.getId(), network1.getId());
    this.nodeRef1 = nodes.get(0);
    this.folderNodeRef1 = nodes.get(1);
    this.nodeRef2 = nodes.get(2);
    this.nodeRef3 = nodes.get(3);
    this.nodeRef4 = nodes.get(4);
    this.cmObjectNodeRef = nodes.get(5);
    this.customTypeObject = nodes.get(6);
    this.nodeRef5 = nodes.get(7);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) ArrayList(java.util.ArrayList) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) Before(org.junit.Before)

Example 17 with TestSite

use of org.alfresco.rest.api.tests.RepoService.TestSite in project alfresco-remote-api by Alfresco.

the class TestPersonSites method getPersonSites.

private List<MemberOfSite> getPersonSites(TestPerson person, TestSite... sites) throws PublicApiException {
    Sites sitesProxy = publicApiClient.sites();
    List<MemberOfSite> memberOfSiteList = new ArrayList<>();
    for (TestSite site : sites) {
        memberOfSiteList.add(sitesProxy.getPersonSite(person.getId(), site.getSiteId()));
    }
    return memberOfSiteList;
}
Also used : TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) ArrayList(java.util.ArrayList) MemberOfSite(org.alfresco.rest.api.tests.client.data.MemberOfSite) Sites(org.alfresco.rest.api.tests.client.PublicApiClient.Sites)

Example 18 with TestSite

use of org.alfresco.rest.api.tests.RepoService.TestSite in project alfresco-remote-api by Alfresco.

the class TestPersonSites method initializeSites.

// TODO switch to use V1 createSite (instead of RepoService)
private void initializeSites() throws Exception {
    /*
         * Create data for testing the site sorting. We create the sites as
         * person31 and assign roles to person32. The list requests will be
         * performed as person32.
         */
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            person31 = network1.createUser();
            person32 = network1.createUser();
            return null;
        }
    }, network1.getId());
    this.site1 = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

        @Override
        public TestSite doWork() throws Exception {
            SiteInformation siteInfo = new SiteInformation(site1_id, site1_title, site1_title, SiteVisibility.PRIVATE);
            TestSite site = network1.createSite(siteInfo);
            site.inviteToSite(person32.getId(), site1_role);
            return site;
        }
    }, person31.getId(), network1.getId());
    this.site2 = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

        @Override
        public TestSite doWork() throws Exception {
            SiteInformation siteInfo = new SiteInformation(site2_id, site2_title, site2_title, SiteVisibility.PRIVATE);
            TestSite site = network1.createSite(siteInfo);
            site.inviteToSite(person32.getId(), site2_role);
            return site;
        }
    }, person31.getId(), network1.getId());
    this.site3 = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

        @Override
        public TestSite doWork() throws Exception {
            SiteInformation siteInfo = new SiteInformation(site3_id, site3_title, site3_title, SiteVisibility.PRIVATE);
            TestSite site = network1.createSite(siteInfo);
            site.inviteToSite(person32.getId(), site3_role);
            return site;
        }
    }, person31.getId(), network1.getId());
}
Also used : SiteInformation(org.alfresco.rest.api.tests.RepoService.SiteInformation) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException)

Example 19 with TestSite

use of org.alfresco.rest.api.tests.RepoService.TestSite in project alfresco-remote-api by Alfresco.

the class TestSiteContainers method setup.

@Override
@Before
public void setup() throws Exception {
    // init networks
    super.setup();
    Iterator<TestNetwork> networksIt = getTestFixture().getNetworksIt();
    assertTrue(networksIt.hasNext());
    this.network1 = networksIt.next();
    assertTrue(networksIt.hasNext());
    this.network2 = networksIt.next();
    // Create some users in different networks
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            // add as external user
            TestPerson person1 = network1.createUser();
            people1.add(person1);
            TestPerson person2 = network1.createUser();
            people1.add(person2);
            TestPerson person3 = network1.createUser();
            people1.add(person3);
            return null;
        }
    }, network1.getId());
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            TestPerson person1 = network2.createUser();
            people2.add(person1);
            return null;
        }
    }, network2.getId());
    // site creator
    this.person11 = people1.get(0);
    // same network, not invited to site
    this.person12 = people1.get(1);
    // different network, not invited to site
    this.person21 = people2.get(0);
    // same network, invited to site
    this.person13 = people1.get(2);
    // TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>()
    // {
    // @Override
    // public Void doWork() throws Exception
    // {
    // // add as external user
    // TestPerson person1 = network2.createUser();
    // network2People.add(person1);
    // 
    // return null;
    // }
    // }, network2.getId());
    // 
    // final TestPerson person3 = network2People.get(0);
    // Create a public site
    this.site1 = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

        @Override
        public TestSite doWork() throws Exception {
            TestSite testSite = network1.createSite(SiteVisibility.PUBLIC);
            return testSite;
        }
    }, person11.getId(), network1.getId());
    TenantUtil.runAsUserTenant(new TenantRunAsWork<Map<String, NodeRef>>() {

        @Override
        public Map<String, NodeRef> doWork() throws Exception {
            Map<String, NodeRef> containers = new HashMap<String, NodeRef>();
            containers.put("test1", site1.createContainer("test1"));
            containers.put("test2", site1.createContainer("test2"));
            containers.put("test3", site1.createContainer("test3"));
            return containers;
        }
    }, person11.getId(), network1.getId());
}
Also used : TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 20 with TestSite

use of org.alfresco.rest.api.tests.RepoService.TestSite in project alfresco-remote-api by Alfresco.

the class TestSiteMembers method testSiteMembers.

// TODO set create member for a user who is a member of the site (not the creator)
// TODO split into more manageable test methods
@Test
public void testSiteMembers() throws Exception {
    Iterator<TestNetwork> networksIt = getTestFixture().getNetworksIt();
    final TestNetwork testNetwork = networksIt.next();
    final List<String> networkPeople = testNetwork.getPersonIds();
    String personId = networkPeople.get(0);
    Sites sitesProxy = publicApiClient.sites();
    {
        final List<SiteMember> expectedSiteMembers = new ArrayList<SiteMember>();
        // Create a private site and invite some users
        // TODO create site members using public api rather than directly using the services
        TestSite testSite = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

            @Override
            public TestSite doWork() throws Exception {
                TestSite testSite = testNetwork.createSite(SiteVisibility.PRIVATE);
                for (int i = 1; i <= 5; i++) {
                    String inviteeId = networkPeople.get(i);
                    testSite.inviteToSite(inviteeId, SiteRole.SiteConsumer);
                    SiteMember sm = new SiteMember(inviteeId, repoService.getPerson(inviteeId), testSite.getSiteId(), SiteRole.SiteConsumer.toString());
                    expectedSiteMembers.add(sm);
                }
                return testSite;
            }
        }, personId, testNetwork.getId());
        {
            SiteMember sm = new SiteMember(personId, repoService.getPerson(personId), testSite.getSiteId(), SiteRole.SiteManager.toString());
            expectedSiteMembers.add(sm);
            Collections.sort(expectedSiteMembers);
        }
        // Test Case cloud-1482
        {
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, expectedSiteMembers.size(), null);
            publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), personId));
            ListResponse<SiteMember> siteMembers = sitesProxy.getSiteMembers(testSite.getSiteId(), createParams(paging, null));
            checkList(expectedSiteMembers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), siteMembers);
        }
        {
            int skipCount = 2;
            int maxItems = 10;
            Paging paging = getPaging(skipCount, maxItems, expectedSiteMembers.size(), null);
            publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), personId));
            ListResponse<SiteMember> siteMembers = sitesProxy.getSiteMembers(testSite.getSiteId(), createParams(paging, null));
            checkList(expectedSiteMembers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), siteMembers);
            HttpResponse response = sitesProxy.getAll("sites", testSite.getSiteId(), "members", null, createParams(paging, Collections.singletonMap("includeSource", "true")), "Failed to get all site members");
            checkList(expectedSiteMembers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), SiteMember.parseSiteMembers(testSite.getSiteId(), response.getJsonResponse()));
            JSONObject source = sitesProxy.parseListSource(response.getJsonResponse());
            Site sourceSite = SiteImpl.parseSite(source);
            assertNotNull(sourceSite);
            testSite.expected(sourceSite);
        }
        // invalid site id
        try {
            int skipCount = 2;
            int maxItems = 10;
            Paging paging = getPaging(skipCount, maxItems, expectedSiteMembers.size(), null);
            publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), personId));
            sitesProxy.getSiteMembers(GUID.generate(), createParams(paging, null));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // invalid methods
        try {
            SiteMember siteMember = expectedSiteMembers.get(0);
            publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), personId));
            sitesProxy.update("sites", testSite.getSiteId(), "members", null, siteMember.toJSON().toString(), "Unable to PUT site members");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        // Test Case cloud-1965
        try {
            SiteMember siteMember1 = expectedSiteMembers.get(0);
            publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), personId));
            sitesProxy.create("sites", testSite.getSiteId(), "members", siteMember1.getMemberId(), siteMember1.toJSON().toString(), "Unable to POST to a site member");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            SiteMember siteMember1 = expectedSiteMembers.get(0);
            publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), personId));
            sitesProxy.update("sites", testSite.getSiteId(), "members", null, siteMember1.toJSON().toString(), "Unable to PUT site members");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        try {
            publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), personId));
            sitesProxy.remove("sites", testSite.getSiteId(), "members", null, "Unable to DELETE site members");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
        // update site member
        {
            SiteMember siteMember1 = expectedSiteMembers.get(0);
            publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), personId));
            SiteMember ret = sitesProxy.updateSiteMember(testSite.getSiteId(), siteMember1);
            assertEquals(siteMember1.getRole(), ret.getRole());
            Person expectedSiteMember = repoService.getPerson(siteMember1.getMemberId());
            expectedSiteMember.expected(ret.getMember());
        }
        // GET single site member
        {
            SiteMember siteMember1 = expectedSiteMembers.get(0);
            publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), personId));
            SiteMember ret = sitesProxy.getSingleSiteMember(testSite.getSiteId(), siteMember1.getMemberId());
            siteMember1.expected(ret);
        }
    }
    // test: user is member of different tenant, but has site membership(s) in common with the http request user
    {
        Iterator<TestNetwork> accountsIt = getTestFixture().getNetworksIt();
        assertTrue(accountsIt.hasNext());
        final TestNetwork network1 = accountsIt.next();
        assertTrue(accountsIt.hasNext());
        final TestNetwork network2 = accountsIt.next();
        final List<TestPerson> people = new ArrayList<TestPerson>();
        // Create users
        TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                TestPerson person = network1.createUser();
                people.add(person);
                person = network1.createUser();
                people.add(person);
                person = network1.createUser();
                people.add(person);
                return null;
            }
        }, network1.getId());
        final TestPerson person1 = people.get(0);
        final TestPerson person2 = people.get(1);
        final TestPerson person3 = people.get(2);
        TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                TestPerson person = network2.createUser();
                people.add(person);
                return null;
            }
        }, network2.getId());
        final TestPerson person4 = people.get(3);
        // Create site
        final TestSite site = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

            @Override
            public TestSite doWork() throws Exception {
                TestSite site = network1.createSite(SiteVisibility.PUBLIC);
                return site;
            }
        }, person2.getId(), network1.getId());
        // invalid role - 400
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person1.getId(), "dodgyRole"));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
        }
        // user in network but not site member, try to create site member
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person3.getId(), SiteRole.SiteContributor.toString()));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
        }
        // unknown invitee - 404
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember("dodgyUser", SiteRole.SiteContributor.toString()));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // unknown site - 404
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            sitesProxy.createSiteMember("dodgySite", new SiteMember(person1.getId(), SiteRole.SiteContributor.toString()));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // inviter is not a member of the site
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person1.getId(), SiteRole.SiteContributor.toString()));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(e.getMessage(), HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
        }
        // inviter is not a member of the site nor a member of the tenant
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person4.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person1.getId(), SiteRole.SiteContributor.toString()));
            fail("");
        } catch (PublicApiException e) {
            // TODO check that 404 is correct here - external user of network can't see public site??
            assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
        }
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            SiteMember sm = new SiteMember(person1.getId(), SiteRole.SiteConsumer.toString());
            SiteMember siteMember = sitesProxy.createSiteMember(site.getSiteId(), sm);
            assertEquals(person1.getId(), siteMember.getMemberId());
            assertEquals(SiteRole.SiteConsumer.toString(), siteMember.getRole());
        }
        // already invited
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person1.getId(), SiteRole.SiteContributor.toString()));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_CONFLICT, e.getHttpResponse().getStatusCode());
        }
        // inviter is consumer member of the site, should not be able to add site member
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person4.getId(), SiteRole.SiteContributor.toString()));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(e.getMessage(), HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // invitee from another network
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person4.getId(), SiteRole.SiteContributor.toString()));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(e.getMessage(), HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
        // missing person id
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(null, SiteRole.SiteContributor.toString()));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
        }
        // missing role
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person1.getId(), null));
            fail("");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
        }
        // check site membership in GET
        List<SiteMember> expectedSiteMembers = site.getMembers();
        {
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedSiteMembers.size(), null);
            ListResponse<SiteMember> siteMembers = sitesProxy.getSiteMembers(site.getSiteId(), createParams(paging, null));
            checkList(expectedSiteMembers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), siteMembers);
        }
    }
    // test: create site membership, remove it, get list of site memberships
    {
        Iterator<TestNetwork> accountsIt = getTestFixture().getNetworksIt();
        assertTrue(accountsIt.hasNext());
        final TestNetwork network1 = accountsIt.next();
        assertTrue(accountsIt.hasNext());
        final List<TestPerson> people = new ArrayList<TestPerson>();
        // Create user
        TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                TestPerson person = network1.createUser();
                people.add(person);
                person = network1.createUser();
                people.add(person);
                person = network1.createUser();
                people.add(person);
                return null;
            }
        }, network1.getId());
        TestPerson person1 = people.get(0);
        TestPerson person2 = people.get(1);
        TestPerson person3 = people.get(2);
        // Create site
        TestSite site = TenantUtil.runAsUserTenant(new TenantRunAsWork<TestSite>() {

            @Override
            public TestSite doWork() throws Exception {
                TestSite site = network1.createSite(SiteVisibility.PRIVATE);
                return site;
            }
        }, person2.getId(), network1.getId());
        // remove site membership
        // for -me- user (PUBLICAPI-90)
        {
            // create a site member
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            SiteMember siteMember = sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person1.getId(), SiteRole.SiteContributor.toString()));
            assertEquals(person1.getId(), siteMember.getMemberId());
            assertEquals(SiteRole.SiteContributor.toString(), siteMember.getRole());
            SiteMember toRemove = new SiteMember("-me-");
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            sitesProxy.removeSiteMember(site.getSiteId(), toRemove);
        }
        {
            // create a site member
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            SiteMember siteMember = sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person1.getId(), SiteRole.SiteContributor.toString()));
            assertEquals(person1.getId(), siteMember.getMemberId());
            assertEquals(SiteRole.SiteContributor.toString(), siteMember.getRole());
            // note: needed for contains check below, ugh
            siteMember.setSiteId(site.getSiteId());
            // create another site member
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
            SiteMember siteMemberAno = sitesProxy.createSiteMember(site.getSiteId(), new SiteMember(person3.getId(), SiteRole.SiteCollaborator.toString()));
            assertEquals(person3.getId(), siteMemberAno.getMemberId());
            assertEquals(SiteRole.SiteCollaborator.toString(), siteMemberAno.getRole());
            // note: needed for contains check below, ugh
            siteMemberAno.setSiteId(site.getSiteId());
            // unknown site
            try {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                sitesProxy.removeSiteMember(GUID.generate(), siteMember);
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
            }
            // unknown user
            try {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                sitesProxy.removeSiteMember(site.getSiteId(), new SiteMember(GUID.generate()));
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
            }
            // cannot update site member (without appropriate site "permission" - see SiteService)
            try {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
                sitesProxy.updateSiteMember(site.getSiteId(), new SiteMember(person1.getId(), SiteRole.SiteCollaborator.toString()));
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_UNPROCESSABLE_ENTITY, e.getHttpResponse().getStatusCode());
            }
            // cannot remove another site member (without appropriate site "permission" - see SiteService)
            try {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
                sitesProxy.removeSiteMember(site.getSiteId(), new SiteMember(person3.getId()));
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_UNPROCESSABLE_ENTITY, e.getHttpResponse().getStatusCode());
            }
            // remove site member
            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                sitesProxy.removeSiteMember(site.getSiteId(), siteMember);
            }
            // check site membership in GET
            List<SiteMember> expectedSiteMembers = site.getMembers();
            assertFalse(expectedSiteMembers.contains(siteMember));
            assertTrue(expectedSiteMembers.contains(siteMemberAno));
            {
                int skipCount = 0;
                int maxItems = Integer.MAX_VALUE;
                Paging paging = getPaging(skipCount, maxItems, expectedSiteMembers.size(), null);
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                ListResponse<SiteMember> siteMembers = sitesProxy.getSiteMembers(site.getSiteId(), createParams(paging, null));
                checkList(expectedSiteMembers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), siteMembers);
            }
            // unknown site
            try {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                sitesProxy.updateSiteMember(GUID.generate(), siteMember);
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
            }
            // unknown user
            try {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                sitesProxy.updateSiteMember(site.getSiteId(), new SiteMember(GUID.generate()));
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
            }
            // invalid role
            try {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                sitesProxy.updateSiteMember(site.getSiteId(), new SiteMember(person1.getId(), "invalidRole"));
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
            }
            // user is not a member of the site - 400
            try {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                sitesProxy.updateSiteMember(site.getSiteId(), new SiteMember(person1.getId(), SiteRole.SiteContributor.toString()));
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
            }
            // cannot update last member of site to be a non-manager
            try {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                sitesProxy.updateSiteMember(site.getSiteId(), new SiteMember(person2.getId(), SiteRole.SiteContributor.toString()));
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_UNPROCESSABLE_ENTITY, e.getHttpResponse().getStatusCode());
            }
            // successful update
            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                SiteMember sm = new SiteMember(person1.getId(), SiteRole.SiteContributor.toString());
                SiteMember ret = sitesProxy.createSiteMember(site.getSiteId(), sm);
                assertEquals(SiteRole.SiteContributor.toString(), ret.getRole());
                person1.expected(ret.getMember());
                sm = new SiteMember(person1.getId(), SiteRole.SiteCollaborator.toString());
                ret = sitesProxy.updateSiteMember(site.getSiteId(), sm);
                assertEquals(SiteRole.SiteCollaborator.toString(), ret.getRole());
                person1.expected(ret.getMember());
                // check site membership in GET
                SiteMember smToCheck = sitesProxy.getSingleSiteMember(site.getSiteId(), person1.getId());
                // check that the update site membership is present
                assertNotNull(smToCheck);
                // check that the role is correct
                assertEquals(sm.getRole(), smToCheck.getRole());
                expectedSiteMembers = new ArrayList<>();
                SiteMember sm1 = new SiteMember(person1.getId(), person1, site.getSiteId(), SiteRole.SiteCollaborator.toString());
                expectedSiteMembers.add(sm1);
                SiteMember sm2 = new SiteMember(person2.getId(), person2, site.getSiteId(), SiteRole.SiteManager.toString());
                expectedSiteMembers.add(sm2);
                SiteMember sm3 = new SiteMember(person3.getId(), person3, site.getSiteId(), SiteRole.SiteCollaborator.toString());
                expectedSiteMembers.add(sm3);
                Collections.sort(expectedSiteMembers);
                int skipCount = 0;
                int maxItems = Integer.MAX_VALUE;
                Paging paging = getPaging(skipCount, maxItems, expectedSiteMembers.size(), null);
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
                ListResponse<SiteMember> siteMembers = sitesProxy.getSiteMembers(site.getSiteId(), createParams(paging, null));
                checkList(expectedSiteMembers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), siteMembers);
            }
        }
    }
}
Also used : TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) Site(org.alfresco.rest.api.tests.client.data.Site) ListResponse(org.alfresco.rest.api.tests.client.PublicApiClient.ListResponse) SiteMember(org.alfresco.rest.api.tests.client.data.SiteMember) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) Sites(org.alfresco.rest.api.tests.client.PublicApiClient.Sites) PublicApiException(org.alfresco.rest.api.tests.client.PublicApiException) JSONObject(org.json.simple.JSONObject) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) TestNetwork(org.alfresco.rest.api.tests.RepoService.TestNetwork) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) TestPerson(org.alfresco.rest.api.tests.RepoService.TestPerson) Person(org.alfresco.rest.api.tests.client.data.Person) Test(org.junit.Test)

Aggregations

TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)47 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)42 Test (org.junit.Test)39 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)35 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)33 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)31 NodeRef (org.alfresco.service.cmr.repository.NodeRef)29 SiteInformation (org.alfresco.rest.api.tests.RepoService.SiteInformation)27 ArrayList (java.util.ArrayList)23 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)19 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)19 CmisInvalidArgumentException (org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)19 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)19 CmisPermissionDeniedException (org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException)19 CmisUpdateConflictException (org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException)19 HashMap (java.util.HashMap)18 CmisSession (org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession)18 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)17 List (java.util.List)15 AlfrescoDocument (org.alfresco.cmis.client.AlfrescoDocument)15