brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/7] git commit: Fix testSecurityProviderUpdatesWhenPropertiesReloaded
Date Wed, 15 Oct 2014 14:19:56 GMT
Fix testSecurityProviderUpdatesWhenPropertiesReloaded

- was failing for me because my brooklyn.properties has authentication
  set up. Therefore set an explicit brooklynProperties just for
  the test.

Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/31e1448f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/31e1448f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/31e1448f

Branch: refs/heads/master
Commit: 31e1448feeeb2558513536ec2d4b72c9b678db16
Parents: 7a760fa
Author: Aled Sage <aled.sage@gmail.com>
Authored: Tue Oct 14 15:41:01 2014 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Tue Oct 14 15:41:01 2014 +0100

----------------------------------------------------------------------
 .../ServerResourceIntegrationTest.java          | 81 +++++++++++++-------
 1 file changed, 54 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/31e1448f/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceIntegrationTest.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceIntegrationTest.java
b/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceIntegrationTest.java
index 3244cb4..311afaf 100644
--- a/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceIntegrationTest.java
+++ b/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceIntegrationTest.java
@@ -18,6 +18,7 @@
  */
 package brooklyn.rest.resources;
 
+import static brooklyn.util.http.HttpTool.httpClientBuilder;
 import static org.testng.Assert.assertEquals;
 
 import java.net.URI;
@@ -30,50 +31,75 @@ import org.apache.http.client.HttpClient;
 import org.eclipse.jetty.server.Server;
 import org.testng.annotations.Test;
 
-import com.google.common.collect.ImmutableMap;
-
-import brooklyn.entity.BrooklynAppLiveTestSupport;
+import brooklyn.config.BrooklynProperties;
 import brooklyn.management.ManagementContext;
+import brooklyn.management.internal.LocalManagementContext;
+import brooklyn.management.internal.ManagementContextInternal;
 import brooklyn.rest.BrooklynRestApiLauncher;
 import brooklyn.rest.BrooklynRestApiLauncherTestFixture;
-import brooklyn.rest.security.provider.AnyoneSecurityProvider;
 import brooklyn.rest.security.provider.TestSecurityProvider;
 import brooklyn.test.HttpTestUtils;
-import brooklyn.test.entity.LocalManagementContextForTests;
 import brooklyn.util.http.HttpTool;
 import brooklyn.util.http.HttpToolResponse;
 
+import com.google.common.collect.ImmutableMap;
+
 public class ServerResourceIntegrationTest extends BrooklynRestApiLauncherTestFixture {
 
     /**
      * [sam] Other tests rely on brooklyn.properties not containing security properties so
..
      * I think the best way to test this is to set a security provider, then reload properties
      * and check no authentication is required.
+     * 
+     * [aled] Changing this test so doesn't rely on brooklyn.properties having no security
+     * provider (that can lead to failures locally when running just this test). Asserts

      */
     @Test(groups = "Integration")
     public void testSecurityProviderUpdatesWhenPropertiesReloaded() {
-        Server server = useServerForTest(BrooklynRestApiLauncher.launcher()
-                .withoutJsgui()
-                .securityProvider(TestSecurityProvider.class)
-                .start());
-        HttpTool.HttpClientBuilder builder = HttpTool.httpClientBuilder()
-                .uri(getBaseUri(server));
-
-        HttpToolResponse response;
-        final URI uri = URI.create(getBaseUri() + "/v1/server/properties/reload");
-        final Map<String, String> args = Collections.emptyMap();
+        BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newEmpty();
+        brooklynProperties.put("brooklyn.webconsole.security.users", "admin");
+        brooklynProperties.put("brooklyn.webconsole.security.user.admin.password", "mypassword");
+        UsernamePasswordCredentials defaultCredential = new UsernamePasswordCredentials("admin",
"mypassword");
 
-        // Unauthorised
-        response = HttpTool.httpPost(builder.build(), uri, args, args);
-        assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);
+        ManagementContext mgmt = new LocalManagementContext(brooklynProperties);
+        
+        try {
+            Server server = useServerForTest(BrooklynRestApiLauncher.launcher()
+                    .managementContext(mgmt)
+                    .withoutJsgui()
+                    .securityProvider(TestSecurityProvider.class)
+                    .start());
+            String baseUri = getBaseUri(server);
+    
+            HttpToolResponse response;
+            final URI uri = URI.create(getBaseUri() + "/v1/server/properties/reload");
+            final Map<String, String> args = Collections.emptyMap();
+    
+            // Unauthorised when no credentials, and when default credentials.
+            response = HttpTool.httpPost(httpClientBuilder().uri(baseUri).build(), uri, args,
args);
+            assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);
+    
+            response = HttpTool.httpPost(httpClientBuilder().uri(baseUri).credentials(defaultCredential).build(),

+                    uri, args, args);
+            assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);
 
-        response = HttpTool.httpPost(builder.credentials(TestSecurityProvider.CREDENTIAL).build(),
-                uri, args, args);
-        HttpTestUtils.assertHealthyStatusCode(response.getResponseCode());
-
-        // Lack of credentials now accepted.
-        response = HttpTool.httpPost(builder.build(), uri, args, args);
-        HttpTestUtils.assertHealthyStatusCode(response.getResponseCode());
+            // Accepts TestSecurityProvider credentials, and we reload.
+            response = HttpTool.httpPost(httpClientBuilder().uri(baseUri).credentials(TestSecurityProvider.CREDENTIAL).build(),
+                    uri, args, args);
+            HttpTestUtils.assertHealthyStatusCode(response.getResponseCode());
+    
+            // Has no gone back to credentials from brooklynProperties; TestSecurityProvider
credentials no longer work
+            response = HttpTool.httpPost(httpClientBuilder().uri(baseUri).credentials(defaultCredential).build(),

+                    uri, args, args);
+            HttpTestUtils.assertHealthyStatusCode(response.getResponseCode());
+            
+            response = HttpTool.httpPost(httpClientBuilder().uri(baseUri).credentials(TestSecurityProvider.CREDENTIAL).build(),

+                    uri, args, args);
+            assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);
+    
+        } finally {
+            ((ManagementContextInternal)mgmt).terminate();
+        }
     }
 
     @Test(groups = "Integration")
@@ -86,11 +112,12 @@ public class ServerResourceIntegrationTest extends BrooklynRestApiLauncherTestFi
     }
 
     private String getServerUser(Server server) throws Exception {
-        HttpClient client = HttpTool.httpClientBuilder()
+        HttpClient client = httpClientBuilder()
                 .uri(getBaseUri(server))
                 .credentials(TestSecurityProvider.CREDENTIAL)
                 .build();
-        HttpToolResponse response = HttpTool.httpGet(client, URI.create(getBaseUri() + "/v1/server/user"),
+        
+        HttpToolResponse response = HttpTool.httpGet(client, URI.create(getBaseUri(server)
+ "/v1/server/user"),
                 ImmutableMap.<String, String>of());
         HttpTestUtils.assertHealthyStatusCode(response.getResponseCode());
         return response.getContentAsString();


Mime
View raw message