}
@SuppressWarnings("unchecked")
@Test
public void assignPrivilegeToRoleTest() {
SecurityAPI api = new SecurityAPI();
APIResponse response = api.addPrivilege("testprivilege",
helper.loginWithPrivilege(Privileges.EDIT_PRIVILEGE));
assertTrue(response.statusCode == Status.SUCCESS);
response = api.addRole("testrole",
helper.loginWithPrivilege(Privileges.EDIT_ROLE));
assertTrue(response.statusCode == Status.SUCCESS);
//should have the right privilege to assign
response = api.assignPrivilegeToRole("testrole",
"testprivilege", null);
assertTrue(response.statusCode == Status.ERROR_LOGIN_REQUIRED);
response = api.assignPrivilegeToRole("testrole",
"testprivilege", helper.loginAsInvalidUser());
assertTrue(response.statusCode == Status.ERROR_INVALID_USER);
response = api.assignPrivilegeToRole("testrole",
"testprivilege", helper.loginAsValidUser());
assertTrue(response.statusCode == Status.ERROR_INSUFFICIENT_PERMISSION);
response = api.assignPrivilegeToRole("testrole",
"testprivilege", helper.loginWithPrivilege(Privileges.EDIT_ROLE));
assertTrue(response.statusCode == Status.SUCCESS);
response = api.getAllRoles();
TreeSet<RoleProp> allRoles = (TreeSet<RoleProp>) response.object;
assertTrue(allRoles.first().privileges.contains("TESTPRIVILEGE"));
//cannot assign non existant privilege/role
response = api.assignPrivilegeToRole("testrole",
"non existant privilege", helper.loginWithPrivilege(Privileges.EDIT_ROLE));
assertTrue(response.statusCode == Status.ERROR_RESOURCE_DOES_NOT_EXIST);
response = api.assignPrivilegeToRole("non existant role",
"testprivilege", helper.loginWithPrivilege(Privileges.EDIT_ROLE));
assertTrue(response.statusCode == Status.ERROR_RESOURCE_DOES_NOT_EXIST);
//admin can assign as well
api.addPrivilege("testprivilege2", helper.loginAsSuperUser());
response = api.assignPrivilegeToRole("testrole",
"testprivilege2", helper.loginAsSuperUser());
assertTrue(response.statusCode == Status.SUCCESS);
response = api.getAllRoles();
allRoles = (TreeSet<RoleProp>) response.object;
assertTrue(allRoles.first().privileges.contains("TESTPRIVILEGE2"));
}