Browse Source

Increased test coverage of the .jaas package to 93.7%

Ray Krueger 21 năm trước cách đây
mục cha
commit
3b284231da

+ 37 - 2
core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.java

@@ -6,7 +6,8 @@ import net.sf.acegisecurity.AuthenticationException;
 import net.sf.acegisecurity.GrantedAuthority;
 import net.sf.acegisecurity.GrantedAuthorityImpl;
 import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
-import org.springframework.context.support.FileSystemXmlApplicationContext;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 import java.util.Arrays;
 import java.util.List;
@@ -21,10 +22,13 @@ import java.util.List;
 public class JaasAuthenticationProviderTests extends TestCase {
 
     private JaasAuthenticationProvider jaasProvider;
+    private ApplicationContext context;
+    private JaasEventCheck eventCheck;
 
     protected void setUp() throws Exception {
         String resName = "/" + getClass().getName().replace('.', '/') + ".xml";
-        FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(getClass().getResource(resName).toString());
+        context = new ClassPathXmlApplicationContext(resName);
+        eventCheck = (JaasEventCheck) context.getBean("eventCheck");
         jaasProvider = (JaasAuthenticationProvider) context.getBean("jaasAuthenticationProvider");
     }
 
@@ -40,8 +44,15 @@ public class JaasAuthenticationProviderTests extends TestCase {
 
         UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password", defaultAuths);
 
+        assertTrue(jaasProvider.supports(UsernamePasswordAuthenticationToken.class));
+
         Authentication auth = jaasProvider.authenticate(token);
 
+        assertNotNull(jaasProvider.getAuthorityGranters());
+        assertNotNull(jaasProvider.getCallbackHandlers());
+        assertNotNull(jaasProvider.getLoginConfig());
+        assertNotNull(jaasProvider.getLoginContextName());
+
         List list = Arrays.asList(auth.getAuthorities());
 
         assertTrue("GrantedAuthorities does not contain ROLE_TEST",
@@ -50,6 +61,22 @@ public class JaasAuthenticationProviderTests extends TestCase {
         assertTrue("GrantedAuthorities does not contain ROLE_1", list.contains(role1));
 
         assertTrue("GrantedAuthorities does not contain ROLE_2", list.contains(role2));
+
+        boolean foundit = false;
+        for (int i = 0; i < list.size(); i++) {
+            Object obj = list.get(i);
+            if (obj instanceof JaasGrantedAuthority) {
+                JaasGrantedAuthority grant = (JaasGrantedAuthority) obj;
+                assertNotNull("Principal was null on JaasGrantedAuthority", grant.getPrincipal());
+                foundit = true;
+            }
+        }
+        assertTrue("Could not find a JaasGrantedAuthority", foundit);
+
+        assertNotNull("Success event not fired", eventCheck.successEvent);
+        assertEquals("Auth objects are not equal", auth, eventCheck.successEvent.getAuthentication());
+
+        assertNull("Failure event was fired", eventCheck.failedEvent);
     }
 
     public void testBadUser() {
@@ -58,6 +85,10 @@ public class JaasAuthenticationProviderTests extends TestCase {
             fail("LoginException should have been thrown for the bad user");
         } catch (AuthenticationException e) {
         }
+
+        assertNotNull("Failure event not fired", eventCheck.failedEvent);
+        assertNotNull("Failure event exception was null", eventCheck.failedEvent.getException());
+        assertNull("Success event was fired", eventCheck.successEvent);
     }
 
     public void testBadPassword() {
@@ -66,6 +97,10 @@ public class JaasAuthenticationProviderTests extends TestCase {
             fail("LoginException should have been thrown for the bad password");
         } catch (AuthenticationException e) {
         }
+
+        assertNotNull("Failure event not fired", eventCheck.failedEvent);
+        assertNotNull("Failure event exception was null", eventCheck.failedEvent.getException());
+        assertNull("Success event was fired", eventCheck.successEvent);
     }
 
 }

+ 3 - 0
core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.xml

@@ -2,6 +2,9 @@
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 
 <beans>
+
+    <bean id="eventCheck" class="net.sf.acegisecurity.providers.jaas.JaasEventCheck"/>
+
     <bean id="jaasAuthenticationProvider" class="net.sf.acegisecurity.providers.jaas.JaasAuthenticationProvider">
         <property name="loginContextName">
             <value>JAASTest</value>

+ 4 - 3
core/src/test/java/org/acegisecurity/providers/jaas/TestAuthorityGranter.java

@@ -5,14 +5,15 @@ import java.security.Principal;
 /**
  * Insert comments here...
  * <br>
- * 
+ *
  * @author Ray Krueger
  * @version $Id$
  */
 public class TestAuthorityGranter implements AuthorityGranter {
     public String grant(Principal principal) {
+        String role = null;
         if (principal.getName().equals("TEST_PRINCIPAL"))
-            return "ROLE_TEST";
-        return null;
+            role = "ROLE_TEST";
+        return role;
     }
 }

+ 1 - 4
core/src/test/java/org/acegisecurity/providers/jaas/TestCallbackHandler.java

@@ -23,12 +23,9 @@ public class TestCallbackHandler implements JaasAuthenticationCallbackHandler {
     }
 
     public void handle(Callback callback) throws IOException, UnsupportedCallbackException {
-
-        if (auth == null) throw new RuntimeException("TEST FAILURE: setAuthentication was never called");
-
         if (callback instanceof TextInputCallback) {
             TextInputCallback tic = (TextInputCallback) callback;
-            tic.setText(getClass().getName());
+            tic.setText(auth.getPrincipal().toString());
         }
     }
 }

+ 6 - 4
core/src/test/java/org/acegisecurity/providers/jaas/TestLoginModule.java

@@ -43,6 +43,12 @@ public class TestLoginModule implements LoginModule {
                 return "TEST_PRINCIPAL";
             }
         });
+
+        subject.getPrincipals().add(new Principal() {
+            public String getName() {
+                return "NULL_PRINCIPAL";
+            }
+        });
         return true;
     }
 
@@ -63,11 +69,7 @@ public class TestLoginModule implements LoginModule {
             password = new String(passwordCallback.getPassword());
             user = nameCallback.getName();
 
-            if (!TestCallbackHandler.class.getName().equals(textCallback.getText()))
-                throw new RuntimeException("TEST FAILURE: " + textCallback.getText() + "!=" + TestCallbackHandler.class.getName());
-
         } catch (Exception e) {
-            e.printStackTrace();
             throw new RuntimeException(e);
         }
     }