Description
Currently there is no way to provide a Principal (user) and Roles for mock tests.
Sven Pfeiffer provided the following impl on the mainlig list which can be added to the Mock API:
====================
public class AuthenticatedMockRequest extends MockRequest {
MockPrincipal userPrincipal;
public AuthenticatedMockRequest(MockPrincipal userPrincipal)
{ this.userPrincipal = userPrincipal; } @Override
public boolean isUserInRole(String name)
@Override
public Principal getUserPrincipal()
}
and
MockPrincipal:
public class MockPrincipal implements Principal {
private String name;
private List<String> roles = new ArrayList<String>();
public MockPrincipal(String userName)
{ this.name = userName; }public MockPrincipal(String userName, List<String> roles)
{ this.name = userName; this.roles = roles; } @Override
public String getName()
public List<String> getRoles()
{ return roles; } public void setRoles(String... newRoles) {
roles.clear();
for (String role : newRoles)
}
}
And I added the following lines to my testcase:
MockPrincipal principal = new MockPrincipal("admin");
principal.setRoles("admin", "user");
container.setRequest(new AuthenticatedMockRequest(principal));