running (getFakeApplication(),
new Runnable() {
public void run() {
try {
String sFakeRole = getRouteAddress();
FakeRequest requestCreation = new FakeRequest(POST, sFakeRole);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
Result result = route(requestCreation);
assertRoute(result, "testRoleCreate.create", Status.CREATED, null, true);
//creates one user in this Role
String sFakeCreateUser = getFakeUserCreationAddress();
requestCreation = new FakeRequest(POST, sFakeCreateUser);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree("{\"username\":\""+userName+"\","
+ "\"password\":\"test\","
+ "\"role\":\""+ roleName +"\"}");
requestCreation = requestCreation.withJsonBody(actualObj);
requestCreation = requestCreation.withHeader("Content-Type", "application/json");
result = route(requestCreation);
assertRoute(result, "testRoleCreate.createUser", Status.CREATED, null, true);
//checks the user
String sFakeCheckUser = getFakeUserAddress();
requestCreation = new FakeRequest(GET, sFakeCheckUser);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
result = route(requestCreation);
assertRoute(result, "testRoleCreate.checkUser", Status.OK, "\"name\":\""+roleName+"\"", true);
//admin creates a document
String sFakeCollection = new AdminCollectionFunctionalTest().routeCreateCollection();
result = routeCreateDocumentAsAdmin("/document/"+sFakeCollection);
assertRoute(result, "adminCreateDocument", Status.OK, null, true);
String sUUID = getUuid(result);
//and gives to the registered user the read grant
requestCreation = new FakeRequest(PUT, "/document/"+sFakeCollection + "/" + sUUID + "/read/role/registered");
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
result = route(requestCreation);
assertRoute(result, "testRoleCreate.AdminGivesGrant", Status.OK, null, false);
//now the new user should see it
requestCreation = new FakeRequest(GET, "/document/"+sFakeCollection + "/" + sUUID);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(userName, "test"));
result = route(requestCreation);
assertRoute(result, "testRoleCreate.UserShouldRead", Status.OK, "id\":\""+sUUID, true);
//registered user should see it as well
String sFakeRegUser = "regUser_"+UUID.randomUUID();
requestCreation = new FakeRequest(POST, "/admin/user");
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
mapper = new ObjectMapper();
actualObj = mapper.readTree("{\"username\":\""+sFakeRegUser+"\","
+ "\"password\":\"test\","
+ "\"role\":\"registered\"}");
requestCreation = requestCreation.withJsonBody(actualObj);
requestCreation = requestCreation.withHeader("Content-Type", "application/json");
result = route(requestCreation);
assertRoute(result, "testRoleCreate.createRegUser", Status.CREATED, null, true);
requestCreation = new FakeRequest(GET, "/document/"+sFakeCollection + "/" + sUUID);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(sFakeRegUser, "test"));
result = route(requestCreation);
assertRoute(result, "testRoleCreate.RegShouldRead", Status.OK, "id\":\""+sUUID, true);
//user create a document and gives grant to its role
result = routeCreateDocumentAsUser(sFakeCollection,userName,"test");
assertRoute(result, "testRoleCreate.userCreateDoc", Status.OK,null, false);
sUUID = getUuid(result);
requestCreation = new FakeRequest(PUT, "/document/"+sFakeCollection + "/" + sUUID + "/read/role/"+roleName);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(userName, "test"));
result = route(requestCreation);
assertRoute(result, "testRoleCreate.UserGivesGrant", Status.OK, null, false);
//registered user should not see it
requestCreation = new FakeRequest(GET, "/document/"+sFakeCollection + "/" + sUUID);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(sFakeRegUser, "test"));
result = route(requestCreation);
assertRoute(result, "testRoleCreate.RegShouldNotSeeRead", Status.NOT_FOUND, null, false);
//
//finally... drop the new role
requestCreation = new FakeRequest(DELETE, sFakeRole);
requestCreation = requestCreation.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
requestCreation = requestCreation.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
result = route(requestCreation);
assertRoute(result, "testRoleCreate.drop_final", Status.OK, null, false);
}catch (Exception e) {