|
| 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.method; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.platform.commons.util.ReflectionUtils; |
| 21 | + |
| 22 | +import org.springframework.expression.Expression; |
| 23 | +import org.springframework.expression.ExpressionParser; |
| 24 | +import org.springframework.security.access.annotation.BusinessService; |
| 25 | +import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; |
| 26 | +import org.springframework.security.authentication.TestAuthentication; |
| 27 | +import org.springframework.security.authorization.AuthorizationDecision; |
| 28 | +import org.springframework.security.util.SimpleMethodInvocation; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 32 | +import static org.mockito.BDDMockito.given; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | + |
| 36 | +class MethodExpressionAuthorizationManagerTests { |
| 37 | + |
| 38 | + @Test |
| 39 | + void instantiateWhenExpressionStringNullThenIllegalArgumentException() { |
| 40 | + assertThatIllegalArgumentException().isThrownBy(() -> new MethodExpressionAuthorizationManager(null)) |
| 41 | + .withMessage("expressionString cannot be empty"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void instantiateWhenExpressionStringEmptyThenIllegalArgumentException() { |
| 46 | + assertThatIllegalArgumentException().isThrownBy(() -> new MethodExpressionAuthorizationManager("")) |
| 47 | + .withMessage("expressionString cannot be empty"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void instantiateWhenExpressionStringBlankThenIllegalArgumentException() { |
| 52 | + assertThatIllegalArgumentException().isThrownBy(() -> new MethodExpressionAuthorizationManager(" ")) |
| 53 | + .withMessage("expressionString cannot be empty"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void instantiateWhenExpressionHandlerNotSetThenDefaultUsed() { |
| 58 | + MethodExpressionAuthorizationManager manager = new MethodExpressionAuthorizationManager("hasRole('ADMIN')"); |
| 59 | + assertThat(manager).extracting("expressionHandler").isInstanceOf(DefaultMethodSecurityExpressionHandler.class); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void setExpressionHandlerWhenNullThenIllegalArgumentException() { |
| 64 | + MethodExpressionAuthorizationManager manager = new MethodExpressionAuthorizationManager("hasRole('ADMIN')"); |
| 65 | + assertThatIllegalArgumentException().isThrownBy(() -> manager.setExpressionHandler(null)) |
| 66 | + .withMessage("expressionHandler cannot be null"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void setExpressionHandlerWhenNotNullThenVerifyExpressionHandler() { |
| 71 | + String expressionString = "hasRole('ADMIN')"; |
| 72 | + MethodExpressionAuthorizationManager manager = new MethodExpressionAuthorizationManager(expressionString); |
| 73 | + DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler(); |
| 74 | + ExpressionParser mockExpressionParser = mock(ExpressionParser.class); |
| 75 | + Expression mockExpression = mock(Expression.class); |
| 76 | + given(mockExpressionParser.parseExpression(expressionString)).willReturn(mockExpression); |
| 77 | + expressionHandler.setExpressionParser(mockExpressionParser); |
| 78 | + manager.setExpressionHandler(expressionHandler); |
| 79 | + assertThat(manager).extracting("expressionHandler").isEqualTo(expressionHandler); |
| 80 | + assertThat(manager).extracting("expression").isEqualTo(mockExpression); |
| 81 | + verify(mockExpressionParser).parseExpression(expressionString); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void checkWhenExpressionHasRoleAdminConfiguredAndRoleAdminThenGrantedDecision() { |
| 86 | + MethodExpressionAuthorizationManager manager = new MethodExpressionAuthorizationManager("hasRole('ADMIN')"); |
| 87 | + AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedAdmin, |
| 88 | + new SimpleMethodInvocation(new Object(), |
| 89 | + ReflectionUtils.getRequiredMethod(BusinessService.class, "someAdminMethod"))); |
| 90 | + assertThat(decision).isNotNull(); |
| 91 | + assertThat(decision.isGranted()).isTrue(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void checkWhenExpressionHasRoleAdminConfiguredAndRoleUserThenDeniedDecision() { |
| 96 | + MethodExpressionAuthorizationManager manager = new MethodExpressionAuthorizationManager("hasRole('ADMIN')"); |
| 97 | + AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedUser, |
| 98 | + new SimpleMethodInvocation(new Object(), |
| 99 | + ReflectionUtils.getRequiredMethod(BusinessService.class, "someAdminMethod"))); |
| 100 | + assertThat(decision).isNotNull(); |
| 101 | + assertThat(decision.isGranted()).isFalse(); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments