001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.acl.basic;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: import org.acegisecurity.acl.AclEntry;
024:
025: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
026:
027: import org.acegisecurity.userdetails.User;
028:
029: /**
030: * Tests {@link GrantedAuthorityEffectiveAclsResolver}.
031: *
032: * @author Ben Alex
033: * @version $Id: GrantedAuthorityEffectiveAclsResolverTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class GrantedAuthorityEffectiveAclsResolverTests extends
036: TestCase {
037: //~ Instance fields ================================================================================================
038:
039: private SimpleAclEntry entry100RoleEverybody = new SimpleAclEntry(
040: "ROLE_EVERYBODY", new NamedEntityObjectIdentity("OBJECT",
041: "100"), null, 14);
042: private SimpleAclEntry entry100RoleOne = new SimpleAclEntry(
043: "ROLE_ONE", new NamedEntityObjectIdentity("OBJECT", "100"),
044: null, 0);
045: private SimpleAclEntry entry100RoleTwo = new SimpleAclEntry(
046: "ROLE_TWO", new NamedEntityObjectIdentity("OBJECT", "100"),
047: null, 2);
048: private UsernamePasswordAuthenticationToken scott = new UsernamePasswordAuthenticationToken(
049: "scott", "not used", new GrantedAuthority[] {
050: new GrantedAuthorityImpl("ROLE_EVERYBODY"),
051: new GrantedAuthorityImpl("ROLE_TWO") });
052: private SimpleAclEntry entry100Scott = new SimpleAclEntry(scott
053: .getPrincipal(), new NamedEntityObjectIdentity("OBJECT",
054: "100"), null, 4);
055: private UsernamePasswordAuthenticationToken dianne = new UsernamePasswordAuthenticationToken(
056: "dianne", "not used");
057: private UsernamePasswordAuthenticationToken marissa = new UsernamePasswordAuthenticationToken(
058: "marissa", "not used", new GrantedAuthority[] {
059: new GrantedAuthorityImpl("ROLE_EVERYBODY"),
060: new GrantedAuthorityImpl("ROLE_ONE") });
061: private SimpleAclEntry entry100Marissa = new SimpleAclEntry(marissa
062: .getPrincipal(), new NamedEntityObjectIdentity("OBJECT",
063: "100"), null, 2);
064: private UsernamePasswordAuthenticationToken scottWithUserDetails = new UsernamePasswordAuthenticationToken(
065: new User("scott", "NOT_USED", true, true, true, true,
066: new GrantedAuthority[] { new GrantedAuthorityImpl(
067: "ROLE_EVERYBODY") }), "not used",
068: new GrantedAuthority[] {
069: new GrantedAuthorityImpl("ROLE_EVERYBODY"),
070: new GrantedAuthorityImpl("ROLE_TWO") });
071:
072: // convenience group
073: private SimpleAclEntry[] acls = { entry100Marissa, entry100Scott,
074: entry100RoleEverybody, entry100RoleOne, entry100RoleTwo };
075:
076: //~ Constructors ===================================================================================================
077:
078: public GrantedAuthorityEffectiveAclsResolverTests() {
079: super ();
080: }
081:
082: public GrantedAuthorityEffectiveAclsResolverTests(String arg0) {
083: super (arg0);
084: }
085:
086: //~ Methods ========================================================================================================
087:
088: public static void main(String[] args) {
089: junit.textui.TestRunner
090: .run(GrantedAuthorityEffectiveAclsResolverTests.class);
091: }
092:
093: public final void setUp() throws Exception {
094: super .setUp();
095: }
096:
097: public void testResolveAclsForDianneWhoHasANullForAuthorities() {
098: GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
099: assertNull(resolver.resolveEffectiveAcls(acls, dianne));
100: }
101:
102: public void testResolveAclsForMarissa() {
103: GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
104: assertEquals(3,
105: resolver.resolveEffectiveAcls(acls, marissa).length);
106: assertEquals(entry100Marissa, resolver.resolveEffectiveAcls(
107: acls, marissa)[0]);
108: assertEquals(entry100RoleEverybody, resolver
109: .resolveEffectiveAcls(acls, marissa)[1]);
110: assertEquals(entry100RoleOne, resolver.resolveEffectiveAcls(
111: acls, marissa)[2]);
112: }
113:
114: public void testResolveAclsForScottWithStringObjectAsPrincipal() {
115: GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
116: assertEquals(3,
117: resolver.resolveEffectiveAcls(acls, scott).length);
118: assertEquals(entry100Scott, resolver.resolveEffectiveAcls(acls,
119: scott)[0]);
120: assertEquals(entry100RoleEverybody, resolver
121: .resolveEffectiveAcls(acls, scott)[1]);
122: assertEquals(entry100RoleTwo, resolver.resolveEffectiveAcls(
123: acls, scott)[2]);
124: }
125:
126: public void testResolveAclsForScottWithUserDetailsObjectAsPrincipal() {
127: GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
128: assertEquals(3, resolver.resolveEffectiveAcls(acls,
129: scottWithUserDetails).length);
130: assertEquals(entry100Scott, resolver.resolveEffectiveAcls(acls,
131: scottWithUserDetails)[0]);
132: assertEquals(entry100RoleEverybody, resolver
133: .resolveEffectiveAcls(acls, scottWithUserDetails)[1]);
134: assertEquals(entry100RoleTwo, resolver.resolveEffectiveAcls(
135: acls, scottWithUserDetails)[2]);
136: }
137:
138: public void testResolveAclsReturnsNullIfNoAclsInFirstPlace() {
139: GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
140: assertNull(resolver.resolveEffectiveAcls(null, scott));
141: }
142:
143: public void testSkipsNonBasicAclEntryObjects() {
144: GrantedAuthorityEffectiveAclsResolver resolver = new GrantedAuthorityEffectiveAclsResolver();
145: AclEntry[] basicAcls = { entry100Marissa, entry100Scott,
146: entry100RoleEverybody, entry100RoleOne, new MockAcl(),
147: entry100RoleTwo };
148: assertEquals(3, resolver.resolveEffectiveAcls(basicAcls,
149: marissa).length);
150: }
151:
152: //~ Inner Classes ==================================================================================================
153:
154: private class MockAcl implements AclEntry {
155: // does nothing
156: }
157: }
|