Search in sources :

Example 41 with TestingAuthenticationToken

use of org.springframework.security.authentication.TestingAuthenticationToken in project spring-security by spring-projects.

the class EhCacheBasedAclCacheTests method putInCacheAclWithParent.

@Test
public void putInCacheAclWithParent() throws Exception {
    Authentication auth = new TestingAuthenticationToken("user", "password", "ROLE_GENERAL");
    auth.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(auth);
    ObjectIdentity identityParent = new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(2));
    AclAuthorizationStrategy aclAuthorizationStrategy = new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ROLE_OWNERSHIP"), new SimpleGrantedAuthority("ROLE_AUDITING"), new SimpleGrantedAuthority("ROLE_GENERAL"));
    MutableAcl parentAcl = new AclImpl(identityParent, Long.valueOf(2), aclAuthorizationStrategy, new ConsoleAuditLogger());
    acl.setParent(parentAcl);
    myCache.putInCache(acl);
    verify(cache, times(4)).put(element.capture());
    List<Element> allValues = element.getAllValues();
    assertThat(allValues.get(0).getKey()).isEqualTo(parentAcl.getObjectIdentity());
    assertThat(allValues.get(0).getObjectValue()).isEqualTo(parentAcl);
    assertThat(allValues.get(1).getKey()).isEqualTo(parentAcl.getId());
    assertThat(allValues.get(1).getObjectValue()).isEqualTo(parentAcl);
    assertThat(allValues.get(2).getKey()).isEqualTo(acl.getObjectIdentity());
    assertThat(allValues.get(2).getObjectValue()).isEqualTo(acl);
    assertThat(allValues.get(3).getKey()).isEqualTo(acl.getId());
    assertThat(allValues.get(3).getObjectValue()).isEqualTo(acl);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ObjectIdentity(org.springframework.security.acls.model.ObjectIdentity) Authentication(org.springframework.security.core.Authentication) Element(net.sf.ehcache.Element) MutableAcl(org.springframework.security.acls.model.MutableAcl) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 42 with TestingAuthenticationToken

use of org.springframework.security.authentication.TestingAuthenticationToken in project spring-security by spring-projects.

the class AspectJMethodSecurityInterceptorTests method setUp.

// ~ Methods
// ========================================================================================================
@Before
public final void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    SecurityContextHolder.clearContext();
    token = new TestingAuthenticationToken("Test", "Password");
    interceptor = new AspectJMethodSecurityInterceptor();
    interceptor.setAccessDecisionManager(adm);
    interceptor.setAuthenticationManager(authman);
    interceptor.setSecurityMetadataSource(mds);
    // Set up joinpoint information for the countLength method on TargetObject
    // new MockJoinPoint(new
    joinPoint = mock(ProceedingJoinPoint.class);
    // TargetObject(), method);
    Signature sig = mock(Signature.class);
    when(sig.getDeclaringType()).thenReturn(TargetObject.class);
    JoinPoint.StaticPart staticPart = mock(JoinPoint.StaticPart.class);
    when(joinPoint.getSignature()).thenReturn(sig);
    when(joinPoint.getStaticPart()).thenReturn(staticPart);
    CodeSignature codeSig = mock(CodeSignature.class);
    when(codeSig.getName()).thenReturn("countLength");
    when(codeSig.getDeclaringType()).thenReturn(TargetObject.class);
    when(codeSig.getParameterTypes()).thenReturn(new Class[] { String.class });
    when(staticPart.getSignature()).thenReturn(codeSig);
    when(mds.getAttributes(any(JoinPoint.class))).thenReturn(SecurityConfig.createList("ROLE_USER"));
    when(authman.authenticate(token)).thenReturn(token);
}
Also used : CodeSignature(org.aspectj.lang.reflect.CodeSignature) Signature(org.aspectj.lang.Signature) CodeSignature(org.aspectj.lang.reflect.CodeSignature) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Before(org.junit.Before)

Example 43 with TestingAuthenticationToken

use of org.springframework.security.authentication.TestingAuthenticationToken in project spring-security by spring-projects.

the class RoleHierarchyVoterTests method hierarchicalRoleIsIncludedInDecision.

@Test
public void hierarchicalRoleIsIncludedInDecision() {
    RoleHierarchyImpl roleHierarchyImpl = new RoleHierarchyImpl();
    roleHierarchyImpl.setHierarchy("ROLE_A > ROLE_B");
    // User has role A, role B is required
    TestingAuthenticationToken auth = new TestingAuthenticationToken("user", "password", "ROLE_A");
    RoleHierarchyVoter voter = new RoleHierarchyVoter(roleHierarchyImpl);
    assertThat(voter.vote(auth, new Object(), SecurityConfig.createList("ROLE_B"))).isEqualTo(RoleHierarchyVoter.ACCESS_GRANTED);
}
Also used : RoleHierarchyVoter(org.springframework.security.access.vote.RoleHierarchyVoter) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) RoleHierarchyImpl(org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl) Test(org.junit.Test)

Example 44 with TestingAuthenticationToken

use of org.springframework.security.authentication.TestingAuthenticationToken in project spring-security by spring-projects.

the class UnanimousBasedTests method testOneAffirmativeVoteTwoAbstainVotesGrantsAccess.

@Test
public void testOneAffirmativeVoteTwoAbstainVotesGrantsAccess() throws Exception {
    TestingAuthenticationToken auth = makeTestToken();
    UnanimousBased mgr = makeDecisionManager();
    List<ConfigAttribute> config = SecurityConfig.createList("ROLE_2");
    mgr.decide(auth, new Object(), config);
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 45 with TestingAuthenticationToken

use of org.springframework.security.authentication.TestingAuthenticationToken in project spring-security by spring-projects.

the class UnanimousBasedTests method testThreeAbstainVotesGrantsAccessWithoutDefault.

@Test
public void testThreeAbstainVotesGrantsAccessWithoutDefault() throws Exception {
    TestingAuthenticationToken auth = makeTestToken();
    UnanimousBased mgr = makeDecisionManager();
    mgr.setAllowIfAllAbstainDecisions(true);
    // check changed
    assertThat(mgr.isAllowIfAllAbstainDecisions()).isTrue();
    List<ConfigAttribute> config = SecurityConfig.createList("IGNORED_BY_ALL");
    mgr.decide(auth, new Object(), config);
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Aggregations

TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)183 Test (org.junit.Test)106 Authentication (org.springframework.security.core.Authentication)76 SecurityContext (org.springframework.security.core.context.SecurityContext)46 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)38 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)38 MifosUser (org.mifos.security.MifosUser)36 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)32 MifosUserBuilder (org.mifos.builders.MifosUserBuilder)28 Before (org.junit.Before)25 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 ConfigAttribute (org.springframework.security.access.ConfigAttribute)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 MockFilterChain (org.springframework.mock.web.MockFilterChain)9 GrantedAuthority (org.springframework.security.core.GrantedAuthority)9 FilterChain (javax.servlet.FilterChain)8 MutableAcl (org.springframework.security.acls.model.MutableAcl)8 PrincipalSid (org.springframework.security.acls.domain.PrincipalSid)7