|
| 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 com.cloud.domain.dao.DomainDao; |
| 20 | +import com.cloud.exception.InvalidParameterValueException; |
| 21 | +import com.cloud.user.AccountManager; |
| 22 | +import org.apache.cloudstack.api.command.LdapTestConfigurationCmd; |
| 23 | +import org.apache.cloudstack.ldap.dao.LdapConfigurationDao; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.junit.MockitoJUnitRunner; |
| 29 | +import org.springframework.test.util.ReflectionTestUtils; |
| 30 | + |
| 31 | +import javax.naming.NamingException; |
| 32 | + |
| 33 | +import static org.junit.Assert.assertThrows; |
| 34 | +import static org.mockito.ArgumentMatchers.any; |
| 35 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 36 | +import static org.mockito.Mockito.doThrow; |
| 37 | +import static org.mockito.Mockito.never; |
| 38 | +import static org.mockito.Mockito.verify; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | +/** |
| 42 | + * Regression tests for #6934: testing an LDAP connection must not persist a |
| 43 | + * configuration, and adding one must still bind before persisting. |
| 44 | + */ |
| 45 | +@RunWith(MockitoJUnitRunner.class) |
| 46 | +public class LdapManagerImplTest { |
| 47 | + |
| 48 | + private static final long DOMAIN_ID = 1L; |
| 49 | + |
| 50 | + private LdapManagerImpl ldapManager; |
| 51 | + |
| 52 | + @Mock |
| 53 | + private LdapConfigurationDao ldapConfigurationDao; |
| 54 | + |
| 55 | + @Mock |
| 56 | + private LdapContextFactory ldapContextFactory; |
| 57 | + |
| 58 | + @Mock |
| 59 | + private DomainDao domainDao; |
| 60 | + |
| 61 | + @Mock |
| 62 | + private AccountManager accountManager; |
| 63 | + |
| 64 | + @Before |
| 65 | + public void setup() { |
| 66 | + ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, null, null); |
| 67 | + ReflectionTestUtils.setField(ldapManager, "domainDao", domainDao); |
| 68 | + ReflectionTestUtils.setField(ldapManager, "accountManager", accountManager); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testConnectionDoesNotPersistOnSuccess() throws Exception { |
| 73 | + LdapTestConfigurationCmd cmd = buildCmd("ldap.example.com", 389, DOMAIN_ID); |
| 74 | + |
| 75 | + ldapManager.testConnection(cmd); |
| 76 | + |
| 77 | + verify(ldapContextFactory).createBindContext("ldap://ldap.example.com:389", DOMAIN_ID); |
| 78 | + verify(ldapConfigurationDao, never()).persist(any()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testConnectionDefaultsPortWhenNotGiven() throws Exception { |
| 83 | + LdapTestConfigurationCmd cmd = buildCmd("ldap.example.com", 0, DOMAIN_ID); |
| 84 | + |
| 85 | + ldapManager.testConnection(cmd); |
| 86 | + |
| 87 | + verify(ldapContextFactory).createBindContext("ldap://ldap.example.com:389", DOMAIN_ID); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testConnectionThrowsOnBindFailure() throws Exception { |
| 92 | + LdapTestConfigurationCmd cmd = buildCmd("ldap.example.com", 389, DOMAIN_ID); |
| 93 | + doThrow(new NamingException("bind failed")).when(ldapContextFactory).createBindContext(any(), anyLong()); |
| 94 | + |
| 95 | + assertThrows(InvalidParameterValueException.class, () -> ldapManager.testConnection(cmd)); |
| 96 | + |
| 97 | + verify(ldapConfigurationDao, never()).persist(any()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void addConfigurationStillBindsBeforePersisting() throws Exception { |
| 102 | + when(ldapConfigurationDao.find("ldap.example.com", 389, DOMAIN_ID)).thenReturn(null); |
| 103 | + when(ldapConfigurationDao.persist(any())).thenAnswer(invocation -> invocation.getArgument(0)); |
| 104 | + |
| 105 | + ldapManager.addConfiguration("ldap.example.com", 389, DOMAIN_ID); |
| 106 | + |
| 107 | + verify(ldapContextFactory).createBindContext("ldap://ldap.example.com:389", DOMAIN_ID); |
| 108 | + verify(ldapConfigurationDao).persist(any()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void addConfigurationDoesNotPersistOnBindFailure() throws Exception { |
| 113 | + when(ldapConfigurationDao.find("ldap.example.com", 389, DOMAIN_ID)).thenReturn(null); |
| 114 | + doThrow(new NamingException("bind failed")).when(ldapContextFactory).createBindContext(any(), anyLong()); |
| 115 | + |
| 116 | + assertThrows(InvalidParameterValueException.class, () -> ldapManager.addConfiguration("ldap.example.com", 389, DOMAIN_ID)); |
| 117 | + |
| 118 | + verify(ldapConfigurationDao, never()).persist(any()); |
| 119 | + } |
| 120 | + |
| 121 | + private LdapTestConfigurationCmd buildCmd(String hostname, int port, long domainId) { |
| 122 | + LdapTestConfigurationCmd cmd = new LdapTestConfigurationCmd(); |
| 123 | + ReflectionTestUtils.setField(cmd, "hostname", hostname); |
| 124 | + ReflectionTestUtils.setField(cmd, "port", port); |
| 125 | + ReflectionTestUtils.setField(cmd, "domainId", domainId); |
| 126 | + return cmd; |
| 127 | + } |
| 128 | +} |
0 commit comments