|
21 | 21 |
|
22 | 22 | import org.springframework.mock.web.MockHttpServletRequest;
|
23 | 23 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
| 24 | +import org.springframework.security.core.AuthenticatedPrincipal; |
24 | 25 | import org.springframework.security.core.Authentication;
|
25 | 26 | import org.springframework.security.core.authority.AuthorityUtils;
|
26 | 27 | import org.springframework.security.core.context.SecurityContextHolder;
|
27 | 28 | import org.springframework.security.core.userdetails.User;
|
28 | 29 |
|
29 | 30 | import static org.assertj.core.api.Assertions.assertThat;
|
| 31 | +import static org.mockito.BDDMockito.given; |
| 32 | +import static org.mockito.Mockito.mock; |
| 33 | +import static org.mockito.Mockito.times; |
| 34 | +import static org.mockito.Mockito.verify; |
30 | 35 |
|
31 | 36 | /**
|
32 | 37 | * Tests {@link SecurityContextHolderAwareRequestWrapper}.
|
@@ -130,4 +135,18 @@ public void testRolePrefixNotAppliedIfRoleStartsWith() {
|
130 | 135 | assertThat(wrapper.isUserInRole("ROLE_FOOBAR")).isTrue();
|
131 | 136 | }
|
132 | 137 |
|
| 138 | + @Test |
| 139 | + public void testGetRemoteUserStringWithAuthenticatedPrinciple() { |
| 140 | + String username = "authPrincipleUsername"; |
| 141 | + AuthenticatedPrincipal principal = mock(AuthenticatedPrincipal.class); |
| 142 | + given(principal.getName()).willReturn(username); |
| 143 | + Authentication auth = new TestingAuthenticationToken(principal, "user"); |
| 144 | + SecurityContextHolder.getContext().setAuthentication(auth); |
| 145 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 146 | + request.setRequestURI("/"); |
| 147 | + SecurityContextHolderAwareRequestWrapper wrapper = new SecurityContextHolderAwareRequestWrapper(request, ""); |
| 148 | + assertThat(wrapper.getRemoteUser()).isEqualTo(username); |
| 149 | + verify(principal, times(1)).getName(); |
| 150 | + } |
| 151 | + |
133 | 152 | }
|
0 commit comments