Ver Fonte

Added extra tests for jdbc-user-details service to make sure it works within an <authentication-provider> element.

Luke Taylor há 17 anos atrás
pai
commit
2c6fb3d1c9

+ 11 - 2
core/src/test/java/org/springframework/security/config/DataSourcePopulator.java

@@ -17,8 +17,13 @@ package org.springframework.security.config;
 import javax.sql.DataSource;
 
 import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.DisposableBean;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.util.Assert;
+import org.springframework.context.ApplicationEventPublisherAware;
+import org.springframework.context.ApplicationEventPublisher;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.ApplicationEvent;
 
 
 /**
@@ -27,7 +32,7 @@ import org.springframework.util.Assert;
  * @author Ben Alex
  * @version $Id: DataSourcePopulator.java 2291 2007-12-03 02:56:52Z benalex $
  */
-public class DataSourcePopulator implements InitializingBean {
+public class DataSourcePopulator implements InitializingBean, DisposableBean {
     //~ Instance fields ================================================================================================
 
     JdbcTemplate template;
@@ -50,7 +55,7 @@ public class DataSourcePopulator implements InitializingBean {
            Encoded password for jane is "wombat"
 
          */
-        template.execute("INSERT INTO USERS VALUES('rod','a564de63c2d0da68cf47586ee05984d7',TRUE);");
+        template.execute("INSERT INTO USERS VALUES('rod','koala',TRUE);");
         template.execute("INSERT INTO USERS VALUES('dianne','65d15fe9156f9c4bbffd98085992a44e',TRUE);");
         template.execute("INSERT INTO USERS VALUES('scott','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);");
         template.execute("INSERT INTO USERS VALUES('peter','22b5c9accc6e1ba628cedc63a72d57f8',FALSE);");
@@ -71,4 +76,8 @@ public class DataSourcePopulator implements InitializingBean {
         this.template = new JdbcTemplate(dataSource);
     }
 
+    public void destroy() throws Exception {
+        template.execute("DROP TABLE AUTHORITIES");
+        template.execute("DROP TABLE USERS");
+    }
 }

+ 36 - 16
core/src/test/java/org/springframework/security/config/JdbcUserServiceBeanDefinitionParserTests.java

@@ -2,15 +2,13 @@ package org.springframework.security.config;
 
 import static org.junit.Assert.assertTrue;
 
-import java.util.List;
-
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
 import org.junit.Test;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
-import org.springframework.security.providers.ProviderManager;
-import org.springframework.security.providers.dao.DaoAuthenticationProvider;
+import org.junit.After;
+
 import org.springframework.security.userdetails.jdbc.JdbcUserDetailsManager;
+import org.springframework.security.util.InMemoryXmlApplicationContext;
+import org.springframework.security.AuthenticationManager;
+import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
 
 /**
  * @author Ben Alex
@@ -18,15 +16,21 @@ import org.springframework.security.userdetails.jdbc.JdbcUserDetailsManager;
  * @version $Id$
  */
 public class JdbcUserServiceBeanDefinitionParserTests {
-    private static ClassPathXmlApplicationContext appContext;
+    private InMemoryXmlApplicationContext appContext;
 
-    @BeforeClass
-    public static void loadContext() {
-        appContext = new ClassPathXmlApplicationContext("org/springframework/security/config/jdbc-user-details.xml");
-    }
+    private static String DATA_SOURCE =
+            "    <b:bean id='populator' class='org.springframework.security.config.DataSourcePopulator'>" +
+            "        <b:property name='dataSource' ref='dataSource'/>" +
+            "    </b:bean>" +
+            "    <b:bean id='dataSource' class='org.springframework.jdbc.datasource.DriverManagerDataSource'>" +
+            "        <b:property name='driverClassName' value='org.hsqldb.jdbcDriver'/>" +
+            "        <b:property name='url' value='jdbc:hsqldb:mem:jdbcnamespaces'/>" +
+            "        <b:property name='username' value='sa'/>" +
+            "        <b:property name='password' value=''/>" +
+            "    </b:bean>";
 
-    @AfterClass
-    public static void closeAppContext() {
+    @After
+    public void closeAppContext() {
         if (appContext != null) {
             appContext.close();
         }
@@ -34,12 +38,28 @@ public class JdbcUserServiceBeanDefinitionParserTests {
 
     @Test
     public void validUsernameIsFound() {
-    	JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean(BeanIds.USER_DETAILS_SERVICE);
+        setContext("<jdbc-user-service data-source-ref='dataSource'/>" + DATA_SOURCE);
+        JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean(BeanIds.USER_DETAILS_SERVICE);
     	assertTrue(mgr.loadUserByUsername("rod") != null);
     }
 
     @Test
     public void beanIdIsParsedCorrectly() {
-    	JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean("customUserService");
+        setContext("<jdbc-user-service id='customUserService' data-source-ref='dataSource'/>" + DATA_SOURCE);
+        JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean("customUserService");
+    }
+
+    @Test
+    public void isSupportedByAuthenticationProviderElement() {
+        setContext(
+                "<authentication-provider>" +
+                "    <jdbc-user-service data-source-ref='dataSource'/>" +
+                "</authentication-provider>" + DATA_SOURCE);
+        AuthenticationManager mgr = (AuthenticationManager) appContext.getBean(BeanIds.AUTHENTICATION_MANAGER);
+        mgr.authenticate(new UsernamePasswordAuthenticationToken("rod", "koala"));
+    }
+
+    private void setContext(String context) {
+        appContext = new InMemoryXmlApplicationContext(context);
     }
 }

+ 13 - 0
core/src/test/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParserTests.java

@@ -3,6 +3,10 @@ package org.springframework.security.config;
 import org.springframework.security.util.InMemoryXmlApplicationContext;
 import org.springframework.security.userdetails.UserDetailsService;
 import org.springframework.security.userdetails.UserDetails;
+import org.springframework.security.AuthenticationManager;
+import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
+import org.springframework.security.providers.ProviderManager;
+import org.springframework.security.providers.dao.DaoAuthenticationProvider;
 
 import org.junit.Test;
 import org.junit.After;
@@ -48,6 +52,15 @@ public class LdapUserServiceBeanDefinitionParserTests {
         assertEquals("Joe Smeth", joe.getUsername());
     }
 
+    @Test
+    public void isSupportedByAuthenticationProviderElement() {
+        setContext(
+                "<ldap-server url='ldap://127.0.0.1:343/dc=springframework,dc=org'/>" +
+                "<authentication-provider>" +
+                "    <ldap-user-service user-search-filter='(uid={0})' />" +
+                "</authentication-provider>");
+    }
+
     private void setContext(String context) {
         appCtx = new InMemoryXmlApplicationContext(context);
     }

+ 0 - 24
core/src/test/resources/org/springframework/security/config/jdbc-user-details.xml

@@ -1,24 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<beans:beans xmlns="http://www.springframework.org/schema/security"
-    xmlns:beans="http://www.springframework.org/schema/beans"
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
-http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
-
-	<beans:bean id="populator" class="org.springframework.security.config.DataSourcePopulator">
-		<beans:property name="dataSource" ref="dataSource"/>
-	</beans:bean>
-
-	<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-		<beans:property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
-		<beans:property name="url" value="jdbc:hsqldb:mem:jdbcnamespaces"/>
-		<beans:property name="username" value="sa"/>
-		<beans:property name="password" value=""/>
-	</beans:bean>
-
-    <jdbc-user-service data-source-ref="dataSource"/>
-
-    <jdbc-user-service id="customUserService" data-source-ref="dataSource"/>
-
-</beans:beans>