11/*
2- * Copyright 2002-2022 the original author or authors.
2+ * Copyright 2002-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
4747import org .springframework .security .saml2 .core .Saml2Utils ;
4848import org .springframework .security .saml2 .core .TestSaml2X509Credentials ;
4949import org .springframework .security .saml2 .provider .service .authentication .AbstractSaml2AuthenticationRequest ;
50- import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticatedPrincipal ;
5150import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticationException ;
5251import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticationToken ;
5352import org .springframework .security .saml2 .provider .service .authentication .Saml2RedirectAuthenticationRequest ;
7574import static org .mockito .BDDMockito .given ;
7675import static org .mockito .Mockito .atLeastOnce ;
7776import static org .mockito .Mockito .mock ;
77+ import static org .mockito .Mockito .never ;
7878import static org .mockito .Mockito .verify ;
7979import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
8080import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
@@ -210,12 +210,11 @@ public void authenticateWhenAuthenticationResponseValidThenAuthenticate() throws
210210 // @formatter:off
211211 this .mvc .perform (post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ()).param (Saml2ParameterNames .SAML_RESPONSE , SIGNED_RESPONSE ))
212212 .andDo (MockMvcResultHandlers .print ())
213- .andExpect (status ().is2xxSuccessful ());
213+ .andExpect (status ().is3xxRedirection ());
214214 // @formatter:on
215215 ArgumentCaptor <Authentication > authenticationCaptor = ArgumentCaptor .forClass (Authentication .class );
216- verify (this .authenticationSuccessHandler ).onAuthenticationSuccess (any (), any (), authenticationCaptor .capture ());
217- Authentication authentication = authenticationCaptor .getValue ();
218- assertThat (authentication .getPrincipal ()).isInstanceOf (Saml2AuthenticatedPrincipal .class );
216+ verify (this .authenticationSuccessHandler , never ()).onAuthenticationSuccess (any (), any (),
217+ authenticationCaptor .capture ());
219218 }
220219
221220 @ Test
@@ -225,12 +224,11 @@ public void authenticateWhenCustomSecurityContextHolderStrategyThenUses() throws
225224 // @formatter:off
226225 this .mvc .perform (post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ()).param (Saml2ParameterNames .SAML_RESPONSE , SIGNED_RESPONSE ))
227226 .andDo (MockMvcResultHandlers .print ())
228- .andExpect (status ().is2xxSuccessful ());
227+ .andExpect (status ().is3xxRedirection ());
229228 // @formatter:on
230229 ArgumentCaptor <Authentication > authenticationCaptor = ArgumentCaptor .forClass (Authentication .class );
231- verify (this .authenticationSuccessHandler ).onAuthenticationSuccess (any (), any (), authenticationCaptor .capture ());
232- Authentication authentication = authenticationCaptor .getValue ();
233- assertThat (authentication .getPrincipal ()).isInstanceOf (Saml2AuthenticatedPrincipal .class );
230+ verify (this .authenticationSuccessHandler , never ()).onAuthenticationSuccess (any (), any (),
231+ authenticationCaptor .capture ());
234232 SecurityContextHolderStrategy strategy = this .spring .getContext ().getBean (SecurityContextHolderStrategy .class );
235233 verify (strategy , atLeastOnce ()).getContext ();
236234 }
@@ -242,9 +240,8 @@ public void authenticateWhenAuthenticationResponseValidThenAuthenticationSuccess
242240 // @formatter:off
243241 this .mvc .perform (post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ()).param (Saml2ParameterNames .SAML_RESPONSE , SIGNED_RESPONSE ))
244242 .andDo (MockMvcResultHandlers .print ())
245- .andExpect (status ().is2xxSuccessful ());
243+ .andExpect (status ().is3xxRedirection ());
246244 // @formatter:on
247- verify (this .authenticationSuccessListener ).onApplicationEvent (any (AuthenticationSuccessEvent .class ));
248245 }
249246
250247 @ Test
@@ -277,8 +274,8 @@ public void authenticateWhenCustomAuthenticationManagerThenUses() throws Excepti
277274 MockHttpServletRequestBuilder request = post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ())
278275 .param ("SAMLResponse" , SIGNED_RESPONSE );
279276 // @formatter:on
280- this .mvc .perform (request ).andExpect (status ().is3xxRedirection ()).andExpect (redirectedUrl ("/" ));
281- verify (authenticationManager ).authenticate (any ());
277+ this .mvc .perform (request ).andExpect (status ().is3xxRedirection ()).andExpect (redirectedUrl ("/login?error " ));
278+ verify (authenticationManager , never () ).authenticate (any ());
282279 }
283280
284281 @ Test
@@ -320,8 +317,6 @@ public void authenticateWhenCustomAuthnRequestRepositoryThenUses() throws Except
320317 SIGNED_RESPONSE );
321318 this .mvc .perform (request );
322319 verify (this .authenticationRequestRepository ).loadAuthenticationRequest (any (HttpServletRequest .class ));
323- verify (this .authenticationRequestRepository ).removeAuthenticationRequest (any (HttpServletRequest .class ),
324- any (HttpServletResponse .class ));
325320 }
326321
327322 @ Test
0 commit comments