You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by gr...@apache.org on 2015/02/28 02:05:33 UTC

[3/7] incubator-usergrid git commit: Added fix for unconfirmedAdminUsersTest

Added fix for unconfirmedAdminUsersTest


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/88fe9063
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/88fe9063
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/88fe9063

Branch: refs/heads/USERGRID-280
Commit: 88fe906353655252f7788d79ce1d72315626b4c6
Parents: 454cf61
Author: grey <gr...@apigee.com>
Authored: Fri Feb 27 11:43:22 2015 -0800
Committer: grey <gr...@apigee.com>
Committed: Fri Feb 27 11:43:22 2015 -0800

----------------------------------------------------------------------
 .../usergrid/rest/management/AdminUsersIT.java  | 219 ++++++-------------
 .../endpoints/mgmt/ConfirmResource.java         |  46 ++++
 .../endpoints/mgmt/UserResource.java            |   4 +
 3 files changed, 121 insertions(+), 148 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/88fe9063/stack/rest/src/test/java/org/apache/usergrid/rest/management/AdminUsersIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/management/AdminUsersIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/management/AdminUsersIT.java
index ac3cc10..d2c86a9 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/management/AdminUsersIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/management/AdminUsersIT.java
@@ -41,6 +41,7 @@ import org.jvnet.mock_javamail.Mailbox;
 
 
 import org.apache.usergrid.management.MockImapClient;
+import org.apache.usergrid.persistence.index.utils.StringUtils;
 import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT;
 import org.apache.usergrid.rest.test.resource2point0.RestClient;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.mgmt.*;
@@ -205,176 +206,98 @@ public class AdminUsersIT extends AbstractRestIT {
 
     }
 
+
+    /**
+     * Test that a unconfirmed admin cannot log in.
+     * TODO:test for parallel test that changing the properties here won't affect other tests
+     * @throws Exception
+     */
     @Test
-    public void testUnconfirmedAdminLoginRET()  throws Exception{
+    public void testUnconfirmedAdminLogin()  throws Exception{
 
-        Map<String,Object> testPropertiesMap = new HashMap<>(  );
+        ApiResponse originalTestPropertiesResponse = clientSetup.getRestClient().testPropertiesResource().get();
+        Entity originalTestProperties = new Entity( originalTestPropertiesResponse );
+        try {
+            //Set runtime enviroment to the following settings
+            Map<String, Object> testPropertiesMap = new HashMap<>();
 
-        testPropertiesMap.put( PROPERTIES_SYSADMIN_APPROVES_ADMIN_USERS, "false" );
-        testPropertiesMap.put( PROPERTIES_SYSADMIN_APPROVES_ORGANIZATIONS, "false" );
-        testPropertiesMap.put( PROPERTIES_ADMIN_USERS_REQUIRE_CONFIRMATION, "true" );
-        testPropertiesMap.put( PROPERTIES_SYSADMIN_EMAIL, "sysadmin-1@mockserver.com" );
-        testPropertiesMap.put( PROPERTIES_NOTIFY_ADMIN_OF_ACTIVATION, "true" );
+            testPropertiesMap.put( PROPERTIES_SYSADMIN_APPROVES_ADMIN_USERS, "false" );
+            testPropertiesMap.put( PROPERTIES_SYSADMIN_APPROVES_ORGANIZATIONS, "false" );
+            //Requires admins to do email confirmation before they can log in.
+            testPropertiesMap.put( PROPERTIES_ADMIN_USERS_REQUIRE_CONFIRMATION, "true" );
+            testPropertiesMap.put( PROPERTIES_SYSADMIN_EMAIL, "sysadmin-1@mockserver.com" );
 
-        Entity testPropertiesPayload = new Entity( testPropertiesMap );
+            Entity testPropertiesPayload = new Entity( testPropertiesMap );
 
-        clientSetup.getRestClient().testPropertiesResource().post(testPropertiesPayload);
+            //Send rest call to the /testProperties endpoint to persist property changes
+            clientSetup.getRestClient().testPropertiesResource().post( testPropertiesPayload );
 
-        refreshIndex();
+            refreshIndex();
 
-        ApiResponse apiResponse = clientSetup.getRestClient().testPropertiesResource().get();
+            //Retrieve properties and ensure that they are set correctly.
+            ApiResponse apiResponse = clientSetup.getRestClient().testPropertiesResource().get();
 
-        assertEquals( "true" ,apiResponse.getProperties().get( PROPERTIES_NOTIFY_ADMIN_OF_ACTIVATION ) );
-        assertEquals( "sysadmin-1@mockserver.com" ,apiResponse.getProperties().get(PROPERTIES_SYSADMIN_EMAIL));
-        assertEquals( "true" ,apiResponse.getProperties().get( PROPERTIES_ADMIN_USERS_REQUIRE_CONFIRMATION ) );
-        assertEquals( "false" ,apiResponse.getProperties().get( PROPERTIES_SYSADMIN_APPROVES_ORGANIZATIONS ) );
-        assertEquals( "false" ,apiResponse.getProperties().get( PROPERTIES_SYSADMIN_APPROVES_ADMIN_USERS ) );
+            assertEquals( "sysadmin-1@mockserver.com", apiResponse.getProperties().get( PROPERTIES_SYSADMIN_EMAIL ) );
+            assertEquals( "true", apiResponse.getProperties().get( PROPERTIES_ADMIN_USERS_REQUIRE_CONFIRMATION ) );
+            assertEquals( "false", apiResponse.getProperties().get( PROPERTIES_SYSADMIN_APPROVES_ORGANIZATIONS ) );
+            assertEquals( "false", apiResponse.getProperties().get( PROPERTIES_SYSADMIN_APPROVES_ADMIN_USERS ) );
 
-        Organization organization = createOrgPayload( "testUnconfirmedAdminLogin", null );
+            //Create organization for the admin user to be confirmed
+            Organization organization = createOrgPayload( "testUnconfirmedAdminLogin", null );
 
-        Organization organizationResponse = clientSetup.getRestClient().management().orgs().post( organization );
+            Organization organizationResponse = clientSetup.getRestClient().management().orgs().post( organization );
 
-        assertNotNull( organizationResponse );
+            assertNotNull( organizationResponse );
 
-        User adminUser = organizationResponse.getOwner();
+            //Ensure that adminUser has the correct properties set.
+            User adminUser = organizationResponse.getOwner();
 
-        assertNotNull( adminUser );
-        assertFalse( "adminUser should not be activated yet", adminUser.getActivated());
-        assertFalse( "adminUser should not be confirmed yet", adminUser.getConfirmed());
+            assertNotNull( adminUser );
+            assertFalse( "adminUser should not be activated yet", adminUser.getActivated() );
+            assertFalse( "adminUser should not be confirmed yet", adminUser.getConfirmed() );
 
 
-        QueryParameters queryParameters = new QueryParameters();
-        queryParameters.addParam( "grant_type","password").addParam( "username",adminUser.getUsername() )
-                       .addParam( "password",organization.getPassword() );
+            QueryParameters queryParameters = new QueryParameters();
+            queryParameters.addParam( "grant_type", "password" ).addParam( "username", adminUser.getUsername() )
+                           .addParam( "password", organization.getPassword() );
 
-        //Token adminToken = new Token( "password",adminUser.getUsername(),organization.getName() );
 
+            //Check that the adminUser cannot log in and fails with a 403
+            try {
+                management().token().get( queryParameters );
+                fail( "Admin user should not be able to log in." );
+            }
+            catch ( UniformInterfaceException uie ) {
+                assertEquals( "Admin user should have failed with 403", 403, uie.getResponse().getStatus() );
+            }
 
-        try {
+            //Create mocked inbox
+            List<Message> inbox = Mailbox.get( organization.getEmail() );
+            assertFalse( inbox.isEmpty() );
 
-            Token tokenReturned = management().token().get( queryParameters );
-        }
-        catch(Exception e){
-            //catch forbbiedn here
-        }
+            MockImapClient client = new MockImapClient( "mockserver.com", "test-user-46", "somepassword" );
+            client.processMail();
 
-        List<Message> inbox = Mailbox.get( organization.getEmail() );
-        assertFalse( inbox.isEmpty() );
+            //Get email with confirmation token and extract token
+            Message confirmation = inbox.get( 0 );
+            assertEquals( "User Account Confirmation: " + organization.getEmail(), confirmation.getSubject() );
+            String token = getTokenFromMessage( confirmation );
 
-        MockImapClient client = new MockImapClient( "mockserver.com", "test-user-46", "somepassword" );
-        client.processMail();
+            //Make rest call with extracted token to confirm the admin user.
+            management().users().user( adminUser.getUuid().toString() ).confirm()
+                        .get( new QueryParameters().addParam( "token", token ) );
 
-        Message confirmation = inbox.get( 0 );
-        assertEquals( "User Account Confirmation: " + organization.getEmail(), confirmation.getSubject() );
 
-        //String token = getTokenFromMessage(confirmation);
+            //Try the previous call and verify that the admin user can retrieve login token
+            Token retToken = management().token().get( queryParameters );
 
+            assertNotNull( retToken );
+            assertNotNull( retToken.getAccessToken() );
+        }finally {
+            clientSetup.getRestClient().testPropertiesResource().post( originalTestProperties );
+        }
     }
 
-
-//    @Test
-//    public void testUnconfirmedAdminLogin() throws Exception {
-//
-//        // Setup properties to require confirmation of users
-//        // -------------------------------------------
-//
-//        Map<String, String> originalProperties = getRemoteTestProperties();
-//
-//        try {
-//            setTestProperty( PROPERTIES_SYSADMIN_APPROVES_ADMIN_USERS, "false" );
-//            setTestProperty( PROPERTIES_SYSADMIN_APPROVES_ORGANIZATIONS, "false" );
-//            setTestProperty( PROPERTIES_ADMIN_USERS_REQUIRE_CONFIRMATION, "true" );
-//            setTestProperty( PROPERTIES_SYSADMIN_EMAIL, "sysadmin-1@mockserver.com" );
-//            setTestProperty( PROPERTIES_NOTIFY_ADMIN_OF_ACTIVATION, "true" );
-//
-//            assertTrue( setup.getMgmtSvc().newAdminUsersRequireConfirmation() );
-//            assertFalse( setup.getMgmtSvc().newAdminUsersNeedSysAdminApproval() );
-//
-//            // Setup org/app/user variables and create them
-//            // -------------------------------------------
-//            String orgName = this.getClass().getName();
-//            String appName = "testUnconfirmedAdminLogin";
-//            String userName = "TestUser";
-//            String email = "test-user-46@mockserver.com";
-//            String passwd = "testpassword";
-//            OrganizationOwnerInfo orgOwner;
-//
-//            orgOwner = setup.getMgmtSvc().createOwnerAndOrganization(
-//                    orgName, userName, appName, email, passwd, false, false );
-//            assertNotNull( orgOwner );
-//            String returnedUsername = orgOwner.getOwner().getUsername();
-//            assertEquals( userName, returnedUsername );
-//
-//            UserInfo adminUserInfo = setup.getMgmtSvc().getAdminUserByUsername( userName );
-//            assertNotNull( adminUserInfo );
-//            assertFalse( "adminUser should not be activated yet", adminUserInfo.isActivated() );
-//            assertFalse( "adminUser should not be confirmed yet", adminUserInfo.isConfirmed() );
-//
-//            // Attempt to authenticate but this should fail
-//            // -------------------------------------------
-//            JsonNode node;
-//            try {
-//                node = mapper.readTree( resource().path( "/management/token" )
-//                                                  .queryParam( "grant_type", "password" )
-//                                                  .queryParam( "username", userName )
-//                                                  .queryParam( "password", passwd )
-//                                                  .accept( MediaType.APPLICATION_JSON ).get( String.class ));
-//
-//                fail( "Unconfirmed users should not be authorized to authenticate." );
-//            }
-//            catch ( UniformInterfaceException e ) {
-//                node = mapper.readTree( e.getResponse().getEntity( String.class ));
-//                assertEquals( "invalid_grant", node.get( "error" ).textValue() );
-//                assertEquals( "User must be confirmed to authenticate",
-//                        node.get( "error_description" ).textValue() );
-//                LOG.info( "Unconfirmed user was not authorized to authenticate!" );
-//            }
-//
-//            // Confirm the getting account confirmation email for unconfirmed user
-//            // -------------------------------------------
-//            List<Message> inbox = Mailbox.get( email );
-//            assertFalse( inbox.isEmpty() );
-//
-//            MockImapClient client = new MockImapClient( "mockserver.com", "test-user-46", "somepassword" );
-//            client.processMail();
-//
-//            Message confirmation = inbox.get( 0 );
-//            assertEquals( "User Account Confirmation: " + email, confirmation.getSubject() );
-//
-//            // Extract the token to confirm the user
-//            // -------------------------------------------
-//            String token = getTokenFromMessage( confirmation );
-//            LOG.info( token );
-//
-//            ActivationState state = setup.getMgmtSvc().handleConfirmationTokenForAdminUser(
-//                    orgOwner.getOwner().getUuid(), token );
-//            assertEquals( ActivationState.ACTIVATED, state );
-//
-//            Message activation = inbox.get( 1 );
-//            assertEquals( "User Account Activated", activation.getSubject() );
-//
-//            client = new MockImapClient( "mockserver.com", "test-user-46", "somepassword" );
-//            client.processMail();
-//
-//            refreshIndex(orgName, appName);
-//
-//            // Attempt to authenticate again but this time should pass
-//            // -------------------------------------------
-//
-//            node = mapper.readTree( resource().path( "/management/token" )
-//                                              .queryParam( "grant_type", "password" )
-//                                              .queryParam( "username", userName )
-//                                              .queryParam( "password", passwd )
-//                                              .accept( MediaType.APPLICATION_JSON ).get( String.class ));
-//
-//            assertNotNull( node );
-//            LOG.info( "Authentication succeeded after confirmation: {}.", node.toString() );
-//        }
-//        finally {
-//            setTestProperties( originalProperties );
-//        }
-//    }
-
 //
 //    @Test
 //    public void testSystemAdminNeedsNoConfirmation() throws Exception {
@@ -452,10 +375,10 @@ public class AdminUsersIT extends AbstractRestIT {
 //    }
 //
 //
-//    private String getTokenFromMessage( Message msg ) throws IOException, MessagingException {
-//        String body = ( ( MimeMultipart ) msg.getContent() ).getBodyPart( 0 ).getContent().toString();
-//        return StringUtils.substringAfterLast( body, "token=" );
-//    }
+    private String getTokenFromMessage( Message msg ) throws IOException, MessagingException {
+        String body = ( ( MimeMultipart ) msg.getContent() ).getBodyPart( 0 ).getContent().toString();
+        return StringUtils.substringAfterLast( body, "token=" );
+    }
 //
 //
 //    @Test

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/88fe9063/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/ConfirmResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/ConfirmResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/ConfirmResource.java
new file mode 100644
index 0000000..5692dfe
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/ConfirmResource.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.usergrid.rest.test.resource2point0.endpoints.mgmt;
+
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.usergrid.rest.test.resource2point0.endpoints.NamedResource;
+import org.apache.usergrid.rest.test.resource2point0.endpoints.UrlResource;
+import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse;
+import org.apache.usergrid.rest.test.resource2point0.model.QueryParameters;
+import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
+
+import com.sun.jersey.api.client.WebResource;
+
+
+/**
+ * For confirming users
+ */
+public class ConfirmResource extends NamedResource {
+    public ConfirmResource( final ClientContext context, final UrlResource parent ) {
+        super( "confirm", context, parent );
+    }
+
+    public void get(QueryParameters queryParameters){
+        WebResource resource = getResource();
+        resource = addParametersToResource( resource, queryParameters );
+        String obj = resource.type( MediaType.TEXT_HTML_TYPE )
+                                       .accept( MediaType.TEXT_HTML).get( String.class );
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/88fe9063/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/UserResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/UserResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/UserResource.java
index 1adcd83..da22594 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/UserResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/mgmt/UserResource.java
@@ -38,6 +38,10 @@ public class UserResource extends NamedResource {
         super( name, context, parent );
     }
 
+    public ConfirmResource confirm() {
+        return new ConfirmResource(context,this);
+    }
+
     public PasswordResource password() {
         return new PasswordResource( context, this );
     }