|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.ldap; |
| 18 | + |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | +import org.mockito.Mock; |
| 23 | +import org.mockito.runners.MockitoJUnitRunner; |
| 24 | + |
| 25 | +import javax.naming.directory.SearchControls; |
| 26 | +import javax.naming.ldap.LdapContext; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | +import static org.mockito.Matchers.any; |
| 30 | +import static org.mockito.Mockito.when; |
| 31 | + |
| 32 | +@RunWith(MockitoJUnitRunner.class) |
| 33 | +public class ADLdapUserManagerImplTest { |
| 34 | + |
| 35 | + ADLdapUserManagerImpl adLdapUserManager; |
| 36 | + |
| 37 | + @Mock |
| 38 | + LdapConfiguration ldapConfiguration; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void init() throws Exception { |
| 42 | + adLdapUserManager = new ADLdapUserManagerImpl(); |
| 43 | + adLdapUserManager._ldapConfiguration = ldapConfiguration; |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testGenerateADSearchFilterWithNestedGroupsEnabled() { |
| 48 | + when(ldapConfiguration.getUserObject(any())).thenReturn("user"); |
| 49 | + when(ldapConfiguration.getCommonNameAttribute()).thenReturn("CN"); |
| 50 | + when(ldapConfiguration.getBaseDn(any())).thenReturn("DC=cloud,DC=citrix,DC=com"); |
| 51 | + when(ldapConfiguration.isNestedGroupsEnabled(any())).thenReturn(true); |
| 52 | + |
| 53 | + String [] groups = {"dev", "dev-hyd"}; |
| 54 | + for (String group: groups) { |
| 55 | + String result = adLdapUserManager.generateADGroupSearchFilter(group, 1L); |
| 56 | + assertTrue(("(&(objectClass=user)(memberOf:1.2.840.113556.1.4.1941:=CN=" + group + ",DC=cloud,DC=citrix,DC=com))").equals(result)); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testGenerateADSearchFilterWithNestedGroupsDisabled() { |
| 63 | + when(ldapConfiguration.getUserObject(any())).thenReturn("user"); |
| 64 | + when(ldapConfiguration.getCommonNameAttribute()).thenReturn("CN"); |
| 65 | + when(ldapConfiguration.getBaseDn(any())).thenReturn("DC=cloud,DC=citrix,DC=com"); |
| 66 | + when(ldapConfiguration.isNestedGroupsEnabled(any())).thenReturn(false); |
| 67 | + |
| 68 | + String [] groups = {"dev", "dev-hyd"}; |
| 69 | + for (String group: groups) { |
| 70 | + String result = adLdapUserManager.generateADGroupSearchFilter(group, 1L); |
| 71 | + assertTrue(("(&(objectClass=user)(memberOf=CN=" + group + ",DC=cloud,DC=citrix,DC=com))").equals(result)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Mock |
| 76 | + LdapContext ldapContext; |
| 77 | + |
| 78 | + @Test(expected = IllegalArgumentException.class) |
| 79 | + public void testGetUsersInGroupUsingNullGroup() throws Exception { |
| 80 | + String[] returnAttributes = {"username", "firstname", "lastname", "email"}; |
| 81 | + when(ldapConfiguration.getScope()).thenReturn(SearchControls.SUBTREE_SCOPE); |
| 82 | + when(ldapConfiguration.getReturnAttributes(null)).thenReturn(returnAttributes); |
| 83 | + when(ldapConfiguration.getBaseDn(any())).thenReturn(null).thenReturn(null).thenReturn("DC=cloud,DC=citrix,DC=com"); |
| 84 | + |
| 85 | + LdapContext context = ldapContext; |
| 86 | + String [] groups = {null, "group", null}; |
| 87 | + for (String group: groups) { |
| 88 | + adLdapUserManager.getUsersInGroup(group, context,null); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments