Search in sources :

Example 41 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class PrePostAnnotationSecurityMetadataSourceTests method proxyFactoryInterfaceAttributesFound.

@Test
public void proxyFactoryInterfaceAttributesFound() throws Exception {
    MockMethodInvocation mi = MethodInvocationFactory.createSec2150MethodInvocation();
    Collection<ConfigAttribute> attributes = this.mds.getAttributes(mi);
    assertThat(attributes).hasSize(1);
    Expression expression = (Expression) ReflectionTestUtils.getField(attributes.iterator().next(), "authorizeExpression");
    assertThat(expression.getExpressionString()).isEqualTo("hasRole('ROLE_PERSON')");
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) Expression(org.springframework.expression.Expression) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) Test(org.junit.jupiter.api.Test)

Example 42 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class Jsr250MethodSecurityMetadataSourceTests method interfacesNeverContributeAnnotationsMethodLevel.

/**
 * The interfaces implemented by a class never contribute annotations to the class
 * itself or any of its members.
 * @throws Exception
 */
@Test
public void interfacesNeverContributeAnnotationsMethodLevel() throws Exception {
    Parent target = new Parent();
    MockMethodInvocation mi = new MockMethodInvocation(target, target.getClass(), "interfaceMethod");
    Collection<ConfigAttribute> accessAttributes = this.mds.getAttributes(mi);
    assertThat(accessAttributes).isEmpty();
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) Test(org.junit.jupiter.api.Test)

Example 43 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class Jsr250MethodSecurityMetadataSourceTests method classLevelAnnotationsOnlyAffectTheClassTheyAnnotateAndTheirMembersOverriden.

@Test
public void classLevelAnnotationsOnlyAffectTheClassTheyAnnotateAndTheirMembersOverriden() throws Exception {
    Child target = new Child();
    MockMethodInvocation mi = new MockMethodInvocation(target, target.getClass(), "overriden");
    Collection<ConfigAttribute> accessAttributes = this.mds.getAttributes(mi);
    assertThat(accessAttributes).hasSize(1);
    assertThat(accessAttributes.toArray()[0].toString()).isEqualTo("ROLE_DERIVED");
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) Test(org.junit.jupiter.api.Test)

Example 44 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class Jsr250MethodSecurityMetadataSourceTests method classLevelAnnotationsImpactMemberLevel.

@Test
public void classLevelAnnotationsImpactMemberLevel() throws Exception {
    Child target = new Child();
    MockMethodInvocation mi = new MockMethodInvocation(target, target.getClass(), "defaults");
    Collection<ConfigAttribute> accessAttributes = this.mds.getAttributes(mi);
    assertThat(accessAttributes).hasSize(1);
    assertThat(accessAttributes.toArray()[0].toString()).isEqualTo("ROLE_DERIVED");
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) Test(org.junit.jupiter.api.Test)

Example 45 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class Jsr250MethodSecurityMetadataSourceTests method interfacesNeverContributeAnnotationsClassLevel.

@Test
public void interfacesNeverContributeAnnotationsClassLevel() throws Exception {
    Parent target = new Parent();
    MockMethodInvocation mi = new MockMethodInvocation(target, target.getClass(), "notOverriden");
    Collection<ConfigAttribute> accessAttributes = this.mds.getAttributes(mi);
    assertThat(accessAttributes).isEmpty();
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) Test(org.junit.jupiter.api.Test)

Aggregations

MockMethodInvocation (org.springframework.security.access.intercept.method.MockMethodInvocation)72 Test (org.junit.jupiter.api.Test)70 TestAuthentication (org.springframework.security.authentication.TestAuthentication)36 AuthorizationDecision (org.springframework.security.authorization.AuthorizationDecision)27 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)23 Authentication (org.springframework.security.core.Authentication)20 AnnotationConfigurationException (org.springframework.core.annotation.AnnotationConfigurationException)13 ConfigAttribute (org.springframework.security.access.ConfigAttribute)12 PreInvocationAttribute (org.springframework.security.access.prepost.PreInvocationAttribute)7 EvaluationContext (org.springframework.expression.EvaluationContext)3 SecurityExpressionRoot (org.springframework.security.access.expression.SecurityExpressionRoot)3 ArrayList (java.util.ArrayList)2 List (java.util.List)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 Expression (org.springframework.expression.Expression)1