|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 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.web.access; |
| 18 | + |
| 19 | +import javax.servlet.http.HttpServletRequest; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.InjectMocks; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.junit.MockitoJUnitRunner; |
| 26 | + |
| 27 | +import org.springframework.security.authentication.TestAuthentication; |
| 28 | +import org.springframework.security.authorization.AuthorizationDecision; |
| 29 | +import org.springframework.security.authorization.AuthorizationManager; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 33 | +import static org.mockito.ArgumentMatchers.any; |
| 34 | +import static org.mockito.BDDMockito.given; |
| 35 | +import static org.mockito.Mockito.verify; |
| 36 | + |
| 37 | +@RunWith(MockitoJUnitRunner.class) |
| 38 | +public class AuthorizationManagerWebInvocationPrivilegeEvaluatorTests { |
| 39 | + |
| 40 | + @InjectMocks |
| 41 | + private AuthorizationManagerWebInvocationPrivilegeEvaluator privilegeEvaluator; |
| 42 | + |
| 43 | + @Mock |
| 44 | + private AuthorizationManager<HttpServletRequest> authorizationManager; |
| 45 | + |
| 46 | + @Test |
| 47 | + public void constructorWhenAuthorizationManagerNullThenIllegalArgument() { |
| 48 | + assertThatIllegalArgumentException() |
| 49 | + .isThrownBy(() -> new AuthorizationManagerWebInvocationPrivilegeEvaluator(null)) |
| 50 | + .withMessage("authorizationManager cannot be null"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void isAllowedWhenAuthorizationManagerAllowsThenAllowedTrue() { |
| 55 | + given(this.authorizationManager.check(any(), any())).willReturn(new AuthorizationDecision(true)); |
| 56 | + boolean allowed = this.privilegeEvaluator.isAllowed("/test", TestAuthentication.authenticatedUser()); |
| 57 | + assertThat(allowed).isTrue(); |
| 58 | + verify(this.authorizationManager).check(any(), any()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void isAllowedWhenAuthorizationManagerDeniesAllowedFalse() { |
| 63 | + given(this.authorizationManager.check(any(), any())).willReturn(new AuthorizationDecision(false)); |
| 64 | + boolean allowed = this.privilegeEvaluator.isAllowed("/test", TestAuthentication.authenticatedUser()); |
| 65 | + assertThat(allowed).isFalse(); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments