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 2014/12/24 01:16:16 UTC

[4/6] incubator-usergrid git commit: Updated Group Tests, added token login methods

Updated Group Tests, added token login methods


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

Branch: refs/heads/two-dot-o
Commit: 233f733fc307dd397c0a6dd5d5c7555e7f5947f1
Parents: f699a8d
Author: Rod Simpson <ro...@apigee.com>
Authored: Tue Dec 23 10:40:55 2014 -0700
Committer: Rod Simpson <ro...@apigee.com>
Committed: Tue Dec 23 10:40:55 2014 -0700

----------------------------------------------------------------------
 .../collection/groups/GroupResourceIT.java      | 128 +++++++++++--------
 .../test/resource2point0/AbstractRestIT.java    |  22 ++++
 .../endpoints/ApplicationsResource.java         |   3 +
 .../endpoints/OrganizationResource.java         |   4 +
 .../endpoints/TokenResource.java                |   2 +-
 5 files changed, 107 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/233f733f/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/groups/GroupResourceIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/groups/GroupResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/groups/GroupResourceIT.java
index ac1e1d6..4afc51d 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/groups/GroupResourceIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/groups/GroupResourceIT.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.NoSuchElementException;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.sun.jersey.api.client.UniformInterfaceException;
@@ -143,29 +144,32 @@ public class GroupResourceIT extends AbstractRestIT {
      * Verify that we can create a group, change the name, then delete it
      */
     @Test()
-    public void changeGroupNameValidation() throws IOException {
+    public void groupCRUDOperations() throws IOException {
 
         //1. create a group
         String groupName = "testgroup";
         String groupPath = "testgroup";
         Entity group = this.createGroup(groupName, groupPath);
 
-        //2. change the name
+        //2. do a GET to verify the property really was set
+        Entity groupResponseGET = this.app().collection("groups").entity(group).get();
+        assertEquals(groupResponseGET.get("path"), groupPath);
+
+        //3. change the name
         String newGroupPath = "newtestgroup";
         group.put("path", newGroupPath);
         Entity groupResponse = this.app().collection("groups").entity(group).put(group);
         assertEquals(groupResponse.get("path"), newGroupPath);
         this.refreshIndex();
 
-        //3. do a GET to verify the property really was set
-        Entity groupResponseGET = this.app().collection("groups").entity(group).get();
+        //4. do a GET to verify the property really was set
+        groupResponseGET = this.app().collection("groups").entity(group).get();
         assertEquals(groupResponseGET.get("path"), newGroupPath);
 
-        //4. now delete the group
-        ApiResponse response = this.app().collection("groups").entity(group).delete();
-        //todo: what to do with delete responses?
+        //5. now delete the group
+        this.app().collection("groups").entity(group).delete();
 
-        //5. do a GET to make sure the entity was deleted
+        //6. do a GET to make sure the entity was deleted
         try {
             this.app().collection("groups").uniqueID(groupName).get();
             fail("Entity still exists");
@@ -211,9 +215,8 @@ public class GroupResourceIT extends AbstractRestIT {
         assertEquals(entity.get("name"), groupName);
 
         //6. remove the user from the group
-        ApiResponse responseDel = this.app().collection("group").entity(group).connection().collection("users").entity(user).delete();
+        this.app().collection("group").entity(group).connection().collection("users").entity(user).delete();
         this.refreshIndex();
-        //todo: how to check response from delete
 
         //6. make sure the connection no longer exists
         collection = this.app().collection("group").entity(group).connection().collection("users").get();
@@ -241,10 +244,11 @@ public class GroupResourceIT extends AbstractRestIT {
         String roleName = "tester";
         String roleTitle = "tester";
         Entity role = this.createRole(roleName, roleTitle);
+        this.refreshIndex();
 
         //3. add role to the group
         Entity response = this.app().collection("role").entity(role).connection().collection("groups").entity(group).post();
-        assertEquals(response.get("name"), roleName);
+        assertEquals(response.get("name"), groupName);
         this.refreshIndex();
 
         //4. make sure the role is in the group
@@ -253,19 +257,16 @@ public class GroupResourceIT extends AbstractRestIT {
         assertEquals(entity.get("name"), roleName);
 
         //5. remove Role from the group (should only delete the connection)
-        ApiResponse responseDel = this.app().collection("role").entity(role).connection().collection("groups").entity(group).delete();
+        this.app().collection("groups").entity(group).connection().collection("roles").entity(role).delete();
         this.refreshIndex();
-        //todo: how to check response from delete
 
         //6. make sure the connection no longer exists
         collection = this.app().collection("groups").entity(group).connection().collection("roles").get();
         try {
             collection.next();
             fail("Entity still exists");
-        } catch (UniformInterfaceException e) {
-            //verify the correct error was returned
-            JsonNode node = mapper.readTree( e.getResponse().getEntity( String.class ));
-            assertEquals( "service_resource_not_found", node.get( "error" ).textValue() );
+        } catch (NoSuchElementException e) {
+            //all good - there shouldn't be an element!
         }
 
         //7. check root roles to make sure role still exists
@@ -273,9 +274,7 @@ public class GroupResourceIT extends AbstractRestIT {
         assertEquals(role.get("name"), roleName);
 
         //8. delete the role
-        ApiResponse responseDel2 = this.app().collection("role").entity(role).delete();
-        this.refreshIndex();
-        //todo: what to do with response from delete call?
+        this.app().collection("role").entity(role).delete();
 
         //9. do a GET to make sure the role was deleted
         try {
@@ -329,36 +328,29 @@ public class GroupResourceIT extends AbstractRestIT {
 
         //4. add permissions to role
         Entity payload = new Entity();
-        payload.put("permission","get,post:/cats");
+        payload.put("permission","get,post:/cats/*");
         Entity permission = this.app().collection("roles").uniqueID(roleName).connection("permissions").post(payload);
-        assertEquals(permission.get("data"), "get,post:/cats");
-        this.refreshIndex();
+        assertEquals(permission.get("data"), "get,post:/cats/*");
 
         //5. add role to the group
         Entity addRoleResponse = this.app().collection("groups").entity(group).connection().collection("roles").entity(role).post();
         assertEquals(addRoleResponse.get("name"), roleName);
-        this.refreshIndex();
 
         //6. add user to group
         Entity addUserResponse = this.app().collection("users").entity(user).connection().collection("groups").entity(group).post();
         assertEquals(addUserResponse.get("name"), groupName);
-        this.refreshIndex();
 
         //7. delete the default role
-        ApiResponse responseDelDefault = this.app().collection("role").uniqueID("Default").delete();
-        this.refreshIndex();
-        //todo: what to do with response from delete call?
+        this.app().collection("role").uniqueID("Default").delete();
 
         //8. delete the guest role
-        ApiResponse responseDelGuest = this.app().collection("role").uniqueID("Guest").delete();
-        this.refreshIndex();
-        //todo: what to do with response from delete call?
+        this.app().collection("role").uniqueID("Guest").delete();
 
-        //log user in, should then be using the app user's token not the admin token
-        //todo: need to log user in here - how?
+        //9. log user in, should then be using the app user's token not the admin token
+        this.getAppUserToken(username, password);
 
 
-        //create a cat - permissions should allow this
+        //10. create a cat - permissions should allow this
         String catName = "fluffy";
         payload = new Entity();
         payload.put("name", catName);
@@ -366,11 +358,11 @@ public class GroupResourceIT extends AbstractRestIT {
         assertEquals(fluffy.get("name"), catName);
         this.refreshIndex();
 
-        //get the cat - permissions should allow this
+        //11. get the cat - permissions should allow this
         fluffy = this.app().collection("cats").uniqueID(catName).get();
         assertEquals(fluffy.get("name"), catName);
 
-        //edit the cat - permissions should not allow this
+        //12. edit the cat - permissions should not allow this
         fluffy.put("color", "brown");
         try {
             this.app().collection("cats").uniqueID(catName).put(fluffy);
@@ -378,17 +370,17 @@ public class GroupResourceIT extends AbstractRestIT {
         } catch (UniformInterfaceException e) {
             //verify the correct error was returned
             JsonNode node = mapper.readTree( e.getResponse().getEntity( String.class ));
-            assertEquals( "improper credentials or something", node.get( "error" ).textValue() );
+            assertEquals( "unauthorized", node.get( "error" ).textValue() );
         }
 
-        //delete the cat - permissions should not allow this
+        //13. delete the cat - permissions should not allow this
         try {
             this.app().collection("cats").uniqueID(catName).delete();
             fail("permissions should not allow this");
         } catch (UniformInterfaceException e) {
             //verify the correct error was returned
             JsonNode node = mapper.readTree( e.getResponse().getEntity( String.class ));
-            assertEquals( "improper credentials or something", node.get( "error" ).textValue() );
+            assertEquals( "unauthorized", node.get( "error" ).textValue() );
         }
 
     }
@@ -402,23 +394,57 @@ public class GroupResourceIT extends AbstractRestIT {
     @Test
     public void postGroupActivity() throws IOException {
 
-        /*
 
         //1. create a group
-        GroupsCollection groups = context.groups();
+        String groupName = "testgroup";
+        String groupPath = "testgroup";
+        Entity group = this.createGroup(groupName, groupPath);
+
+        //2. create user 1
+        String username = "fred";
+        String email = "fred@usergrid.com";
+        String password = "password";
+        Entity user1 = this.createUser(username, email, password);
+
+        //3. create user 2
+        username = "barney";
+        email = "fred@usergrid.com";
+        password = "password";
+        Entity user2 = this.createUser(username, email, password);
+
+        //4. add user1 to the group
+        Entity addUser1Response = this.app().collection("users").entity(user1).connection().collection("groups").entity(group).post();
+        assertEquals(addUser1Response.get("name"), groupName);
+
+        //5. add user2 to the group
+        Entity addUser2Response = this.app().collection("users").entity(user2).connection().collection("groups").entity(group).post();
+        assertEquals(addUser2Response.get("name"), groupName);
+
+        //6. post an activity to the group
+        //JSON should look like this:
+        //{'{"actor":{"displayName":"fdsafdsa","uuid":"2b70e83a-8a3f-11e4-9716-235107bcadb1","username":"fdsafdsa"},
+        // "verb":"post","content":"fdsafdsa"}'
+        Entity payload = new Entity();
+        payload.put("displayName", "fred");
+        payload.put("uuid", user1.get("uuid"));
+        payload.put("username", "fred");
+        Entity activity = new Entity();
+        activity.put("actor", payload);
+        activity.put("verb", "post");
+        activity.put("content", "content");
+        Entity activityResponse = this.app().collection("users").post(activity);
+        assertEquals(activityResponse.get("content"), "content");
+        assertEquals(activityResponse, activity);
+        this.refreshIndex();
+
+        //7. make sure the activity appears in the feed of user 1
+
+
+        //8. make sure the activity appears in the feed of user 2
+
 
-        //create a group with a normal name
-        String groupName = "groupTitle";
-        String groupPath = "groupPath";
-        JsonNode testGroup = groups.create(groupName, groupPath);
-        //verify the group was created
-        assertNull(testGroup.get("errors"));
-        assertEquals(testGroup.get("path").asText(), groupPath);
 
-        //2. post group activity
 
-        //TODO: actually post a group activity
-        */
     }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/233f733f/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
index 242bb43..2e47ba8 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
@@ -26,6 +26,8 @@ import javax.ws.rs.core.MediaType;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.ApplicationsResource;
 import org.apache.usergrid.rest.test.resource2point0.endpoints.OrganizationResource;
+import org.apache.usergrid.rest.test.resource2point0.model.Entity;
+import org.apache.usergrid.rest.test.resource2point0.model.Token;
 import org.junit.ClassRule;
 import org.junit.Rule;
 
@@ -133,4 +135,24 @@ public class AbstractRestIT extends JerseyTest {
         assertEquals( expectedErrorMessage, errorJson.get( "error" ).asText() );
 
     }
+
+    protected Token getAppUserToken(String username, String password){
+        Token payload = new Token();
+        payload.put("username", username);
+        payload.put("password", password);
+        payload.put("grant_type", "password");
+        return this.clientSetup.getRestClient().org(clientSetup.getOrganization().getName()).app(clientSetup.getAppName()).token().post(payload);
+    }
+
+    protected Token getAdminToken(String username, String password){
+        return this.clientSetup.getRestClient().management().token().post(
+                new Token(username, password)
+        );
+    }
+
+    protected Token getAdminToken(){
+        return this.clientSetup.getRestClient().management().token().post(
+                new Token(this.clientSetup.getUsername(),this.clientSetup.getUsername())
+        );
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/233f733f/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/ApplicationsResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/ApplicationsResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/ApplicationsResource.java
index a08dff5..ef26508 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/ApplicationsResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/ApplicationsResource.java
@@ -43,4 +43,7 @@ public class ApplicationsResource extends CollectionEndpoint {
     }
 
 
+    public TokenResource token() {
+        return new TokenResource(context,this);
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/233f733f/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java
index 9834b6b..158b6ad 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/OrganizationResource.java
@@ -41,6 +41,10 @@ public class OrganizationResource extends NamedResource {
         return new ApplicationsResource( app, context ,this );
     }
 
+    public ApplicationsResource token(){
+        return new ApplicationsResource( "token", context ,this );
+    }
+
     public void post(Map<String,String> organization) {
 
         getResource().type( MediaType.APPLICATION_JSON_TYPE ).accept( MediaType.APPLICATION_JSON )

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/233f733f/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/TokenResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/TokenResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/TokenResource.java
index 41c9f2a..3ae08c5 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/TokenResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/endpoints/TokenResource.java
@@ -37,7 +37,7 @@ public class TokenResource extends NamedResource {
 
 
     /**
-     * Obtains an access token of type "application user"
+     * Obtains an access token
      *
      * @param token
      * @return