|
| 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 org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +/** |
| 24 | + * Tests for {@link AuthorizationManagers}. |
| 25 | + * |
| 26 | + * @author Evgeniy Cheban |
| 27 | + */ |
| 28 | +class AuthorizationManagersTests { |
| 29 | + |
| 30 | + @Test |
| 31 | + void checkAnyOfWhenOneGrantedThenGrantedDecision() { |
| 32 | + AuthorizationManager<?> composed = AuthorizationManagers.anyOf((a, o) -> new AuthorizationDecision(false), |
| 33 | + (a, o) -> new AuthorizationDecision(true)); |
| 34 | + AuthorizationDecision decision = composed.check(null, null); |
| 35 | + assertThat(decision).isNotNull(); |
| 36 | + assertThat(decision.isGranted()).isTrue(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void checkAnyOfWhenOneAbstainedThenAbstainedDecision() { |
| 41 | + AuthorizationManager<?> composed = AuthorizationManagers.anyOf((a, o) -> new AuthorizationDecision(false), |
| 42 | + (a, o) -> null); |
| 43 | + AuthorizationDecision decision = composed.check(null, null); |
| 44 | + assertThat(decision).isNull(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void checkAnyOfWhenEmptyThenDeniedDecision() { |
| 49 | + AuthorizationManager<?> composed = AuthorizationManagers.anyOf(); |
| 50 | + AuthorizationDecision decision = composed.check(null, null); |
| 51 | + assertThat(decision).isNotNull(); |
| 52 | + assertThat(decision.isGranted()).isFalse(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void checkAllOfWhenAllGrantedThenGrantedDecision() { |
| 57 | + AuthorizationManager<?> composed = AuthorizationManagers.allOf((a, o) -> new AuthorizationDecision(true), |
| 58 | + (a, o) -> new AuthorizationDecision(true)); |
| 59 | + AuthorizationDecision decision = composed.check(null, null); |
| 60 | + assertThat(decision).isNotNull(); |
| 61 | + assertThat(decision.isGranted()).isTrue(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void checkAllOfWhenOneAbstainedThenGrantedDecision() { |
| 66 | + AuthorizationManager<?> composed = AuthorizationManagers.allOf((a, o) -> new AuthorizationDecision(true), |
| 67 | + (a, o) -> null); |
| 68 | + AuthorizationDecision decision = composed.check(null, null); |
| 69 | + assertThat(decision).isNotNull(); |
| 70 | + assertThat(decision.isGranted()).isTrue(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void checkAllOfWhenOneDeniedThenDeniedDecision() { |
| 75 | + AuthorizationManager<?> composed = AuthorizationManagers.allOf((a, o) -> new AuthorizationDecision(true), |
| 76 | + (a, o) -> new AuthorizationDecision(false)); |
| 77 | + AuthorizationDecision decision = composed.check(null, null); |
| 78 | + assertThat(decision).isNotNull(); |
| 79 | + assertThat(decision.isGranted()).isFalse(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void checkAllOfWhenEmptyThenGrantedDecision() { |
| 84 | + AuthorizationManager<?> composed = AuthorizationManagers.allOf(); |
| 85 | + AuthorizationDecision decision = composed.check(null, null); |
| 86 | + assertThat(decision).isNotNull(); |
| 87 | + assertThat(decision.isGranted()).isTrue(); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments