Update unit tests, add filter to list all users, update ssl

Signed-off-by: Abhinandan Prateek <aprateek@apache.org>
This commit is contained in:
Ian Duffy 2013-08-12 10:01:14 +01:00 committed by Abhinandan Prateek
parent 25e8e9b85f
commit 7f7035d516
29 changed files with 1137 additions and 940 deletions

View File

@ -666,9 +666,8 @@ listDedicatedClusters=1
listDedicatedHosts=1
### LDAP
searchLdap=3
listLdapConfigurations=15
addLdapConfiguration=3
deleteLdapConfiguration=3
listAllLdapUsers=3
listLdapUsers=3
ldapCreateAccount=3

View File

@ -24,19 +24,28 @@
<section id="LDAP-for-user-authentication">
<title>Using an LDAP Server for User Authentication</title>
<para>You can use an external LDAP server such as Microsoft Active Directory or OpenLDAP to authenticate &PRODUCT; end-users.</para>
<para>To set up LDAP authentication in &PRODUCT;, open the global settings page and set:</para>
<para>In order to do this you must:</para>
<itemizedlist>
<listitem><para>ldap.basedn - The base directory you want to search within for uses</para></listitem>
<listitem><para>ldap.bind.password - The password you wish to use to bind, this can be blank if the server supports anonymous binding</para></listitem>
<listitem><para>ldap.bind.principal - The account you wish to use to bind, this can be blank if the server supports anonymous binding</para></listitem>
<listitem><para>ldap.email.attribute - The attribute within your LDAP server that holds a value for users email address</para></listitem>
<listitem><para>ldap.realname.attribute - The attribute within your LDAP server that holds a value users realname</para></listitem>
<listitem><para>ldap.user.object - The object class that identifies a user</para></listitem>
<listitem><para>ldap.username.attribute - The attribute within your LDAP server that has a value that will match the cloudstack accounts username field</para></listitem>
<listitem><para>Set your LDAP configuration within &PRODUCT;</para></listitem>
<listitem><para>Create &PRODUCT; accounts for LDAP users</para></listitem>
</itemizedlist>
<para>Finally you can add LDAP servers from Global Settings -> Select View -> LDAP Configuration. This requires a hostname and port</para>
<para>To set up LDAP authentication in &PRODUCT;, open the global settings page and search for LDAP</para>
<para>Set ldap.basedn to match your sever's base directory.</para>
<para>Review the defaults for the following, ensure that they match your schema.</para>
<itemizedlist>
<listitem><para>ldap.email.attribute</para></listitem>
<listitem><para>ldap.firstname.attribute</para></listitem>
<listitem><para>ldap.lastname.attribute</para></listitem>
<listitem><para>ldap.username.attribute</para></listitem>
<listitem><para>ldap.user.object</para></listitem>
</itemizedlist>
<para>Optionally you can set the following:</para>
<itemizedlist>
<listitem><para>If you do not want to use anonymous binding you can set ldap.bind.principle and ldap.bind.password as credentials for your LDAP server that will grant &PRODUCT; permission to perform a search on the LDAP server.</para></listitem>
<listitem><para>For SSL support set ldap.truststore to a path on the file system where your trusted store is located. Along with this set ldap.truststore.password as the password that unlocks the truststore.</para></listitem>
<listitem><para>If you wish to filter down the user set that is granted access to &PRODUCT; via the LDAP attribute memberof you can do so using ldap.search.group.principle.</para></listitem>
</itemizedlist>
<para>Finally, you can add your LDAP server. To do so select LDAP Configuration from the views section within global settings. Click on "Configure LDAP" and fill in your server's hostname and port.</para>
<xi:include href="example-activedirectory-configuration.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="example-openldap-configuration.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<!-- Support for SSL has been removed but will be back shortly. -->
<!-- <xi:include href="SSL-keystore-path-and-password.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> -->
</section>

View File

@ -24,14 +24,14 @@
<section id="example-activedirectory-configuration">
<title>Example LDAP Configuration for Active Directory</title>
<para>This shows the configuration settings required for using ActiveDirectory</para>
<para>This shows the configuration settings required for using ActiveDirectory.</para>
<itemizedlist>
<listitem><para>samAccountName - Logon name</para></listitem>
<listitem><para>mail - Email Address</para></listitem>
<listitem><para>cn - Real name</para></listitem>
</itemizedlist>
<para>Along with this the ldap.user.object name needs to be modified, by default ActiveDirectory uses the value "user" for this.</para>
<para>Map the following attributes accordingly as shown below within the cloudstack ldap configuration:</para>
<para>Map the following attributes accordingly as shown below:</para>
<mediaobject>
<imageobject>
<imagedata fileref="./images/add-ldap-configuration-ad.png"/>

View File

@ -24,8 +24,8 @@
<section id="example-openldap-configuration">
<title>Example LDAP Configuration for OpenLdap</title>
<para>This shows the configuration settings required for using OpenLDAP</para>
<para>The default values supplied are suited for OpenLDAP</para>
<para>This shows the configuration settings required for using OpenLDAP.</para>
<para>The default values supplied are suited for OpenLDAP.</para>
<itemizedlist>
<listitem><para>uid - Logon name</para></listitem>
<listitem><para>mail - Email Address</para></listitem>

View File

