Selaa lähdekoodia

Expand test coverage now that prefix is configurable.

Ben Alex 21 vuotta sitten
vanhempi
commit
7b59d5f189

+ 31 - 0
core/src/test/java/org/acegisecurity/runas/RunAsManagerImplTests.java

@@ -75,6 +75,37 @@ public class RunAsManagerImplTests extends TestCase {
         assertEquals(null, resultingToken);
     }
 
+    public void testRespectsRolePrefix() throws Exception {
+        ConfigAttributeDefinition def = new ConfigAttributeDefinition();
+        def.addConfigAttribute(new SecurityConfig("RUN_AS_SOMETHING"));
+
+        UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test",
+                "Password",
+                new GrantedAuthority[] {new GrantedAuthorityImpl("ONE"), new GrantedAuthorityImpl("TWO")});
+
+        RunAsManagerImpl runAs = new RunAsManagerImpl();
+        runAs.setKey("my_password");
+        runAs.setRolePrefix("FOOBAR_");
+
+        Authentication resultingToken = runAs.buildRunAs(inputToken,
+                new Object(), def);
+
+        if (!(resultingToken instanceof RunAsUserToken)) {
+            fail("Should have returned a RunAsUserToken");
+        }
+
+        assertEquals(inputToken.getPrincipal(), resultingToken.getPrincipal());
+        assertEquals(inputToken.getCredentials(),
+            resultingToken.getCredentials());
+        assertEquals("FOOBAR_RUN_AS_SOMETHING",
+            resultingToken.getAuthorities()[0].getAuthority());
+        assertEquals("ONE", resultingToken.getAuthorities()[1].getAuthority());
+        assertEquals("TWO", resultingToken.getAuthorities()[2].getAuthority());
+
+        RunAsUserToken resultCast = (RunAsUserToken) resultingToken;
+        assertEquals("my_password".hashCode(), resultCast.getKeyHash());
+    }
+
     public void testReturnsAdditionalGrantedAuthorities()
         throws Exception {
         ConfigAttributeDefinition def = new ConfigAttributeDefinition();

+ 34 - 0
core/src/test/java/org/acegisecurity/vote/UnanimousBasedTests.java

@@ -100,6 +100,18 @@ public class UnanimousBasedTests extends TestCase {
         }
     }
 
+    public void testRoleVoterPrefixObserved() throws Exception {
+        TestingAuthenticationToken auth = makeTestTokenWithFooBarPrefix();
+        UnanimousBased mgr = makeDecisionManagerWithFooBarPrefix();
+
+        ConfigAttributeDefinition config = new ConfigAttributeDefinition();
+        config.addConfigAttribute(new SecurityConfig("FOOBAR_1")); // grant
+        config.addConfigAttribute(new SecurityConfig("FOOBAR_2")); // grant
+
+        mgr.decide(auth, new Object(), config);
+        assertTrue(true);
+    }
+
     public void testThreeAbstainVotesDeniesAccessWithDefault()
         throws Exception {
         TestingAuthenticationToken auth = makeTestToken();
@@ -159,9 +171,31 @@ public class UnanimousBasedTests extends TestCase {
         return decisionManager;
     }
 
+    private UnanimousBased makeDecisionManagerWithFooBarPrefix() {
+        UnanimousBased decisionManager = new UnanimousBased();
+        RoleVoter roleVoter = new RoleVoter();
+        roleVoter.setRolePrefix("FOOBAR_");
+
+        DenyVoter denyForSureVoter = new DenyVoter();
+        DenyAgainVoter denyAgainForSureVoter = new DenyAgainVoter();
+        List voters = new Vector();
+        voters.add(roleVoter);
+        voters.add(denyForSureVoter);
+        voters.add(denyAgainForSureVoter);
+        decisionManager.setDecisionVoters(voters);
+
+        return decisionManager;
+    }
+
     private TestingAuthenticationToken makeTestToken() {
         return new TestingAuthenticationToken("somebody", "password",
             new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_1"), new GrantedAuthorityImpl(
                     "ROLE_2")});
     }
+
+    private TestingAuthenticationToken makeTestTokenWithFooBarPrefix() {
+        return new TestingAuthenticationToken("somebody", "password",
+            new GrantedAuthority[] {new GrantedAuthorityImpl("FOOBAR_1"), new GrantedAuthorityImpl(
+                    "FOOBAR_2")});
+    }
 }