From 355ae575efe105df8cbab03e55b293babdee88f2 Mon Sep 17 00:00:00 2001 From: Jamal Kaabi-Mofrad Date: Thu, 2 Jun 2016 21:39:14 +0000 Subject: [PATCH] Merged API-STRIKES-BACK (5.2.0) to HEAD (5.2) 126000 jkaabimofrad: RA-933, RA-934, RA-972: Changed the "tickets" API namespace from "alfresco" to "authentication". Also, added support to the REST API test fwk to handle different API namespaces. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127567 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../api/authentications/package-info.java | 2 +- .../rest/api/tests/AbstractBaseApiTest.java | 39 ++++++++++++-- .../rest/api/tests/AuthenticationsTest.java | 53 ++++++++++--------- .../api/tests/client/PublicApiHttpClient.java | 41 ++++++++++++-- 4 files changed, 100 insertions(+), 35 deletions(-) diff --git a/source/java/org/alfresco/rest/api/authentications/package-info.java b/source/java/org/alfresco/rest/api/authentications/package-info.java index 9852da8a6c..359938d17a 100644 --- a/source/java/org/alfresco/rest/api/authentications/package-info.java +++ b/source/java/org/alfresco/rest/api/authentications/package-info.java @@ -1,4 +1,4 @@ -@WebApi(name = "alfresco", scope = Api.SCOPE.PUBLIC, version = 1) +@WebApi(name = "authentication", scope = Api.SCOPE.PUBLIC, version = 1) package org.alfresco.rest.api.authentications; import org.alfresco.rest.framework.Api; diff --git a/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java b/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java index 9eedaeb34a..fb56cbb591 100644 --- a/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java @@ -24,15 +24,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; - -import org.alfresco.model.ContentModel; import org.alfresco.repo.content.MimetypeMap; -import org.alfresco.repo.site.SiteInfoImpl; import org.alfresco.repo.tenant.TenantUtil; import org.alfresco.rest.api.Nodes; import org.alfresco.rest.api.model.Site; import org.alfresco.rest.api.nodes.NodesEntityResource; -import org.alfresco.rest.api.sites.SiteEntityResource; import org.alfresco.rest.api.tests.RepoService.SiteInformation; import org.alfresco.rest.api.tests.RepoService.TestNetwork; import org.alfresco.rest.api.tests.RepoService.TestPerson; @@ -48,7 +44,6 @@ import org.alfresco.rest.api.tests.client.data.Node; import org.alfresco.rest.api.tests.client.data.Rendition; import org.alfresco.rest.api.tests.util.MultiPartBuilder; import org.alfresco.rest.api.tests.util.RestApiUtil; -import org.alfresco.service.cmr.site.SiteInfo; import org.alfresco.service.cmr.site.SiteVisibility; import org.alfresco.util.TempFileProvider; import org.springframework.util.ResourceUtils; @@ -98,6 +93,22 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi return response; } + protected HttpResponse post(String url, String runAsUser, String body, Map params, Map headers, String apiName, int expectedStatus) throws Exception + { + RequestBuilder requestBuilder = httpClient.new PostRequestBuilder() + .setBodyAsString(body) + .setRequestContext(new RequestContext(runAsUser)) + .setScope(getScope()) + .setApiName(apiName) + .setEntityCollectionName(url) + .setHeaders(headers) + .setParams(params); + HttpResponse response = publicApiClient.execute(requestBuilder); + checkStatus(expectedStatus, response.getStatusCode()); + + return response; + } + protected HttpResponse post(String url, String runAsUser, String body, String queryString, int expectedStatus) throws Exception { publicApiClient.setRequestContext(new RequestContext(runAsUser)); @@ -173,11 +184,17 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi } protected HttpResponse getAll(String url, String runAsUser, PublicApiClient.Paging paging, Map otherParams, Map headers, int expectedStatus) throws Exception + { + return getAll(url, runAsUser, paging, otherParams, headers, null, expectedStatus); + } + + protected HttpResponse getAll(String url, String runAsUser, PublicApiClient.Paging paging, Map otherParams, Map headers, String apiName, int expectedStatus) throws Exception { Map params = createParams(paging, otherParams); RequestBuilder requestBuilder = httpClient.new GetRequestBuilder() .setRequestContext(new RequestContext(runAsUser)) .setScope(getScope()) + .setApiName(apiName) .setEntityCollectionName(url) .setParams(params) .setHeaders(headers); @@ -214,10 +231,16 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi } protected HttpResponse getSingle(String url, String runAsUser, String entityId, Map params, Map headers, int expectedStatus) throws Exception + { + return getSingle(url, runAsUser, entityId, params, headers, null, expectedStatus); + } + + protected HttpResponse getSingle(String url, String runAsUser, String entityId, Map params, Map headers, String apiName, int expectedStatus) throws Exception { RequestBuilder requestBuilder = httpClient.new GetRequestBuilder() .setRequestContext(new RequestContext(runAsUser)) .setScope(getScope()) + .setApiName(apiName) .setEntityCollectionName(url) .setEntityId(entityId) .setParams(params) @@ -298,10 +321,16 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi } protected HttpResponse delete(String url, String runAsUser, String entityId, Map params, Map headers, int expectedStatus) throws Exception + { + return delete(url, runAsUser, entityId, params, headers, null, expectedStatus); + } + + protected HttpResponse delete(String url, String runAsUser, String entityId, Map params, Map headers, String apiName, int expectedStatus) throws Exception { RequestBuilder requestBuilder = httpClient.new DeleteRequestBuilder() .setRequestContext(new RequestContext(runAsUser)) .setScope(getScope()) + .setApiName(apiName) .setEntityCollectionName(url) .setEntityId(entityId) .setParams(params) diff --git a/source/test-java/org/alfresco/rest/api/tests/AuthenticationsTest.java b/source/test-java/org/alfresco/rest/api/tests/AuthenticationsTest.java index c8c5ec4549..384d5b3860 100644 --- a/source/test-java/org/alfresco/rest/api/tests/AuthenticationsTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/AuthenticationsTest.java @@ -46,15 +46,20 @@ import java.util.List; import java.util.Map; /** + * Authentication tickets API tests. + * * @author Jamal Kaabi-Mofrad */ public class AuthenticationsTest extends AbstractBaseApiTest { + private static final String TICKETS_URL = "tickets"; + private static final String TICKETS_API_NAME = "authentication"; + private String user1; private String user2; private List users = new ArrayList<>(); - protected MutableAuthenticationService authenticationService; - protected PersonService personService; + private MutableAuthenticationService authenticationService; + private PersonService personService; @Before public void setup() throws Exception @@ -98,13 +103,13 @@ public class AuthenticationsTest extends AbstractBaseApiTest * Tests login (create ticket), logout (delete ticket), and validate (get ticket). * *

POST:

- * {@literal :/alfresco/api//public/alfresco/versions/1/tickets} + * {@literal :/alfresco/api//public/authentication/versions/1/tickets} * *

GET:

- * {@literal :/alfresco/api//public/alfresco/versions/1/tickets/-me-} + * {@literal :/alfresco/api//public/authentication/versions/1/tickets/-me-} * *

DELETE:

- * {@literal :/alfresco/api//public/alfresco/versions/1/tickets/-me-} + * {@literal :/alfresco/api//public/authentication/versions/1/tickets/-me-} */ @Test public void testCreateValidateDeleteTicket() throws Exception @@ -120,22 +125,22 @@ public class AuthenticationsTest extends AbstractBaseApiTest // User1 login request LoginTicket loginRequest = new LoginTicket(); // Invalid login details - post("tickets", null, RestApiUtil.toJsonAsString(loginRequest), 400); + post(TICKETS_URL, null, RestApiUtil.toJsonAsString(loginRequest), null, null, TICKETS_API_NAME, 400); loginRequest.setUserId(null); loginRequest.setPassword("user1Password"); // Invalid login details - post("tickets", null, RestApiUtil.toJsonAsString(loginRequest), 400); + post(TICKETS_URL, null, RestApiUtil.toJsonAsString(loginRequest), null, null, TICKETS_API_NAME, 400); loginRequest.setUserId(user1); loginRequest.setPassword(null); // Invalid login details - post("tickets", null, RestApiUtil.toJsonAsString(loginRequest), 400); + post(TICKETS_URL, null, RestApiUtil.toJsonAsString(loginRequest), null, null, TICKETS_API_NAME, 400); loginRequest.setUserId(user1); loginRequest.setPassword("user1Password"); // Authenticate and create a ticket - HttpResponse response = post("tickets", null, RestApiUtil.toJsonAsString(loginRequest), 201); + HttpResponse response = post(TICKETS_URL, null, RestApiUtil.toJsonAsString(loginRequest), null, null, TICKETS_API_NAME, 201); LoginTicketResponse loginResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), LoginTicketResponse.class); assertNotNull(loginResponse.getId()); assertNotNull(loginResponse.getUserId()); @@ -149,26 +154,26 @@ public class AuthenticationsTest extends AbstractBaseApiTest getAll(SiteEntityResource.class, null, paging, Collections.singletonMap("alf_ticket", "TICKET_" + System.currentTimeMillis()), 401); // Validate ticket - Invalid parameter. Only '-me-' is supported - getSingle("tickets", null, loginResponse.getId(), ticket, 400); + getSingle(TICKETS_URL, null, loginResponse.getId(), ticket, null, TICKETS_API_NAME, 400); // Validate ticket - response = getSingle("tickets", null, People.DEFAULT_USER, ticket, 200); + response = getSingle(TICKETS_URL, null, People.DEFAULT_USER, ticket, null, TICKETS_API_NAME, 200); LoginTicketResponse validatedTicket = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), LoginTicketResponse.class); assertEquals(loginResponse.getId(), validatedTicket.getId()); // Validate ticket - Invalid parameter. Only '-me-' is supported - getSingle("tickets", null, loginResponse.getId(), ticket, 400); + getSingle(TICKETS_URL, null, loginResponse.getId(), ticket, null, TICKETS_API_NAME, 400); // Delete the ticket - Logout - delete("tickets", null, People.DEFAULT_USER, ticket, 204); + delete(TICKETS_URL, null, People.DEFAULT_USER, ticket, null, TICKETS_API_NAME, 204); // Validate ticket - 401 as ticket has been invalidated so the API call is unauthorized - getSingle("tickets", null, People.DEFAULT_USER, ticket, 401); + getSingle(TICKETS_URL, null, People.DEFAULT_USER, ticket, null, TICKETS_API_NAME, 401); // Check the ticket has been invalidated - the difference with the above is that the API call is authorized - getSingle("tickets", user1, People.DEFAULT_USER, ticket, 404); + getSingle(TICKETS_URL, user1, People.DEFAULT_USER, ticket, null, TICKETS_API_NAME, 404); // Ticket has already been invalidated - delete("tickets", user1, People.DEFAULT_USER, ticket, 404); + delete(TICKETS_URL, user1, People.DEFAULT_USER, ticket, null, TICKETS_API_NAME, 404); // Get list of site by appending the invalidated ticket getAll(SiteEntityResource.class, null, paging, ticket, 401); @@ -189,18 +194,18 @@ public class AuthenticationsTest extends AbstractBaseApiTest loginRequest.setUserId(user2); loginRequest.setPassword("wrongPassword"); // Authentication failed - wrong password - post("tickets", null, RestApiUtil.toJsonAsString(loginRequest), 403); + post(TICKETS_URL, null, RestApiUtil.toJsonAsString(loginRequest), null, null, TICKETS_API_NAME, 403); loginRequest.setUserId(user1); loginRequest.setPassword("user2Password"); // Authentication failed - userId/password mismatch - post("tickets", null, RestApiUtil.toJsonAsString(loginRequest), 403); + post(TICKETS_URL, null, RestApiUtil.toJsonAsString(loginRequest), null, null, TICKETS_API_NAME, 403); // Set the correct details loginRequest.setUserId(user2); loginRequest.setPassword("user2Password"); // Authenticate and create a ticket - response = post("tickets", null, RestApiUtil.toJsonAsString(loginRequest), 201); + response = post(TICKETS_URL, null, RestApiUtil.toJsonAsString(loginRequest), null, null, TICKETS_API_NAME, 201); loginResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), LoginTicketResponse.class); assertNotNull(loginResponse.getId()); assertNotNull(loginResponse.getUserId()); @@ -214,10 +219,10 @@ public class AuthenticationsTest extends AbstractBaseApiTest assertEquals(1, nodes.size()); // Validate ticket - Invalid parameter. Only '-me-' is supported - getSingle("tickets", null, loginResponse.getId(), null, header, 400); + getSingle(TICKETS_URL, null, loginResponse.getId(), null, header, TICKETS_API_NAME, 400); // Validate ticket - user2 - response = getSingle("tickets", null, People.DEFAULT_USER, null, header, 200); + response = getSingle(TICKETS_URL, null, People.DEFAULT_USER, null, header, TICKETS_API_NAME, 200); validatedTicket = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), LoginTicketResponse.class); assertEquals(loginResponse.getId(), validatedTicket.getId()); @@ -238,14 +243,14 @@ public class AuthenticationsTest extends AbstractBaseApiTest assertEquals(1, nodes.size()); // Try to validate the ticket without supplying the Authorization header or the alf_ticket param - getSingle("tickets", user2, People.DEFAULT_USER, null, null, 400); + getSingle(TICKETS_URL, user2, People.DEFAULT_USER, null, null, TICKETS_API_NAME, 400); // Delete the ticket - Invalid parameter. Only '-me-' is supported header = Collections.singletonMap("Authorization", "Basic " + encodedUserIdAndTicket); - delete("tickets", null, loginResponse.getId(), null, header, 400); + delete(TICKETS_URL, null, loginResponse.getId(), null, header, TICKETS_API_NAME, 400); // Delete the ticket - Logout - delete("tickets", null, People.DEFAULT_USER, null, header, 204); + delete(TICKETS_URL, null, People.DEFAULT_USER, null, header, TICKETS_API_NAME, 204); // Get children of user2 home folder - invalidated ticket getAll(getNodeChildrenUrl(Nodes.PATH_MY), null, paging, null, header, 401); diff --git a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java index a7589f0935..24d0b8f26a 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java @@ -58,6 +58,7 @@ import org.apache.commons.httpclient.methods.PutMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.methods.TraceMethod; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; @@ -661,10 +662,16 @@ public class PublicApiHttpClient Api api = ResourceInspector.inspectApi(resourceClass); SCOPE scope = api.getScope(); int version = api.getVersion(); + String name = api.getName(); + if (StringUtils.isEmpty(name)) + { + name = apiName; + } + Pair relationshipCollectionInfo = getRelationCollectionInfo(resourceClass); sb.append(MessageFormat.format(BASE_URL, new Object[] { scheme, host, String.valueOf(port), contextPath, servletName, - tenantDomain == null ? TenantUtil.DEFAULT_TENANT : tenantDomain, scope.toString(), apiName, version })); + tenantDomain == null ? TenantUtil.DEFAULT_TENANT : tenantDomain, scope.toString(), name, version })); if (relationshipCollectionInfo != null) { @@ -737,6 +744,12 @@ public class PublicApiHttpClient RestApiEndpoint(String tenantDomain, String scope, int version, String collectionName, Object collectionEntityId, String relationName, Object relationEntityId, Map params) throws IOException + { + this(tenantDomain, scope, apiName, version, collectionName, collectionEntityId, relationName, relationEntityId, params); + } + + RestApiEndpoint(String tenantDomain, String scope, String apiName, int version, String collectionName, Object collectionEntityId, + String relationName, Object relationEntityId, Map params) throws IOException { StringBuilder sb = new StringBuilder(); @@ -744,6 +757,12 @@ public class PublicApiHttpClient { tenantDomain = TenantUtil.DEFAULT_TENANT; } + + if (StringUtils.isEmpty(apiName)) + { + apiName = PublicApiHttpClient.this.apiName; + } + sb.append(MessageFormat.format(BASE_URL, new Object[] { scheme, host, String.valueOf(port), contextPath, servletName, tenantDomain, scope, apiName, version })); @@ -985,6 +1004,7 @@ public class PublicApiHttpClient { private RequestContext requestContext; private String scope; + private String apiName = "alfresco"; // default api namespace private int version = 1; private String entityCollectionName; private Object entityId; @@ -1028,6 +1048,17 @@ public class PublicApiHttpClient return this; } + public String getApiName() + { + return apiName; + } + + public RequestBuilder setApiName(String apiName) + { + this.apiName = apiName; + return this; + } + public int getVersion() { return version; @@ -1125,7 +1156,7 @@ public class PublicApiHttpClient public GetMethod getHttpMethod() throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(getRequestContext().getNetworkId(), - getScope(), getVersion(), getEntityCollectionName(), + getScope(), getApiName(), getVersion(), getEntityCollectionName(), getEntityId(), getRelationCollectionName(), getRelationshipEntityId(), getParams()); String url = endpoint.getUrl(); @@ -1141,7 +1172,7 @@ public class PublicApiHttpClient public DeleteMethod getHttpMethod() throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(getRequestContext().getNetworkId(), - getScope(), getVersion(), getEntityCollectionName(), + getScope(), getApiName(), getVersion(), getEntityCollectionName(), getEntityId(), getRelationCollectionName(), getRelationshipEntityId(), getParams()); String url = endpoint.getUrl(); @@ -1198,7 +1229,7 @@ public class PublicApiHttpClient public PostMethod getHttpMethod() throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(getRequestContext().getNetworkId(), - getScope(), getVersion(), getEntityCollectionName(), + getScope(), getApiName(), getVersion(), getEntityCollectionName(), getEntityId(), getRelationCollectionName(), getRelationshipEntityId(), getParams()); String url = endpoint.getUrl(); @@ -1247,7 +1278,7 @@ public class PublicApiHttpClient public PutMethod getHttpMethod() throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(getRequestContext().getNetworkId(), - getScope(), getVersion(), getEntityCollectionName(), + getScope(), getApiName(), getVersion(), getEntityCollectionName(), getEntityId(), getRelationCollectionName(), getRelationshipEntityId(), getParams()); String url = endpoint.getUrl();