1 package org.apache.archiva.redback.common.ldap.role;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import junit.framework.TestCase;
22 import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
23 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
24 import org.apache.archiva.redback.components.apacheds.ApacheDs;
25 import org.apache.archiva.redback.policy.PasswordEncoder;
26 import org.apache.archiva.redback.policy.encoders.SHA1PasswordEncoder;
27 import org.fest.assertions.Assertions;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.test.annotation.DirtiesContext;
35 import org.springframework.test.context.ContextConfiguration;
36 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import javax.naming.NamingEnumeration;
41 import javax.naming.NamingException;
42 import javax.naming.directory.Attribute;
43 import javax.naming.directory.Attributes;
44 import javax.naming.directory.BasicAttribute;
45 import javax.naming.directory.BasicAttributes;
46 import javax.naming.directory.DirContext;
47 import javax.naming.directory.InitialDirContext;
48 import javax.naming.directory.SearchControls;
49 import javax.naming.directory.SearchResult;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.HashMap;
53 import java.util.List;
57 * @author Olivier Lamy
59 @RunWith(SpringJUnit4ClassRunner.class)
60 @ContextConfiguration(
61 locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-role-mapper.xml" })
62 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
63 public class TestLdapRoleMapper
67 Logger log = LoggerFactory.getLogger( getClass() );
70 @Named(value = "apacheDS#test")
71 private ApacheDs apacheDs;
73 private String suffix;
75 private String groupSuffix;
77 private PasswordEncoder passwordEncoder;
80 //private LdapCacheService ldapCacheService;
83 @Named(value = "ldapRoleMapper#test")
84 LdapRoleMapper ldapRoleMapper;
87 LdapConnectionFactory ldapConnectionFactory;
89 LdapConnection ldapConnection;
93 private Map<String, List<String>> usersPerGroup;
95 private List<String> users;
103 usersPerGroup = new HashMap<String, List<String>>( 3 );
105 usersPerGroup.put( "internal-repo-manager", Arrays.asList( "admin", "user.9" ) );
106 usersPerGroup.put( "internal-repo-observer", Arrays.asList( "admin", "user.7", "user.8" ) );
107 usersPerGroup.put( "archiva-admin", Arrays.asList( "admin", "user.7" ) );
109 users = new ArrayList<String>( 4 );
110 users.add( "admin" );
111 users.add( "user.7" );
112 users.add( "user.8" );
113 users.add( "user.9" );
115 passwordEncoder = new SHA1PasswordEncoder();
117 groupSuffix = apacheDs.addSimplePartition( "test", new String[]{ "archiva", "apache", "org" } ).getSuffix();
119 log.info( "groupSuffix: {}", groupSuffix );
121 suffix = "ou=People,dc=archiva,dc=apache,dc=org";
123 log.info( "DN Suffix: {}", suffix );
125 apacheDs.startServer();
127 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
128 objectClass.add( "top" );
129 objectClass.add( "organizationalUnit" );
131 Attributes attributes = new BasicAttributes( true );
132 attributes.put( objectClass );
133 attributes.put( "organizationalUnitName", "foo" );
135 apacheDs.getAdminContext().createSubcontext( suffix, attributes );
145 public void tearDown()
149 //ldapCacheService.removeAllUsers();
151 InitialDirContext context = apacheDs.getAdminContext();
153 for ( String uid : users )
155 context.unbind( createDn( uid ) );
158 for ( Map.Entry<String, List<String>> group : usersPerGroup.entrySet() )
160 context.unbind( createGroupDn( group.getKey() ) );
163 context.unbind( suffix );
167 ldapConnection.close();
169 apacheDs.stopServer();
174 protected DirContext getDirContext()
177 ldapConnection = ldapConnectionFactory.getConnection();
178 context = ldapConnection.getDirContext();
182 private void createGroups()
185 InitialDirContext context = apacheDs.getAdminContext();
187 for ( Map.Entry<String, List<String>> group : usersPerGroup.entrySet() )
189 createGroup( context, group.getKey(), createGroupDn( group.getKey() ), group.getValue() );
194 private void createGroup( DirContext context, String groupName, String dn, List<String> users )
198 Attributes attributes = new BasicAttributes( true );
199 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
200 objectClass.add( "top" );
201 objectClass.add( "groupOfUniqueNames" );
202 attributes.put( objectClass );
203 attributes.put( "cn", groupName );
204 BasicAttribute basicAttribute = new BasicAttribute( "uniquemember" );
205 for ( String user : users )
207 basicAttribute.add( "uid=" + user + "," + suffix );// dc=archiva,dc=apache,dc=org" );
210 attributes.put( basicAttribute );
211 context.createSubcontext( dn, attributes );
214 private void bindUserObject( DirContext context, String cn, String dn )
217 Attributes attributes = new BasicAttributes( true );
218 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
219 objectClass.add( "top" );
220 objectClass.add( "inetOrgPerson" );
221 objectClass.add( "person" );
222 objectClass.add( "organizationalperson" );
223 attributes.put( objectClass );
224 attributes.put( "cn", cn );
225 attributes.put( "sn", "foo" );
226 attributes.put( "mail", cn + "@apache.org" );
227 attributes.put( "userPassword", passwordEncoder.encodePassword( "foo" ) );
228 attributes.put( "givenName", "foo" );
229 context.createSubcontext( dn, attributes );
232 private void makeUsers()
236 for ( String uid : users )
243 private void makeUser( String uid )
246 InitialDirContext context = apacheDs.getAdminContext();
248 bindUserObject( context, uid, createDn( uid ) );
249 assertExist( context, createDn( uid ), "cn", uid );
253 private void assertExist( DirContext context, String dn, String attribute, String value )
254 throws NamingException
256 SearchControls ctls = new SearchControls();
258 ctls.setDerefLinkFlag( true );
259 ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
260 ctls.setReturningAttributes( new String[]{ "*" } );
262 BasicAttributes matchingAttributes = new BasicAttributes();
263 matchingAttributes.put( attribute, value );
264 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
265 objectClass.add( "inetOrgPerson" );
266 matchingAttributes.put( objectClass );
268 NamingEnumeration<SearchResult> results = context.search( suffix, matchingAttributes );
270 assertTrue( results.hasMoreElements() );
271 SearchResult result = results.nextElement();
272 Attributes attrs = result.getAttributes();
273 Attribute testAttr = attrs.get( attribute );
274 assertEquals( value, testAttr.get() );
278 private String createDn( String cn )
280 return "cn=" + cn + "," + suffix;
283 private String createGroupDn( String cn )
285 return "cn=" + cn + "," + groupSuffix;
290 public void getAllGroups()
293 List<String> allGroups = ldapRoleMapper.getAllGroups( getDirContext() );
295 log.info( "allGroups: {}", allGroups );
297 Assertions.assertThat( allGroups ).isNotNull().isNotEmpty().contains( "archiva-admin",
298 "internal-repo-manager" );
302 public void getGroupsMember()
305 List<String> users = ldapRoleMapper.getGroupsMember( "archiva-admin", getDirContext() );
307 log.info( "users for archiva-admin: {}", users );
309 Assertions.assertThat( users ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "admin", "user.7" );
311 users = ldapRoleMapper.getGroupsMember( "internal-repo-observer", getDirContext() );
313 Assertions.assertThat( users ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "admin", "user.7", "user.8" );
317 public void getGroups()
320 List<String> groups = ldapRoleMapper.getGroups( "admin", getDirContext() );
322 log.info( "groups for admin: {}", groups );
324 Assertions.assertThat( groups ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "archiva-admin",
325 "internal-repo-manager",
326 "internal-repo-observer" );
328 groups = ldapRoleMapper.getGroups( "user.8", getDirContext() );
330 Assertions.assertThat( groups ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "internal-repo-observer" );
332 groups = ldapRoleMapper.getGroups( "user.7", getDirContext() );
334 Assertions.assertThat( groups ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "archiva-admin",
335 "internal-repo-observer" );
339 public void getRoles()
342 List<String> roles = ldapRoleMapper.getRoles( "admin", getDirContext() );
344 log.info( "roles for admin: {}", roles );
346 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "Archiva System Administrator",
347 "Internal Repo Manager",
348 "Internal Repo Observer" );
350 roles = ldapRoleMapper.getRoles( "user.7", getDirContext() );
352 log.info( "roles for user.7: {}", roles );
354 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "Archiva System Administrator",
355 "Internal Repo Observer" );
357 roles = ldapRoleMapper.getRoles( "user.8", getDirContext() );
359 log.info( "roles for user.8: {}", roles );
361 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "Internal Repo Observer" );