|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 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.expression; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.mockito.Mock; |
| 24 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 27 | +import org.springframework.context.support.StaticApplicationContext; |
| 28 | +import org.springframework.expression.EvaluationContext; |
| 29 | +import org.springframework.expression.Expression; |
| 30 | +import org.springframework.expression.ExpressionParser; |
| 31 | +import org.springframework.security.access.SecurityConfig; |
| 32 | +import org.springframework.security.authentication.AuthenticationTrustResolver; |
| 33 | +import org.springframework.security.core.Authentication; |
| 34 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 35 | +import org.springframework.security.web.access.intercept.RequestAuthorizationContext; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 39 | +import static org.mockito.Mockito.mock; |
| 40 | +import static org.mockito.Mockito.verify; |
| 41 | + |
| 42 | +@ExtendWith(MockitoExtension.class) |
| 43 | +public class DefaultHttpSecurityExpressionHandlerTests { |
| 44 | + |
| 45 | + @Mock |
| 46 | + private AuthenticationTrustResolver trustResolver; |
| 47 | + |
| 48 | + @Mock |
| 49 | + private Authentication authentication; |
| 50 | + |
| 51 | + @Mock |
| 52 | + private RequestAuthorizationContext context; |
| 53 | + |
| 54 | + private DefaultHttpSecurityExpressionHandler handler; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + public void setup() { |
| 58 | + this.handler = new DefaultHttpSecurityExpressionHandler(); |
| 59 | + } |
| 60 | + |
| 61 | + @AfterEach |
| 62 | + public void cleanup() { |
| 63 | + SecurityContextHolder.clearContext(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void expressionPropertiesAreResolvedAgainstAppContextBeans() { |
| 68 | + StaticApplicationContext appContext = new StaticApplicationContext(); |
| 69 | + RootBeanDefinition bean = new RootBeanDefinition(SecurityConfig.class); |
| 70 | + bean.getConstructorArgumentValues().addGenericArgumentValue("ROLE_A"); |
| 71 | + appContext.registerBeanDefinition("role", bean); |
| 72 | + this.handler.setApplicationContext(appContext); |
| 73 | + EvaluationContext ctx = this.handler.createEvaluationContext(mock(Authentication.class), |
| 74 | + mock(RequestAuthorizationContext.class)); |
| 75 | + ExpressionParser parser = this.handler.getExpressionParser(); |
| 76 | + assertThat(parser.parseExpression("@role.getAttribute() == 'ROLE_A'").getValue(ctx, Boolean.class)).isTrue(); |
| 77 | + assertThat(parser.parseExpression("@role.attribute == 'ROLE_A'").getValue(ctx, Boolean.class)).isTrue(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void setTrustResolverNull() { |
| 82 | + assertThatIllegalArgumentException().isThrownBy(() -> this.handler.setTrustResolver(null)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void createEvaluationContextCustomTrustResolver() { |
| 87 | + this.handler.setTrustResolver(this.trustResolver); |
| 88 | + Expression expression = this.handler.getExpressionParser().parseExpression("anonymous"); |
| 89 | + EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.context); |
| 90 | + assertThat(expression.getValue(context, Boolean.class)).isFalse(); |
| 91 | + verify(this.trustResolver).isAnonymous(this.authentication); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments