]> source.dussan.org Git - archiva.git/commitdiff
fix units tets
authorOlivier Lamy <olamy@apache.org>
Thu, 3 Jan 2013 17:03:44 +0000 (17:03 +0000)
committerOlivier Lamy <olamy@apache.org>
Thu, 3 Jan 2013 17:03:44 +0000 (17:03 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/redback/redback-core/trunk@1428478 13f79535-47bb-0310-9956-ffa450edef68

redback-rbac/redback-rbac-providers/redback-rbac-ldap/pom.xml
redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/java/org/apache/archiva/redback/rbac/ldap/TestLdapRoleMapper.java
redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/resources/spring-context.xml
redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java

index 0423d7d3e2a16b142d7fc5c56a8329b62987fcda..2a2e08fa5940661c6550eeb766503516b936638b 100644 (file)
       <artifactId>fest-assert</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.archiva.redback.components</groupId>
+      <artifactId>spring-apacheds</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
       </plugin>
+      <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>build-helper-maven-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>allocate-ldap-port</id>
+            <phase>process-classes</phase>
+            <goals>
+              <goal>reserve-network-port</goal>
+            </goals>
+            <configuration>
+              <portNames>
+                <portName>ldapPort</portName>
+              </portNames>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <systemPropertyVariables>
+            <ldapPort>${ldapPort}</ldapPort>
+            <basedir>${basedir}</basedir>
+          </systemPropertyVariables>
+        </configuration>
+      </plugin>
     </plugins>
   </build>
 
index c556cfd42ceb1154cee5341bf422ce57b3d9eeb0..5a4a18ec76e3a9efa7a9ac1585d5f451f66ced83 100644 (file)
@@ -19,7 +19,14 @@ package org.apache.archiva.redback.rbac.ldap;
  */
 
 import junit.framework.TestCase;
+import org.apache.archiva.redback.components.apacheds.ApacheDs;
+import org.apache.archiva.redback.policy.PasswordEncoder;
+import org.apache.archiva.redback.policy.encoders.SHA1PasswordEncoder;
+import org.apache.archiva.redback.users.UserManager;
+import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
 import org.fest.assertions.Assertions;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.slf4j.Logger;
@@ -30,6 +37,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 import javax.inject.Inject;
 import javax.inject.Named;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
 import java.util.List;
 
 /**
@@ -44,10 +61,191 @@ public class TestLdapRoleMapper
 
     Logger log = LoggerFactory.getLogger( getClass() );
 
+    @Inject
+    @Named( value = "userManager#ldap" )
+    private UserManager userManager;
+
+    @Inject
+    @Named( value = "apacheDS#test" )
+    private ApacheDs apacheDs;
+
+    private String suffix;
+
+    private String groupSuffix;
+
+    private PasswordEncoder passwordEncoder;
+
+    @Inject
+    private LdapCacheService ldapCacheService;
+
     @Inject
     @Named( value = "ldapRoleMapper#test" )
     LdapRoleMapper ldapRoleMapper;
 
+    @Before
+    public void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        passwordEncoder = new SHA1PasswordEncoder();
+
+        groupSuffix = "dc=archiva,dc=apache,dc=org";
+        log.info( "groupSuffix: {}", groupSuffix );
+
+        suffix = apacheDs.addSimplePartition( "test", new String[]{ "archiva", "apache", "org" } ).getSuffix();
+
+        log.info( "DN Suffix: {}", suffix );
+
+        apacheDs.startServer();
+
+        clearManyUsers();
+
+        makeUsers();
+
+        createGroups();
+    }
+
+    @After
+    public void tearDown()
+        throws Exception
+    {
+        // clear cache
+        ldapCacheService.removeAllUsers();
+
+        InitialDirContext context = apacheDs.getAdminContext();
+
+        context.unbind( createDn( "admin" ) );
+
+        context.unbind( createDn( "user.7" ) );
+
+        context.unbind( createGroupDn( "internal-repo-manager" ) );
+
+        context.unbind( createGroupDn( "archiva-admin" ) );
+
+        apacheDs.stopServer();
+
+        super.tearDown();
+    }
+
+    private void createGroups()
+        throws Exception
+    {
+        InitialDirContext context = apacheDs.getAdminContext();
+
+        createGroup( context, "internal-repo-manager", createGroupDn( "internal-repo-manager" ) );
+
+        createGroup( context, "archiva-admin", createGroupDn( "archiva-admin" ) );
+    }
+
+    private void createGroup( DirContext context, String groupName, String dn )
+        throws Exception
+    {
+
+        Attributes attributes = new BasicAttributes( true );
+        BasicAttribute objectClass = new BasicAttribute( "objectClass" );
+        objectClass.add( "top" );
+        objectClass.add( "groupOfUniqueNames" );
+        attributes.put( objectClass );
+        attributes.put( "cn", groupName );
+        BasicAttribute basicAttribute = new BasicAttribute( "uniquemember"  );
+        basicAttribute.add( "uid=admin,dc=archiva,dc=apache,dc=org" );
+        basicAttribute.add( "uid=user.7,dc=archiva,dc=apache,dc=org" );
+        attributes.put( basicAttribute );
+        //attributes.put( "uniquemember", "uid=admin,dc=archiva,dc=apache,dc=org" );
+        //attributes.put( "uniquemember", "uid=user.7,dc=archiva,dc=apache,dc=org" );
+
+        context.createSubcontext( dn, attributes );
+    }
+
+    private void bindUserObject( DirContext context, String cn, String dn )
+        throws Exception
+    {
+        Attributes attributes = new BasicAttributes( true );
+        BasicAttribute objectClass = new BasicAttribute( "objectClass" );
+        objectClass.add( "top" );
+        objectClass.add( "inetOrgPerson" );
+        objectClass.add( "person" );
+        objectClass.add( "organizationalperson" );
+        attributes.put( objectClass );
+        attributes.put( "cn", cn );
+        attributes.put( "sn", "foo" );
+        attributes.put( "mail", cn + "@apache.org" );
+        attributes.put( "userPassword", passwordEncoder.encodePassword( "foo" ) );
+        attributes.put( "givenName", "foo" );
+        context.createSubcontext( dn, attributes );
+    }
+
+    private void makeUsers()
+        throws Exception
+    {
+        InitialDirContext context = apacheDs.getAdminContext();
+
+        String cn = "admin";
+        bindUserObject( context, cn, createDn( cn ) );
+        assertExist( context, createDn( cn ), "cn", cn );
+
+        cn = "user.7";
+        bindUserObject( context, cn, createDn( cn ) );
+        assertExist( context, createDn( cn ), "cn", cn );
+
+    }
+
+    private void clearManyUsers()
+        throws Exception
+    {
+        InitialDirContext context = apacheDs.getAdminContext();
+
+        for ( int i = 0; i < 10000; i++ )
+        {
+            String cn = "user" + i;
+            try
+            {
+                context.unbind( createDn( cn ) );
+            }
+            catch ( NamingException e )
+            {
+                // OK lets try with next one
+            }
+        }
+
+    }
+
+
+    private void assertExist( DirContext context, String dn, String attribute, String value )
+        throws NamingException
+    {
+        SearchControls ctls = new SearchControls();
+
+        ctls.setDerefLinkFlag( true );
+        ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
+        ctls.setReturningAttributes( new String[]{ "*" } );
+
+        BasicAttributes matchingAttributes = new BasicAttributes();
+        matchingAttributes.put( attribute, value );
+        BasicAttribute objectClass = new BasicAttribute( "objectClass" );
+        objectClass.add( "inetOrgPerson" );
+        matchingAttributes.put( objectClass );
+
+        NamingEnumeration<SearchResult> results = context.search( suffix, matchingAttributes );
+
+        assertTrue( results.hasMoreElements() );
+        SearchResult result = results.nextElement();
+        Attributes attrs = result.getAttributes();
+        Attribute testAttr = attrs.get( attribute );
+        assertEquals( value, testAttr.get() );
+
+    }
+
+    private String createDn( String cn )
+    {
+        return "cn=" + cn + "," + suffix;
+    }
+
+    private String createGroupDn( String cn )
+    {
+        return "cn=" + cn + "," + groupSuffix;
+    }
 
     @Test
     public void getAllGroups()
index 403f4195ceefab5d6e35e5db63110a7ebee42d29..1540d7b3164165e2b8ec58ca1c990a839daa62fc 100755 (executable)
   -->
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
-           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
+           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+           http://www.springframework.org/schema/context
+           http://www.springframework.org/schema/context/spring-context-3.0.xsd"
        default-lazy-init="false">
+  <context:property-placeholder system-properties-mode="OVERRIDE"/>
 
   <alias name="userConfiguration#redback" alias="userConfiguration#default"/>
 
-  <bean name="ldapConnectionFactory#configurable" class="org.apache.archiva.redback.common.ldap.connection.ConfigurableLdapConnectionFactory">
+  <bean name="apacheDS#test" class="org.apache.archiva.redback.components.apacheds.DefaultApacheDs"
+        scope="prototype">
+    <property name="basedir" value="${basedir}/target/apacheds"/>
+    <property name="port" value="${ldapPort}"/>
+    <property name="enableNetworking" value="true"/>
+    <property name="password" value="secret"/>
+  </bean>
+
+  <bean name="ldapConnectionFactory#configurable"
+        class="org.apache.archiva.redback.common.ldap.connection.ConfigurableLdapConnectionFactory">
     <property name="hostname" value="localhost"/>
-    <property name="port" value="1389"/>
+    <property name="port" value="${ldapPort}"/>
     <!--property name="baseDn" value="dc=redback,dc=plexus,dc=codehaus,dc=org"/-->
     <property name="baseDn" value="dc=archiva,dc=apache,dc=org"/>
     <property name="contextFactory" value="com.sun.jndi.ldap.LdapCtxFactory"/>
-    <property name="password" value="theadmin"/>
-    <!--property name="bindDn" value="uid=admin,ou=system"/-->
-    <property name="bindDn" value="uid=admin,ou=People,dc=archiva,dc=apache,dc=org"/>
+    <property name="password" value="secret"/>
+    <property name="bindDn" value="uid=admin,ou=system"/>
     <property name="userConf" ref="userConfiguration#default"/>
   </bean>
 
 
-
   <bean name="ldapRoleMapper#test" class="org.apache.archiva.redback.rbac.ldap.DefaultLdapRoleMapper">
     <property name="groupsDn" value="dc=archiva,dc=apache,dc=org"/>
     <property name="ldapGroupClass" value="groupOfUniqueNames"/>
index 1cb8f09875873b7f8f91e0746597d42c3ea61bac..591a023c2c4ecc6f35a87a6ca69be8f1fdb41a57 100644 (file)
@@ -86,11 +86,6 @@ public class LdapUserManagerTest
     @Inject
     private LdapCacheService ldapCacheService;
 
-    public void testFoo()
-        throws Exception
-    {
-
-    }
 
     @Before
     public void setUp()