@ -42,9 +42,9 @@ import com.cloud.user.AccountService;
import com.cloud.user.UserAccount;
@APICommand(name = "ldapCreateAccount", description = "Creates an account from an LDAP user", responseObject = AccountResponse.class, since = "4.2.0")
public class LdapCreateAccount extends BaseCmd {
public class LdapCreateAccountCmd extends BaseCmd {
public static final Logger s_logger = Logger
.getLogger(LdapCreateAccount.class.getName());
.getLogger(LdapCreateAccountCmd.class.getName());
private static final String s_name = "createaccountresponse";
@Inject
@ -77,28 +77,33 @@ public class LdapCreateAccount extends BaseCmd {
@Parameter(name = ApiConstants.USER_ID, type = CommandType.STRING, description = "User UUID, required for adding account from external provisioning system")
private String userUUID;
public LdapCreateAccount() {
public LdapCreateAccountCmd() {
super();
}
public LdapCreateAccount(final LdapManager ldapManager,
public LdapCreateAccountCmd(final LdapManager ldapManager,
final AccountService accountService) {
super();
_ldapManager = ldapManager;
_accountService = accountService;
}
UserAccount createCloudstackUserAccount(final LdapUser user) {
return _accountService.createUserAccount(username, generatePassword(),
user.getFirstname(), user.getLastname(), user.getEmail(),
timezone, accountName, accountType, domainId, networkDomain,
details, accountUUID, userUUID);
}
@Override
public void execute() throws ServerApiException {
updateCallContext();
final CallContext callContext = getCurrentContext();
callContext.setEventDetails("Account Name: " + accountName
+ ", Domain Id:" + domainId);
try {
final LdapUser user = _ldapManager.getUser(username);
validateUser(user);
final UserAccount userAccount = _accountService.createUserAccount(
username, generatePassword(), user.getFirstname(),
user.getLastname(), user.getEmail(), timezone, accountName,
accountType, domainId, networkDomain, details, accountUUID,
userUUID);
final UserAccount userAccount = createCloudstackUserAccount(user);
if (userAccount != null) {
final AccountResponse response = _responseGenerator
.createUserAccountResponse(userAccount);
@ -132,16 +137,15 @@ public class LdapCreateAccount extends BaseCmd {
return s_name;
}
CallContext getCurrentContext() {
return CallContext.current();
}
@Override
public long getEntityOwnerId() {
return Account.ACCOUNT_ID_SYSTEM;
}
private void updateCallContext() {
CallContext.current().setEventDetails(
"Account Name: " + accountName + ", Domain Id:" + domainId);
}
private boolean validateUser(final LdapUser user) throws ServerApiException {
if (user.getEmail() == null) {
throw new ServerApiException(

View File

@ -23,42 +23,56 @@ import javax.inject.Inject;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.BaseListCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.command.admin.user.ListUsersCmd;
import org.apache.cloudstack.api.response.LdapUserResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.UserResponse;
import org.apache.cloudstack.ldap.LdapManager;
import org.apache.cloudstack.ldap.LdapUser;
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
import org.apache.cloudstack.query.QueryService;
import org.apache.log4j.Logger;
import com.cloud.user.Account;
@APICommand(name = "listAllLdapUsers", responseObject = LdapUserResponse.class, description = "Lists all LDAP Users", since = "4.2.0")
public class LdapListAllUsersCmd extends BaseListCmd {
@APICommand(name = "listLdapUsers", responseObject = LdapUserResponse.class, description = "Lists all LDAP Users", since = "4.2.0")
public class LdapListUsersCmd extends BaseListCmd {
public static final Logger s_logger = Logger
.getLogger(LdapListAllUsersCmd.class.getName());
.getLogger(LdapListUsersCmd.class.getName());
private static final String s_name = "ldapuserresponse";
@Inject
private LdapManager _ldapManager;
public LdapListAllUsersCmd() {
@Inject
private QueryService _queryService;
@Parameter(name = "listtype", type = CommandType.STRING, required = false, description = "Determines whether all ldap users are returned or just non-cloudstack users")
private String listType;
public LdapListUsersCmd() {
super();
}
public LdapListAllUsersCmd(final LdapManager ldapManager) {
public LdapListUsersCmd(final LdapManager ldapManager,
final QueryService queryService) {
super();
_ldapManager = ldapManager;
_queryService = queryService;
}
private List<LdapUserResponse> createLdapUserResponse(
final List<LdapUser> users) {
final List<LdapUserResponse> ldapResponses = new ArrayList<LdapUserResponse>();
for (final LdapUser user : users) {
final LdapUserResponse ldapResponse = _ldapManager
.createLdapUserResponse(user);
ldapResponse.setObjectName("LdapUser");
ldapResponses.add(ldapResponse);
if (getListType().equals("all") || !isACloudstackUser(user)) {
final LdapUserResponse ldapResponse = _ldapManager
.createLdapUserResponse(user);
ldapResponse.setObjectName("LdapUser");
ldapResponses.add(ldapResponse);
}
}
return ldapResponses;
}
@ -88,4 +102,22 @@ public class LdapListAllUsersCmd extends BaseListCmd {
public long getEntityOwnerId() {
return Account.ACCOUNT_ID_SYSTEM;
}
private String getListType() {
return listType == null ? "all" : listType;
}
private boolean isACloudstackUser(final LdapUser ldapUser) {
final ListResponse<UserResponse> response = _queryService
.searchForUsers(new ListUsersCmd());
final List<UserResponse> cloudstackUsers = response.getResponses();
if (cloudstackUsers != null && cloudstackUsers.size() != 0) {
for (final UserResponse cloudstackUser : response.getResponses()) {
if (ldapUser.getUsername().equals(cloudstackUser.getUsername())) {
return true;
}
}
}
return false;
}
}

View File

@ -26,10 +26,10 @@ import javax.naming.directory.DirContext;
import org.apache.cloudstack.api.LdapValidator;
import org.apache.cloudstack.api.command.LdapAddConfigurationCmd;
import org.apache.cloudstack.api.command.LdapCreateAccount;
import org.apache.cloudstack.api.command.LdapCreateAccountCmd;
import org.apache.cloudstack.api.command.LdapDeleteConfigurationCmd;
import org.apache.cloudstack.api.command.LdapListAllUsersCmd;
import org.apache.cloudstack.api.command.LdapListConfigurationCmd;
import org.apache.cloudstack.api.command.LdapListUsersCmd;
import org.apache.cloudstack.api.command.LdapUserSearchCmd;
import org.apache.cloudstack.api.response.LdapConfigurationResponse;
import org.apache.cloudstack.api.response.LdapUserResponse;
@ -159,11 +159,11 @@ public class LdapManagerImpl implements LdapManager, LdapValidator {
public List<Class<?>> getCommands() {
final List<Class<?>> cmdList = new ArrayList<Class<?>>();
cmdList.add(LdapUserSearchCmd.class);
cmdList.add(LdapListAllUsersCmd.class);
cmdList.add(LdapListUsersCmd.class);
cmdList.add(LdapAddConfigurationCmd.class);
cmdList.add(LdapDeleteConfigurationCmd.class);
cmdList.add(LdapListConfigurationCmd.class);
cmdList.add(LdapCreateAccount.class);
cmdList.add(LdapCreateAccountCmd.class);
return cmdList;
}

View File

@ -16,43 +16,41 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import java.util.LinkedList;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchResult;
import javax.naming.NamingEnumeration
import javax.naming.NamingException
import javax.naming.directory.SearchResult
class BasicNamingEnumerationImpl implements NamingEnumeration {
private LinkedList<String> items = new LinkedList<SearchResult>();
private LinkedList<String> items = new LinkedList<SearchResult>();
public void add(SearchResult item) {
items.add(item)
}
public void add(SearchResult item) {
items.add(item)
}
@Override
public void close() throws NamingException {
}
@Override
public void close() throws NamingException {
}
@Override
public boolean hasMore() throws NamingException {
return hasMoreElements();
}
@Override
public boolean hasMore() throws NamingException {
return hasMoreElements();
}
@Override
public boolean hasMoreElements() {
return items.size != 0;
}
@Override
public boolean hasMoreElements() {
return items.size != 0;
}
@Override
public Object next() throws NamingException {
return nextElement();
}
@Override
public Object next() throws NamingException {
return nextElement();
}
@Override
public Object nextElement() {
SearchResult result = items.getFirst();
items.removeFirst();
return result;
}
@Override
public Object nextElement() {
SearchResult result = items.getFirst();
items.removeFirst();
return result;
}
}

View File

@ -16,69 +16,74 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import com.cloud.exception.InvalidParameterValueException
import org.apache.cloudstack.api.ServerApiException
import org.apache.cloudstack.api.command.LdapAddConfigurationCmd
import org.apache.cloudstack.api.response.LdapConfigurationResponse
import org.apache.cloudstack.ldap.LdapManager
class LdapAddConfigurationCmdSpec extends spock.lang.Specification {
def "Test failed response from execute"() {
given: "We have an LDAP manager, no configuration and a LdapAddConfigurationCmd"
def ldapManager = Mock(LdapManager)
ldapManager.addConfiguration(_, _) >> { throw new InvalidParameterValueException() }
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "LdapAddCofnigurationCmd is executed"
ldapAddConfigurationCmd.execute()
then: "an exception is thrown"
thrown ServerApiException
}
def "Test failed response from execute"() {
given: "We have an LDAP manager, no configuration and a LdapAddConfigurationCmd"
def ldapManager = Mock(LdapManager)
ldapManager.addConfiguration(_, _) >> { throw new InvalidParameterValueException() }
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "LdapAddCofnigurationCmd is executed"
ldapAddConfigurationCmd.execute()
then: "an exception is thrown"
thrown ServerApiException
}
def "Test getEntityOwnerId is 1"() {
given: "We have an LdapManager and LdapConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "Get Entity Owner Id is called"
long ownerId = ldapAddConfigurationCmd.getEntityOwnerId()
then: "1 is returned"
ownerId == 1
}
def "Test getEntityOwnerId is 1"() {
given: "We have an LdapManager and LdapConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "Get Entity Owner Id is called"
long ownerId = ldapAddConfigurationCmd.getEntityOwnerId()
then: "1 is returned"
ownerId == 1
}
def "Test successful response from execute"() {
given: "We have an LDAP Manager that has a configuration and a LdapAddConfigurationCmd"
def ldapManager = Mock(LdapManager)
ldapManager.addConfiguration(_, _) >> new LdapConfigurationResponse("localhost", 389)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "LdapAddConfigurationCmd is executed"
ldapAddConfigurationCmd.execute()
then: "the responseObject should have the hostname localhost and port 389"
ldapAddConfigurationCmd.responseObject.hostname == "localhost"
ldapAddConfigurationCmd.responseObject.port == 389
}
def "Test successful response from execute"() {
given: "We have an LDAP Manager that has a configuration and a LdapAddConfigurationCmd"
def ldapManager = Mock(LdapManager)
ldapManager.addConfiguration(_, _) >> new LdapConfigurationResponse("localhost", 389)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "LdapAddConfigurationCmd is executed"
ldapAddConfigurationCmd.execute()
then: "the responseObject should have the hostname localhost and port 389"
ldapAddConfigurationCmd.responseObject.hostname == "localhost"
ldapAddConfigurationCmd.responseObject.port == 389
}
def "Test successful return of getCommandName"() {
given: "We have an LdapManager and LdapConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "Get Command Name is called"
String commandName = ldapAddConfigurationCmd.getCommandName()
then: "ldapconfigurationresponse is returned"
commandName == "ldapconfigurationresponse"
}
def "Test successful return of getCommandName"() {
given: "We have an LdapManager and LdapConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "Get Command Name is called"
String commandName = ldapAddConfigurationCmd.getCommandName()
then: "ldapconfigurationresponse is returned"
commandName == "ldapconfigurationresponse"
}
def "Test successful setting of hostname"() {
given: "We have an LdapManager and LdapAddConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "The hostname is set"
ldapAddConfigurationCmd.setHostname("localhost")
then: "Get hostname returns the set hostname"
ldapAddConfigurationCmd.getHostname() == "localhost"
}
def "Test successful setting of hostname"() {
given: "We have an LdapManager and LdapAddConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "The hostname is set"
ldapAddConfigurationCmd.setHostname("localhost")
then: "Get hostname returns the set hostname"
ldapAddConfigurationCmd.getHostname() == "localhost"
}
def "Test successful setting of port"() {
given: "We have an LdapManager and LdapAddConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "The port is set"
ldapAddConfigurationCmd.setPort(389)
then: "Get port returns the port"
ldapAddConfigurationCmd.getPort() == 389
}
def "Test successful setting of port"() {
given: "We have an LdapManager and LdapAddConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapAddConfigurationCmd = new LdapAddConfigurationCmd(ldapManager)
when: "The port is set"
ldapAddConfigurationCmd.setPort(389)
then: "Get port returns the port"
ldapAddConfigurationCmd.getPort() == 389
}
}

View File

@ -16,51 +16,57 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import com.cloud.user.UserAccountVO
import com.cloud.user.dao.UserAccountDao
import com.cloud.utils.Pair
import org.apache.cloudstack.ldap.LdapAuthenticator
import org.apache.cloudstack.ldap.LdapConfigurationVO
import org.apache.cloudstack.ldap.LdapManager
class LdapAuthenticatorSpec extends spock.lang.Specification {
def "Test a failed authentication due to user not being found within cloudstack"() {
given: "We have an LdapManager, userAccountDao and ldapAuthenticator and the user doesn't exist within cloudstack."
given: "We have an LdapManager, userAccountDao and ldapAuthenticator and the user doesn't exist within cloudstack."
LdapManager ldapManager = Mock(LdapManager)
UserAccountDao userAccountDao = Mock(UserAccountDao)
userAccountDao.getUserAccount(_, _) >> null
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "A user authentications"
when: "A user authentications"
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
then: "their authentication fails"
result == false
then: "their authentication fails"
result == false
}
def "Test failed authentication due to ldap bind being unsuccessful"() {
given: "We have an LdapManager, LdapConfiguration, userAccountDao and LdapAuthenticator"
def ldapManager = Mock(LdapManager)
ldapManager.isLdapEnabled() >> true
ldapManager.canAuthenticate(_, _) >> false
given: "We have an LdapManager, LdapConfiguration, userAccountDao and LdapAuthenticator"
def ldapManager = Mock(LdapManager)
ldapManager.isLdapEnabled() >> true
ldapManager.canAuthenticate(_, _) >> false
UserAccountDao userAccountDao = Mock(UserAccountDao)
userAccountDao.getUserAccount(_, _) >> new UserAccountVO()
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
UserAccountDao userAccountDao = Mock(UserAccountDao)
userAccountDao.getUserAccount(_, _) >> new UserAccountVO()
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "The user authenticates with an incorrect password"
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
when: "The user authenticates with an incorrect password"
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
then: "their authentication fails"
result == false
then: "their authentication fails"
result == false
}
def "Test failed authentication due to ldap not being configured"() {
given: "We have an LdapManager, A configured LDAP server, a userAccountDao and LdapAuthenticator"
def ldapManager = Mock(LdapManager)
given: "We have an LdapManager, A configured LDAP server, a userAccountDao and LdapAuthenticator"
def ldapManager = Mock(LdapManager)
ldapManager.isLdapEnabled() >> false
UserAccountDao userAccountDao = Mock(UserAccountDao)
userAccountDao.getUserAccount(_, _) >> new UserAccountVO()
UserAccountDao userAccountDao = Mock(UserAccountDao)
userAccountDao.getUserAccount(_, _) >> new UserAccountVO()
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "The user authenticates"
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
then: "their authentication fails"
result == false
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "The user authenticates"
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
then: "their authentication fails"
result == false
}
def "Test successful authentication"() {
@ -81,13 +87,13 @@ class LdapAuthenticatorSpec extends spock.lang.Specification {
}
def "Test that encode doesn't change the input"() {
given: "We have an LdapManager, userAccountDao and LdapAuthenticator"
LdapManager ldapManager = Mock(LdapManager)
UserAccountDao userAccountDao = Mock(UserAccountDao)
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "a users password is encoded"
def result = ldapAuthenticator.encode("password")
then: "it doesn't change"
result == "password"
given: "We have an LdapManager, userAccountDao and LdapAuthenticator"
LdapManager ldapManager = Mock(LdapManager)
UserAccountDao userAccountDao = Mock(UserAccountDao)
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "a users password is encoded"
def result = ldapAuthenticator.encode("password")
then: "it doesn't change"
result == "password"
}
}

View File

@ -16,12 +16,13 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.ldap.dao.LdapConfigurationDaoImpl
class LdapConfigurationDaoImplSpec extends spock.lang.Specification {
def "Test setting up of a LdapConfigurationDao"() {
given: "We have an LdapConfigurationDao implementation"
given: "We have an LdapConfigurationDao implementation"
def ldapConfigurationDaoImpl = new LdapConfigurationDaoImpl();
expect: "that hostnameSearch and listAllConfigurationsSearch is configured"
expect: "that hostnameSearch and listAllConfigurationsSearch is configured"
ldapConfigurationDaoImpl.hostnameSearch != null;
ldapConfigurationDaoImpl.listAllConfigurationsSearch != null
}

View File

@ -16,33 +16,34 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.api.response.LdapConfigurationResponse
class LdapConfigurationResponseSpec extends spock.lang.Specification {
def "Testing succcessful setting of LdapConfigurationResponse hostname"() {
given: "We have a LdapConfigurationResponse"
given: "We have a LdapConfigurationResponse"
LdapConfigurationResponse response = new LdapConfigurationResponse();
when: "The hostname is set"
when: "The hostname is set"
response.setHostname("localhost");
then: "Get hostname should return the set value"
response.getHostname() == "localhost";
then: "Get hostname should return the set value"
response.getHostname() == "localhost";
}
def "Testing successful setting of LdapConfigurationResponse hostname and port via constructor"() {
given: "We have a LdapConfiguration response"
LdapConfigurationResponse response
when: "both hostname and port are set by constructor"
given: "We have a LdapConfiguration response"
LdapConfigurationResponse response
when: "both hostname and port are set by constructor"
response = new LdapConfigurationResponse("localhost", 389)
then: "Get hostname and port should return the set values."
then: "Get hostname and port should return the set values."
response.getHostname() == "localhost"
response.getPort() == 389
}
def "Testing successful setting of LdapConfigurationResponse port"() {
given: "We have a LdapConfigurationResponse"
LdapConfigurationResponse response = new LdapConfigurationResponse()
when: "The port is set"
response.setPort(389)
then: "Get port should return the set value"
response.getPort() == 389
given: "We have a LdapConfigurationResponse"
LdapConfigurationResponse response = new LdapConfigurationResponse()
when: "The port is set"
response.setPort(389)
then: "Get port should return the set value"
response.getPort() == 389
}
}

View File

@ -16,9 +16,169 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import com.cloud.configuration.dao.ConfigurationDao
import com.cloud.utils.Pair
import org.apache.cloudstack.api.ServerApiException
import org.apache.cloudstack.ldap.LdapConfiguration
import org.apache.cloudstack.ldap.LdapConfigurationVO
import org.apache.cloudstack.ldap.LdapManager
import javax.naming.directory.SearchControls
class LdapConfigurationSpec extends spock.lang.Specification {
def "Test that get search group principle returns successfully"() {
def "Test that getAuthentication returns none"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get authentication is called"
String authentication = ldapConfiguration.getAuthentication()
then: "none should be returned"
authentication == "none"
}
def "Test that getAuthentication returns simple"() {
given: "We have a configDao, LdapManager and LdapConfiguration with bind principle and password set"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
configDao.getValue("ldap.bind.password") >> "password"
configDao.getValue("ldap.bind.principal") >> "cn=rmurphy,dc=cloudstack,dc=org"
when: "Get authentication is called"
String authentication = ldapConfiguration.getAuthentication()
then: "authentication should be set to simple"
authentication == "simple"
}
def "Test that getBaseDn returns dc=cloudstack,dc=org"() {
given: "We have a ConfigDao, LdapManager and ldapConfiguration with a baseDn value set."
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.basedn") >> "dc=cloudstack,dc=org"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get basedn is called"
String baseDn = ldapConfiguration.getBaseDn();
then: "The set baseDn should be returned"
baseDn == "dc=cloudstack,dc=org"
}
def "Test that getEmailAttribute returns mail"() {
given: "Given that we have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.email.attribute") >> "mail"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get Email Attribute is called"
String emailAttribute = ldapConfiguration.getEmailAttribute()
then: "mail should be returned"
emailAttribute == "mail"
}
def "Test that getFactory returns com.sun.jndi.ldap.LdapCtxFactory"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get Factory is scalled"
String factory = ldapConfiguration.getFactory();
then: "com.sun.jndi.ldap.LdapCtxFactory is returned"
factory == "com.sun.jndi.ldap.LdapCtxFactory"
}
def "Test that getFirstnameAttribute returns givenname"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.firstname.attribute") >> "givenname"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get firstname attribute is called"
String firstname = ldapConfiguration.getFirstnameAttribute()
then: "givennam should be returned"
firstname == "givenname"
}
def "Test that getLastnameAttribute returns givenname"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.lastname.attribute") >> "sn"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get Lastname Attribute is scalled "
String lastname = ldapConfiguration.getLastnameAttribute()
then: "sn should be returned"
lastname == "sn"
}
def "Test that getReturnAttributes returns the correct data"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.firstname.attribute") >> "givenname"
configDao.getValue("ldap.lastname.attribute") >> "sn"
configDao.getValue("ldap.username.attribute") >> "uid"
configDao.getValue("ldap.email.attribute") >> "mail"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get return attributes is called"
String[] returnAttributes = ldapConfiguration.getReturnAttributes()
then: "An array containing uid, mail, givenname and sn is returned"
returnAttributes == ["uid", "mail", "givenname", "sn"]
}
def "Test that getScope returns SearchControls.SUBTREE_SCOPE"() {
given: "We have ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get scope is called"
int scope = ldapConfiguration.getScope()
then: "SearchControls.SUBTRE_SCOPE should be returned"
scope == SearchControls.SUBTREE_SCOPE;
}
def "Test that getUsernameAttribute returns uid"() {
given: "We have ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.username.attribute") >> "uid"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get Username Attribute is called"
String usernameAttribute = ldapConfiguration.getUsernameAttribute()
then: "uid should be returned"
usernameAttribute == "uid"
}
def "Test that getUserObject returns inetOrgPerson"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.user.object") >> "inetOrgPerson"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get user object is called"
String userObject = ldapConfiguration.getUserObject()
then: "inetOrgPerson is returned"
userObject == "inetOrgPerson"
}
def "Test that providerUrl successfully returns a URL when a configuration is available"() {
given: "We have a ConfigDao, LdapManager, LdapConfiguration"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
List<LdapConfigurationVO> ldapConfigurationList = new ArrayList()
ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
Pair<List<LdapConfigurationVO>, Integer> result = new Pair<List<LdapConfigurationVO>, Integer>();
result.set(ldapConfigurationList, ldapConfigurationList.size())
ldapManager.listConfigurations(_) >> result
LdapConfiguration ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "A request is made to get the providerUrl"
String providerUrl = ldapConfiguration.getProviderUrl()
then: "The providerUrl should be given."
providerUrl == "ldap://localhost:389"
}
def "Test that get search group principle returns successfully"() {
given: "We have a ConfigDao with a value for ldap.search.group.principle and an LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.search.group.principle") >> "cn=cloudstack,cn=users,dc=cloudstack,dc=org"
@ -32,131 +192,7 @@ class LdapConfigurationSpec extends spock.lang.Specification {
result == "cn=cloudstack,cn=users,dc=cloudstack,dc=org"
}
def "Test that getAuthentication returns none"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get authentication is called"
String authentication = ldapConfiguration.getAuthentication()
then: "none should be returned"
authentication == "none"
}
def "Test that getAuthentication returns simple"() {
given: "We have a configDao, LdapManager and LdapConfiguration with bind principle and password set"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
configDao.getValue("ldap.bind.password") >> "password"
configDao.getValue("ldap.bind.principal") >> "cn=rmurphy,dc=cloudstack,dc=org"
when: "Get authentication is called"
String authentication = ldapConfiguration.getAuthentication()
then: "authentication should be set to simple"
authentication == "simple"
}
def "Test that getBaseDn returns dc=cloudstack,dc=org"() {
given: "We have a ConfigDao, LdapManager and ldapConfiguration with a baseDn value set."
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.basedn") >> "dc=cloudstack,dc=org"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get basedn is called"
String baseDn = ldapConfiguration.getBaseDn();
then: "The set baseDn should be returned"
baseDn == "dc=cloudstack,dc=org"
}
def "Test that getEmailAttribute returns mail"() {
given: "Given that we have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.email.attribute") >> "mail"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get Email Attribute is called"
String emailAttribute = ldapConfiguration.getEmailAttribute()
then: "mail should be returned"
emailAttribute == "mail"
}
def "Test that getFactory returns com.sun.jndi.ldap.LdapCtxFactory"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get Factory is scalled"
String factory = ldapConfiguration.getFactory();
then: "com.sun.jndi.ldap.LdapCtxFactory is returned"
factory == "com.sun.jndi.ldap.LdapCtxFactory"
}
def "Test that getFirstnameAttribute returns givenname"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.firstname.attribute") >> "givenname"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get firstname attribute is called"
String firstname = ldapConfiguration.getFirstnameAttribute()
then: "givennam should be returned"
firstname == "givenname"
}
def "Test that getLastnameAttribute returns givenname"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.lastname.attribute") >> "sn"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get Lastname Attribute is scalled "
String lastname = ldapConfiguration.getLastnameAttribute()
then: "sn should be returned"
lastname == "sn"
}
def "Test that getReturnAttributes returns the correct data"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.firstname.attribute") >> "givenname"
configDao.getValue("ldap.lastname.attribute") >> "sn"
configDao.getValue("ldap.username.attribute") >> "uid"
configDao.getValue("ldap.email.attribute") >> "mail"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get return attributes is called"
String[] returnAttributes = ldapConfiguration.getReturnAttributes()
then: "An array containing uid, mail, givenname and sn is returned"
returnAttributes == ["uid", "mail", "givenname", "sn"]
}
def "Test that getScope returns SearchControls.SUBTREE_SCOPE"() {
given: "We have ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get scope is called"
int scope = ldapConfiguration.getScope()
then: "SearchControls.SUBTRE_SCOPE should be returned"
scope == SearchControls.SUBTREE_SCOPE;
}
def "Test that getSSLStatus can be true"() {
given: "We have a ConfigDao with values for truststore and truststore password set"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.truststore") >> "/tmp/ldap.ts"
configDao.getValue("ldap.truststore.password") >> "password"
def ldapManager = Mock(LdapManager)
LdapConfiguration ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "A request is made to get the status of SSL"
boolean result = ldapConfiguration.getSSLStatus();
then: "The response should be true"
result == true
}
def "Test that getTrustStorePassword resopnds"() {
def "Test that getTrustStorePassword resopnds"() {
given: "We have a ConfigDao with a value for truststore password"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.truststore.password") >> "password"
@ -170,46 +206,18 @@ class LdapConfigurationSpec extends spock.lang.Specification {
result == "password";
}
def "Test that getUsernameAttribute returns uid"() {
given: "We have ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.username.attribute") >> "uid"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get Username Attribute is called"
String usernameAttribute = ldapConfiguration.getUsernameAttribute()
then: "uid should be returned"
usernameAttribute == "uid"
}
def "Test that getSSLStatus can be true"() {
given: "We have a ConfigDao with values for truststore and truststore password set"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.truststore") >> "/tmp/ldap.ts"
configDao.getValue("ldap.truststore.password") >> "password"
def ldapManager = Mock(LdapManager)
LdapConfiguration ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
def "Test that getUserObject returns inetOrgPerson"() {
given: "We have a ConfigDao, LdapManager and LdapConfiguration"
def configDao = Mock(ConfigurationDao)
configDao.getValue("ldap.user.object") >> "inetOrgPerson"
def ldapManager = Mock(LdapManager)
def ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "Get user object is called"
String userObject = ldapConfiguration.getUserObject()
then: "inetOrgPerson is returned"
userObject == "inetOrgPerson"
}
when: "A request is made to get the status of SSL"
boolean result = ldapConfiguration.getSSLStatus();
def "Test that providerUrl successfully returns a URL when a configuration is available"() {
given: "We have a ConfigDao, LdapManager, LdapConfiguration"
def configDao = Mock(ConfigurationDao)
def ldapManager = Mock(LdapManager)
List<LdapConfigurationVO> ldapConfigurationList = new ArrayList()
ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
Pair<List<LdapConfigurationVO>, Integer> result = new Pair<List<LdapConfigurationVO>, Integer>();
result.set(ldapConfigurationList, ldapConfigurationList.size())
ldapManager.listConfigurations(_) >> result
LdapConfiguration ldapConfiguration = new LdapConfiguration(configDao, ldapManager)
when: "A request is made to get the providerUrl"
String providerUrl = ldapConfiguration.getProviderUrl()
then: "The providerUrl should be given."
providerUrl == "ldap://localhost:389"
}
then: "The response should be true"
result == true
}
}

View File

@ -16,6 +16,7 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.ldap.LdapConfigurationVO
class LdapConfigurationVOSpec extends spock.lang.Specification {

View File

@ -16,7 +16,13 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import spock.lang.Shared;
import org.apache.cloudstack.ldap.LdapConfiguration
import org.apache.cloudstack.ldap.LdapContextFactory
import spock.lang.Shared
import javax.naming.NamingException
import javax.naming.directory.SearchControls
import javax.naming.ldap.LdapContext
class LdapContextFactorySpec extends spock.lang.Specification {
@Shared
@ -44,75 +50,78 @@ class LdapContextFactorySpec extends spock.lang.Specification {
ldapConfiguration.getFirstnameAttribute() >> "givenname"
ldapConfiguration.getLastnameAttribute() >> "sn"
ldapConfiguration.getBaseDn() >> "dc=cloudstack,dc=org"
ldapConfiguration.getSSLStatus() >> true
ldapConfiguration.getTrustStore() >> "/tmp/ldap.ts"
ldapConfiguration.getTrustStorePassword() >> "password"
username = "rmurphy"
principal = "cn=" + username + "," + ldapConfiguration.getBaseDn()
password = "password"
password = "password"
}
def "Test succcessfully creating a initial context"() {
given: "We have a LdapContextFactory"
def ldapContextFactory = new LdapContextFactory(ldapConfiguration)
when: "A context attempts to bind and no Ldap server is avaiable"
ldapContextFactory.createInitialDirContext(null, null, true)
then: "An expection is thrown"
thrown NamingException
given: "We have a LdapContextFactory"
def ldapContextFactory = new LdapContextFactory(ldapConfiguration)
when: "A context attempts to bind and no Ldap server is avaiable"
ldapContextFactory.createInitialDirContext(null, null, true)
then: "An expection is thrown"
thrown NamingException
}
def "Test successful failed connection"() {
given: "We have a LdapContextFactory"
def ldapContextFactory = Spy(LdapContextFactory, constructorArgs: [ldapConfiguration])
when: "Test connection is executed"
ldapContextFactory.testConnection(ldapConfiguration.getProviderUrl())
then: "An exception is thrown"
thrown NamingException
given: "We have a LdapContextFactory"
def ldapContextFactory = Spy(LdapContextFactory, constructorArgs: [ldapConfiguration])
when: "Test connection is executed"
ldapContextFactory.testConnection(ldapConfiguration.getProviderUrl())
then: "An exception is thrown"
thrown NamingException
}
def "Test successfully binding as a user"() {
given: "We have a LdapContextFactory"
def ldapContextFactory = new LdapContextFactory(ldapConfiguration)
when: "A user attempts to bind and no LDAP server is avaiable"
ldapContextFactory.createUserContext(principal, password)
then: "An exception is thrown"
thrown NamingException
given: "We have a LdapContextFactory"
def ldapContextFactory = new LdapContextFactory(ldapConfiguration)
when: "A user attempts to bind and no LDAP server is avaiable"
ldapContextFactory.createUserContext(principal, password)
then: "An exception is thrown"
thrown NamingException
}
def "Test successfully creating a environment with username and password"() {
given: "We have an LdapContextFactory"
given: "We have an LdapContextFactory"
def ldapContextFactory = new LdapContextFactory(ldapConfiguration)
when: "A request for an environment is made"
when: "A request for an environment is made"
def result = ldapContextFactory.getEnvironment(null, null, null, true)
then: "The resulting values should be set"
then: "The resulting values should be set"
result['java.naming.provider.url'] == ldapConfiguration.getProviderUrl()
result['java.naming.factory.initial'] == ldapConfiguration.getFactory()
result['java.naming.security.principal'] == null
result['java.naming.security.authentication'] == ldapConfiguration.getAuthentication()
result['java.naming.security.credentials'] == null
result['java.naming.security.credentials'] == null
}
def "Test successfully creating a system environment with anon bind"() {
given: "We have an LdapContext Factory"
def ldapContextFactory = new LdapContextFactory(ldapConfiguration)
given: "We have an LdapContext Factory"
def ldapContextFactory = new LdapContextFactory(ldapConfiguration)
when: "A request for an environment is made"
def result = ldapContextFactory.getEnvironment(principal, password, null, false)
when: "A request for an environment is made"
def result = ldapContextFactory.getEnvironment(principal, password, null, false)
then: "The resulting values should be set"
result['java.naming.provider.url'] == ldapConfiguration.getProviderUrl()
result['java.naming.factory.initial'] == ldapConfiguration.getFactory()
result['java.naming.security.principal'] == principal
result['java.naming.security.authentication'] == "simple"
result['java.naming.security.credentials'] == password
}
then: "The resulting values should be set"
result['java.naming.provider.url'] == ldapConfiguration.getProviderUrl()
result['java.naming.factory.initial'] == ldapConfiguration.getFactory()
result['java.naming.security.principal'] == principal
result['java.naming.security.authentication'] == "simple"
result['java.naming.security.credentials'] == password
}
def "Test successully binding as system"() {
given: "We have a LdapContextFactory"
def ldapContextFactory = new LdapContextFactory(ldapConfiguration)
when: "A bind context attempts to bind and no Ldap server is avaiable"
ldapContextFactory.createBindContext()
then: "An exception is thrown"
thrown NamingException
def "Test successully binding as system"() {
given: "We have a LdapContextFactory"
def ldapContextFactory = new LdapContextFactory(ldapConfiguration)
when: "A bind context attempts to bind and no Ldap server is avaiable"
ldapContextFactory.createBindContext()
then: "An exception is thrown"
thrown NamingException
}
}

View File

@ -16,30 +16,60 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import com.cloud.exception.InvalidParameterValueException
import org.apache.cloudstack.api.ServerApiException
import org.apache.cloudstack.api.command.LdapAddConfigurationCmd
import org.apache.cloudstack.api.response.LdapConfigurationResponse
import org.apache.cloudstack.ldap.LdapUser;
import org.apache.cloudstack.ldap.LdapManager;
import org.apache.cloudstack.api.command.LdapCreateAccountCmd;
import org.apache.cloudstack.context.CallContext;
import com.cloud.user.AccountService;
import com.cloud.user.UserAccount;
import com.cloud.user.UserAccountVO;
import javax.naming.NamingException
class LdapCreateAccountCmdSpec extends spock.lang.Specification {
/*
def "Test failure to retrive LDAP user"() {
given:
LdapManager ldapManager = Mock(LdapManager)
ldapManager.getUser(_) >> { throw new NamingException() }
AccountService accountService = Mock(AccountService)
def ldapCreateAccount = Spy(LdapCreateAccount, constructorArgs: [ldapManager, accountService])
ldapCreateAccount.updateCallContext() >> System.out.println("Hello World");
when:
ldapCreateAccount.execute()
then:
thrown ServerApiException
} */
def "Test failure to retrive LDAP user"() {
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd and LDAP user that doesn't exist"
LdapManager ldapManager = Mock(LdapManager)
ldapManager.getUser(_) >> { throw new NamingException() }
AccountService accountService = Mock(AccountService)
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
ldapCreateAccountCmd.getCurrentContext() >> Mock(CallContext)
CallContext context = ldapCreateAccountCmd.getCurrentContext()
when: "An an account is created"
ldapCreateAccountCmd.execute()
then: "It fails and an exception is thrown"
thrown ServerApiException
}
def "Test failed creation due to a null response from cloudstack account creater"() {
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
LdapManager ldapManager = Mock(LdapManager)
ldapManager.getUser(_) >> new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
AccountService accountService = Mock(AccountService)
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
ldapCreateAccountCmd.getCurrentContext() >> Mock(CallContext)
ldapCreateAccountCmd.createCloudstackUserAccount(_) >> null
when: "Cloudstack fail to create the user"
ldapCreateAccountCmd.execute()
then: "An exception is thrown"
thrown ServerApiException
}
def "Test command name"() {
given: "We have an LdapManager, AccountService and LdapCreateAccount"
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
LdapManager ldapManager = Mock(LdapManager)
AccountService accountService = Mock(AccountService)
def ldapCreateAccount = new LdapCreateAccount(ldapManager, accountService)
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
when: "Get command name is called"
def result = ldapCreateAccount.getCommandName()
def result = ldapCreateAccountCmd.getCommandName()
then: "createaccountresponse is returned"
result == "createaccountresponse"
}
@ -49,20 +79,20 @@ class LdapCreateAccountCmdSpec extends spock.lang.Specification {
LdapManager ldapManager = Mock(LdapManager)
AccountService accountService = Mock(AccountService)
def ldapCreateAccount = Spy(LdapCreateAccount, constructorArgs: [ldapManager, accountService])
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
when: "Get entity owner id is called"
long ownerId = ldapCreateAccount.getEntityOwnerId()
long ownerId = ldapCreateAccountCmd.getEntityOwnerId()
then: "1 is returned"
ownerId == 1
}
def "Test password generation"() {
given: "We have an LdapManager, AccountService and LdapCreateAccount"
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
LdapManager ldapManager = Mock(LdapManager)
AccountService accountService = Mock(AccountService)
def ldapCreateAccount = new LdapCreateAccount(ldapManager, accountService)
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
when: "A password is generated"
def result = ldapCreateAccount.generatePassword()
def result = ldapCreateAccountCmd.generatePassword()
then: "The result shouldn't be null or empty"
result != ""
result != null
@ -72,9 +102,9 @@ class LdapCreateAccountCmdSpec extends spock.lang.Specification {
given: "We have an LdapManager, AccountService andL dapCreateAccount"
LdapManager ldapManager = Mock(LdapManager)
AccountService accountService = Mock(AccountService)
def ldapCreateAccount = new LdapCreateAccount(ldapManager, accountService);
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService);
when: "a user with an username, email, firstname and lastname is validated"
def result = ldapCreateAccount.validateUser(new LdapUser("username","email","firstname","lastname","principal"))
def result = ldapCreateAccountCmd.validateUser(new LdapUser("username","email","firstname","lastname","principal"))
then: "the result is true"
result == true
}
@ -83,9 +113,9 @@ class LdapCreateAccountCmdSpec extends spock.lang.Specification {
given: "We have an LdapManager, AccountService andL dapCreateAccount"
LdapManager ldapManager = Mock(LdapManager)
AccountService accountService = Mock(AccountService)
def ldapCreateAccount = new LdapCreateAccount(ldapManager, accountService)
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
when: "A user with no email address attempts to validate"
ldapCreateAccount.validateUser(new LdapUser("username",null,"firstname","lastname","principal"))
ldapCreateAccountCmd.validateUser(new LdapUser("username",null,"firstname","lastname","principal"))
then: "An exception is thrown"
thrown Exception
}
@ -94,20 +124,20 @@ class LdapCreateAccountCmdSpec extends spock.lang.Specification {
given: "We have an LdapManager, AccountService andL dapCreateAccount"
LdapManager ldapManager = Mock(LdapManager)
AccountService accountService = Mock(AccountService)
def ldapCreateAccount = new LdapCreateAccount(ldapManager, accountService)
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
when: "A user with no firstname attempts to validate"
ldapCreateAccount.validateUser(new LdapUser("username","email",null,"lastname","principal"))
ldapCreateAccountCmd.validateUser(new LdapUser("username","email",null,"lastname","principal"))
then: "An exception is thrown"
thrown Exception
}
def "Test validate User empty lastname"() {
given: "We have an LdapManager, AccountService and LdapCreateAccount"
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
LdapManager ldapManager = Mock(LdapManager)
AccountService accountService = Mock(AccountService)
def ldapCreateAccount = new LdapCreateAccount(ldapManager, accountService)
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
when: "A user with no lastname attempts to validate"
ldapCreateAccount.validateUser(new LdapUser("username","email","firstname",null,"principal"))
ldapCreateAccountCmd.validateUser(new LdapUser("username","email","firstname",null,"principal"))
then: "An exception is thown"
thrown Exception
}
@ -116,12 +146,9 @@ class LdapCreateAccountCmdSpec extends spock.lang.Specification {
given: "We have an LdapManager, AccountService andL dapCreateAccount"
LdapManager ldapManager = Mock(LdapManager)
AccountService accountService = Mock(AccountService)
def ldapCreateAccount = Spy(LdapCreateAccount, constructorArgs: [ldapManager, accountService])
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
when: "Get command name is called"
def commandName = ldapCreateAccount.getCommandName()
def commandName = ldapCreateAccountCmd.getCommandName()
then: "createaccountresponse is returned"
commandName == "createaccountresponse"
}

View File

@ -16,48 +16,53 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import com.cloud.exception.InvalidParameterValueException
import org.apache.cloudstack.api.ServerApiException
import org.apache.cloudstack.api.command.LdapDeleteConfigurationCmd
import org.apache.cloudstack.api.response.LdapConfigurationResponse
import org.apache.cloudstack.ldap.LdapManager
class LdapDeleteConfigurationCmdSpec extends spock.lang.Specification {
def "Test failed response from execute"() {
given: "We have an LdapManager and LdapDeleteConfigurationCmd"
def ldapManager = Mock(LdapManager)
given: "We have an LdapManager and LdapDeleteConfigurationCmd"
def ldapManager = Mock(LdapManager)
ldapManager.deleteConfiguration(_) >> { throw new InvalidParameterValueException() }
def ldapDeleteConfigurationCmd = new LdapDeleteConfigurationCmd(ldapManager)
when:"LdapDeleteConfigurationCmd is executed and no configuration exists"
when:"LdapDeleteConfigurationCmd is executed and no configuration exists"
ldapDeleteConfigurationCmd.execute()
then: "An exception is thrown"
then: "An exception is thrown"
thrown ServerApiException
}
def "Test getEntityOwnerId is 1"() {
given: "We have an LdapManager and LdapDeleteConfigurationCmd"
given: "We have an LdapManager and LdapDeleteConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapDeleteConfigurationCmd = new LdapDeleteConfigurationCmd(ldapManager)
when: "Get entity owner id is called"
when: "Get entity owner id is called"
long ownerId = ldapDeleteConfigurationCmd.getEntityOwnerId()
then: "1 is returned"
ownerId == 1
then: "1 is returned"
ownerId == 1
}
def "Test successful response from execute"() {
given: "We have an LdapManager and LdapDeleteConfigurationCmd"
def ldapManager = Mock(LdapManager)
ldapManager.deleteConfiguration(_) >> new LdapConfigurationResponse("localhost")
def ldapDeleteConfigurationCmd = new LdapDeleteConfigurationCmd(ldapManager)
when: "LdapDeleteConfigurationCmd is executed"
ldapDeleteConfigurationCmd.execute()
then: "The given configuration should be deleted and returned"
ldapDeleteConfigurationCmd.responseObject.hostname == "localhost"
given: "We have an LdapManager and LdapDeleteConfigurationCmd"
def ldapManager = Mock(LdapManager)
ldapManager.deleteConfiguration(_) >> new LdapConfigurationResponse("localhost")
def ldapDeleteConfigurationCmd = new LdapDeleteConfigurationCmd(ldapManager)
when: "LdapDeleteConfigurationCmd is executed"
ldapDeleteConfigurationCmd.execute()
then: "The given configuration should be deleted and returned"
ldapDeleteConfigurationCmd.responseObject.hostname == "localhost"
}
def "Test successful return of getCommandName"() {
given: "We have an LdapManager and LdapDeleteConfigurationCmd"
def ldapManager = Mock(LdapManager)
given: "We have an LdapManager and LdapDeleteConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapDeleteConfigurationCmd = new LdapDeleteConfigurationCmd(ldapManager)
when: "Get Command name is called"
when: "Get Command name is called"
String commandName = ldapDeleteConfigurationCmd.getCommandName()
then: "ldapconfigurationresponse is returned"
then: "ldapconfigurationresponse is returned"
commandName == "ldapconfigurationresponse"
}
}

View File

@ -1,66 +0,0 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package groovy.org.apache.cloudstack.ldap
class LdapListAllUsersCmdSpec extends spock.lang.Specification {
def "Test getEntityOwnerId is 1"() {
given: "We have an LdapManager and LdapListAllUsersCmd"
def ldapManager = Mock(LdapManager)
def ldapListAllUsersCmd = new LdapListAllUsersCmd(ldapManager)
when: "Get entity owner id is called"
long ownerId = ldapListAllUsersCmd.getEntityOwnerId()
then: "a 1 should be returned"
ownerId == 1
}
def "Test successful empty response from execute"() {
given: "We have a LdapManager with no users and a LdapListAllUsersCmd"
def ldapManager = Mock(LdapManager)
ldapManager.getUsers() >> {throw new NoLdapUserMatchingQueryException()}
def ldapListAllUsersCmd = new LdapListAllUsersCmd(ldapManager)
when: "LdapListAllUsersCmd is executed"
ldapListAllUsersCmd.execute()
then: "An array of size 0 is returned"
ldapListAllUsersCmd.responseObject.getResponses().size() == 0
}
def "Test successful response from execute"() {
given: "We have an LdapManager, one user and a LdapListAllUsersCmd"
def ldapManager = Mock(LdapManager)
List<LdapUser> users = new ArrayList()
users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
ldapManager.getUsers() >> users
LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
ldapManager.createLdapUserResponse(_) >> response
def ldapListAllUsersCmd = new LdapListAllUsersCmd(ldapManager)
when: "LdapListAllUsersCmd is executed"
ldapListAllUsersCmd.execute()
then: "a list of size not 0 is returned"
ldapListAllUsersCmd.responseObject.getResponses().size() != 0
}
def "Test successful return of getCommandName"() {
given: "We have an LdapManager and a LdapListAllUsersCmd"
def ldapManager = Mock(LdapManager)
def ldapListAllUsersCmd = new LdapListAllUsersCmd(ldapManager)
when: "Get command name is called"
String commandName = ldapListAllUsersCmd.getCommandName()
then: "ldapuserresponse is returned"
commandName == "ldapuserresponse"
}
}

View File

@ -16,36 +16,43 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.api.ServerApiException
import org.apache.cloudstack.api.command.LdapListConfigurationCmd
import org.apache.cloudstack.api.response.LdapConfigurationResponse
import org.apache.cloudstack.ldap.LdapConfigurationVO
import org.apache.cloudstack.ldap.LdapManager
import com.cloud.utils.Pair
class LdapListConfigurationCmdSpec extends spock.lang.Specification {
def "Test failed response from execute"() {
given: "We have an LdapManager and a LdapListConfigurationsCmd"
def ldapManager = Mock(LdapManager)
List<LdapConfigurationVO> ldapConfigurationList = new ArrayList()
Pair<List<LdapConfigurationVO>, Integer> ldapConfigurations = new Pair<List<LdapConfigurationVO>, Integer>();
ldapConfigurations.set(ldapConfigurationList, ldapConfigurationList.size())
ldapManager.listConfigurations(_) >> ldapConfigurations
def ldapListConfigurationCmd = new LdapListConfigurationCmd(ldapManager)
when: "LdapListConfigurationCmd is executed"
ldapListConfigurationCmd.execute()
then: "Its response object contains an array that is 0"
ldapListConfigurationCmd.getResponseObject().getResponses().size() == 0
given: "We have an LdapManager and a LdapListConfigurationsCmd"
def ldapManager = Mock(LdapManager)
List<LdapConfigurationVO> ldapConfigurationList = new ArrayList()
Pair<List<LdapConfigurationVO>, Integer> ldapConfigurations = new Pair<List<LdapConfigurationVO>, Integer>();
ldapConfigurations.set(ldapConfigurationList, ldapConfigurationList.size())
ldapManager.listConfigurations(_) >> ldapConfigurations
def ldapListConfigurationCmd = new LdapListConfigurationCmd(ldapManager)
when: "LdapListConfigurationCmd is executed"
ldapListConfigurationCmd.execute()
then: "Its response object contains an array that is 0"
ldapListConfigurationCmd.getResponseObject().getResponses().size() == 0
}
def "Test getEntityOwnerId is 1"() {
given: "We have an LdapManager and ListLdapConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapListConfigurationCmd = new LdapListConfigurationCmd(ldapManager)
when: "Get entity owner id is called"
long ownerId = ldapListConfigurationCmd.getEntityOwnerId()
then: "a 1 is returned"
ownerId == 1
given: "We have an LdapManager and ListLdapConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapListConfigurationCmd = new LdapListConfigurationCmd(ldapManager)
when: "Get entity owner id is called"
long ownerId = ldapListConfigurationCmd.getEntityOwnerId()
then: "a 1 is returned"
ownerId == 1
}
def "Test successful response from execute"() {
given: "We have an LdapManager with a configuration and a LdapListConfigurationsCmd"
def ldapManager = Mock(LdapManager)
given: "We have an LdapManager with a configuration and a LdapListConfigurationsCmd"
def ldapManager = Mock(LdapManager)
List<LdapConfigurationVO> ldapConfigurationList = new ArrayList()
ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
Pair<List<LdapConfigurationVO>, Integer> ldapConfigurations = new Pair<List<LdapConfigurationVO>, Integer>();
@ -53,39 +60,39 @@ class LdapListConfigurationCmdSpec extends spock.lang.Specification {
ldapManager.listConfigurations(_) >> ldapConfigurations
ldapManager.createLdapConfigurationResponse(_) >> new LdapConfigurationResponse("localhost", 389)
def ldapListConfigurationCmd = new LdapListConfigurationCmd(ldapManager)
when: "LdapListConfigurationsCmd is executed"
when: "LdapListConfigurationsCmd is executed"
ldapListConfigurationCmd.execute()
then: "Its response object contains an array that is not 0 in size"
ldapListConfigurationCmd.getResponseObject().getResponses().size() != 0
then: "Its response object contains an array that is not 0 in size"
ldapListConfigurationCmd.getResponseObject().getResponses().size() != 0
}
def "Test successful return of getCommandName"() {
given: "We have an LdapManager and LdapListConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapListConfigurationCmd = new LdapListConfigurationCmd(ldapManager)
when: "Get command name is called"
String commandName = ldapListConfigurationCmd.getCommandName()
then: "ldapconfigurationresponse is returned"
commandName == "ldapconfigurationresponse"
given: "We have an LdapManager and LdapListConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapListConfigurationCmd = new LdapListConfigurationCmd(ldapManager)
when: "Get command name is called"
String commandName = ldapListConfigurationCmd.getCommandName()
then: "ldapconfigurationresponse is returned"
commandName == "ldapconfigurationresponse"
}
def "Test successful setting of hostname"() {
given: "We have an LdapManager and LdapListConfigurationCmd"
given: "We have an LdapManager and LdapListConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapListConfigurationCmd = new LdapListConfigurationCmd(ldapManager)
when: "The hostname is set"
when: "The hostname is set"
ldapListConfigurationCmd.setHostname("localhost")
then: "Get hostname returns the set value"
ldapListConfigurationCmd.getHostname() == "localhost"
then: "Get hostname returns the set value"
ldapListConfigurationCmd.getHostname() == "localhost"
}
def "Test successful setting of Port"() {
given: "We have an LdapManager and LdapListConfigurationCmd"
def ldapManager = Mock(LdapManager)
given: "We have an LdapManager and LdapListConfigurationCmd"
def ldapManager = Mock(LdapManager)
def ldapListConfigurationCmd = new LdapListConfigurationCmd(ldapManager)
when: "The port is set"
when: "The port is set"
ldapListConfigurationCmd.setPort(389)
then: "Get port returns the set value"
ldapListConfigurationCmd.getPort() == 389
then: "Get port returns the set value"
ldapListConfigurationCmd.getPort() == 389
}
}

View File

@ -0,0 +1,123 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.api.command.LdapListUsersCmd
import org.apache.cloudstack.api.ServerApiException
import org.apache.cloudstack.api.command.admin.user.ListUsersCmd
import org.apache.cloudstack.api.response.LdapUserResponse
import org.apache.cloudstack.api.response.ListResponse
import org.apache.cloudstack.api.response.UserResponse
import org.apache.cloudstack.ldap.LdapManager
import org.apache.cloudstack.ldap.LdapUser
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException
import org.apache.cloudstack.query.QueryService
class LdapListUsersCmdSpec extends spock.lang.Specification {
def "Test getEntityOwnerId is 1"() {
given: "We have an LdapManager, QueryService and LdapListUsersCmd"
def ldapManager = Mock(LdapManager)
def queryService = Mock(QueryService)
def ldapListUsersCmd = new LdapListUsersCmd(ldapManager, queryService)
when: "Get entity owner id is called"
long ownerId = ldapListUsersCmd.getEntityOwnerId()
then: "a 1 should be returned"
ownerId == 1
}
def "Test successful empty response from execute"() {
given: "We have a LdapManager with no users, QueryService and a LdapListUsersCmd"
def ldapManager = Mock(LdapManager)
ldapManager.getUsers() >> {throw new NoLdapUserMatchingQueryException()}
def queryService = Mock(QueryService)
def ldapListUsersCmd = new LdapListUsersCmd(ldapManager, queryService)
when: "LdapListUsersCmd is executed"
ldapListUsersCmd.execute()
then: "An array of size 0 is returned"
ldapListUsersCmd.responseObject.getResponses().size() == 0
}
def "Test successful response from execute"() {
given: "We have an LdapManager, one user, QueryService and a LdapListUsersCmd"
def ldapManager = Mock(LdapManager)
List<LdapUser> users = new ArrayList()
users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
ldapManager.getUsers() >> users
LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
ldapManager.createLdapUserResponse(_) >> response
def queryService = Mock(QueryService)
def ldapListUsersCmd = new LdapListUsersCmd(ldapManager, queryService)
when: "LdapListUsersCmd is executed"
ldapListUsersCmd.execute()
then: "a list of size not 0 is returned"
ldapListUsersCmd.responseObject.getResponses().size() != 0
}
def "Test successful return of getCommandName"() {
given: "We have an LdapManager, QueryService and a LdapListUsersCmd"
def ldapManager = Mock(LdapManager)
def queryService = Mock(QueryService)
def ldapListUsersCmd = new LdapListUsersCmd(ldapManager, queryService)
when: "Get command name is called"
String commandName = ldapListUsersCmd.getCommandName()
then: "ldapuserresponse is returned"
commandName == "ldapuserresponse"
}
def "Test successful result from isACloudstackUser"() {
given: "We have an LdapManager and a LdapListUsersCmd"
def ldapManager = Mock(LdapManager)
def queryService = Mock(QueryService)
UserResponse userResponse = new UserResponse()
userResponse.setUsername("rmurphy")
ArrayList<UserResponse> responses = new ArrayList<UserResponse>()
responses.add(userResponse);
ListResponse<UserResponse> queryServiceResponse = new ListResponse<UserResponse>()
queryServiceResponse.setResponses(responses)
queryService.searchForUsers(_) >> queryServiceResponse
def ldapUser = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
def ldapListUsersCmd = new LdapListUsersCmd(ldapManager,queryService)
when: "isACloudstackUser is executed"
def result = ldapListUsersCmd.isACloudstackUser(ldapUser);
then: "The result is true"
result == true;
}
def "Test failed result from isACloudstackUser"() {
given: "We have an LdapManager and a LdapListUsersCmd"
def ldapManager = Mock(LdapManager)
def queryService = Mock(QueryService)
queryService.searchForUsers(_) >> new ListResponse<UserResponse>()
def ldapUser = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
def ldapListUsersCmd = new LdapListUsersCmd(ldapManager,queryService)
when: "isACloudstackUser is executed"
def result = ldapListUsersCmd.isACloudstackUser(ldapUser);
then: "The result is true"
result == false;
}
}

View File

@ -16,300 +16,292 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import javax.naming.NamingException
import javax.naming.ldap.InitialLdapContext
import org.apache.cloudstack.api.command.LdapListConfigurationCmd
import org.apache.cloudstack.ldap.*
import org.apache.cloudstack.ldap.dao.LdapConfigurationDaoImpl
import com.cloud.exception.InvalidParameterValueException
import com.cloud.utils.Pair
class LdapManagerImplSpec extends spock.lang.Specification {
def "Test failing of getUser due to bind issue"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> { throw new NamingException() }
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a user but there is a bind issue"
ldapManager.getUser("rmurphy")
then: "an exception is thrown"
thrown NamingException
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> { throw new NamingException() }
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a user but there is a bind issue"
ldapManager.getUser("rmurphy")
then: "an exception is thrown"
thrown NamingException
}
def "Test failing of getUsers due to bind issue"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> { throw new NamingException() }
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a group of users but there is a bind issue"
ldapManager.getUsers()
then: "An exception is thrown"
thrown NoLdapUserMatchingQueryException
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> { throw new NamingException() }
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a group of users but there is a bind issue"
ldapManager.getUsers()
then: "An exception is thrown"
thrown NoLdapUserMatchingQueryException
}
def "Test failing of searchUsers due to a failure to bind"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> { throw new NamingException() }
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for users"
ldapManager.searchUsers("rmurphy")
then: "An exception is thrown"
thrown NoLdapUserMatchingQueryException
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> { throw new NamingException() }
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for users"
ldapManager.searchUsers("rmurphy")
then: "An exception is thrown"
thrown NoLdapUserMatchingQueryException
}
def "Test LdapConfigurationResponse generation"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap configuration response is generated"
def result = ldapManager.createLdapConfigurationResponse(new LdapConfigurationVO("localhost", 389))
then: "the result of the response should match the given LdapConfigurationVO"
result.hostname == "localhost"
result.port == 389
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap configuration response is generated"
def result = ldapManager.createLdapConfigurationResponse(new LdapConfigurationVO("localhost", 389))
then: "the result of the response should match the given LdapConfigurationVO"
result.hostname == "localhost"
result.port == 389
}
def "Test LdapUserResponse generation"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap user response is generated"
def result = ldapManager.createLdapUserResponse(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
then: "TThe result of the response should match the given ldap user"
result.username == "rmurphy"
result.email == "rmurphy@test.com"
result.firstname == "Ryan"
result.lastname == "Murphy"
result.principal == "cn=rmurphy,dc=cloudstack,dc=org"
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap user response is generated"
def result = ldapManager.createLdapUserResponse(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
then: "The result of the response should match the given ldap user"
result.username == "rmurphy"
result.email == "rmurphy@test.com"
result.firstname == "Ryan"
result.lastname == "Murphy"
result.principal == "cn=rmurphy,dc=cloudstack,dc=org"
}
def "Test success getUsers"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> null
List<LdapUser> users = new ArrayList<>();
users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
ldapUserManager.getUsers(_) >> users;
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a group of users"
def result = ldapManager.getUsers()
then: "A list greater than 0 is returned"
result.size() > 0;
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> null
List<LdapUser> users = new ArrayList<>();
users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
ldapUserManager.getUsers(_) >> users;
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a group of users"
def result = ldapManager.getUsers()
then: "A list greater than 0 is returned"
result.size() > 0;
}
def "Test success of getUser"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> null
ldapUserManager.getUser(_, _) >> new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a user"
def result = ldapManager.getUser("rmurphy")
then: "The user is returned"
result.username == "rmurphy"
result.email == "rmurphy@test.com"
result.firstname == "Ryan"
result.lastname == "Murphy"
result.principal == "cn=rmurphy,dc=cloudstack,dc=org"
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> null
ldapUserManager.getUser(_, _) >> new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for a user"
def result = ldapManager.getUser("rmurphy")
then: "The user is returned"
result.username == "rmurphy"
result.email == "rmurphy@test.com"
result.firstname == "Ryan"
result.lastname == "Murphy"
result.principal == "cn=rmurphy,dc=cloudstack,dc=org"
}
def "Test successful closing of context"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "The context is closed"
def context = Mock(InitialLdapContext)
ldapManager.closeContext(context)
then: "The context is null"
context.defaultInitCtx == null
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "The context is closed"
def context = Mock(InitialLdapContext)
ldapManager.closeContext(context)
then: "The context is null"
context.defaultInitCtx == null
}
def "Test successful failed result from canAuthenticate due to bad password"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
ldapContextFactory.createUserContext(_, _) >> { throw new NamingException() }
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = Spy(LdapManagerImpl, constructorArgs: [ldapConfigurationDao, ldapContextFactory, ldapUserManager])
ldapManager.getUser(_) >> { new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org") }
when: "The user attempts to authenticate with a bad password"
when: "The user attempts to authenticate with a bad password"
def result = ldapManager.canAuthenticate("rmurphy", "password")
then: "The authentication fails"
result == false
then: "The authentication fails"
result == false
}
def "Test successful failed result from canAuthenticate due to user not found"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = Spy(LdapManagerImpl, constructorArgs: [ldapConfigurationDao, ldapContextFactory, ldapUserManager])
ldapManager.getUser(_) >> { throw new NamingException() }
when: "The user attempts to authenticate and the user is not found"
def result = ldapManager.canAuthenticate("rmurphy", "password")
then: "the authentication fails"
result == false
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = Spy(LdapManagerImpl, constructorArgs: [ldapConfigurationDao, ldapContextFactory, ldapUserManager])
ldapManager.getUser(_) >> { throw new NamingException() }
when: "The user attempts to authenticate and the user is not found"
def result = ldapManager.canAuthenticate("rmurphy", "password")
then: "the authentication fails"
result == false
}
def "Test successful failed result from deleteConfiguration due to configuration not existing"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapConfigurationDao.findByHostname(_) >> null
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap configuration that doesn't exist is deleted"
ldapManager.deleteConfiguration("localhost")
then: "A exception is thrown"
thrown InvalidParameterValueException
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapConfigurationDao.findByHostname(_) >> null
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap configuration that doesn't exist is deleted"
ldapManager.deleteConfiguration("localhost")
then: "A exception is thrown"
thrown InvalidParameterValueException
}
def "Test successful failing to close of context"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "The context is closed"
when: "The context is closed"
def context = Mock(InitialLdapContext)
context.close() >> { throw new NamingException() }
ldapManager.closeContext(context)
then: "An exception is thrown"
context.defaultInitCtx == null
then: "An exception is thrown"
context.defaultInitCtx == null
}
def "Test successful result from canAuthenticate"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
ldapContextFactory.createUserContext(_, _) >> null
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = Spy(LdapManagerImpl, constructorArgs: [ldapConfigurationDao, ldapContextFactory, ldapUserManager])
ldapManager.getUser(_) >> { new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org") }
when: "A user authenticates"
def result = ldapManager.canAuthenticate("rmurphy", "password")
then: "The result is true"
result == true
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
ldapContextFactory.createUserContext(_, _) >> null
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = Spy(LdapManagerImpl, constructorArgs: [ldapConfigurationDao, ldapContextFactory, ldapUserManager])
ldapManager.getUser(_) >> { new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org") }
when: "A user authenticates"
def result = ldapManager.canAuthenticate("rmurphy", "password")
then: "The result is true"
result == true
}
def "Test successful result from deleteConfiguration"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapConfigurationDao.findByHostname(_) >> {
def configuration = new LdapConfigurationVO("localhost", 389)
configuration.setId(0);
return configuration;
}
ldapConfigurationDao.remove(_) >> null
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap configuration is deleted"
def result = ldapManager.deleteConfiguration("localhost")
then: "The deleted configuration is returned"
result.hostname == "localhost"
result.port == 389
}
def "Test successful result from searchUsers"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> null;
List<LdapUser> users = new ArrayList<LdapUser>();
users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
ldapUserManager.getUsers(_, _) >> users;
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for users"
def result = ldapManager.searchUsers("rmurphy");
then: "A list of atleast 1 is returned"
result.size() > 0;
}
def "Test successfully addConfiguration"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext(_) >> null
ldapConfigurationDao.persist(_) >> null
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap configuration is added"
def result = ldapManager.addConfiguration("localhost", 389)
then: "the resulting object contain the given hostname and port"
result.hostname == "localhost"
result.port == 389
}
def "Test that addConfiguration fails when a binding fails"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext(_) >> { throw new NamingException() }
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A configuration is added that can not be binded"
ldapManager.addConfiguration("localhost", 389)
then: "An exception is thrown"
thrown InvalidParameterValueException
}
def "Test that addConfiguration fails when a duplicate configuration exists"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapConfigurationDao.findByHostname(_) >> new LdapConfigurationVO("localhost", 389)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "a configuration that already exists is added"
ldapManager.addConfiguration("localhost", 389)
then: "An exception is thrown"
thrown InvalidParameterValueException
}
def "Test that getCommands isn't empty"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "Get commands is called"
def result = ldapManager.getCommands()
then: "it must have atleast 1 command"
result.size() > 0
}
def "Testing of isLdapEnabled"() {
given:
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
List<LdapConfigurationVO> ldapConfigurationList = new ArrayList()
ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
Pair<List<LdapConfigurationVO>, Integer> configurations = new Pair<List<LdapConfigurationVO>, Integer>();
configurations.set(ldapConfigurationList, ldapConfigurationList.size())
ldapConfigurationDao.searchConfigurations(_, _) >> configurations
ldapConfigurationDao.findByHostname(_) >> {
def configuration = new LdapConfigurationVO("localhost", 389)
configuration.setId(0);
return configuration;
}
ldapConfigurationDao.remove(_) >> null
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when:
def result = ldapManager.isLdapEnabled();
then:
result == true;
}
when: "A ldap configuration is deleted"
def result = ldapManager.deleteConfiguration("localhost")
then: "The deleted configuration is returned"
result.hostname == "localhost"
result.port == 389
}
def "Testing of listConfigurations"() {
given:
def "Test successful result from searchUsers"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext() >> null;
List<LdapUser> users = new ArrayList<LdapUser>();
users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
ldapUserManager.getUsers(_, _) >> users;
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "We search for users"
def result = ldapManager.searchUsers("rmurphy");
then: "A list of atleast 1 is returned"
result.size() > 0;
}
def "Test successfully addConfiguration"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext(_) >> null
ldapConfigurationDao.persist(_) >> null
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A ldap configuration is added"
def result = ldapManager.addConfiguration("localhost", 389)
then: "the resulting object contain the given hostname and port"
result.hostname == "localhost"
result.port == 389
}
def "Test that addConfiguration fails when a binding fails"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapContextFactory.createBindContext(_) >> { throw new NamingException() }
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A configuration is added that can not be binded"
ldapManager.addConfiguration("localhost", 389)
then: "An exception is thrown"
thrown InvalidParameterValueException
}
def "Test that addConfiguration fails when a duplicate configuration exists"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
ldapConfigurationDao.findByHostname(_) >> new LdapConfigurationVO("localhost", 389)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "a configuration that already exists is added"
ldapManager.addConfiguration("localhost", 389)
then: "An exception is thrown"
thrown InvalidParameterValueException
}
def "Test that getCommands isn't empty"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "Get commands is called"
def result = ldapManager.getCommands()
then: "it must have atleast 1 command"
result.size() > 0
}
def "Testing of listConfigurations"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
@ -319,9 +311,26 @@ class LdapManagerImplSpec extends spock.lang.Specification {
configurations.set(ldapConfigurationList, ldapConfigurationList.size())
ldapConfigurationDao.searchConfigurations(_, _) >> configurations
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when:
when: "A request for configurations is made"
def result = ldapManager.listConfigurations(new LdapListConfigurationCmd())
then:
result.second() > 0
then: "Then atleast 1 ldap configuration is returned"
result.second() > 0
}
def "Testing of isLdapEnabled"() {
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
def ldapConfigurationDao = Mock(LdapConfigurationDaoImpl)
def ldapContextFactory = Mock(LdapContextFactory)
def ldapUserManager = Mock(LdapUserManager)
List<LdapConfigurationVO> ldapConfigurationList = new ArrayList()
ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
Pair<List<LdapConfigurationVO>, Integer> configurations = new Pair<List<LdapConfigurationVO>, Integer>();
configurations.set(ldapConfigurationList, ldapConfigurationList.size())
ldapConfigurationDao.searchConfigurations(_, _) >> configurations
def ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, ldapUserManager)
when: "A request to find out is ldap enabled"
def result = ldapManager.isLdapEnabled();
then: "true is returned because a configuration was found"
result == true;
}
}

