|
21 | 21 |
|
22 | 22 | import org.springframework.security.authentication.AuthenticationTrustResolver; |
23 | 23 | import org.springframework.security.authentication.TestingAuthenticationToken; |
| 24 | +import org.springframework.security.authorization.AuthorizationManager; |
| 25 | +import org.springframework.security.authorization.AuthorizationManagerFactory; |
| 26 | +import org.springframework.security.authorization.SingleResultAuthorizationManager; |
24 | 27 | import org.springframework.security.core.Authentication; |
25 | 28 | import org.springframework.security.core.authority.AuthorityUtils; |
26 | 29 |
|
27 | 30 | import static org.assertj.core.api.Assertions.assertThat; |
28 | 31 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
29 | 32 | import static org.mockito.BDDMockito.given; |
30 | 33 | import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.verify; |
31 | 35 |
|
32 | 36 | /** |
33 | 37 | * @author Luke Taylor |
@@ -174,4 +178,148 @@ void isAuthenticatedWhenTrustResolverTrueThenTrue() { |
174 | 178 | assertThat(this.root.isAuthenticated()).isTrue(); |
175 | 179 | } |
176 | 180 |
|
| 181 | + // gh-18486 |
| 182 | + @Test |
| 183 | + @SuppressWarnings("unchecked") |
| 184 | + public void hasAuthorityDelegatesToAuthorizationManagerFactoryHasAuthority() { |
| 185 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 186 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 187 | + given(factory.hasAuthority("CUSTOM_AUTHORITY")).willReturn(manager); |
| 188 | + this.root.setAuthorizationManagerFactory(factory); |
| 189 | + assertThat(this.root.hasAuthority("CUSTOM_AUTHORITY")).isFalse(); |
| 190 | + verify(factory).hasAuthority("CUSTOM_AUTHORITY"); |
| 191 | + } |
| 192 | + |
| 193 | + // gh-18486 |
| 194 | + @Test |
| 195 | + @SuppressWarnings("unchecked") |
| 196 | + public void hasAnyAuthorityDelegatesToAuthorizationManagerFactoryHasAnyAuthority() { |
| 197 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 198 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 199 | + given(factory.hasAnyAuthority("CUSTOM_AUTHORITY")).willReturn(manager); |
| 200 | + this.root.setAuthorizationManagerFactory(factory); |
| 201 | + assertThat(this.root.hasAnyAuthority("CUSTOM_AUTHORITY")).isFalse(); |
| 202 | + verify(factory).hasAnyAuthority("CUSTOM_AUTHORITY"); |
| 203 | + } |
| 204 | + |
| 205 | + // gh-18486 |
| 206 | + @Test |
| 207 | + @SuppressWarnings("unchecked") |
| 208 | + public void hasAllAuthoritiesDelegatesToAuthorizationManagerFactoryHasAllAuthorities() { |
| 209 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 210 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 211 | + given(factory.hasAllAuthorities("A", "B")).willReturn(manager); |
| 212 | + this.root.setAuthorizationManagerFactory(factory); |
| 213 | + assertThat(this.root.hasAllAuthorities("A", "B")).isFalse(); |
| 214 | + verify(factory).hasAllAuthorities("A", "B"); |
| 215 | + } |
| 216 | + |
| 217 | + // gh-18486 |
| 218 | + @Test |
| 219 | + @SuppressWarnings("unchecked") |
| 220 | + public void hasRoleDelegatesToAuthorizationManagerFactoryHasRole() { |
| 221 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 222 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 223 | + given(factory.hasRole("CUSTOM_ROLE")).willReturn(manager); |
| 224 | + this.root.setAuthorizationManagerFactory(factory); |
| 225 | + assertThat(this.root.hasRole("CUSTOM_ROLE")).isFalse(); |
| 226 | + verify(factory).hasRole("CUSTOM_ROLE"); |
| 227 | + } |
| 228 | + |
| 229 | + // gh-18486 |
| 230 | + @Test |
| 231 | + @SuppressWarnings("unchecked") |
| 232 | + public void hasAnyRoleDelegatesToAuthorizationManagerFactoryHasAnyRole() { |
| 233 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 234 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 235 | + given(factory.hasAnyRole("A", "B")).willReturn(manager); |
| 236 | + this.root.setAuthorizationManagerFactory(factory); |
| 237 | + assertThat(this.root.hasAnyRole("A", "B")).isFalse(); |
| 238 | + verify(factory).hasAnyRole("A", "B"); |
| 239 | + } |
| 240 | + |
| 241 | + // gh-18486 |
| 242 | + @Test |
| 243 | + @SuppressWarnings("unchecked") |
| 244 | + public void hasAllRolesDelegatesToAuthorizationManagerFactoryHasAllRoles() { |
| 245 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 246 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 247 | + given(factory.hasAllRoles("A", "B")).willReturn(manager); |
| 248 | + this.root.setAuthorizationManagerFactory(factory); |
| 249 | + assertThat(this.root.hasAllRoles("A", "B")).isFalse(); |
| 250 | + verify(factory).hasAllRoles("A", "B"); |
| 251 | + } |
| 252 | + |
| 253 | + // gh-18486 |
| 254 | + @Test |
| 255 | + @SuppressWarnings("unchecked") |
| 256 | + public void permitAllDelegatesToAuthorizationManagerFactoryPermitAll() { |
| 257 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 258 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 259 | + given(factory.permitAll()).willReturn(manager); |
| 260 | + this.root.setAuthorizationManagerFactory(factory); |
| 261 | + assertThat(this.root.permitAll()).isFalse(); |
| 262 | + verify(factory).permitAll(); |
| 263 | + } |
| 264 | + |
| 265 | + // gh-18486 |
| 266 | + @Test |
| 267 | + @SuppressWarnings("unchecked") |
| 268 | + public void denyAllDelegatesToAuthorizationManagerFactoryDenyAll() { |
| 269 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 270 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 271 | + given(factory.denyAll()).willReturn(manager); |
| 272 | + this.root.setAuthorizationManagerFactory(factory); |
| 273 | + assertThat(this.root.denyAll()).isFalse(); |
| 274 | + verify(factory).denyAll(); |
| 275 | + } |
| 276 | + |
| 277 | + // gh-18486 |
| 278 | + @Test |
| 279 | + @SuppressWarnings("unchecked") |
| 280 | + public void isAnonymousDelegatesToAuthorizationManagerFactoryAnonymous() { |
| 281 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 282 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 283 | + given(factory.anonymous()).willReturn(manager); |
| 284 | + this.root.setAuthorizationManagerFactory(factory); |
| 285 | + assertThat(this.root.isAnonymous()).isFalse(); |
| 286 | + verify(factory).anonymous(); |
| 287 | + } |
| 288 | + |
| 289 | + // gh-18486 |
| 290 | + @Test |
| 291 | + @SuppressWarnings("unchecked") |
| 292 | + public void isAuthenticatedDelegatesToAuthorizationManagerFactoryAuthenticated() { |
| 293 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 294 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 295 | + given(factory.authenticated()).willReturn(manager); |
| 296 | + this.root.setAuthorizationManagerFactory(factory); |
| 297 | + assertThat(this.root.isAuthenticated()).isFalse(); |
| 298 | + verify(factory).authenticated(); |
| 299 | + } |
| 300 | + |
| 301 | + // gh-18486 |
| 302 | + @Test |
| 303 | + @SuppressWarnings("unchecked") |
| 304 | + public void isRememberMeDelegatesToAuthorizationManagerFactoryRememberMe() { |
| 305 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 306 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 307 | + given(factory.rememberMe()).willReturn(manager); |
| 308 | + this.root.setAuthorizationManagerFactory(factory); |
| 309 | + assertThat(this.root.isRememberMe()).isFalse(); |
| 310 | + verify(factory).rememberMe(); |
| 311 | + } |
| 312 | + |
| 313 | + // gh-18486 |
| 314 | + @Test |
| 315 | + @SuppressWarnings("unchecked") |
| 316 | + public void isFullyAuthenticatedDelegatesToAuthorizationManagerFactoryFullyAuthenticated() { |
| 317 | + AuthorizationManagerFactory<Object> factory = mock(AuthorizationManagerFactory.class); |
| 318 | + AuthorizationManager<Object> manager = SingleResultAuthorizationManager.denyAll(); |
| 319 | + given(factory.fullyAuthenticated()).willReturn(manager); |
| 320 | + this.root.setAuthorizationManagerFactory(factory); |
| 321 | + assertThat(this.root.isFullyAuthenticated()).isFalse(); |
| 322 | + verify(factory).fullyAuthenticated(); |
| 323 | + } |
| 324 | + |
177 | 325 | } |
0 commit comments