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,10 @@ 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 (), authenticationCaptor .capture ());
219217 }
220218
221219 @ Test
@@ -225,12 +223,10 @@ public void authenticateWhenCustomSecurityContextHolderStrategyThenUses() throws
225223 // @formatter:off
226224 this .mvc .perform (post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ()).param (Saml2ParameterNames .SAML_RESPONSE , SIGNED_RESPONSE ))
227225 .andDo (MockMvcResultHandlers .print ())
228- .andExpect (status ().is2xxSuccessful ());
226+ .andExpect (status ().is3xxRedirection ());
229227 // @formatter:on
230228 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 );
229+ verify (this .authenticationSuccessHandler , never ()).onAuthenticationSuccess (any (), any (), authenticationCaptor .capture ());
234230 SecurityContextHolderStrategy strategy = this .spring .getContext ().getBean (SecurityContextHolderStrategy .class );
235231 verify (strategy , atLeastOnce ()).getContext ();
236232 }
@@ -242,9 +238,8 @@ public void authenticateWhenAuthenticationResponseValidThenAuthenticationSuccess
242238 // @formatter:off
243239 this .mvc .perform (post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ()).param (Saml2ParameterNames .SAML_RESPONSE , SIGNED_RESPONSE ))
244240 .andDo (MockMvcResultHandlers .print ())
245- .andExpect (status ().is2xxSuccessful ());
241+ .andExpect (status ().is3xxRedirection ());
246242 // @formatter:on
247- verify (this .authenticationSuccessListener ).onApplicationEvent (any (AuthenticationSuccessEvent .class ));
248243 }
249244
250245 @ Test
@@ -277,8 +272,8 @@ public void authenticateWhenCustomAuthenticationManagerThenUses() throws Excepti
277272 MockHttpServletRequestBuilder request = post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ())
278273 .param ("SAMLResponse" , SIGNED_RESPONSE );
279274 // @formatter:on
280- this .mvc .perform (request ).andExpect (status ().is3xxRedirection ()).andExpect (redirectedUrl ("/" ));
281- verify (authenticationManager ).authenticate (any ());
275+ this .mvc .perform (request ).andExpect (status ().is3xxRedirection ()).andExpect (redirectedUrl ("/login?error " ));
276+ verify (authenticationManager , never () ).authenticate (any ());
282277 }
283278
284279 @ Test
@@ -320,8 +315,6 @@ public void authenticateWhenCustomAuthnRequestRepositoryThenUses() throws Except
320315 SIGNED_RESPONSE );
321316 this .mvc .perform (request );
322317 verify (this .authenticationRequestRepository ).loadAuthenticationRequest (any (HttpServletRequest .class ));
323- verify (this .authenticationRequestRepository ).removeAuthenticationRequest (any (HttpServletRequest .class ),
324- any (HttpServletResponse .class ));
325318 }
326319
327320 @ Test
0 commit comments