diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in
index 0e00e3c0804..7cd1509d927 100644
--- a/client/tomcatconf/commands.properties.in
+++ b/client/tomcatconf/commands.properties.in
@@ -666,9 +666,8 @@ listDedicatedClusters=1
listDedicatedHosts=1
### LDAP
-searchLdap=3
listLdapConfigurations=15
addLdapConfiguration=3
deleteLdapConfiguration=3
-listAllLdapUsers=3
+listLdapUsers=3
ldapCreateAccount=3
diff --git a/docs/en-US/LDAP-for-user-authentication.xml b/docs/en-US/LDAP-for-user-authentication.xml
index 61f204cf8f6..772d1c5e3e2 100644
--- a/docs/en-US/LDAP-for-user-authentication.xml
+++ b/docs/en-US/LDAP-for-user-authentication.xml
@@ -24,19 +24,28 @@
Using an LDAP Server for User Authentication
You can use an external LDAP server such as Microsoft Active Directory or OpenLDAP to authenticate &PRODUCT; end-users.
- To set up LDAP authentication in &PRODUCT;, open the global settings page and set:
+ In order to do this you must:
- ldap.basedn - The base directory you want to search within for uses
- ldap.bind.password - The password you wish to use to bind, this can be blank if the server supports anonymous binding
- ldap.bind.principal - The account you wish to use to bind, this can be blank if the server supports anonymous binding
- ldap.email.attribute - The attribute within your LDAP server that holds a value for users email address
- ldap.realname.attribute - The attribute within your LDAP server that holds a value users realname
- ldap.user.object - The object class that identifies a user
- ldap.username.attribute - The attribute within your LDAP server that has a value that will match the cloudstack accounts username field
+ Set your LDAP configuration within &PRODUCT;
+ Create &PRODUCT; accounts for LDAP users
- Finally you can add LDAP servers from Global Settings -> Select View -> LDAP Configuration. This requires a hostname and port
+ To set up LDAP authentication in &PRODUCT;, open the global settings page and search for LDAP
+ Set ldap.basedn to match your sever's base directory.
+ Review the defaults for the following, ensure that they match your schema.
+
+ ldap.email.attribute
+ ldap.firstname.attribute
+ ldap.lastname.attribute
+ ldap.username.attribute
+ ldap.user.object
+
+ Optionally you can set the following:
+
+ 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.
+ 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.
+ 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.
+
+ 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.
-
-
diff --git a/docs/en-US/example-activedirectory-configuration.xml b/docs/en-US/example-activedirectory-configuration.xml
index 98ab5dae681..5a8178d5843 100644
--- a/docs/en-US/example-activedirectory-configuration.xml
+++ b/docs/en-US/example-activedirectory-configuration.xml
@@ -24,14 +24,14 @@
Example LDAP Configuration for Active Directory
- This shows the configuration settings required for using ActiveDirectory
+ This shows the configuration settings required for using ActiveDirectory.
samAccountName - Logon name
mail - Email Address
cn - Real name
Along with this the ldap.user.object name needs to be modified, by default ActiveDirectory uses the value "user" for this.
- Map the following attributes accordingly as shown below within the cloudstack ldap configuration:
+ Map the following attributes accordingly as shown below:
diff --git a/docs/en-US/example-openldap-configuration.xml b/docs/en-US/example-openldap-configuration.xml
index 2b28032dfe4..aa57a00cf18 100644
--- a/docs/en-US/example-openldap-configuration.xml
+++ b/docs/en-US/example-openldap-configuration.xml
@@ -24,8 +24,8 @@
Example LDAP Configuration for OpenLdap
- This shows the configuration settings required for using OpenLDAP
- The default values supplied are suited for OpenLDAP
+ This shows the configuration settings required for using OpenLDAP.
+ The default values supplied are suited for OpenLDAP.
uid - Logon name
mail - Email Address
diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccount.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java
similarity index 88%
rename from plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccount.java
rename to plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java
index 87944b0942a..981e72e64e1 100644
--- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccount.java
+++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java
@@ -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(
diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListAllUsersCmd.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListUsersCmd.java
similarity index 60%
rename from plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListAllUsersCmd.java
rename to plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListUsersCmd.java
index c371cffb6e6..18e36f56425 100644
--- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListAllUsersCmd.java
+++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapListUsersCmd.java
@@ -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 createLdapUserResponse(
final List users) {
final List ldapResponses = new ArrayList();
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 response = _queryService
+ .searchForUsers(new ListUsersCmd());
+ final List 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;
+ }
}
\ No newline at end of file
diff --git a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java
index d358ae34166..87406ad9c34 100644
--- a/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java
+++ b/plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java
@@ -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> getCommands() {
final List> cmdList = new ArrayList>();
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;
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/BasicNamingEnumerationImpl.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/BasicNamingEnumerationImpl.groovy
index 67d52722ba1..32101601571 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/BasicNamingEnumerationImpl.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/BasicNamingEnumerationImpl.groovy
@@ -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 items = new LinkedList();
+ private LinkedList items = new LinkedList();
- 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;
+ }
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapAddConfigurationCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapAddConfigurationCmdSpec.groovy
index 18df10dbfa4..b7e2f45f3ec 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapAddConfigurationCmdSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapAddConfigurationCmdSpec.groovy
@@ -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
+ }
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapAuthenticatorSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapAuthenticatorSpec.groovy
index a055fbc080b..416c1330359 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapAuthenticatorSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapAuthenticatorSpec.groovy
@@ -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"
}
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationDaoImplSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationDaoImplSpec.groovy
index 02de0f59f33..144890957f2 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationDaoImplSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationDaoImplSpec.groovy
@@ -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
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationResponseSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationResponseSpec.groovy
index cc9de9b3e5f..6f7a3701e25 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationResponseSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationResponseSpec.groovy
@@ -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
}
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationSpec.groovy
index c8555a9b95b..bb866251b59 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationSpec.groovy
@@ -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 ldapConfigurationList = new ArrayList()
+ ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
+ Pair, Integer> result = new Pair, 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 ldapConfigurationList = new ArrayList()
- ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
- Pair, Integer> result = new Pair, 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
+ }
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationVOSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationVOSpec.groovy
index b5d007e72f7..3e713fe2cd3 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationVOSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapConfigurationVOSpec.groovy
@@ -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 {
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapContextFactorySpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapContextFactorySpec.groovy
index 9d9ea659453..0b8f2848a13 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapContextFactorySpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapContextFactorySpec.groovy
@@ -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
}
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapCreateAccountCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapCreateAccountCmdSpec.groovy
index d2b2332b2c8..cc849defef5 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapCreateAccountCmdSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapCreateAccountCmdSpec.groovy
@@ -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"
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapDeleteConfigurationCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapDeleteConfigurationCmdSpec.groovy
index a8374b0224e..31d56ef68cb 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapDeleteConfigurationCmdSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapDeleteConfigurationCmdSpec.groovy
@@ -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"
}
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListAllUsersCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListAllUsersCmdSpec.groovy
deleted file mode 100644
index bb6845cb7bf..00000000000
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListAllUsersCmdSpec.groovy
+++ /dev/null
@@ -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 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"
- }
-}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListConfigurationCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListConfigurationCmdSpec.groovy
index 11226d78c5b..cd8b2a3ddf5 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListConfigurationCmdSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListConfigurationCmdSpec.groovy
@@ -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 ldapConfigurationList = new ArrayList()
- Pair, Integer> ldapConfigurations = new Pair, 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 ldapConfigurationList = new ArrayList()
+ Pair, Integer> ldapConfigurations = new Pair, 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 ldapConfigurationList = new ArrayList()
ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
Pair, Integer> ldapConfigurations = new Pair, 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
}
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListUsersCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListUsersCmdSpec.groovy
new file mode 100644
index 00000000000..5039443c6ef
--- /dev/null
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapListUsersCmdSpec.groovy
@@ -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 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 responses = new ArrayList()
+ responses.add(userResponse);
+
+ ListResponse queryServiceResponse = new ListResponse()
+ 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()
+
+ 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;
+ }
+}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy
index 74929193682..d681eace90a 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy
@@ -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 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 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 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 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 ldapConfigurationList = new ArrayList()
- ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
- Pair, Integer> configurations = new Pair, 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 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 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 ldapConfigurationList = new ArrayList()
+ ldapConfigurationList.add(new LdapConfigurationVO("localhost", 389))
+ Pair, Integer> configurations = new Pair, 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;
+ }
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapSearchUserCmdSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapSearchUserCmdSpec.groovy
index 4bc5ca8787e..fce299d933d 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapSearchUserCmdSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapSearchUserCmdSpec.groovy
@@ -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 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 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"
}
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserManagerSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserManagerSpec.groovy
index d13f0e80163..339923e57c0 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserManagerSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserManagerSpec.groovy
@@ -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
}
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserResponseSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserResponseSpec.groovy
index 2389a9d2d93..f1978fa60d2 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserResponseSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserResponseSpec.groovy
@@ -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"
}
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserSpec.groovy
index 65103521dfe..8fd1ccc680e 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUserSpec.groovy
@@ -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"() {
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUtilsSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUtilsSpec.groovy
index 1825c39c2af..984d2b3f801 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUtilsSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapUtilsSpec.groovy
@@ -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"
}
}
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/NoLdapUserMatchingQueryExceptionSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/NoLdapUserMatchingQueryExceptionSpec.groovy
index a3010126bf2..4c0cc4b688f 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/NoLdapUserMatchingQueryExceptionSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/NoLdapUserMatchingQueryExceptionSpec.groovy
@@ -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"() {
diff --git a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/NoSuchLdapUserExceptionSpec.groovy b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/NoSuchLdapUserExceptionSpec.groovy
index 84cc90867a1..dbdf646721d 100644
--- a/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/NoSuchLdapUserExceptionSpec.groovy
+++ b/plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/NoSuchLdapUserExceptionSpec.groovy
@@ -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"() {
diff --git a/ui/scripts/ui-custom/accountsWizard.js b/ui/scripts/ui-custom/accountsWizard.js
index 88cff581ccb..358e29ca04b 100644
--- a/ui/scripts/ui-custom/accountsWizard.js
+++ b/ui/scripts/ui-custom/accountsWizard.js
@@ -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) {