*/
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;
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;
/**
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()
-->
<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"/>