View File

@ -16,51 +16,57 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.api.ServerApiException
import org.apache.cloudstack.api.command.LdapUserSearchCmd
import org.apache.cloudstack.api.response.LdapUserResponse
import org.apache.cloudstack.ldap.LdapManager
import org.apache.cloudstack.ldap.LdapUser
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException
class LdapSearchUserCmdSpec extends spock.lang.Specification {
def "Test getEntityOwnerId is 1"() {
given:
given: "We have an Ldap manager and ldap user search cmd"
def ldapManager = Mock(LdapManager)
def ldapUserSearchCmd = new LdapUserSearchCmd(ldapManager)
when:
long ownerId = ldapUserSearchCmd.getEntityOwnerId()
then:
ownerId == 1
when: "getEntityOwnerId is called"
long ownerId = ldapUserSearchCmd.getEntityOwnerId()
then: "1 is returned"
ownerId == 1
}
def "Test successful empty response from execute"() {
given:
given: "We have an Ldap manager and ldap user search cmd"
def ldapManager = Mock(LdapManager)
ldapManager.searchUsers(_) >> {throw new NoLdapUserMatchingQueryException()}
def ldapUserSearchCmd = new LdapUserSearchCmd(ldapManager)
when:
when: "The command is executed with no users found"
ldapUserSearchCmd.execute()
then:
then: "An empty array is returned"
ldapUserSearchCmd.responseObject.getResponses().size() == 0
}
def "Test successful response from execute"() {
given:
given: "We have an Ldap manager and ldap user search cmd"
def ldapManager = Mock(LdapManager)
List<LdapUser> users = new ArrayList()
users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
ldapManager.searchUsers(_) >> users
LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
ldapManager.createLdapUserResponse(_) >> response
List<LdapUser> users = new ArrayList()
users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org"))
ldapManager.searchUsers(_) >> users
LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org")
ldapManager.createLdapUserResponse(_) >> response
def ldapUserSearchCmd = new LdapUserSearchCmd(ldapManager)
when:
ldapUserSearchCmd.execute()
then:
ldapUserSearchCmd.responseObject.getResponses().size() != 0
when: "The command is executed"
ldapUserSearchCmd.execute()
then: "A array with length of atleast 1 is returned"
ldapUserSearchCmd.responseObject.getResponses().size() > 0
}
def "Test successful return of getCommandName"() {
given:
given: "We have an Ldap manager and ldap user search cmd"
def ldapManager = Mock(LdapManager)
def ldapUserSearchCmd = new LdapUserSearchCmd(ldapManager)
when:
when: "When a request for the command name is made"
String commandName = ldapUserSearchCmd.getCommandName()
then:
then: "ldapuserresponse is returned"
commandName == "ldapuserresponse"
}
}

