Skip to content

Commit c12ced6

Browse files
philwebbrwinch
authored andcommitted
Migrate SwitchUserWebFilterTests AssertJ
Replace the JUnit Assertions used in `SwitchUserWebFilterTests` with AssertJ. This test appears to have been missed during the original AssertJ migration. Issue gh-8945
1 parent b33cb10 commit c12ced6

1 file changed

Lines changed: 26 additions & 35 deletions

File tree

‎web/src/test/java/org/springframework/security/web/server/authentication/SwitchUserWebFilterTests.java‎

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@
5454
import org.springframework.web.server.WebFilterChain;
5555

5656
import static org.assertj.core.api.Assertions.assertThat;
57-
import static org.junit.Assert.assertEquals;
58-
import static org.junit.Assert.assertSame;
59-
import static org.junit.Assert.assertTrue;
60-
import static org.junit.Assert.fail;
57+
import static org.assertj.core.api.Assertions.fail;
6158
import static org.mockito.ArgumentMatchers.any;
6259
import static org.mockito.ArgumentMatchers.eq;
6360
import static org.mockito.Mockito.mock;
@@ -155,18 +152,15 @@ public void switchUser() {
155152

156153
final Authentication switchUserAuthentication = authenticationCaptor.getValue();
157154

158-
assertSame(savedSecurityContext.getAuthentication(), switchUserAuthentication);
159-
160-
assertEquals("username should point to the switched user", targetUsername, switchUserAuthentication.getName());
161-
assertTrue("switchAuthentication should contain SwitchUserGrantedAuthority", switchUserAuthentication
162-
.getAuthorities().stream().anyMatch(a -> a instanceof SwitchUserGrantedAuthority));
163-
assertTrue("new authentication should get new role ", switchUserAuthentication.getAuthorities().stream()
164-
.map(GrantedAuthority::getAuthority).anyMatch(a -> a.equals(ROLE_PREVIOUS_ADMINISTRATOR)));
165-
assertEquals("SwitchUserGrantedAuthority should contain the original authentication",
166-
originalAuthentication.getName(),
155+
assertThat(switchUserAuthentication).isSameAs(savedSecurityContext.getAuthentication());
156+
assertThat(switchUserAuthentication.getName()).isEqualTo(targetUsername);
157+
assertThat(switchUserAuthentication.getAuthorities()).anyMatch(SwitchUserGrantedAuthority.class::isInstance);
158+
assertThat(switchUserAuthentication.getAuthorities())
159+
.anyMatch((a) -> a.getAuthority().contains(ROLE_PREVIOUS_ADMINISTRATOR));
160+
assertThat(
167161
switchUserAuthentication.getAuthorities().stream().filter(a -> a instanceof SwitchUserGrantedAuthority)
168-
.map(a -> ((SwitchUserGrantedAuthority) a).getSource()).map(Principal::getName).findFirst()
169-
.orElse(null));
162+
.map(a -> ((SwitchUserGrantedAuthority) a).getSource()).map(Principal::getName))
163+
.contains(originalAuthentication.getName());
170164
}
171165

172166
@Test
@@ -206,14 +200,11 @@ public void switchUserWhenUserAlreadySwitchedThenExitSwitchAndSwitchAgain() {
206200

207201
final Authentication secondSwitchUserAuthentication = authenticationCaptor.getValue();
208202

209-
assertEquals("username should point to the switched user", targetUsername,
210-
secondSwitchUserAuthentication.getName());
211-
assertEquals("SwitchUserGrantedAuthority should contain the original authentication",
212-
originalAuthentication.getName(),
213-
secondSwitchUserAuthentication.getAuthorities().stream()
214-
.filter(a -> a instanceof SwitchUserGrantedAuthority)
215-
.map(a -> ((SwitchUserGrantedAuthority) a).getSource()).map(Principal::getName).findFirst()
216-
.orElse(null));
203+
assertThat(secondSwitchUserAuthentication.getName()).isEqualTo(targetUsername);
204+
assertThat(secondSwitchUserAuthentication.getAuthorities().stream()
205+
.filter(a -> a instanceof SwitchUserGrantedAuthority)
206+
.map(a -> ((SwitchUserGrantedAuthority) a).getSource()).map(Principal::getName).findFirst()
207+
.orElse(null)).isEqualTo(originalAuthentication.getName());
217208
}
218209

219210
@Test
@@ -316,8 +307,8 @@ public void exitSwitchThenReturnToOriginalAuthentication() {
316307

317308
final Authentication originalAuthenticationValue = authenticationCaptor.getValue();
318309

319-
assertSame(originalAuthentication, savedSecurityContext.getAuthentication());
320-
assertSame(originalAuthentication, originalAuthenticationValue);
310+
assertThat(savedSecurityContext.getAuthentication()).isSameAs(originalAuthentication);
311+
assertThat(originalAuthenticationValue).isSameAs(originalAuthentication);
321312
verifyNoInteractions(chain);
322313
}
323314

