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.components.apacheds.ApacheDs;
23 import org.apache.archiva.redback.policy.PasswordEncoder;
24 import org.apache.archiva.redback.policy.encoders.SHA1PasswordEncoder;
25 import org.fest.assertions.Assertions;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.test.annotation.DirtiesContext;
33 import org.springframework.test.context.ContextConfiguration;
34 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import javax.naming.NamingEnumeration;
39 import javax.naming.NamingException;
40 import javax.naming.directory.Attribute;
41 import javax.naming.directory.Attributes;
42 import javax.naming.directory.BasicAttribute;
43 import javax.naming.directory.BasicAttributes;
44 import javax.naming.directory.DirContext;
45 import javax.naming.directory.InitialDirContext;
46 import javax.naming.directory.SearchControls;
47 import javax.naming.directory.SearchResult;
48 import java.util.ArrayList;
49 import java.util.Arrays;
50 import java.util.HashMap;
51 import java.util.List;
55 * @author Olivier Lamy
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ContextConfiguration(
59 locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-role-mapper.xml" })
60 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
61 public class TestLdapRoleMapper
65 Logger log = LoggerFactory.getLogger( getClass() );
68 @Named(value = "apacheDS#test")
69 private ApacheDs apacheDs;
71 private String suffix;
73 private String groupSuffix;
75 private PasswordEncoder passwordEncoder;
78 //private LdapCacheService ldapCacheService;
81 @Named(value = "ldapRoleMapper#test")
82 LdapRoleMapper ldapRoleMapper;
84 private Map<String, List<String>> usersPerGroup;
86 private List<String> users;
94 usersPerGroup = new HashMap<String, List<String>>( 3 );
96 usersPerGroup.put( "internal-repo-manager", Arrays.asList( "admin", "user.9" ) );
97 usersPerGroup.put( "internal-repo-observer", Arrays.asList( "admin", "user.7", "user.8" ) );
98 usersPerGroup.put( "archiva-admin", Arrays.asList( "admin", "user.7" ) );
100 users = new ArrayList<String>( 4 );
101 users.add( "admin" );
102 users.add( "user.7" );
103 users.add( "user.8" );
104 users.add( "user.9" );
106 passwordEncoder = new SHA1PasswordEncoder();
108 groupSuffix = apacheDs.addSimplePartition( "test", new String[]{ "archiva", "apache", "org" } ).getSuffix();
110 log.info( "groupSuffix: {}", groupSuffix );
112 suffix = "ou=People,dc=archiva,dc=apache,dc=org";
114 log.info( "DN Suffix: {}", suffix );
116 apacheDs.startServer();
118 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
119 objectClass.add( "top" );
120 objectClass.add( "organizationalUnit" );
122 Attributes attributes = new BasicAttributes( true );
123 attributes.put( objectClass );
124 attributes.put( "organizationalUnitName", "foo" );
126 apacheDs.getAdminContext().createSubcontext( suffix, attributes );
134 public void tearDown()
138 //ldapCacheService.removeAllUsers();
140 InitialDirContext context = apacheDs.getAdminContext();
142 for ( String uid : users )
144 context.unbind( createDn( uid ) );
147 for ( Map.Entry<String, List<String>> group : usersPerGroup.entrySet() )
149 context.unbind( createGroupDn( group.getKey() ) );
152 context.unbind( suffix );
154 apacheDs.stopServer();
159 private void createGroups()
162 InitialDirContext context = apacheDs.getAdminContext();
164 for ( Map.Entry<String, List<String>> group : usersPerGroup.entrySet() )
166 createGroup( context, group.getKey(), createGroupDn( group.getKey() ), group.getValue() );
171 private void createGroup( DirContext context, String groupName, String dn, List<String> users )
175 Attributes attributes = new BasicAttributes( true );
176 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
177 objectClass.add( "top" );
178 objectClass.add( "groupOfUniqueNames" );
179 attributes.put( objectClass );
180 attributes.put( "cn", groupName );
181 BasicAttribute basicAttribute = new BasicAttribute( "uniquemember" );
182 for ( String user : users )
184 basicAttribute.add( "uid=" + user + "," + suffix );// dc=archiva,dc=apache,dc=org" );
187 attributes.put( basicAttribute );
188 context.createSubcontext( dn, attributes );
191 private void bindUserObject( DirContext context, String cn, String dn )
194 Attributes attributes = new BasicAttributes( true );
195 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
196 objectClass.add( "top" );
197 objectClass.add( "inetOrgPerson" );
198 objectClass.add( "person" );
199 objectClass.add( "organizationalperson" );
200 attributes.put( objectClass );
201 attributes.put( "cn", cn );
202 attributes.put( "sn", "foo" );
203 attributes.put( "mail", cn + "@apache.org" );
204 attributes.put( "userPassword", passwordEncoder.encodePassword( "foo" ) );
205 attributes.put( "givenName", "foo" );
206 context.createSubcontext( dn, attributes );
209 private void makeUsers()
213 for ( String uid : users )
220 private void makeUser( String uid )
223 InitialDirContext context = apacheDs.getAdminContext();
225 bindUserObject( context, uid, createDn( uid ) );
226 assertExist( context, createDn( uid ), "cn", uid );
230 private void assertExist( DirContext context, String dn, String attribute, String value )
231 throws NamingException
233 SearchControls ctls = new SearchControls();
235 ctls.setDerefLinkFlag( true );
236 ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
237 ctls.setReturningAttributes( new String[]{ "*" } );
239 BasicAttributes matchingAttributes = new BasicAttributes();
240 matchingAttributes.put( attribute, value );
241 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
242 objectClass.add( "inetOrgPerson" );
243 matchingAttributes.put( objectClass );
245 NamingEnumeration<SearchResult> results = context.search( suffix, matchingAttributes );
247 assertTrue( results.hasMoreElements() );
248 SearchResult result = results.nextElement();
249 Attributes attrs = result.getAttributes();
250 Attribute testAttr = attrs.get( attribute );
251 assertEquals( value, testAttr.get() );
255 private String createDn( String cn )
257 return "cn=" + cn + "," + suffix;
260 private String createGroupDn( String cn )
262 return "cn=" + cn + "," + groupSuffix;
266 public void getAllGroups()
269 List<String> allGroups = ldapRoleMapper.getAllGroups();
271 log.info( "allGroups: {}", allGroups );
273 Assertions.assertThat( allGroups ).isNotNull().isNotEmpty().contains( "archiva-admin",
274 "internal-repo-manager" );
278 public void getGroupsMember()
281 List<String> users = ldapRoleMapper.getGroupsMember( "archiva-admin" );
283 log.info( "users for archiva-admin: {}", users );
285 Assertions.assertThat( users ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "admin", "user.7" );
287 users = ldapRoleMapper.getGroupsMember( "internal-repo-observer" );
289 Assertions.assertThat( users ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "admin", "user.7", "user.8" );
293 public void getGroups()
296 List<String> groups = ldapRoleMapper.getGroups( "admin" );
298 log.info( "groups for admin: {}", groups );
300 Assertions.assertThat( groups ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "archiva-admin",
301 "internal-repo-manager",
302 "internal-repo-observer" );
304 groups = ldapRoleMapper.getGroups( "user.8" );
306 Assertions.assertThat( groups ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "internal-repo-observer" );
308 groups = ldapRoleMapper.getGroups( "user.7" );
310 Assertions.assertThat( groups ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "archiva-admin",
311 "internal-repo-observer" );
315 public void getRoles()
318 List<String> roles = ldapRoleMapper.getRoles( "admin" );
320 log.info( "roles for admin: {}", roles );
322 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "Archiva System Administrator",
323 "Internal Repo Manager",
324 "Internal Repo Observer" );
326 roles = ldapRoleMapper.getRoles( "user.7" );
328 log.info( "roles for user.7: {}", roles );
330 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "Archiva System Administrator",
331 "Internal Repo Observer" );
333 roles = ldapRoleMapper.getRoles( "user.8" );
335 log.info( "roles for user.8: {}", roles );
337 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "Internal Repo Observer" );