11package example ;
22
3+ import java .time .Instant ;
4+
35import org .junit .jupiter .api .Test ;
46
57import org .springframework .beans .factory .annotation .Autowired ;
68import org .springframework .boot .test .context .SpringBootTest ;
79import org .springframework .boot .webmvc .test .autoconfigure .AutoConfigureMockMvc ;
10+ import org .springframework .security .core .authority .FactorGrantedAuthority ;
11+ import org .springframework .security .core .userdetails .User ;
12+ import org .springframework .security .core .userdetails .UserDetails ;
813import org .springframework .security .test .context .support .WithMockUser ;
914import org .springframework .test .context .ActiveProfiles ;
1015import org .springframework .test .web .servlet .MockMvc ;
1116
1217import static org .springframework .security .core .authority .FactorGrantedAuthority .OTT_AUTHORITY ;
1318import static org .springframework .security .core .authority .FactorGrantedAuthority .PASSWORD_AUTHORITY ;
19+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .user ;
1420import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
1521import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .redirectedUrl ;
1622import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
@@ -25,33 +31,73 @@ class DefaultConfigTests {
2531
2632 @ Test
2733 void indexWhenUnauthenticatedThenRedirectsToLogin () throws Exception {
28- this .mvc .perform (get ("/" ))
29- .andExpect (status ().is3xxRedirection ())
30- .andExpect (redirectedUrl ("http://localhost/login" ));
34+ this .mvc .perform (get ("/" )).andExpect (status ().is3xxRedirection ()).andExpect (redirectedUrl ("/login" ));
3135 }
3236
3337 @ Test
3438 @ WithMockUser
3539 void indexWhenAuthenticatedButNoFactorsThenRedirectsToLogin () throws Exception {
3640 this .mvc .perform (get ("/" ))
3741 .andExpect (status ().is3xxRedirection ())
38- .andExpect (redirectedUrl ("http://localhost/login?factor=password" ));
42+ .andExpect (redirectedUrl (
43+ "/login?factor.type=password&factor.type=ott&factor.reason=missing&factor.reason=missing" ));
3944 }
4045
4146 @ Test
4247 @ WithMockUser (authorities = OTT_AUTHORITY )
4348 void indexWhenAuthenticatedWithX509ThenRedirectsToLogin () throws Exception {
4449 this .mvc .perform (get ("/" ))
4550 .andExpect (status ().is3xxRedirection ())
46- .andExpect (redirectedUrl ("http://localhost/ login?factor=password" ));
51+ .andExpect (redirectedUrl ("/ login?factor.type =password&factor.reason=missing " ));
4752 }
4853
4954 @ Test
5055 @ WithMockUser (authorities = PASSWORD_AUTHORITY )
5156 void indexWhenAuthenticatedWithPasswordThenRedirectsToOtt () throws Exception {
5257 this .mvc .perform (get ("/" ))
5358 .andExpect (status ().is3xxRedirection ())
54- .andExpect (redirectedUrl ("http://localhost/login?factor=ott" ));
59+ .andExpect (redirectedUrl ("/login?factor.type=ott&factor.reason=missing" ));
60+ }
61+
62+ @ Test
63+ void profileWhenAuthenticatedWithPasswordThenRedirectsToOtt () throws Exception {
64+ UserDetails user = User .withDefaultPasswordEncoder ()
65+ .username ("user" )
66+ .authorities (FactorGrantedAuthority .fromAuthority (PASSWORD_AUTHORITY ))
67+ .build ();
68+ this .mvc .perform (get ("/profile" ).with (user (user )))
69+ .andExpect (status ().is3xxRedirection ())
70+ .andExpect (redirectedUrl ("/login?factor.type=ott&factor.reason=missing" ));
71+ }
72+
73+ @ Test
74+ void profileWhenAuthenticatedWithOttThenRedirectsToPassword () throws Exception {
75+ UserDetails user = User .withDefaultPasswordEncoder ()
76+ .username ("user" )
77+ .authorities (FactorGrantedAuthority .fromAuthority (OTT_AUTHORITY ))
78+ .build ();
79+ this .mvc .perform (get ("/profile" ).with (user (user )))
80+ .andExpect (status ().is3xxRedirection ())
81+ .andExpect (redirectedUrl ("/login?factor.type=password&factor.reason=missing" ));
82+ }
83+
84+ @ Test
85+ void profileWhenExpiredPasswordAuthorityThenRedirectsToPassword () throws Exception {
86+ FactorGrantedAuthority expiredPassword = FactorGrantedAuthority .withAuthority (PASSWORD_AUTHORITY )
87+ .issuedAt (Instant .now ().minusSeconds (600 ))
88+ .build ();
89+ FactorGrantedAuthority ott = FactorGrantedAuthority .fromAuthority (OTT_AUTHORITY );
90+ UserDetails user = User .withDefaultPasswordEncoder ().username ("user" ).authorities (expiredPassword , ott ).build ();
91+ this .mvc .perform (get ("/profile" ).with (user (user )))
92+ .andExpect (redirectedUrl ("/login?factor.type=password&factor.reason=expired" ));
93+ }
94+
95+ @ Test
96+ void profileWhenAuthenticatedWithPasswordAndOttThenAllows () throws Exception {
97+ FactorGrantedAuthority password = FactorGrantedAuthority .fromAuthority (PASSWORD_AUTHORITY );
98+ FactorGrantedAuthority ott = FactorGrantedAuthority .fromAuthority (OTT_AUTHORITY );
99+ UserDetails user = User .withDefaultPasswordEncoder ().username ("user" ).authorities (password , ott ).build ();
100+ this .mvc .perform (get ("/profile" ).with (user (user ))).andExpect (status ().isOk ());
55101 }
56102
57103}
0 commit comments