@@ -399,10 +390,10 @@ public void constructorFirstDefaultValues() {
399390
// then
400391
final Object securityContextRepository = ReflectionTestUtils.getField(this.switchUserWebFilter,
401392
"securityContextRepository");
402-
assertTrue(securityContextRepository instanceof WebSessionServerSecurityContextRepository);
393+
assertThat(securityContextRepository).isInstanceOf(WebSessionServerSecurityContextRepository.class);
403394

404395
final Object userDetailsChecker = ReflectionTestUtils.getField(this.switchUserWebFilter, "userDetailsChecker");
405-
assertTrue(userDetailsChecker instanceof AccountStatusUserDetailsChecker);
396+
assertThat(userDetailsChecker).isInstanceOf(AccountStatusUserDetailsChecker.class);
406397
}
407398

408399
@Test
@@ -413,17 +404,17 @@ public void constructorSecondDefaultValues() {
413404

414405
// then
415406
final Object successHandler = ReflectionTestUtils.getField(this.switchUserWebFilter, "successHandler");
416-
assertTrue(successHandler instanceof RedirectServerAuthenticationSuccessHandler);
407+
assertThat(successHandler).isInstanceOf(RedirectServerAuthenticationSuccessHandler.class);
417408

418409
final Object failureHandler = ReflectionTestUtils.getField(this.switchUserWebFilter, "failureHandler");
419-
assertTrue(failureHandler instanceof RedirectServerAuthenticationFailureHandler);
410+
assertThat(failureHandler).isInstanceOf(RedirectServerAuthenticationFailureHandler.class);
420411

421412
final Object securityContextRepository = ReflectionTestUtils.getField(this.switchUserWebFilter,
422413
"securityContextRepository");
423-
assertTrue(securityContextRepository instanceof WebSessionServerSecurityContextRepository);
414+
assertThat(securityContextRepository).isInstanceOf(WebSessionServerSecurityContextRepository.class);
424415

425416
final Object userDetailsChecker = ReflectionTestUtils.getField(this.switchUserWebFilter, "userDetailsChecker");
426-
assertTrue(userDetailsChecker instanceof AccountStatusUserDetailsChecker);
417+
assertThat(userDetailsChecker instanceof AccountStatusUserDetailsChecker).isTrue();
427418
}
428419

429420
@Test
@@ -442,7 +433,7 @@ public void setSecurityContextRepositoryWhenDefinedThenChangeDefaultValue() {
442433
// given
443434
final Object oldSecurityContextRepository = ReflectionTestUtils.getField(this.switchUserWebFilter,
444435
"securityContextRepository");
445-
assertSame(this.serverSecurityContextRepository, oldSecurityContextRepository);
436+
assertThat(oldSecurityContextRepository).isSameAs(this.serverSecurityContextRepository);
446437

447438
final ServerSecurityContextRepository newSecurityContextRepository = mock(
448439
ServerSecurityContextRepository.class);
@@ -451,7 +442,7 @@ public void setSecurityContextRepositoryWhenDefinedThenChangeDefaultValue() {
451442
// then
452443
final Object currentSecurityContextRepository = ReflectionTestUtils.getField(this.switchUserWebFilter,
453444
"securityContextRepository");
454-
assertSame(newSecurityContextRepository, currentSecurityContextRepository);
445+
assertThat(currentSecurityContextRepository).isSameAs(newSecurityContextRepository);
455446
}
456447

457448
@Test
@@ -532,7 +523,7 @@ public void setExitUserMatcherWhenDefinedThenChangeDefaultValue() {
532523
final ServerWebExchangeMatcher currentExitUserMatcher = (ServerWebExchangeMatcher) ReflectionTestUtils
533524
.getField(this.switchUserWebFilter, "exitUserMatcher");
534525

535-
assertSame(newExitUserMatcher, currentExitUserMatcher);
526+
assertThat(currentExitUserMatcher).isSameAs(newExitUserMatcher);
536527
}
537528

538529
@Test
@@ -613,7 +604,7 @@ public void setSwitchUserMatcherWhenDefinedThenChangeDefaultValue() {
613604
final ServerWebExchangeMatcher currentExitUserMatcher = (ServerWebExchangeMatcher) ReflectionTestUtils
614605
.getField(this.switchUserWebFilter, "switchUserMatcher");
615606

616-
assertSame(newSwitchUserMatcher, currentExitUserMatcher);
607+
assertThat(currentExitUserMatcher).isSameAs(newSwitchUserMatcher);
617608
}
618609

619610
private UserDetails switchUserDetails(String username, boolean enabled) {

0 commit comments

Comments
 (0)