пытаюсь написать модульные тесты, которые проверяют superUserPrivilege. Вот код
private void checkSuperUserPrivilege(Role r) {
if (r.getRole().equals(RoleConstants.SUPERUSER)
&& !Context.hasPrivilege(PrivilegeConstants.ASSIGN_SYSTEM_DEVELOPER_ROLE)) {
throw new APIException("User.you.must.have.role", new Object[] { RoleConstants.SUPERUSER
}
}
вот мой тест
@Test
public void createUser_shouldNotAllowAssigningSuperUserRoleIfCurrentUserDoesNotHaveAssignSystemDeveloperPrivileges() throws IllegalAccessException {
//setup the currently logged in user
User currentUser = new User();
Role userRole = new Role("User Adder");
userRole.setRole(RoleConstants.AUTHENTICATED);
userRole.addPrivilege(new Privilege("Add Users"));
currentUser.addRole(userRole);
// setup our expected exception
// we expect this to fail because the currently logged-in user lacks a privilege to be
// assigned to the new user
expectedException.expect(APIException.class);
expectedException.expectMessage("You must have the role '{0}' in order to assign it");
// set current user to the user defined above
withCurrentUser(currentUser()->{
// create a role to assign to the new user
//here the logic is that the current user cannot assign a user to superuser role because he
lacks AssignSystemDeveloper privliges
Role role= new Role("add user");
role.setRole(RoleConstants.SUPERUSER);
// add a privilege to the role
role.hasPrivilege(PrivilegeConstants.ASSIGN_SYSTEM_DEVELOPER_ROLE);
// create our new user object with the required fields
User u = new User();
u.setPerson(new Person());
// assign the specified role to the user
u.addName(new PersonName("yon", "g", "sharf"));
u.setUsername("sharf");
u.getPerson().setGender("M");
u.isSuperUser();
u.addRole(role);
// here we expect the exception to be thrown
userService.createUser(u, "something");
});
}
но здесь тест все равно не пройден, я все еще в замешательстве. мне нужна помощь как go за это спасибо