use of org.alfresco.rest.api.tests.client.PublicApiClient.Sites in project alfresco-remote-api by Alfresco.
the class TestSiteContainers method testSiteContainers.
@Test
public void testSiteContainers() throws Exception {
Sites sitesProxy = publicApiClient.sites();
List<SiteContainer> expectedSiteContainers = network1.getSiteContainers(site1.getSiteId(), person11);
{
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
int skipCount = 0;
int maxItems = 2;
Paging paging = getPaging(skipCount, maxItems, expectedSiteContainers.size(), expectedSiteContainers.size());
ListResponse<SiteContainer> resp = sitesProxy.getSiteContainers(site1.getSiteId(), createParams(paging, null));
checkList(expectedSiteContainers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
skipCount = 2;
maxItems = expectedSiteContainers.size();
paging = getPaging(skipCount, maxItems, expectedSiteContainers.size(), expectedSiteContainers.size());
resp = sitesProxy.getSiteContainers(site1.getSiteId(), createParams(paging, null));
checkList(expectedSiteContainers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
skipCount = 2;
maxItems = expectedSiteContainers.size() + 2;
paging = getPaging(skipCount, maxItems, expectedSiteContainers.size(), expectedSiteContainers.size());
resp = sitesProxy.getSiteContainers(site1.getSiteId(), createParams(paging, null));
checkList(expectedSiteContainers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
}
{
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
SiteContainer expectedSiteContainer = new SiteContainer(site1.getSiteId(), "test2", null);
SiteContainer sc = sitesProxy.getSingleSiteContainer(site1.getSiteId(), "test2");
check(expectedSiteContainer, sc);
}
// site does not exist
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.getSingleSiteContainer("gfyuosfgsf8y7s", "documentLibrary");
fail("");
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
// container does not exist
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
sitesProxy.getSingleSiteContainer(site1.getSiteId(), "container1");
fail("");
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
// site containers - site does not exist
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
int skipCount = 0;
int maxItems = 2;
Paging paging = getPaging(skipCount, maxItems, expectedSiteContainers.size(), expectedSiteContainers.size());
sitesProxy.getSiteContainers(GUID.generate(), createParams(paging, null));
fail("");
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
}
// a user in the same network, not invited to the site
{
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
int skipCount = 0;
int maxItems = 2;
Paging paging = getPaging(skipCount, maxItems, expectedSiteContainers.size(), expectedSiteContainers.size());
ListResponse<SiteContainer> ret = sitesProxy.getSiteContainers(site1.getSiteId(), createParams(paging, null));
checkList(expectedSiteContainers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), ret);
}
// a user in a different network
try {
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId()));
int skipCount = 0;
int maxItems = 2;
Paging paging = getPaging(skipCount, maxItems, expectedSiteContainers.size(), expectedSiteContainers.size());
sitesProxy.getSiteContainers(site1.getSiteId(), createParams(paging, null));
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
}
// TODO a user in the same network, invited to the site
{
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
int skipCount = 0;
int maxItems = 2;
Paging paging = getPaging(skipCount, maxItems, expectedSiteContainers.size(), expectedSiteContainers.size());
ListResponse<SiteContainer> ret = sitesProxy.getSiteContainers(site1.getSiteId(), createParams(paging, null));
checkList(expectedSiteContainers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), ret);
}
// person invited to site
{
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person13.getId()));
int skipCount = 0;
int maxItems = 2;
Paging paging = getPaging(skipCount, maxItems, expectedSiteContainers.size(), expectedSiteContainers.size());
ListResponse<SiteContainer> resp = sitesProxy.getSiteContainers(site1.getSiteId(), createParams(paging, null));
checkList(expectedSiteContainers.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp);
}
// invalid methods
try {
sitesProxy.create("sites", site1.getSiteId(), "containers", null, null, "Unable to POST to site containers");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
sitesProxy.create("sites", site1.getSiteId(), "containers", "documentLibrary", null, "Unable to POST to a site container");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
sitesProxy.update("sites", site1.getSiteId(), "containers", null, null, "Unable to PUT site containers");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
sitesProxy.update("sites", site1.getSiteId(), "containers", "documentLibrary", null, "Unable to PUT a site container");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
sitesProxy.remove("sites", site1.getSiteId(), "containers", null, "Unable to DELETE site containers");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
try {
sitesProxy.remove("sites", site1.getSiteId(), "containers", "documentLibrary", "Unable to DELETE a site container");
fail();
} catch (PublicApiException e) {
assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
}
// 1481
// user in external network, list site containers
}
use of org.alfresco.rest.api.tests.client.PublicApiClient.Sites 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(), 6);
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(), 6);
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(), 6);
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(), 2);
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(), 2);
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(), 3);
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);
}
}
}
}
use of org.alfresco.rest.api.tests.client.PublicApiClient.Sites in project alfresco-remote-api by Alfresco.
the class TestSites method testUpdateSite.
@Test
public void testUpdateSite() throws PublicApiException {
Sites sitesProxy = publicApiClient.sites();
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
// +ve test: simple happy path with successful update.
{
SiteImpl siteToCreate = new SiteImpl("Initial Title", SiteVisibility.PRIVATE.toString());
siteToCreate.setRole(SiteRole.SiteManager);
siteToCreate.setDescription("This is the initial description for the site");
Site site = sitesProxy.createSite(siteToCreate, 201);
// Testing with the actual JSON reveals more than testing with rich POJOs.
// It shows us exactly what the input is and is easy to compare to the Open API spec.
String updateJSON = "{\n" + " \"title\": \"Updated Title\",\n" + " \"description\": \"This is an updated description for the site\",\n" + " \"visibility\": \"PUBLIC\"\n" + "}";
// Update the site details
HttpResponse response = sitesProxy.update("sites", site.getSiteId(), null, null, updateJSON, "Failed to update site " + site.getSiteId());
Site updatedSite = SiteImpl.parseSite((JSONObject) response.getJsonResponse().get("entry"));
Site expectedUpdate = new SiteImpl(null, "initial-title", site.getGuid(), "Updated Title", "This is an updated description for the site", SiteVisibility.PUBLIC.toString(), null, SiteRole.SiteManager);
// Check the updated site is as expected.
expectedUpdate.expected(updatedSite);
// Double check by do a fresh get of the details, since the details returned by updateSite()
// aren't necessarily retrieved from the DB.
Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
expectedUpdate.expected(fresh);
removeSiteQuietly(site.getSiteId());
}
// +ve test: remove the description.
// This is a workaround for being unable to explicitly null the field (see REPO-1409)
{
Site site = sitesProxy.createSite(new SiteImpl(null, "NoDescriptionTest", "Initial description", SiteVisibility.PUBLIC.toString()), 201);
Site updatedSite = sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("title", "", SiteVisibility.PUBLIC), 200);
// Note the null value for description, as the framework
// treats empty strings and null as equivalent in responses, and does not return
// those fields (as per API guidelines)
Site expectedUpdate = new SiteImpl(null, "nodescriptiontest", site.getGuid(), "title", null, SiteVisibility.PUBLIC.toString(), null, SiteRole.SiteManager);
// Check the return from updateSite(...)
expectedUpdate.expected(updatedSite);
// TODO improve expected (and/or AssertUtil) and affected test cases
Assert.assertNull(updatedSite.getDescription());
// Double check a fresh retrieval matches.
Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
expectedUpdate.expected(fresh);
// TODO improve expected (and/or AssertUtil) and affected test cases
Assert.assertNull(fresh.getDescription());
removeSiteQuietly(site.getSiteId());
}
// +ve test: partial updates must be allowed
{
Site site = sitesProxy.createSite(new SiteImpl("partial-updates-test", "title-v1", "description-v1", SiteVisibility.PUBLIC.toString()), 201);
// Check we can update just the title
Site updatedSite = sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("title-v2", null, null), 200);
Site expectedUpdate = new SiteImpl(null, "partial-updates-test", site.getGuid(), "title-v2", "description-v1", SiteVisibility.PUBLIC.toString(), null, SiteRole.SiteManager);
expectedUpdate.expected(updatedSite);
// Check we can update just the description
updatedSite = sitesProxy.updateSite(site.getSiteId(), new SiteUpdate(null, "description-v2", null), 200);
expectedUpdate = new SiteImpl(null, "partial-updates-test", site.getGuid(), "title-v2", "description-v2", SiteVisibility.PUBLIC.toString(), null, SiteRole.SiteManager);
expectedUpdate.expected(updatedSite);
// Check we can update just the visibility
updatedSite = sitesProxy.updateSite(site.getSiteId(), new SiteUpdate(null, null, SiteVisibility.PRIVATE), 200);
expectedUpdate = new SiteImpl(null, "partial-updates-test", site.getGuid(), "title-v2", "description-v2", SiteVisibility.PRIVATE.toString(), null, SiteRole.SiteManager);
expectedUpdate.expected(updatedSite);
removeSiteQuietly(site.getSiteId());
}
// -ve test: 400, bad requests
{
Site site = sitesProxy.createSite(new SiteImpl("Site Update Bad Request Tests", SiteVisibility.PRIVATE.toString()), 201);
// long title
sitesProxy.updateSite(site.getSiteId(), new SiteUpdate(new String(new char[257]).replace('\0', 'a'), "description", SiteVisibility.PUBLIC), 400);
// long description
sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("title", new String(new char[513]).replace('\0', 'a'), SiteVisibility.PUBLIC), 400);
// Invalid visibility
sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + " \"title\": \"Updated Title\",\n" + " \"description\": \"This is an updated description for the site\",\n" + " \"visibility\": \"INVALID_VISIBILITY\"\n" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
// Invalid fields
// Check that id, guid and role are not silently ignored. This is until REPO-110
// is implemented, since we currently have to bind to Site rather than SiteUpdate in
// SiteEntityResource.update
sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + " \"id\": \"a-new-id\"," + " \"title\": \"Updated Title\"\n" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + " \"guid\": \"76ba60c1-f05b-406a-86a4-4eeb1bb49aaa\"" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + " \"role\": \"SiteConsumer\"" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + " \"preset\": \"sitePreset\"" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
// Details should not have changed.
Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
site.expected(fresh);
removeSiteQuietly(site.getSiteId());
}
// -ve test: 404 for non-existent site
{
// Attempt to update site details using an invalid site ID.
sitesProxy.updateSite(// Non-existent site.
UUID.randomUUID().toString(), new SiteUpdate("Updated title", "Updated description", SiteVisibility.PUBLIC), HttpStatus.SC_NOT_FOUND);
}
// -ve test: 401 authorization failure
{
Site site = sitesProxy.createSite(new SiteImpl("SiteUpdate401", SiteVisibility.PRIVATE.toString()), 201);
// Invalid auth details
publicApiClient.setRequestContext(new RequestContext(network1.getId(), GUID.generate(), "password"));
// Update the site details
sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("Updated Title", "Updated description", SiteVisibility.PUBLIC), HttpStatus.SC_UNAUTHORIZED);
// Valid authentication again
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
// Check the details haven't changed.
Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
site.expected(fresh);
removeSiteQuietly(site.getSiteId());
}
// -ve test: 403 for incorrect permissions
{
Site site = sitesProxy.createSite(new SiteImpl("SiteUpdate403", SiteVisibility.PUBLIC.toString()), 201);
// Update as person2
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2Id));
// Update the site details
sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("Updated Title", "Updated description", SiteVisibility.PUBLIC), HttpStatus.SC_FORBIDDEN);
// Check details haven't changed as original creator.
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
site.expected(fresh);
removeSiteQuietly(site.getSiteId());
}
}
use of org.alfresco.rest.api.tests.client.PublicApiClient.Sites in project alfresco-remote-api by Alfresco.
the class TestSites method initializeNetwork2WithSites.
private void initializeNetwork2WithSites() throws Exception {
if (network2 == null) {
network2 = getRepoService().createNetwork(this.getClass().getName().toLowerCase() + "-2-" + RUNID, true);
network2.create();
TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
person3Id = network2.createUser().getId();
return null;
}
}, network2.getId());
publicApiClient.setRequestContext(new RequestContext(network2.getId(), person3Id));
Sites sitesProxy = publicApiClient.sites();
Site site = new SiteImpl(site4_id, site4_title, site4_description, SiteVisibility.PRIVATE.toString());
site4 = sitesProxy.createSite(site);
site = new SiteImpl(site5_id, site5_title, site5_description, SiteVisibility.PRIVATE.toString());
site5 = sitesProxy.createSite(site);
site = new SiteImpl(site6_id, site6_title, site6_description, SiteVisibility.PRIVATE.toString());
site6 = sitesProxy.createSite(site);
}
}
use of org.alfresco.rest.api.tests.client.PublicApiClient.Sites in project alfresco-remote-api by Alfresco.
the class TestSites method listSites.
private ListResponse<Site> listSites(final Paging paging, String sortColumn, boolean asc) throws Exception {
final Sites sitesProxy = publicApiClient.sites();
// sort params
final Map<String, String> params = new HashMap<String, String>();
if (sortColumn != null) {
params.put("orderBy", sortColumn + " " + (asc ? "ASC" : "DESC"));
}
return sitesProxy.getSites(createParams(paging, params));
}
Aggregations