Fixed broken test due to missing context file.

This commit is contained in:
Luke Taylor 2008-12-01 00:36:13 +00:00
parent 51549a9f6f
commit 08ea70909d
1 changed files with 54 additions and 39 deletions

View File

@ -17,15 +17,25 @@ package org.springframework.security.intercept.method;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.List;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.ApplicationContext; import org.springframework.security.AccessDecisionManager;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.security.AccessDeniedException;
import org.springframework.security.AuthenticationManager;
import org.springframework.security.ConfigAttribute;
import org.springframework.security.ITargetObject; import org.springframework.security.ITargetObject;
import org.springframework.security.OtherTargetObject; import org.springframework.security.OtherTargetObject;
import org.springframework.security.SecurityConfig;
import org.springframework.security.TargetObject;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.intercept.method.aopalliance.MethodSecurityInterceptor; import org.springframework.security.intercept.method.aopalliance.MethodSecurityInterceptor;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken; import org.springframework.security.providers.TestingAuthenticationToken;
import org.springframework.security.util.AuthorityUtils;
import org.springframework.security.util.MethodInvocationUtils; import org.springframework.security.util.MethodInvocationUtils;
@ -36,32 +46,39 @@ import org.springframework.security.util.MethodInvocationUtils;
* @version $Id$ * @version $Id$
*/ */
public class MethodInvocationPrivilegeEvaluatorTests { public class MethodInvocationPrivilegeEvaluatorTests {
private Mockery jmock = new JUnit4Mockery();
private TestingAuthenticationToken token;
private MethodSecurityInterceptor interceptor;
private AccessDecisionManager adm;
private MethodDefinitionSource mds;
private final List<ConfigAttribute> role = SecurityConfig.createList("ROLE_IGNORED");
//~ Methods ======================================================================================================== //~ Methods ========================================================================================================
private Object lookupTargetObject() { @Before
ApplicationContext context = new ClassPathXmlApplicationContext( public final void setUp() throws Exception {
"org/springframework/security/intercept/method/aopalliance/applicationContext.xml"); SecurityContextHolder.clearContext();
interceptor = new MethodSecurityInterceptor();
return context.getBean("target"); token = new TestingAuthenticationToken("Test", "Password", "ROLE_SOMETHING");
} adm = jmock.mock(AccessDecisionManager.class);
AuthenticationManager authman = jmock.mock(AuthenticationManager.class);
private MethodSecurityInterceptor makeSecurityInterceptor() { mds = jmock.mock(MethodDefinitionSource.class);
ApplicationContext context = new ClassPathXmlApplicationContext( interceptor.setAccessDecisionManager(adm);
"org/springframework/security/intercept/method/aopalliance/applicationContext.xml"); interceptor.setAuthenticationManager(authman);
interceptor.setObjectDefinitionSource(mds);
return (MethodSecurityInterceptor) context.getBean("securityInterceptor");
} }
@Test @Test
public void allowsAccessUsingCreate() throws Exception { public void allowsAccessUsingCreate() throws Exception {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password", Object object = new TargetObject();
AuthorityUtils.createAuthorityList("MOCK_LOWER")); final MethodInvocation mi = MethodInvocationUtils.create(object, "makeLowerCase", "foobar");
Object object = lookupTargetObject();
MethodInvocation mi = MethodInvocationUtils.create(object, "makeLowerCase", "foobar");
MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator(); MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
jmock.checking(new Expectations() {{
oneOf(mds).getAttributes(mi); will(returnValue(role));
oneOf(adm).decide(token, mi, role);
}});
mipe.setSecurityInterceptor(interceptor); mipe.setSecurityInterceptor(interceptor);
mipe.afterPropertiesSet(); mipe.afterPropertiesSet();
@ -70,45 +87,43 @@ public class MethodInvocationPrivilegeEvaluatorTests {
@Test @Test
public void allowsAccessUsingCreateFromClass() throws Exception { public void allowsAccessUsingCreateFromClass() throws Exception {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password", final MethodInvocation mi = MethodInvocationUtils.createFromClass(new OtherTargetObject(), ITargetObject.class, "makeLowerCase",
AuthorityUtils.createAuthorityList("MOCK_LOWER"));
MethodInvocation mi = MethodInvocationUtils.createFromClass(new OtherTargetObject(), ITargetObject.class, "makeLowerCase",
new Class[] {String.class}, new Object[] {"Hello world"}); new Class[] {String.class}, new Object[] {"Hello world"});
MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator(); MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
mipe.setSecurityInterceptor(interceptor); mipe.setSecurityInterceptor(interceptor);
mipe.afterPropertiesSet(); jmock.checking(new Expectations() {{
oneOf(mds).getAttributes(mi); will(returnValue(role));
oneOf(adm).decide(token, mi, role);
}});
assertTrue(mipe.isAllowed(mi, token)); assertTrue(mipe.isAllowed(mi, token));
} }
@Test @Test
public void declinesAccessUsingCreate() throws Exception { public void declinesAccessUsingCreate() throws Exception {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password", Object object = new TargetObject();
AuthorityUtils.createAuthorityList("ROLE_NOT_HELD")); final MethodInvocation mi = MethodInvocationUtils.create(object, "makeLowerCase", new Object[] {"foobar"});
Object object = lookupTargetObject();
MethodInvocation mi = MethodInvocationUtils.create(object, "makeLowerCase", new Object[] {"foobar"});
MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator(); MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
mipe.setSecurityInterceptor(interceptor); mipe.setSecurityInterceptor(interceptor);
mipe.afterPropertiesSet(); jmock.checking(new Expectations() {{
oneOf(mds).getAttributes(mi); will(returnValue(role));
oneOf(adm).decide(token, mi, role); will(throwException(new AccessDeniedException("rejected")));
}});
assertFalse(mipe.isAllowed(mi, token)); assertFalse(mipe.isAllowed(mi, token));
} }
@Test @Test
public void declinesAccessUsingCreateFromClass() throws Exception { public void declinesAccessUsingCreateFromClass() throws Exception {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password", final MethodInvocation mi = MethodInvocationUtils.createFromClass(new OtherTargetObject(), ITargetObject.class, "makeLowerCase",
AuthorityUtils.createAuthorityList("ROLE_NOT_HELD"));
MethodInvocation mi = MethodInvocationUtils.createFromClass(new OtherTargetObject(), ITargetObject.class, "makeLowerCase",
new Class[] {String.class}, new Object[] {"helloWorld"}); new Class[] {String.class}, new Object[] {"helloWorld"});
MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator(); MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
mipe.setSecurityInterceptor(interceptor); mipe.setSecurityInterceptor(interceptor);
mipe.afterPropertiesSet(); jmock.checking(new Expectations() {{
oneOf(mds).getAttributes(mi); will(returnValue(role));
oneOf(adm).decide(token, mi, role); will(throwException(new AccessDeniedException("rejected")));
}});
assertFalse(mipe.isAllowed(mi, token)); assertFalse(mipe.isAllowed(mi, token));
} }