4545import org .springframework .mock .web .MockHttpServletRequest ;
4646import org .springframework .mock .web .MockHttpServletResponse ;
4747import org .springframework .security .authentication .AuthenticationProvider ;
48+ import org .springframework .security .authentication .SecurityAssertions ;
4849import org .springframework .security .authentication .event .AuthenticationSuccessEvent ;
4950import org .springframework .security .config .Customizer ;
5051import org .springframework .security .config .ObjectPostProcessor ;
@@ -217,10 +218,9 @@ public void oauth2Login() throws Exception {
217218 Authentication authentication = this .securityContextRepository
218219 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
219220 .getAuthentication ();
220- assertThat (authentication .getAuthorities ()).hasSize (1 );
221- assertThat (authentication .getAuthorities ()).first ()
222- .isInstanceOf (OAuth2UserAuthority .class )
223- .hasToString ("OAUTH2_USER" );
221+ SecurityAssertions .assertThat (authentication )
222+ .hasAuthority ("OAUTH2_USER" )
223+ .isInstanceOf (OAuth2UserAuthority .class );
224224 }
225225
226226 @ Test
@@ -234,10 +234,9 @@ public void requestWhenCustomSecurityContextHolderStrategyThenUses() throws Exce
234234 Authentication authentication = this .securityContextRepository
235235 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
236236 .getAuthentication ();
237- assertThat (authentication .getAuthorities ()).hasSize (1 );
238- assertThat (authentication .getAuthorities ()).first ()
239- .isInstanceOf (OAuth2UserAuthority .class )
240- .hasToString ("OAUTH2_USER" );
237+ SecurityAssertions .assertThat (authentication )
238+ .hasAuthority ("OAUTH2_USER" )
239+ .isInstanceOf (OAuth2UserAuthority .class );
241240 SecurityContextHolderStrategy strategy = this .context .getBean (SecurityContextHolderStrategy .class );
242241 verify (strategy , atLeastOnce ()).getDeferredContext ();
243242 SecurityContextChangedListener listener = this .context .getBean (SecurityContextChangedListener .class );
@@ -255,10 +254,9 @@ public void requestWhenOauth2LoginInLambdaThenAuthenticationContainsOauth2UserAu
255254 Authentication authentication = this .securityContextRepository
256255 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
257256 .getAuthentication ();
258- assertThat (authentication .getAuthorities ()).hasSize (1 );
259- assertThat (authentication .getAuthorities ()).first ()
260- .isInstanceOf (OAuth2UserAuthority .class )
261- .hasToString ("OAUTH2_USER" );
257+ SecurityAssertions .assertThat (authentication )
258+ .hasAuthority ("OAUTH2_USER" )
259+ .isInstanceOf (OAuth2UserAuthority .class );
262260 }
263261
264262 // gh-6009
@@ -296,9 +294,7 @@ public void oauth2LoginCustomWithConfigurer() throws Exception {
296294 Authentication authentication = this .securityContextRepository
297295 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
298296 .getAuthentication ();
299- assertThat (authentication .getAuthorities ()).hasSize (2 );
300- assertThat (authentication .getAuthorities ()).first ().hasToString ("OAUTH2_USER" );
301- assertThat (authentication .getAuthorities ()).last ().hasToString ("ROLE_OAUTH2_USER" );
297+ SecurityAssertions .assertThat (authentication ).hasAuthorities ("OAUTH2_USER" , "ROLE_OAUTH2_USER" );
302298 }
303299
304300 @ Test
@@ -317,9 +313,7 @@ public void oauth2LoginCustomWithBeanRegistration() throws Exception {
317313 Authentication authentication = this .securityContextRepository
318314 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
319315 .getAuthentication ();
320- assertThat (authentication .getAuthorities ()).hasSize (2 );
321- assertThat (authentication .getAuthorities ()).first ().hasToString ("OAUTH2_USER" );
322- assertThat (authentication .getAuthorities ()).last ().hasToString ("ROLE_OAUTH2_USER" );
316+ SecurityAssertions .assertThat (authentication ).hasAuthorities ("OAUTH2_USER" , "ROLE_OAUTH2_USER" );
323317 }
324318
325319 @ Test
@@ -338,9 +332,7 @@ public void oauth2LoginCustomWithUserServiceBeanRegistration() throws Exception
338332 Authentication authentication = this .securityContextRepository
339333 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
340334 .getAuthentication ();
341- assertThat (authentication .getAuthorities ()).hasSize (2 );
342- assertThat (authentication .getAuthorities ()).first ().hasToString ("OAUTH2_USER" );
343- assertThat (authentication .getAuthorities ()).last ().hasToString ("ROLE_OAUTH2_USER" );
335+ SecurityAssertions .assertThat (authentication ).hasAuthorities ("OAUTH2_USER" , "ROLE_OAUTH2_USER" );
344336 }
345337
346338 // gh-5488
@@ -361,10 +353,9 @@ public void oauth2LoginConfigLoginProcessingUrl() throws Exception {
361353 Authentication authentication = this .securityContextRepository
362354 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
363355 .getAuthentication ();
364- assertThat (authentication .getAuthorities ()).hasSize (1 );
365- assertThat (authentication .getAuthorities ()).first ()
366- .isInstanceOf (OAuth2UserAuthority .class )
367- .hasToString ("OAUTH2_USER" );
356+ SecurityAssertions .assertThat (authentication )
357+ .hasAuthority ("OAUTH2_USER" )
358+ .isInstanceOf (OAuth2UserAuthority .class );
368359 }
369360
370361 // gh-5521
@@ -570,10 +561,7 @@ public void oidcLogin() throws Exception {
570561 Authentication authentication = this .securityContextRepository
571562 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
572563 .getAuthentication ();
573- assertThat (authentication .getAuthorities ()).hasSize (1 );
574- assertThat (authentication .getAuthorities ()).first ()
575- .isInstanceOf (OidcUserAuthority .class )
576- .hasToString ("OIDC_USER" );
564+ SecurityAssertions .assertThat (authentication ).hasAuthority ("OIDC_USER" ).isInstanceOf (OidcUserAuthority .class );
577565 }
578566
579567 @ Test
@@ -593,9 +581,7 @@ public void requestWhenOauth2LoginInLambdaAndOidcThenAuthenticationContainsOidcU
593581 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
594582 .getAuthentication ();
595583 assertThat (authentication .getAuthorities ()).hasSize (1 );
596- assertThat (authentication .getAuthorities ()).first ()
597- .isInstanceOf (OidcUserAuthority .class )
598- .hasToString ("OIDC_USER" );
584+ SecurityAssertions .assertThat (authentication ).hasAuthority ("OIDC_USER" ).isInstanceOf (OidcUserAuthority .class );
599585 }
600586
601587 @ Test
@@ -614,9 +600,7 @@ public void oidcLoginCustomWithConfigurer() throws Exception {
614600 Authentication authentication = this .securityContextRepository
615601 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
616602 .getAuthentication ();
617- assertThat (authentication .getAuthorities ()).hasSize (2 );
618- assertThat (authentication .getAuthorities ()).first ().hasToString ("OIDC_USER" );
619- assertThat (authentication .getAuthorities ()).last ().hasToString ("ROLE_OIDC_USER" );
603+ SecurityAssertions .assertThat (authentication ).hasAuthorities ("OIDC_USER" , "ROLE_OIDC_USER" );
620604 }
621605
622606 @ Test
@@ -635,9 +619,7 @@ public void oidcLoginCustomWithBeanRegistration() throws Exception {
635619 Authentication authentication = this .securityContextRepository
636620 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
637621 .getAuthentication ();
638- assertThat (authentication .getAuthorities ()).hasSize (2 );
639- assertThat (authentication .getAuthorities ()).first ().hasToString ("OIDC_USER" );
640- assertThat (authentication .getAuthorities ()).last ().hasToString ("ROLE_OIDC_USER" );
622+ SecurityAssertions .assertThat (authentication ).hasAuthorities ("OIDC_USER" , "ROLE_OIDC_USER" );
641623 }
642624
643625 @ Test
@@ -690,11 +672,7 @@ public void oidcLoginWhenOAuth2ClientBeansConfiguredThenNotShared() throws Excep
690672 Authentication authentication = this .securityContextRepository
691673 .loadContext (new HttpRequestResponseHolder (this .request , this .response ))
692674 .getAuthentication ();
693- assertThat (authentication .getAuthorities ()).hasSize (1 );
694- assertThat (authentication .getAuthorities ()).first ()
695- .isInstanceOf (OidcUserAuthority .class )
696- .hasToString ("OIDC_USER" );
697-
675+ SecurityAssertions .assertThat (authentication ).hasAuthority ("OIDC_USER" ).isInstanceOf (OidcUserAuthority .class );
698676 // Ensure shared objects set for OAuth2 Client are not used
699677 ClientRegistrationRepository clientRegistrationRepository = this .spring .getContext ()
700678 .getBean (ClientRegistrationRepository .class );
0 commit comments