Author: [email protected]
Date: Wed May 18 09:09:58 2011
New Revision: 1141
Log:
[AMDATUAUTH-23] Added more tests
Added:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/UserAdminRESTTest.java
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/AuthTestBase.java
Modified:
trunk/amdatu-auth/test-integration/base/NOTICE
Modified: trunk/amdatu-auth/test-integration/base/NOTICE
==============================================================================
--- trunk/amdatu-auth/test-integration/base/NOTICE (original)
+++ trunk/amdatu-auth/test-integration/base/NOTICE Wed May 18 09:09:58 2011
@@ -1,4 +1,4 @@
-Amdatu Web Itest base
+Amdatu Auth Itest base
Copyright 2010, 2011 The Amdatu Foundation
I. Included Software
Added:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/UserAdminRESTTest.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/UserAdminRESTTest.java
Wed May 18 09:09:58 2011
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.amdatu.auth.test.integration.tests;
+
+import static org.amdatu.auth.test.integration.base.AuthFixture.HOSTNAME;
+import static org.amdatu.auth.test.integration.base.AuthFixture.IP;
+import static org.amdatu.auth.test.integration.tests.AuthTest.TEST_USERNAME;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.amdatu.auth.test.integration.tests.util.AuthTestBase;
+import org.amdatu.auth.test.integration.tests.util.AuthUtils;
+import org.amdatu.core.tenant.TenantEntity;
+import org.apache.commons.httpclient.HttpStatus;
+import org.osgi.service.log.LogService;
+
+public class UserAdminRESTTest extends AuthTestBase {
+
+ public void execute() throws Exception {
+ // Test the REST interface of the useradmin bundle. First wait before
it comes up
+ AuthUtils.waitForURL(getRESTBaseUrl() + "/users/status",
HttpStatus.SC_OK);
+
+ // -1- First try to create a user and verify that a 401 error
(Unauthorized) is returned
+ String url = "/users/" + TEST_USERNAME;
+ invokeRestApi(url, javax.ws.rs.HttpMethod.PUT,
HttpStatus.SC_UNAUTHORIZED);
+
+ // -2- Now login as Administrator
+ loginAsAdministrator();
+
+ // -3- And try creating the user again
+ invokeRestApi(url, javax.ws.rs.HttpMethod.PUT, HttpStatus.SC_OK);
+
+ // -4- Retrieve the user
+ invokeRestApi(url, javax.ws.rs.HttpMethod.GET, HttpStatus.SC_OK);
+
+ // -5- Delete the user and verify that it has been removed
+ invokeRestApi(url, javax.ws.rs.HttpMethod.DELETE, HttpStatus.SC_OK);
+ invokeRestApi(url, javax.ws.rs.HttpMethod.GET,
HttpStatus.SC_NOT_FOUND);
+
+ // -6- Now create a new tenant associated with 127.0.0.1
+ m_logService.log(LogService.LOG_DEBUG, "Adding test tenant");
+ Map<String, String> properties = new HashMap<String, String>();
+ properties.put("hostname", IP);
+ TenantEntity tenant = m_tenantService.createTenant("testtenant",
"Second Test Tenant", properties);
+ m_logService.log(LogService.LOG_DEBUG, "Test tenant added, switching
from host '" + HOSTNAME + "' to '" + IP + "'");
+
+ // Give some time for tenant adapter services to register
+ Thread.sleep(3000);
+
+ switchHost(IP); // Switch to host by IP address
+ AuthUtils.waitForURL(getRESTBaseUrl() + "/users/status",
HttpStatus.SC_OK);
+ m_logService.log(LogService.LOG_DEBUG, "UserAdmin REST service on '" +
getRESTBaseUrl() + "' now available");
+
+ // -7- Now verify that for this new tenant a new user 'Administrator'
should have been created, verify that.
+ // Note that since the token provider is also tenant specific, using
the same token for this
+ // request should return an unauthorized response
+ url = "/users/Administrator";
+ invokeRestApi(url, javax.ws.rs.HttpMethod.GET,
HttpStatus.SC_UNAUTHORIZED);
+
+ // -8- So login as Administrator and try again
+ loginAsAdministrator();
+ invokeRestApi(url, javax.ws.rs.HttpMethod.GET, HttpStatus.SC_OK);
+
+ // -9- Create a new user on '127.0.0.1' and verify that this new user
comes available on this host
+ // and not on 'localhost'
+ url = "/users/" + TEST_USERNAME;
+ invokeRestApi(url, javax.ws.rs.HttpMethod.PUT, HttpStatus.SC_OK);
+
+ invokeRestApi(url, javax.ws.rs.HttpMethod.GET, HttpStatus.SC_OK);
+ switchHost(HOSTNAME);
+ invokeRestApi(url, javax.ws.rs.HttpMethod.GET,
HttpStatus.SC_NOT_FOUND);
+
+ // -10- Remove the test user
+ switchHost(IP);
+ invokeRestApi(url, javax.ws.rs.HttpMethod.DELETE, HttpStatus.SC_OK);
+
+ // -11- Remove the tenant and verify that a GET returns a 404
+ m_tenantService.deleteTenant(tenant);
+ invokeRestApi(url, javax.ws.rs.HttpMethod.DELETE,
HttpStatus.SC_NOT_FOUND);
+ }
+}
Added:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/AuthTestBase.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/AuthTestBase.java
Wed May 18 09:09:58 2011
@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.amdatu.auth.test.integration.tests.util;
+
+import static junit.framework.Assert.assertTrue;
+import static org.amdatu.auth.test.integration.base.AuthFixture.PORTNR;
+import static org.amdatu.auth.test.integration.tests.AuthTest.ADMIN_PASSWORD;
+import static org.amdatu.auth.test.integration.tests.AuthTest.ADMIN_USERNAME;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.amdatu.auth.test.integration.base.AuthFixture;
+import org.amdatu.auth.test.integration.tests.AuthTest;
+import org.amdatu.authentication.oauth.api.OAuthServiceConsumerRegistry;
+import org.amdatu.authentication.oauth.api.OAuthServiceProvider;
+import org.amdatu.core.tenant.TenantManagementService;
+import org.apache.commons.httpclient.HeaderElement;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.DeleteMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.osgi.service.log.LogService;
+import org.osgi.service.useradmin.UserAdmin;
+
+public abstract class AuthTestBase {
+ protected volatile LogService m_logService;
+ protected volatile OAuthServiceProvider m_oAuthServiceProvider;
+ protected volatile OAuthServiceConsumerRegistry m_consumerRegistry;
+ protected volatile UserAdmin m_userAdmin;
+ protected volatile TenantManagementService m_tenantService;
+
+ private Map<String, HeaderElement> m_cookieHeaderElement = new
HashMap<String, HeaderElement>();
+ private String m_restBaseUrl = null;
+ private String m_host = null;
+
+ public abstract void execute() throws Exception;
+
+ public void setLogService(LogService logService) {
+ m_logService = logService;
+ }
+
+ public void setOAuthServiceProvider(OAuthServiceProvider provider) {
+ m_oAuthServiceProvider = provider;
+ }
+
+ public void setOAuthServiceConsumerRegistry(OAuthServiceConsumerRegistry
registry) {
+ m_consumerRegistry = registry;
+ }
+
+ public void setUserAdmin(UserAdmin userAdmin) {
+ m_userAdmin = userAdmin;
+ }
+
+ public void setTenantService(TenantManagementService tenantService) {
+ m_tenantService = tenantService;
+ }
+
+ protected void login() throws HttpException, IOException {
+ m_cookieHeaderElement.put(m_host,
AuthUtils.login(AuthFixture.HOSTNAME, AuthTest.TEST_USERNAME,
AuthTest.TEST_PASSWORD));
+ }
+
+ protected void addCookieHeader(HttpMethod method) {
+ if (m_cookieHeaderElement.get(m_host) != null) {
+ String header = m_cookieHeaderElement.get(m_host).getName() + "="
+ m_cookieHeaderElement.get(m_host).getValue();
+ method.addRequestHeader("Cookie", header);
+ }
+ }
+
+ protected Map<String, String> getCookieHeader() {
+ Map<String, String> requestHeaders = new HashMap<String, String>();
+ if (m_cookieHeaderElement != null) {
+ for (String key : m_cookieHeaderElement.keySet()) {
+ String header = m_cookieHeaderElement.get(key).getName() + "="
+ m_cookieHeaderElement.get(key).getValue();
+ requestHeaders.put("Cookie", header);
+ }
+
+ }
+ return requestHeaders;
+ }
+
+ protected String getRESTBaseUrl() throws Exception {
+ if (m_restBaseUrl == null) {
+ m_restBaseUrl = "http://" + AuthFixture.HOSTNAME + ":" +
AuthFixture.PORTNR + "/rest";
+ }
+ return m_restBaseUrl;
+ }
+
+ protected void loginAsAdministrator() throws HttpException, IOException {
+ if (m_host == null) {
+ m_host = AuthFixture.HOSTNAME;
+ }
+ m_cookieHeaderElement.put(m_host, AuthUtils.login(m_host,
ADMIN_USERNAME, ADMIN_PASSWORD));
+ }
+
+ protected void switchHost(String newHostName) {
+ m_host = newHostName;
+ m_restBaseUrl = "http://" + newHostName + ":" + PORTNR + "/rest";
+ }
+
+ protected String invokeRestApi(String urlPostfix, String httpMethod, int
expectedStatus) throws Exception {
+ String url = getRESTBaseUrl() + urlPostfix;
+ HttpClient httpClient = new HttpClient();
+ m_logService.log(LogService.LOG_DEBUG, "Invoking REST API " +
httpMethod + " '" + url + "'");
+ HttpMethod method = null;
+ if (httpMethod.equals(javax.ws.rs.HttpMethod.GET)) {
+ method = new GetMethod(url);
+ } else if (httpMethod.equals(javax.ws.rs.HttpMethod.PUT)) {
+ method = new PutMethod(url);
+ } else if (httpMethod.equals(javax.ws.rs.HttpMethod.DELETE)) {
+ method = new DeleteMethod(url);
+ } else if (httpMethod.equals(javax.ws.rs.HttpMethod.POST)) {
+ method = new PostMethod(url);
+ }
+ addCookieHeader(method);
+ try {
+ // Execute the method, this should return a 200
+ int statusCode = httpClient.executeMethod(method);
+ assertTrue("HTTP '" + httpMethod + "' to '" + url + "' returned "
+ statusCode, statusCode == expectedStatus);
+
+ // Read the response body and return it.
+ return new String(method.getResponseBody(), "UTF-8");
+ }
+ finally {
+ // Release the connection.
+ method.releaseConnection();
+ }
+ }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits