'How can I test the Remember Me checkbox
I am currently setting up a service allowing user login on a web portal, for comfort I have added the "Remember Me" function using Spring Security by using a persistent approach that stores tokens in DB for a defined time.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/h2-console/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.rememberMe()
.rememberMeCookieName("ugeddit-cookie")
.tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(172800);
http.csrf().disable();
http.headers().frameOptions().disable();
}
How is it possible to write a test covering this case, without adding too many dependencies to the project?
I can see at the moment how to add a User and simulate a connection to the formLogin but I can't see what method can allow me to simulate the activation of the "Remember Me" checkbox
@Test
void whenRememberedUser_TokenIsAddedInDB() throws Exception {
User user = new User(1L, "test", "$2a$10$/fAVkMGa6gHeM6Q0VHJyFOTznAo3qrXbj5CF0ZvMhxVldxYbxnk/6", "ROLE_USER");
userRepo.save(user);
mvc.perform(formLogin("/login").user("test").password("spring")).andExpect(authenticated());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|