45
45
import org .springframework .mock .web .MockHttpServletRequest ;
46
46
import org .springframework .mock .web .MockHttpServletResponse ;
47
47
import org .springframework .security .authentication .AuthenticationProvider ;
48
+ import org .springframework .security .authentication .SecurityAssertions ;
48
49
import org .springframework .security .authentication .event .AuthenticationSuccessEvent ;
49
50
import org .springframework .security .config .Customizer ;
50
51
import org .springframework .security .config .ObjectPostProcessor ;
@@ -217,10 +218,9 @@ public void oauth2Login() throws Exception {
217
218
Authentication authentication = this .securityContextRepository
218
219
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
219
220
.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 );
224
224
}
225
225
226
226
@ Test
@@ -234,10 +234,9 @@ public void requestWhenCustomSecurityContextHolderStrategyThenUses() throws Exce
234
234
Authentication authentication = this .securityContextRepository
235
235
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
236
236
.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 );
241
240
SecurityContextHolderStrategy strategy = this .context .getBean (SecurityContextHolderStrategy .class );
242
241
verify (strategy , atLeastOnce ()).getDeferredContext ();
243
242
SecurityContextChangedListener listener = this .context .getBean (SecurityContextChangedListener .class );
@@ -255,10 +254,9 @@ public void requestWhenOauth2LoginInLambdaThenAuthenticationContainsOauth2UserAu
255
254
Authentication authentication = this .securityContextRepository
256
255
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
257
256
.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 );
262
260
}
263
261
264
262
// gh-6009
@@ -296,9 +294,7 @@ public void oauth2LoginCustomWithConfigurer() throws Exception {
296
294
Authentication authentication = this .securityContextRepository
297
295
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
298
296
.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" );
302
298
}
303
299
304
300
@ Test
@@ -317,9 +313,7 @@ public void oauth2LoginCustomWithBeanRegistration() throws Exception {
317
313
Authentication authentication = this .securityContextRepository
318
314
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
319
315
.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" );
323
317
}
324
318
325
319
@ Test
@@ -338,9 +332,7 @@ public void oauth2LoginCustomWithUserServiceBeanRegistration() throws Exception
338
332
Authentication authentication = this .securityContextRepository
339
333
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
340
334
.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" );
344
336
}
345
337
346
338
// gh-5488
@@ -361,10 +353,9 @@ public void oauth2LoginConfigLoginProcessingUrl() throws Exception {
361
353
Authentication authentication = this .securityContextRepository
362
354
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
363
355
.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 );
368
359
}
369
360
370
361
// gh-5521
@@ -570,10 +561,7 @@ public void oidcLogin() throws Exception {
570
561
Authentication authentication = this .securityContextRepository
571
562
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
572
563
.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 );
577
565
}
578
566
579
567
@ Test
@@ -593,9 +581,7 @@ public void requestWhenOauth2LoginInLambdaAndOidcThenAuthenticationContainsOidcU
593
581
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
594
582
.getAuthentication ();
595
583
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 );
599
585
}
600
586
601
587
@ Test
@@ -614,9 +600,7 @@ public void oidcLoginCustomWithConfigurer() throws Exception {
614
600
Authentication authentication = this .securityContextRepository
615
601
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
616
602
.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" );
620
604
}
621
605
622
606
@ Test
@@ -635,9 +619,7 @@ public void oidcLoginCustomWithBeanRegistration() throws Exception {
635
619
Authentication authentication = this .securityContextRepository
636
620
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
637
621
.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" );
641
623
}
642
624
643
625
@ Test
@@ -690,11 +672,7 @@ public void oidcLoginWhenOAuth2ClientBeansConfiguredThenNotShared() throws Excep
690
672
Authentication authentication = this .securityContextRepository
691
673
.loadContext (new HttpRequestResponseHolder (this .request , this .response ))
692
674
.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 );
698
676
// Ensure shared objects set for OAuth2 Client are not used
699
677
ClientRegistrationRepository clientRegistrationRepository = this .spring .getContext ()
700
678
.getBean (ClientRegistrationRepository .class );
0 commit comments