|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.lang.annotation.Retention;
|
20 | 20 | import java.lang.annotation.RetentionPolicy;
|
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Set; |
21 | 23 | import java.util.function.Supplier;
|
22 | 24 |
|
23 | 25 | import org.junit.jupiter.api.Test;
|
|
29 | 31 | import org.springframework.security.authentication.TestAuthentication;
|
30 | 32 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
31 | 33 | import org.springframework.security.authorization.AuthorizationDecision;
|
| 34 | +import org.springframework.security.authorization.AuthorizationManager; |
32 | 35 | import org.springframework.security.core.Authentication;
|
33 | 36 |
|
34 | 37 | import static org.assertj.core.api.Assertions.assertThat;
|
35 | 38 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 39 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | +import static org.mockito.Mockito.verify; |
36 | 42 |
|
37 | 43 | /**
|
38 | 44 | * Tests for {@link SecuredAuthorizationManager}.
|
|
41 | 47 | */
|
42 | 48 | public class SecuredAuthorizationManagerTests {
|
43 | 49 |
|
| 50 | + @Test |
| 51 | + public void setAuthoritiesAuthorizationManagerWhenNullThenException() { |
| 52 | + SecuredAuthorizationManager manager = new SecuredAuthorizationManager(); |
| 53 | + assertThatIllegalArgumentException().isThrownBy(() -> manager.setAuthoritiesAuthorizationManager(null)) |
| 54 | + .withMessage("authoritiesAuthorizationManager cannot be null"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void setAuthoritiesAuthorizationManagerWhenNotNullThenVerifyUsage() throws Exception { |
| 59 | + AuthorizationManager<Collection<String>> authoritiesAuthorizationManager = mock(AuthorizationManager.class); |
| 60 | + SecuredAuthorizationManager manager = new SecuredAuthorizationManager(); |
| 61 | + manager.setAuthoritiesAuthorizationManager(authoritiesAuthorizationManager); |
| 62 | + MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, |
| 63 | + "securedUserOrAdmin"); |
| 64 | + Supplier<Authentication> authentication = TestAuthentication::authenticatedUser; |
| 65 | + AuthorizationDecision decision = manager.check(authentication, methodInvocation); |
| 66 | + assertThat(decision).isNull(); |
| 67 | + verify(authoritiesAuthorizationManager).check(authentication, Set.of("ROLE_USER", "ROLE_ADMIN")); |
| 68 | + } |
| 69 | + |
44 | 70 | @Test
|
45 | 71 | public void checkDoSomethingWhenNoSecuredAnnotationThenNullDecision() throws Exception {
|
46 | 72 | MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class,
|
|
0 commit comments