From 9005e30fd6130636dc0f81d4bfa02b7b523b1993 Mon Sep 17 00:00:00 2001 From: Rod Simpson Date: Sat, 20 Dec 2014 14:04:54 -0700 Subject: [PATCH 1/5] updated groups tests to work with new framework --- .../collection/groups/GroupResourceIT.java | 359 +++++++++++------- 1 file changed, 215 insertions(+), 144 deletions(-) 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 79ce8c56b8..f536269303 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 @@ -26,6 +26,7 @@ import com.sun.jersey.api.client.UniformInterfaceException; import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT; import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse; +import org.apache.usergrid.rest.test.resource2point0.model.Collection; import org.apache.usergrid.rest.test.resource2point0.model.Entity; import org.junit.Test; import org.slf4j.Logger; @@ -41,6 +42,39 @@ public class GroupResourceIT extends AbstractRestIT { public GroupResourceIT() throws Exception { } + private Entity createGroup(String groupName, String groupPath) throws IOException{ + Entity payload = new Entity(); + payload.put("name", groupName); + payload.put("path", groupPath); + Entity entity = this.app().collection("groups").post(payload); + assertEquals(entity.get("name"), groupName); + assertEquals(entity.get("path"), groupPath); + this.refreshIndex(); + return entity; + } + + private Entity createRole(String roleName, String roleTitle) throws IOException{ + Entity payload = new Entity(); + payload.put("name", roleName); + payload.put("title", roleTitle); + Entity entity = this.app().collection("roles").post(payload); + assertEquals(entity.get("name"), roleName); + assertEquals(entity.get("title"), roleTitle); + this.refreshIndex(); + return entity; + } + + private Entity createUser(String username, String email) throws IOException{ + Entity payload = new Entity(); + payload.put("username", username); + payload.put("email", email); + Entity entity = this.app().collection("users").post(payload); + assertEquals(entity.get("username"), username); + assertEquals(entity.get("email"), email); + this.refreshIndex(); + return entity; + } + /*** * * Verify that we can create a group with a standard string in the name and path @@ -50,15 +84,7 @@ public void createGroupValidation() throws IOException { String groupName = "testgroup"; String groupPath = "testgroup"; - - Entity payload = new Entity(); - payload.put("name", groupName); - payload.put("path", groupPath); - - Entity group = this.app().collection("groups").post(payload); - - assertEquals(group.get("path"), groupPath); - assertEquals(group.get("name"), groupName); + this.createGroup(groupName, groupPath); } @@ -70,18 +96,9 @@ public void createGroupValidation() throws IOException { @Test() public void createGroupSlashInNameAndPathValidation() throws IOException { - //create the group with a slash in the name String groupNameSlash = "test/group"; String groupPathSlash = "test/group"; - - Entity payload = new Entity(); - payload.put("name", groupNameSlash); - payload.put("path", groupPathSlash); - - Entity group = this.app().collection("groups").post(payload); - - assertEquals(group.get("name"), groupNameSlash); - assertEquals(group.get("path"), groupPathSlash); + this.createGroup(groupNameSlash, groupPathSlash); } @@ -93,18 +110,9 @@ public void createGroupSlashInNameAndPathValidation() throws IOException { @Test() public void createGroupSpaceInNameValidation() throws IOException { - //create the group with a space in the name String groupSpaceName = "test group"; String groupPath = "testgroup"; - - Entity payload = new Entity(); - payload.put("name", groupSpaceName); - payload.put("path", groupPath); - - Entity group = this.app().collection("groups").post(payload); - - assertEquals(group.get("name"), groupSpaceName); - assertEquals(group.get("path"), groupPath); + this.createGroup(groupSpaceName, groupPath); } @@ -118,14 +126,8 @@ public void createGroupSpaceInPathValidation() throws IOException { String groupName = "testgroup"; String groupSpacePath = "test group"; - - Entity payload = new Entity(); - payload.put("name", groupName); - payload.put("path", groupSpacePath); - - try { - this.app().collection("groups").post(payload); + Entity group = this.createGroup(groupName, groupSpacePath); fail("Should not be able to create a group with a space in the path"); } catch (UniformInterfaceException e) { //verify the correct error was returned @@ -137,50 +139,204 @@ public void createGroupSpaceInPathValidation() throws IOException { /*** * - * Verify that we can create a group and then change the name + * Verify that we can create a group, change the name, then delete it */ @Test() public void changeGroupNameValidation() throws IOException { + //1. create a group String groupName = "testgroup"; String groupPath = "testgroup"; + Entity group = this.createGroup(groupName, groupPath); + + //2. 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(); - Entity payload = new Entity(); - payload.put("name", groupName); - payload.put("path", groupPath); + //3. do a GET to verify the property really was set + Entity groupResponseGET = this.app().collection("groups").entity(group).get(); + assertEquals(groupResponseGET.get("path"), newGroupPath); - Entity group = this.app().collection("groups").post(payload); - assertEquals(group.get("path"), groupPath); + //4. now delete the group + ApiResponse response = this.app().collection("groups").entity(group).delete(); + //todo: what to do with delete responses? - //now change the name - group.put("path", newGroupPath); - this.app().collection("groups").entity(group).put(group); - assertEquals(group.get("path"), newGroupPath); + //5. do a GET to make sure the entity was deleted + try { + this.app().collection("groups").uniqueID(groupName).get(); + 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() ); + } - //now delete the group - ApiResponse response = this.app().collection("groups").entity(group).delete(); + } + /*** + * + * Verify that we can create a group, user, add user to group, delete connection + */ + @Test() + public void addRemoveUserGroup() throws IOException { + //1. create a group + String groupName = "testgroup"; + String groupPath = "testgroup"; + Entity group = this.createGroup(groupName, groupPath); + + // 2. create a user + String username = "fred"; + String email = "fred@usergrid.com"; + Entity user = this.createUser(username, email); + + // 3. add the user to the group + Entity response = this.app().collection("users").entity(user).connection().collection("groups").entity(group).post(); + assertEquals(response.get("name"), groupName); + this.refreshIndex(); + + // 4. make sure the user is in the group + Collection collection = this.app().collection("groups").entity(group).connection().collection("users").get(); + Entity entity = collection.next(); + assertEquals(entity.get("username"), username); + + //5. try it the other way around + collection = this.app().collection("users").entity(user).connection().collection("groups").get(); + entity = collection.next(); + 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.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(); + assertEquals(collection.hasNext(), false); + + //8. do a GET to make sure the user still exists and did not get deleted with the collection delete + Entity userEntity = this.app().collection("user").entity(user).get(); + assertEquals(userEntity.get("username"), username); + + } + + /*** + * + * Verify that we can create a group, role, add role to group, delete connection + */ + @Test + public void addRemoveRoleGroup() throws IOException { + + //1. create a group + String groupName = "testgroup"; + String groupPath = "testgroup"; + Entity group = this.createGroup(groupName, groupPath); + + //2. create a role + String roleName = "tester"; + String roleTitle = "tester"; + Entity role = this.createRole(roleName, roleTitle); + + //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); + this.refreshIndex(); + + //4. make sure the role is in the group + Collection collection = this.app().collection("groups").entity(group).connection().collection("roles").get(); + Entity entity = collection.next(); + 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.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 { - Entity newGroup = this.app().collection("groups").uniqueID(groupName).get(); + 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( "illegal_argument", node.get( "error" ).textValue() ); + assertEquals( "service_resource_not_found", node.get( "error" ).textValue() ); + } + + //7. check root roles to make sure role still exists + role = this.app().collection("roles").uniqueID(roleName).get(); + 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? + + //9. do a GET to make sure the role was deleted + try { + this.app().collection("role").entity(role).get(); + 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() ); } } + /*** * - * Verify that we can create a group and then add a user to it + * Verify that group / role permissions work + * + * create group + * create user + * create role + * add permissions to role (e.g. POST, GET on /cats) + * add role to group + * add user to group + * delete default role (to ensure no app-level user operations are allowed) + * delete guest role (to ensure no app-level user operations are allowed) + * create a /cats/fluffy + * read /cats/fluffy + * update /cats/fluffy (should fail) + * delete /cats/fluffy (should fail) */ @Test() - public void createGroupAndAddAUserValidation() throws IOException { + public void addRolePermissionToGroupVerifyPermission() throws IOException { + + //1. create a group + String groupName = "testgroup"; + String groupPath = "testgroup"; + Entity group = this.createGroup(groupName, groupPath); + + //2. create a user + String username = "fred"; + String email = "fred@usergrid.com"; + Entity user = this.createUser(username, email); + //3. create a role + String roleName = "tester"; + String roleTitle = "tester"; + Entity role = this.createRole(roleName, roleTitle); + + //4. add permissions to role + + + //5. add role to the group + Entity addRoleresponse = this.app().collection("role").entity(role).connection().collection("groups").entity(group).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(); } + /*** * * Verify that we can create a group and then add a role to it @@ -188,6 +344,13 @@ public void createGroupAndAddAUserValidation() throws IOException { @Test() public void createGroupAndAddARoleValidation() throws IOException { + /* + JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) + .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); + assertNull( node.get( "errors" ) ); + assertTrue( node.get( "entities" ).findValuesAsText( "name" ).contains( roleName ) ); + + */ } /* @@ -252,98 +415,6 @@ public void addRemovePermission() throws IOException { assertTrue( node.get( "data" ).size() == 0 ); } -/* - @Test - public void addRemoveRole() throws IOException { - - UUID id = UUIDUtils.newTimeUUID(); - - String groupName = "groupname" + id; - String roleName = "rolename" + id; - - ApiResponse response = client.createGroup( groupName ); - assertNull( "Error was: " + response.getErrorDescription(), response.getError() ); - - UUID createdId = response.getEntities().get( 0 ).getUuid(); - - refreshIndex("test-organization", "test-app"); - - // create Role - - String json = "{\"title\":\"" + roleName + "\",\"name\":\"" + roleName + "\"}"; - JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ) - .post( String.class, json )); - - // check it - assertNull( node.get( "errors" ) ); - - - refreshIndex("test-organization", "test-app"); - - // add Role - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/roles/" + roleName ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class )); - - refreshIndex("test-organization", "test-app"); - - // check it - assertNull( node.get( "errors" ) ); - assertEquals( node.get( "entities" ).get( 0 ).get( "name" ).asText(), roleName ); - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/roles" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertEquals( node.get( "entities" ).get( 0 ).get( "name" ).asText(), roleName ); - - // check root roles - node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "entities" ).findValuesAsText( "name" ).contains( roleName ) ); - - refreshIndex("test-organization", "test-app"); - - // remove Role - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/roles/" + roleName ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).delete( String.class )); - assertNull( node.get( "errors" ) ); - - refreshIndex("test-organization", "test-app"); - - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/roles" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "entities" ).size() == 0 ); - - // check root roles - role should remain - node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "entities" ).findValuesAsText( "name" ).contains( roleName ) ); - - // now kill the root role - node = mapper.readTree( resource().path( "/test-organization/test-app/roles/" + roleName ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).delete( String.class )); - assertNull( node.get( "errors" ) ); - - refreshIndex("test-organization", "test-app"); - - // now it should be gone - node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertFalse( node.get( "entities" ).findValuesAsText( "name" ).contains( roleName ) ); - } - */ - /*** * From 118aa73e2c42d35e458a3d4cd529077442229349 Mon Sep 17 00:00:00 2001 From: Rod Simpson Date: Sat, 20 Dec 2014 15:39:35 -0700 Subject: [PATCH 2/5] added data to response, flushed out groupsrolespermissionsusers test --- .../collection/groups/GroupResourceIT.java | 128 ++++++++---------- .../test/resource2point0/model/Entity.java | 8 +- 2 files changed, 60 insertions(+), 76 deletions(-) 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 f536269303..ac1e1d694a 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 @@ -64,10 +64,11 @@ private Entity createRole(String roleName, String roleTitle) throws IOException{ return entity; } - private Entity createUser(String username, String email) throws IOException{ + private Entity createUser(String username, String email, String password) throws IOException{ Entity payload = new Entity(); payload.put("username", username); payload.put("email", email); + payload.put("password", password); Entity entity = this.app().collection("users").post(payload); assertEquals(entity.get("username"), username); assertEquals(entity.get("email"), email); @@ -191,7 +192,8 @@ public void addRemoveUserGroup() throws IOException { // 2. create a user String username = "fred"; String email = "fred@usergrid.com"; - Entity user = this.createUser(username, email); + String password = "password"; + Entity user = this.createUser(username, email, password); // 3. add the user to the group Entity response = this.app().collection("users").entity(user).connection().collection("groups").entity(group).post(); @@ -300,6 +302,7 @@ public void addRemoveRoleGroup() throws IOException { * add user to group * delete default role (to ensure no app-level user operations are allowed) * delete guest role (to ensure no app-level user operations are allowed) + * log the user in with * create a /cats/fluffy * read /cats/fluffy * update /cats/fluffy (should fail) @@ -316,7 +319,8 @@ public void addRolePermissionToGroupVerifyPermission() throws IOException { //2. create a user String username = "fred"; String email = "fred@usergrid.com"; - Entity user = this.createUser(username, email); + String password = "password"; + Entity user = this.createUser(username, email, password); //3. create a role String roleName = "tester"; @@ -324,95 +328,69 @@ public void addRolePermissionToGroupVerifyPermission() throws IOException { Entity role = this.createRole(roleName, roleTitle); //4. add permissions to role - + Entity payload = new Entity(); + 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(); //5. add role to the group - Entity addRoleresponse = this.app().collection("role").entity(role).connection().collection("groups").entity(group).post(); - assertEquals(addRoleresponse.get("name"), roleName); + 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(); - } - - /*** - * - * Verify that we can create a group and then add a role to it - */ - @Test() - public void createGroupAndAddARoleValidation() throws IOException { - - /* - JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/roles" ).queryParam( "access_token", access_token ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "entities" ).findValuesAsText( "name" ).contains( roleName ) ); - - */ - } - - /* - - @Test - public void addRemovePermission() throws IOException { - - GroupsCollection groups = context.groups(); - - - - UUID id = UUIDUtils.newTimeUUID(); - String groupName = "groupname" + id; - - ApiResponse response = client.createGroup( groupName ); - assertNull( "Error was: " + response.getErrorDescription(), response.getError() ); - - refreshIndex("test-organization", "test-app"); - - UUID createdId = response.getEntities().get( 0 ).getUuid(); - - // add Permission - String orgName = context.getOrgName(); - String appName = context.getAppName(); - String path = "/"+orgName+"/"+appName+"/groups/"; - - String json = "{\"permission\":\"delete:/test\"}"; - JsonNode node = mapper.readTree( resource().path( path + createdId + "/permissions" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, json )); - - // check it - assertNull( node.get( "errors" ) ); - assertEquals( node.get( "data" ).get( 0 ).asText(), "delete:/test" ); + //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? - refreshIndex("test-organization", "test-app"); + //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? - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/permissions" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertEquals( node.get( "data" ).get( 0 ).asText(), "delete:/test" ); + //log user in, should then be using the app user's token not the admin token + //todo: need to log user in here - how? - // remove Permission + //create a cat - permissions should allow this + String catName = "fluffy"; + payload = new Entity(); + payload.put("name", catName); + Entity fluffy = this.app().collection("cats").post(payload); + assertEquals(fluffy.get("name"), catName); + this.refreshIndex(); - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/permissions" ) - .queryParam( "access_token", access_token ).queryParam( "permission", "delete%3A%2Ftest" ) - .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE ).delete( String.class )); + //get the cat - permissions should allow this + fluffy = this.app().collection("cats").uniqueID(catName).get(); + assertEquals(fluffy.get("name"), catName); - // check it - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "data" ).size() == 0 ); + //edit the cat - permissions should not allow this + fluffy.put("color", "brown"); + try { + this.app().collection("cats").uniqueID(catName).put(fluffy); + 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() ); + } - refreshIndex("test-organization", "test-app"); + //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() ); + } - node = mapper.readTree( resource().path( "/test-organization/test-app/groups/" + createdId + "/permissions" ) - .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON ) - .type( MediaType.APPLICATION_JSON_TYPE ).get( String.class )); - assertNull( node.get( "errors" ) ); - assertTrue( node.get( "data" ).size() == 0 ); } diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Entity.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Entity.java index c4ac5ca01a..73cdda58ce 100644 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Entity.java +++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/model/Entity.java @@ -55,11 +55,17 @@ public Entity (Map payload){ public Entity(ApiResponse response){ this.response = response; - if(response.getEntities() !=null && response.getEntities().size()>=1){ + if(response.getEntities() != null && response.getEntities().size()>=1){ List entities = response.getEntities(); Map entity = entities.get(0); this.putAll(entity); } + else if (response.getData() != null){ + ArrayList data = (ArrayList ) response.getData(); + Entity entity = new Entity(); + entity.put("data", data.get(0)); + this.putAll(entity); + } } //For the owner , should have different cases that looks at the different types it could be From 233f733fc307dd397c0a6dd5d5c7555e7f5947f1 Mon Sep 17 00:00:00 2001 From: Rod Simpson Date: Tue, 23 Dec 2014 10:40:55 -0700 Subject: [PATCH 3/5] Updated Group Tests, added token login methods --- .../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(-) 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 ac1e1d694a..4afc51d7ee 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.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 void createGroupSpaceInPathValidation() throws IOException { * 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 void addRemoveUserGroup() throws IOException { 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 void addRemoveRoleGroup() throws IOException { 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 void addRemoveRoleGroup() throws IOException { 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 void addRemoveRoleGroup() throws IOException { 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 void addRolePermissionToGroupVerifyPermission() throws IOException { //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 void addRolePermissionToGroupVerifyPermission() throws IOException { 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 void addRolePermissionToGroupVerifyPermission() throws IOException { } 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 void addRolePermissionToGroupVerifyPermission() throws IOException { @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 - */ } 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 242bb4366c..2e47ba8342 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 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 void errorParse(Integer expectedStatus, String expectedErrorMessage, Unif 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()) + ); + } } 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 a08dff576f..ef26508899 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 CollectionEndpoint collection(String name) { } + public TokenResource token() { + return new TokenResource(context,this); + } } 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 9834b6b9b9..158b6ad210 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 ApplicationsResource app(final String app){ return new ApplicationsResource( app, context ,this ); } + public ApplicationsResource token(){ + return new ApplicationsResource( "token", context ,this ); + } + public void post(Map organization) { getResource().type( MediaType.APPLICATION_JSON_TYPE ).accept( MediaType.APPLICATION_JSON ) 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 41c9f2a666..3ae08c5f79 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 TokenResource(final ClientContext context, final UrlResource parent) { /** - * Obtains an access token of type "application user" + * Obtains an access token * * @param token * @return From f8aeed3ff920251566cb140e5b4aa072fff5f1c8 Mon Sep 17 00:00:00 2001 From: Rod Simpson Date: Tue, 23 Dec 2014 14:23:08 -0700 Subject: [PATCH 4/5] updated groups, activity test failing due to problems in stack --- .../collection/groups/GroupResourceIT.java | 141 +++++++++++++----- 1 file changed, 102 insertions(+), 39 deletions(-) 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 4afc51d7ee..e0c271b62b 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 @@ -43,6 +43,11 @@ public class GroupResourceIT extends AbstractRestIT { public GroupResourceIT() throws Exception { } + /*** + * + * helper method to create a group + * + */ private Entity createGroup(String groupName, String groupPath) throws IOException{ Entity payload = new Entity(); payload.put("name", groupName); @@ -54,6 +59,11 @@ private Entity createGroup(String groupName, String groupPath) throws IOExceptio return entity; } + /*** + * + * helper method to create a role + * + */ private Entity createRole(String roleName, String roleTitle) throws IOException{ Entity payload = new Entity(); payload.put("name", roleName); @@ -65,6 +75,11 @@ private Entity createRole(String roleName, String roleTitle) throws IOException{ return entity; } + /*** + * + * helper method to create an app level user + * + */ private Entity createUser(String username, String email, String password) throws IOException{ Entity payload = new Entity(); payload.put("username", username); @@ -80,6 +95,7 @@ private Entity createUser(String username, String email, String password) throws /*** * * Verify that we can create a group with a standard string in the name and path + * */ @Test() public void createGroupValidation() throws IOException { @@ -93,8 +109,8 @@ public void createGroupValidation() throws IOException { /*** * * Verify that we can create a group with a slash in the name and path + * */ - @Test() public void createGroupSlashInNameAndPathValidation() throws IOException { @@ -107,8 +123,8 @@ public void createGroupSlashInNameAndPathValidation() throws IOException { /*** * * Verify that we can create a group with a space in the name + * */ - @Test() public void createGroupSpaceInNameValidation() throws IOException { @@ -121,8 +137,8 @@ public void createGroupSpaceInNameValidation() throws IOException { /*** * * Verify that we cannot create a group with a space in the path + * */ - @Test() public void createGroupSpaceInPathValidation() throws IOException { @@ -142,6 +158,7 @@ public void createGroupSpaceInPathValidation() throws IOException { /*** * * Verify that we can create a group, change the name, then delete it + * */ @Test() public void groupCRUDOperations() throws IOException { @@ -184,6 +201,7 @@ public void groupCRUDOperations() throws IOException { /*** * * Verify that we can create a group, user, add user to group, delete connection + * */ @Test() public void addRemoveUserGroup() throws IOException { @@ -231,6 +249,7 @@ public void addRemoveUserGroup() throws IOException { /*** * * Verify that we can create a group, role, add role to group, delete connection + * */ @Test public void addRemoveRoleGroup() throws IOException { @@ -293,19 +312,6 @@ public void addRemoveRoleGroup() throws IOException { * * Verify that group / role permissions work * - * create group - * create user - * create role - * add permissions to role (e.g. POST, GET on /cats) - * add role to group - * add user to group - * delete default role (to ensure no app-level user operations are allowed) - * delete guest role (to ensure no app-level user operations are allowed) - * log the user in with - * create a /cats/fluffy - * read /cats/fluffy - * update /cats/fluffy (should fail) - * delete /cats/fluffy (should fail) */ @Test() public void addRolePermissionToGroupVerifyPermission() throws IOException { @@ -343,14 +349,10 @@ public void addRolePermissionToGroupVerifyPermission() throws IOException { //7. delete the default role this.app().collection("role").uniqueID("Default").delete(); - //8. delete the guest role - this.app().collection("role").uniqueID("Guest").delete(); - - //9. log user in, should then be using the app user's token not the admin token + //8. log user in, should then be using the app user's token not the admin token this.getAppUserToken(username, password); - - //10. create a cat - permissions should allow this + //9. create a cat - permissions should allow this String catName = "fluffy"; payload = new Entity(); payload.put("name", catName); @@ -358,11 +360,11 @@ public void addRolePermissionToGroupVerifyPermission() throws IOException { assertEquals(fluffy.get("name"), catName); this.refreshIndex(); - //11. get the cat - permissions should allow this + //10. get the cat - permissions should allow this fluffy = this.app().collection("cats").uniqueID(catName).get(); assertEquals(fluffy.get("name"), catName); - //12. edit the cat - permissions should not allow this + //11. edit the cat - permissions should not allow this fluffy.put("color", "brown"); try { this.app().collection("cats").uniqueID(catName).put(fluffy); @@ -373,7 +375,7 @@ public void addRolePermissionToGroupVerifyPermission() throws IOException { assertEquals( "unauthorized", node.get( "error" ).textValue() ); } - //13. delete the cat - permissions should not allow this + //12. delete the cat - permissions should not allow this try { this.app().collection("cats").uniqueID(catName).delete(); fail("permissions should not allow this"); @@ -389,8 +391,8 @@ public void addRolePermissionToGroupVerifyPermission() throws IOException { /*** * * Post a group activity + * */ - @Test public void postGroupActivity() throws IOException { @@ -401,26 +403,40 @@ public void postGroupActivity() throws IOException { Entity group = this.createGroup(groupName, groupPath); //2. create user 1 - String username = "fred"; - String email = "fred@usergrid.com"; + String user1Username = "fred"; + String user1Email = "fred@usergrid.com"; String password = "password"; - Entity user1 = this.createUser(username, email, password); + Entity user1 = this.createUser(user1Username, user1Email, password); //3. create user 2 - username = "barney"; - email = "fred@usergrid.com"; + String user2Username = "barney"; + String user2Email = "barney@usergrid.com"; + password = "password"; + Entity user2 = this.createUser(user2Username, user2Email, password); + + //4. create user 3 + String user3Username = "wilma"; + String user3Email = "wilma@usergrid.com"; password = "password"; - Entity user2 = this.createUser(username, email, password); + Entity user3 = this.createUser(user3Username, user3Email, password); - //4. add user1 to the group + //5. 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 + //6. 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 + // user 3 does not get added to the group + + //7. get all the users in the groups + this.refreshIndex(); + Collection usersInGroup = this.app().collection("groups").uniqueID(groupName).connection("users").get(); + assertEquals(usersInGroup.response.getEntityCount(), 2); + + + //8. 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"}' @@ -432,17 +448,64 @@ public void postGroupActivity() throws IOException { activity.put("actor", payload); activity.put("verb", "post"); activity.put("content", "content"); - Entity activityResponse = this.app().collection("users").post(activity); + Entity activityResponse = this.app().collection("groups").uniqueID(groupName).connection("activities").post(activity); assertEquals(activityResponse.get("content"), "content"); - assertEquals(activityResponse, activity); this.refreshIndex(); - //7. make sure the activity appears in the feed of user 1 + //9. delete the default role + this.app().collection("role").uniqueID("Default").delete(); + + //10. add permissions to group: {"permission":"get,post,put,delete:/groups/${group}/**"}' + payload = new Entity(); + String permission = "get,post,put,delete:/groups/${group}/**"; + payload.put("permission",permission); + Entity permissionResponse = this.app().collection("groups").entity(group).connection("permissions").post(payload); + assertEquals(permissionResponse.get("data"), permission); + //11. log user1 in, should then be using the app user's token not the admin token + this.getAppUserToken(user1Username, password); - //8. make sure the activity appears in the feed of user 2 + //TODO: next failing currently because permissions seem to be borked in the stack + //12. make sure the activity appears in the feed of user 1 + Collection user1ActivityResponse = this.app().collection("groups").entity(group).connection("activities").get(); + boolean found = false; + while (user1ActivityResponse.hasNext()) { + Entity tempActivity = user1ActivityResponse.next(); + if (tempActivity.get("type").equals("activity") && tempActivity.get("content").equals("content")) { + found = true; + } + } + assertTrue(found); + + //13. log user2 in, should then be using the app user's token not the admin token + this.getAppUserToken(user2Username, password); + + //14. make sure the activity appears in the feed of user 2 + Collection user2ActivityResponse = this.app().collection("groups").entity(group).connection("activities").get(); + found = false; + while (user2ActivityResponse.hasNext()) { + Entity tempActivity = user2ActivityResponse.next(); + if (tempActivity.get("type").equals("activity") && tempActivity.get("content").equals("content")) { + found = true; + } + } + assertTrue(found); + + //15. log user3 in, should then be using the app user's token not the admin token + this.getAppUserToken(user3Username, password); + + //16. make sure the activity does not appear in the feed of user 3, since they are not part of the group + Collection user3ActivityResponse = this.app().collection("groups").entity(group).connection("activities").get(); + found = false; + while (user3ActivityResponse.hasNext()) { + Entity tempActivity = user3ActivityResponse.next(); + if (tempActivity.get("type").equals("activity") && tempActivity.get("content").equals("content")) { + found = true; + } + } + assertFalse(found); } From e98039870d141165ffd99860789184d8dcc0ce5e Mon Sep 17 00:00:00 2001 From: Rod Simpson Date: Tue, 23 Dec 2014 15:12:04 -0700 Subject: [PATCH 5/5] cleanup for groups test --- .../collection/groups/GroupResourceIT.java | 39 +++++++++---------- .../endpoints/OrganizationResource.java | 3 -- 2 files changed, 18 insertions(+), 24 deletions(-) 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 e0c271b62b..14f1863922 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 @@ -18,15 +18,10 @@ 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; import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT; -import org.apache.usergrid.rest.test.resource2point0.model.ApiResponse; import org.apache.usergrid.rest.test.resource2point0.model.Collection; import org.apache.usergrid.rest.test.resource2point0.model.Entity; import org.junit.Test; @@ -36,10 +31,14 @@ import static org.junit.Assert.*; -/** @author rockerston */ +/** + * @author rockerston + * + * REST tests for /groups endpoint + * + * */ @Concurrent() public class GroupResourceIT extends AbstractRestIT { - private static Logger log = LoggerFactory.getLogger( GroupResourceIT.class ); public GroupResourceIT() throws Exception { } @@ -145,7 +144,7 @@ public void createGroupSpaceInPathValidation() throws IOException { String groupName = "testgroup"; String groupSpacePath = "test group"; try { - Entity group = this.createGroup(groupName, groupSpacePath); + this.createGroup(groupName, groupSpacePath); fail("Should not be able to create a group with a space in the path"); } catch (UniformInterfaceException e) { //verify the correct error was returned @@ -281,11 +280,8 @@ public void addRemoveRoleGroup() throws IOException { //6. make sure the connection no longer exists collection = this.app().collection("groups").entity(group).connection().collection("roles").get(); - try { - collection.next(); + if (collection.hasNext()) { fail("Entity still exists"); - } catch (NoSuchElementException e) { - //all good - there shouldn't be an element! } //7. check root roles to make sure role still exists @@ -390,7 +386,7 @@ public void addRolePermissionToGroupVerifyPermission() throws IOException { /*** * - * Post a group activity + * Post a group activity and make sure it can be read back only by group members * */ @Test @@ -438,8 +434,8 @@ public void postGroupActivity() throws IOException { //8. 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"}' + //{'{"actor":{"displayName":"fred","uuid":"2b70e83a-8a3f-11e4-9716-235107bcadb1","username":"fred"}, + // "verb":"post","content":"content"}' Entity payload = new Entity(); payload.put("displayName", "fred"); payload.put("uuid", user1.get("uuid")); @@ -447,21 +443,22 @@ public void postGroupActivity() throws IOException { Entity activity = new Entity(); activity.put("actor", payload); activity.put("verb", "post"); - activity.put("content", "content"); + String content = "content"; + activity.put("content", content); Entity activityResponse = this.app().collection("groups").uniqueID(groupName).connection("activities").post(activity); - assertEquals(activityResponse.get("content"), "content"); + assertEquals(activityResponse.get("content"), content); this.refreshIndex(); //9. delete the default role this.app().collection("role").uniqueID("Default").delete(); - +/* //10. add permissions to group: {"permission":"get,post,put,delete:/groups/${group}/**"}' payload = new Entity(); String permission = "get,post,put,delete:/groups/${group}/**"; payload.put("permission",permission); Entity permissionResponse = this.app().collection("groups").entity(group).connection("permissions").post(payload); assertEquals(permissionResponse.get("data"), permission); - +*/ //11. log user1 in, should then be using the app user's token not the admin token this.getAppUserToken(user1Username, password); @@ -473,7 +470,7 @@ public void postGroupActivity() throws IOException { boolean found = false; while (user1ActivityResponse.hasNext()) { Entity tempActivity = user1ActivityResponse.next(); - if (tempActivity.get("type").equals("activity") && tempActivity.get("content").equals("content")) { + if (tempActivity.get("type").equals("activity") && tempActivity.get("content").equals(content)) { found = true; } } @@ -487,7 +484,7 @@ public void postGroupActivity() throws IOException { found = false; while (user2ActivityResponse.hasNext()) { Entity tempActivity = user2ActivityResponse.next(); - if (tempActivity.get("type").equals("activity") && tempActivity.get("content").equals("content")) { + if (tempActivity.get("type").equals("activity") && tempActivity.get("content").equals(content)) { found = true; } } 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 158b6ad210..f9f99fcef2 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,9 +41,6 @@ public ApplicationsResource app(final String app){ return new ApplicationsResource( app, context ,this ); } - public ApplicationsResource token(){ - return new ApplicationsResource( "token", context ,this ); - } public void post(Map organization) {