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" );
125 //attributes.put( "ou", "People" );
127 apacheDs.getAdminContext().createSubcontext( suffix, attributes );
135 public void tearDown()
139 //ldapCacheService.removeAllUsers();
141 InitialDirContext context = apacheDs.getAdminContext();
143 for ( String uid : users )
145 context.unbind( createDn( uid ) );
148 for ( Map.Entry<String, List<String>> group : usersPerGroup.entrySet() )
150 context.unbind( createGroupDn( group.getKey() ) );
153 context.unbind( suffix );
155 apacheDs.stopServer();
160 private void createGroups()
163 InitialDirContext context = apacheDs.getAdminContext();
165 for ( Map.Entry<String, List<String>> group : usersPerGroup.entrySet() )
167 createGroup( context, group.getKey(), createGroupDn( group.getKey() ), group.getValue() );
172 private void createGroup( DirContext context, String groupName, String dn, List<String> users )
176 Attributes attributes = new BasicAttributes( true );
177 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
178 objectClass.add( "top" );
179 objectClass.add( "groupOfUniqueNames" );
180 attributes.put( objectClass );
181 attributes.put( "cn", groupName );
182 BasicAttribute basicAttribute = new BasicAttribute( "uniquemember" );
183 for ( String user : users )
185 basicAttribute.add( "uid=" + user + "," + suffix );// dc=archiva,dc=apache,dc=org" );
188 attributes.put( basicAttribute );
189 context.createSubcontext( dn, attributes );
192 private void bindUserObject( DirContext context, String cn, String dn )
195 Attributes attributes = new BasicAttributes( true );
196 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
197 objectClass.add( "top" );
198 objectClass.add( "inetOrgPerson" );
199 objectClass.add( "person" );
200 objectClass.add( "organizationalperson" );
201 attributes.put( objectClass );
202 attributes.put( "cn", cn );
203 attributes.put( "sn", "foo" );
204 attributes.put( "mail", cn + "@apache.org" );
205 attributes.put( "userPassword", passwordEncoder.encodePassword( "foo" ) );
206 attributes.put( "givenName", "foo" );
207 context.createSubcontext( dn, attributes );
210 private void makeUsers()
214 for ( String uid : users )
221 private void makeUser( String uid )
224 InitialDirContext context = apacheDs.getAdminContext();
226 bindUserObject( context, uid, createDn( uid ) );
227 assertExist( context, createDn( uid ), "cn", uid );
231 private void assertExist( DirContext context, String dn, String attribute, String value )
232 throws NamingException
234 SearchControls ctls = new SearchControls();
236 ctls.setDerefLinkFlag( true );
237 ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
238 ctls.setReturningAttributes( new String[]{ "*" } );
240 BasicAttributes matchingAttributes = new BasicAttributes();
241 matchingAttributes.put( attribute, value );
242 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
243 objectClass.add( "inetOrgPerson" );
244 matchingAttributes.put( objectClass );
246 NamingEnumeration<SearchResult> results = context.search( suffix, matchingAttributes );
248 assertTrue( results.hasMoreElements() );
249 SearchResult result = results.nextElement();
250 Attributes attrs = result.getAttributes();
251 Attribute testAttr = attrs.get( attribute );
252 assertEquals( value, testAttr.get() );
256 private String createDn( String cn )
258 return "cn=" + cn + "," + suffix;
261 private String createGroupDn( String cn )
263 return "cn=" + cn + "," + groupSuffix;
267 public void getAllGroups()
270 List<String> allGroups = ldapRoleMapper.getAllGroups();
272 log.info( "allGroups: {}", allGroups );
274 Assertions.assertThat( allGroups ).isNotNull().isNotEmpty().contains( "archiva-admin",
275 "internal-repo-manager" );
279 public void getGroupsMember()
282 List<String> users = ldapRoleMapper.getGroupsMember( "archiva-admin" );
284 log.info( "users for archiva-admin: {}", users );
286 Assertions.assertThat( users ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "admin", "user.7" );
288 users = ldapRoleMapper.getGroupsMember( "internal-repo-observer" );
290 Assertions.assertThat( users ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "admin", "user.7", "user.8" );
294 public void getGroups()
297 List<String> roles = ldapRoleMapper.getGroups( "admin" );
299 log.info( "roles for admin: {}", roles );
301 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 3 ).contains( "archiva-admin",
302 "internal-repo-manager",
303 "internal-repo-observer" );
305 roles = ldapRoleMapper.getGroups( "user.8" );
307 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "internal-repo-observer" );
309 roles = ldapRoleMapper.getGroups( "user.7" );
311 Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "archiva-admin",
312 "internal-repo-observer" );