|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.authorization; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.function.Supplier; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.security.access.hierarchicalroles.NullRoleHierarchy; |
| 26 | +import org.springframework.security.access.hierarchicalroles.RoleHierarchy; |
| 27 | +import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl; |
| 28 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 29 | +import org.springframework.security.core.Authentication; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for {@link AuthoritiesAuthorizationManager}. |
| 36 | + * |
| 37 | + * @author Evgeniy Cheban |
| 38 | + */ |
| 39 | +class AuthoritiesAuthorizationManagerTests { |
| 40 | + |
| 41 | + @Test |
| 42 | + void setRoleHierarchyWhenNullThenIllegalArgumentException() { |
| 43 | + AuthoritiesAuthorizationManager manager = new AuthoritiesAuthorizationManager(); |
| 44 | + assertThatIllegalArgumentException().isThrownBy(() -> manager.setRoleHierarchy(null)) |
| 45 | + .withMessage("roleHierarchy cannot be null"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void setRoleHierarchyWhenNotNullThenVerifyRoleHierarchy() { |
| 50 | + AuthoritiesAuthorizationManager manager = new AuthoritiesAuthorizationManager(); |
| 51 | + RoleHierarchy roleHierarchy = new RoleHierarchyImpl(); |
| 52 | + manager.setRoleHierarchy(roleHierarchy); |
| 53 | + assertThat(manager).extracting("roleHierarchy").isEqualTo(roleHierarchy); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void getRoleHierarchyWhenNotSetThenDefaultsToNullRoleHierarchy() { |
| 58 | + AuthoritiesAuthorizationManager manager = new AuthoritiesAuthorizationManager(); |
| 59 | + assertThat(manager).extracting("roleHierarchy").isInstanceOf(NullRoleHierarchy.class); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void checkWhenUserHasAnyAuthorityThenGrantedDecision() { |
| 64 | + AuthoritiesAuthorizationManager manager = new AuthoritiesAuthorizationManager(); |
| 65 | + Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", "USER"); |
| 66 | + assertThat(manager.check(authentication, Arrays.asList("ADMIN", "USER")).isGranted()).isTrue(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void checkWhenUserHasNotAnyAuthorityThenDeniedDecision() { |
| 71 | + AuthoritiesAuthorizationManager manager = new AuthoritiesAuthorizationManager(); |
| 72 | + Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", "ANONYMOUS"); |
| 73 | + assertThat(manager.check(authentication, Arrays.asList("ADMIN", "USER")).isGranted()).isFalse(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void hasRoleWhenRoleHierarchySetThenGreaterRoleTakesPrecedence() { |
| 78 | + AuthoritiesAuthorizationManager manager = new AuthoritiesAuthorizationManager(); |
| 79 | + RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl(); |
| 80 | + roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER"); |
| 81 | + manager.setRoleHierarchy(roleHierarchy); |
| 82 | + Supplier<Authentication> authentication = () -> new TestingAuthenticationToken("user", "password", |
| 83 | + "ROLE_ADMIN"); |
| 84 | + assertThat(manager.check(authentication, Collections.singleton("ROLE_ADMIN")).isGranted()).isTrue(); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments