You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ol...@apache.org on 2013/01/03 18:03:44 UTC

svn commit: r1428478 - in /archiva/redback/redback-core/trunk: redback-rbac/redback-rbac-providers/redback-rbac-ldap/ redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/java/org/apache/archiva/redback/rbac/ldap/ redback-rbac/redback-rbac-pr...

Author: olamy
Date: Thu Jan  3 17:03:44 2013
New Revision: 1428478

URL: http://svn.apache.org/viewvc?rev=1428478&view=rev
Log:
fix units tets

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

Modified: archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/pom.xml
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/pom.xml?rev=1428478&r1=1428477&r2=1428478&view=diff
==============================================================================
--- archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/pom.xml (original)
+++ archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/pom.xml Thu Jan  3 17:03:44 2013
@@ -93,6 +93,11 @@
       <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>
@@ -101,6 +106,34 @@
         <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>
 

Modified: archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/java/org/apache/archiva/redback/rbac/ldap/TestLdapRoleMapper.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/java/org/apache/archiva/redback/rbac/ldap/TestLdapRoleMapper.java?rev=1428478&r1=1428477&r2=1428478&view=diff
==============================================================================
--- archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/java/org/apache/archiva/redback/rbac/ldap/TestLdapRoleMapper.java (original)
+++ archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/java/org/apache/archiva/redback/rbac/ldap/TestLdapRoleMapper.java Thu Jan  3 17:03:44 2013
@@ -19,7 +19,14 @@ package org.apache.archiva.redback.rbac.
  */
 
 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.
 
 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;
 
 /**
@@ -45,9 +62,190 @@ 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()

Modified: archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/resources/spring-context.xml
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/resources/spring-context.xml?rev=1428478&r1=1428477&r2=1428478&view=diff
==============================================================================
--- archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/resources/spring-context.xml (original)
+++ archiva/redback/redback-core/trunk/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/test/resources/spring-context.xml Thu Jan  3 17:03:44 2013
@@ -20,26 +20,37 @@
   -->
 <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"/>

Modified: archiva/redback/redback-core/trunk/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java?rev=1428478&r1=1428477&r2=1428478&view=diff
==============================================================================
--- archiva/redback/redback-core/trunk/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java (original)
+++ archiva/redback/redback-core/trunk/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java Thu Jan  3 17:03:44 2013
@@ -86,11 +86,6 @@ public class LdapUserManagerTest
     @Inject
     private LdapCacheService ldapCacheService;
 
-    public void testFoo()
-        throws Exception
-    {
-
-    }
 
     @Before
     public void setUp()