I test my Controller with Mockito. test my method in controller:
@RequestMapping("/dispatcher")
public String disp(final HttpServletRequest request) {
if (request.isUserInRole("USER")) {
return "hello";
} else if (request.isUserInRole("ADMIN")) {
return "redirect:listUsers.htm";
}
throw new RuntimeException("This role is not registrated");
}
My test method:
@Test
public void testDispatcher() throws Exception {
User user = userDao.findByLogin("log1");
TestingAuthenticationToken testingAuthenticationToken = new TestingAuthenticationToken(user, AuthorityUtils.createAuthorityList("USER"));
SecurityContextHolder.getContext().setAuthentication(testingAuthenticationToken);
System.out.println(testingAuthenticationToken);
mockMvc.perform(post("/dispatcher")
.principal(testingAuthenticationToken))
.andExpect(status().isOk());
}
Error: org.springframework.security.authentication.TestingAuthenticationToken@bf72b5bd: Principal: User [id=1, firstName=name1, lastName=surname1, login=log1, email=email1@gmail.com, password=psw1, birthDate=1980-01-01 00:00:00.0, role=[id=1, name=USER]]; Credentials: [PROTECTED]; Authenticated: false; Details: null; Not granted any authorities
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.RuntimeException: This role is not registrated
How i can set Role for method request.isUserInRole(); or maybe has another solution to my issue
Aucun commentaire:
Enregistrer un commentaire