View File

@ -16,10 +16,16 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchResult;
import org.apache.cloudstack.ldap.LdapConfiguration
import org.apache.cloudstack.ldap.LdapUserManager
import spock.lang.Shared
import spock.lang.Shared;
import javax.naming.NamingException
import javax.naming.directory.Attribute
import javax.naming.directory.Attributes
import javax.naming.directory.SearchControls
import javax.naming.directory.SearchResult
import javax.naming.ldap.LdapContext
class LdapUserManagerSpec extends spock.lang.Specification {
@ -42,52 +48,51 @@ class LdapUserManagerSpec extends spock.lang.Specification {
private def principal
private def createContext() {
Attributes attributes = createUserAttributes(username, email, firstname, lastname)
SearchResult searchResults = createSearchResult(attributes)
def searchUsersResults = new BasicNamingEnumerationImpl()
searchUsersResults.add(searchResults);
Attributes attributes = createUserAttributes(username, email, firstname, lastname)
SearchResult searchResults = createSearchResult(attributes)
def searchUsersResults = new BasicNamingEnumerationImpl()
searchUsersResults.add(searchResults);
def context = Mock(LdapContext)
context.search(_, _, _) >> searchUsersResults;
def context = Mock(LdapContext)
context.search(_, _, _) >> searchUsersResults;
return context
return context
}
private SearchResult createSearchResult(attributes) {
def search = Mock(SearchResult)
def search = Mock(SearchResult)
search.getName() >> "cn=" + attributes.getAt("uid").get();
search.getName() >> "cn=" + attributes.getAt("uid").get();
search.getAttributes() >> attributes
search.getAttributes() >> attributes
return search
return search
}
private Attributes createUserAttributes(String username, String email, String firstname, String lastname) {
def attributes = Mock(Attributes)
def attributes = Mock(Attributes)
def nameAttribute = Mock(Attribute)
nameAttribute.getId() >> "uid"
nameAttribute.get() >> username
attributes.get("uid") >> nameAttribute
def nameAttribute = Mock(Attribute)
nameAttribute.getId() >> "uid"
nameAttribute.get() >> username
attributes.get("uid") >> nameAttribute
def mailAttribute = Mock(Attribute)
mailAttribute.getId() >> "mail"
mailAttribute.get() >> email
attributes.get("mail") >> mailAttribute
def mailAttribute = Mock(Attribute)
mailAttribute.getId() >> "mail"
mailAttribute.get() >> email
attributes.get("mail") >> mailAttribute
def givennameAttribute = Mock(Attribute)
givennameAttribute.getId() >> "givenname"
givennameAttribute.get() >> firstname
attributes.get("givenname") >> givennameAttribute
def givennameAttribute = Mock(Attribute)
givennameAttribute.getId() >> "givenname"
givennameAttribute.get() >> firstname
attributes.get("givenname") >> givennameAttribute
def snAttribute = Mock(Attribute)
snAttribute.getId() >> "sn"
snAttribute.get() >> lastname
attributes.get("sn") >> snAttribute
def snAttribute = Mock(Attribute)
snAttribute.getId() >> "sn"
snAttribute.get() >> lastname
attributes.get("sn") >> snAttribute
return attributes
return attributes
}
def setupSpec() {
@ -105,97 +110,97 @@ class LdapUserManagerSpec extends spock.lang.Specification {
email = "rmurphy@test.com"
firstname = "Ryan"
lastname = "Murphy"
principal = "cn=" + username + "," + ldapConfiguration.getBaseDn()
principal = "cn=" + username + "," + ldapConfiguration.getBaseDn()
}
def "Test successfully creating an Ldap User from Search result"() {
given: "We have attributes, a search and a user manager"
def attributes = createUserAttributes(username, email, firstname, lastname)
given: "We have attributes, a search and a user manager"
def attributes = createUserAttributes(username, email, firstname, lastname)
def search = createSearchResult(attributes)
def userManager = new LdapUserManager(ldapConfiguration)
def result = userManager.createUser(search)
expect: "The crated user the data supplied from LDAP"
expect: "The crated user the data supplied from LDAP"
result.username == username
result.email == email
result.firstname == firstname
result.lastname == lastname
result.principal == principal
result.principal == principal
}
def "Test successfully returning a list from get users"() {
given: "We have a LdapUserManager"
given: "We have a LdapUserManager"
def userManager = new LdapUserManager(ldapConfiguration)
when: "A request for users is made"
when: "A request for users is made"
def result = userManager.getUsers(username, createContext())
then: "A list of users is returned"
then: "A list of users is returned"
result.size() == 1
}
def "Test successfully returning a list from get users when no username is given"() {
given: "We have a LdapUserManager"
given: "We have a LdapUserManager"
def userManager = new LdapUserManager(ldapConfiguration)
when: "Get users is called without a username"
when: "Get users is called without a username"
def result = userManager.getUsers(createContext())
then: "All users are returned"
result.size() == 1
then: "All users are returned"
result.size() == 1
}
def "Test successfully returning a NamingEnumeration from searchUsers"() {
given: "We have a LdapUserManager"
def userManager = new LdapUserManager(ldapConfiguration)
given: "We have a LdapUserManager"
def userManager = new LdapUserManager(ldapConfiguration)
when: "We search for users"
when: "We search for users"
def result = userManager.searchUsers(createContext())
then: "A list of users are returned."
then: "A list of users are returned."
result.next().getName() + "," + ldapConfiguration.getBaseDn() == principal
}
def "Test successfully returning an Ldap user from a get user request"() {
given: "We have a LdapUserMaanger"
given: "We have a LdapUserMaanger"
def userManager = new LdapUserManager(ldapConfiguration)
def userManager = new LdapUserManager(ldapConfiguration)
when: "A request for a user is made"
def result = userManager.getUser(username, createContext())
when: "A request for a user is made"
def result = userManager.getUser(username, createContext())
then: "The user is returned"
result.username == username
result.email == email
result.firstname == firstname
result.lastname == lastname
result.principal == principal
then: "The user is returned"
result.username == username
result.email == email
result.firstname == firstname
result.lastname == lastname
result.principal == principal
}
def "Test successfully throwing an exception when no users are found with getUser"() {
given: "We have a seachResult of users and a User Manager"
given: "We have a seachResult of users and a User Manager"
def searchUsersResults = new BasicNamingEnumerationImpl()
def searchUsersResults = new BasicNamingEnumerationImpl()
def context = Mock(LdapContext)
context.search(_, _, _) >> searchUsersResults;
def context = Mock(LdapContext)
context.search(_, _, _) >> searchUsersResults;
def userManager = new LdapUserManager(ldapConfiguration)
def userManager = new LdapUserManager(ldapConfiguration)
when: "a get user request is made and no user is found"
def result = userManager.getUser(username, context)
when: "a get user request is made and no user is found"
def result = userManager.getUser(username, context)
then: "An exception is thrown."
thrown NamingException
then: "An exception is thrown."
thrown NamingException
}
def "Test that a newly created Ldap User Manager is not null"() {
given: "You have created a new Ldap user manager object"
def result = new LdapUserManager();
expect: "The result is not null"
result != null
given: "You have created a new Ldap user manager object"
def result = new LdapUserManager();
expect: "The result is not null"
result != null
}
}

View File

@ -16,51 +16,52 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.api.response.LdapUserResponse
class LdapUserResponseSpec extends spock.lang.Specification {
def "Testing succcessful setting of LdapUserResponse email"() {
given: "We have an LdapResponse"
given: "We have an LdapResponse"
LdapUserResponse response = new LdapUserResponse();
when: "An email address is set"
when: "An email address is set"
response.setEmail("rmurphy@test.com");
then: "Get email should return that set email"
response.getEmail() == "rmurphy@test.com";
then: "Get email should return that set email"
response.getEmail() == "rmurphy@test.com";
}
def "Testing successful setting of LdapUserResponse firstname"() {
given: "We have an LdapUserResponse"
LdapUserResponse response = new LdapUserResponse()
when: "A firstname is set"
given: "We have an LdapUserResponse"
LdapUserResponse response = new LdapUserResponse()
when: "A firstname is set"
response.setFirstname("Ryan")
then: "gGet Firstname returns the set value"
then: "gGet Firstname returns the set value"
response.getFirstname() == "Ryan"
}
def "Testing successful setting of LdapUserResponse lastname"() {
given: "We have an LdapUserResponse"
given: "We have an LdapUserResponse"
LdapUserResponse response = new LdapUserResponse()
when: "A lastname is set"
when: "A lastname is set"
response.setLastname("Murphy")
then: "Get lastname is returned"
response.getLastname() == "Murphy"
then: "Get lastname is returned"
response.getLastname() == "Murphy"
}
def "Testing successful setting of LdapUserResponse principal"() {
given: "We have an LdapResponse"
LdapUserResponse response = new LdapUserResponse()
when: "A principal is set"
response.setPrincipal("dc=cloudstack,dc=org")
then: "Get principled returns the set value"
response.getPrincipal() == "dc=cloudstack,dc=org"
given: "We have an LdapResponse"
LdapUserResponse response = new LdapUserResponse()
when: "A principal is set"
response.setPrincipal("dc=cloudstack,dc=org")
then: "Get principled returns the set value"
response.getPrincipal() == "dc=cloudstack,dc=org"
}
def "Testing successful setting of LdapUserResponse username"() {
given: "We have an LdapUserResponse"
LdapUserResponse response = new LdapUserResponse()
when: "A username is set"
response.setUsername("rmurphy")
then: "Get username returns the set value."
response.getUsername() == "rmurphy"
given: "We have an LdapUserResponse"
LdapUserResponse response = new LdapUserResponse()
when: "A username is set"
response.setUsername("rmurphy")
then: "Get username returns the set value."
response.getUsername() == "rmurphy"
}
}

View File

@ -16,41 +16,42 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.ldap.LdapUser
class LdapUserSpec extends spock.lang.Specification {
def "Testing LdapUsers hashCode generation"() {
given:
def userA = new LdapUser(usernameA, "", "", "", "")
expect:
userA.hashCode() == usernameA.hashCode()
where:
usernameA = "A"
given:
def userA = new LdapUser(usernameA, "", "", "", "")
expect:
userA.hashCode() == usernameA.hashCode()
where:
usernameA = "A"
}
def "Testing that LdapUser successfully gives the correct result for a compare to"() {
given: "You have created two LDAP user objects"
def userA = new LdapUser(usernameA, "", "", "", "")
def userB = new LdapUser(usernameB, "", "", "", "")
expect: "That when compared the result is less than or equal to 0"
userA.compareTo(userB) <= 0
where: "The following values are used"
usernameA | usernameB
"A" | "B"
"A" | "A"
given: "You have created two LDAP user objects"
def userA = new LdapUser(usernameA, "", "", "", "")
def userB = new LdapUser(usernameB, "", "", "", "")
expect: "That when compared the result is less than or equal to 0"
userA.compareTo(userB) <= 0
where: "The following values are used"
usernameA | usernameB
"A" | "B"
"A" | "A"
}
def "Testing that LdapUsers equality"() {
given:
def userA = new LdapUser(usernameA, "", "", "", "")
def userB = new LdapUser(usernameB, "", "", "", "")
expect:
userA.equals(userA) == true
userA.equals(new Object()) == false
userA.equals(userB) == false
where:
usernameA | usernameB
"A" | "B"
given:
def userA = new LdapUser(usernameA, "", "", "", "")
def userB = new LdapUser(usernameB, "", "", "", "")
expect:
userA.equals(userA) == true
userA.equals(new Object()) == false
userA.equals(userB) == false
where:
usernameA | usernameB
"A" | "B"
}
def "Testing that the username is correctly set with the ldap object"() {

View File

@ -16,18 +16,22 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.ldap.LdapUtils
import javax.naming.directory.Attribute
import javax.naming.directory.Attributes
class LdapUtilsSpec extends spock.lang.Specification {
def "Testing than an attribute is not successfully returned"() {
given: "You have an attributes object with some attribute"
def attributes = Mock(Attributes)
attributes.get("uid") >> null
def attributes = Mock(Attributes)
attributes.get("uid") >> null
when: "You get the attribute"
String foundValue = LdapUtils.getAttributeValue(attributes, "uid")
when: "You get the attribute"
String foundValue = LdapUtils.getAttributeValue(attributes, "uid")
then: "Its value equals uid"
foundValue == null
then: "Its value equals uid"
foundValue == null
}
def "Testing than an attribute is successfully returned"() {
@ -51,14 +55,14 @@ class LdapUtilsSpec extends spock.lang.Specification {
}
def "Testing that a Ldap Search Filter is correctly escaped"() {
given: "You have some input from a user"
given: "You have some input from a user"
expect: "That the input is escaped"
LdapUtils.escapeLDAPSearchFilter(input) == result
expect: "That the input is escaped"
LdapUtils.escapeLDAPSearchFilter(input) == result
where: "The following inputs are given "
input | result
"Hi This is a test #çà" | "Hi This is a test #çà"
"Hi (This) = is * a \\ test # ç à ô \u0000" | "Hi \\28This\\29 = is \\2a a \\5c test # ç à ô \\00"
where: "The following inputs are given "
input | result
"Hi This is a test #çà" | "Hi This is a test #çà"
"Hi (This) = is * a \\ test # ç à ô \u0000" | "Hi \\28This\\29 = is \\2a a \\5c test # ç à ô \\00"
}
}

View File

@ -16,6 +16,7 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException
class NoLdapUserMatchingQueryExceptionSpec extends spock.lang.Specification {
def "Test that the query is correctly set within the No LDAP user matching query exception object"() {

View File

@ -16,6 +16,7 @@
// under the License.
package groovy.org.apache.cloudstack.ldap
import org.apache.cloudstack.ldap.NoSuchLdapUserException;
class NoSuchLdapUserExceptionSpec extends spock.lang.Specification {
def "Test that the username is correctly set within the No such LDAP user exception object"() {

View File

@ -93,7 +93,7 @@
if (ldapStatus) {
var $table = $wizard.find('.ldap-account-choice tbody');
$.ajax({
url: createURL("listAllLdapUsers"),
url: createURL("listLdapUsers&listtype=new"),
dataType: "json",
async: false,
success: function(json) {