Skip to content

Commit 6572abc

Browse files
committed
CLOUDSTACK-8647 added unittests for new methods in ldapmanager
1 parent dd6d6d1 commit 6572abc

5 files changed

Lines changed: 202 additions & 6 deletions

File tree

plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapAuthenticator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Pair<Boolean, ActionOnFailedAuthentication> authenticate(final String use
6666
LdapTrustMapVO ldapTrustMapVO = _ldapManager.getDomainLinkedToLdap(domainId);
6767
if(ldapTrustMapVO != null) {
6868
try {
69-
LdapUser ldapUser = _ldapManager.getUser(username, ldapTrustMapVO.getType(), ldapTrustMapVO.getName());
69+
LdapUser ldapUser = _ldapManager.getUser(username, ldapTrustMapVO.getType().toString(), ldapTrustMapVO.getName());
7070
if(!ldapUser.isDisabled()) {
7171
result = _ldapManager.canAuthenticate(ldapUser.getPrincipal(), password);
7272
if(result) {

plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
public interface LdapManager extends PluggableService {
3131

32+
enum LinkType { GROUP, OU;}
33+
3234
LdapConfigurationResponse addConfiguration(String hostname, int port) throws InvalidParameterValueException;
3335

3436
boolean canAuthenticate(String principal, String password);

plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapManagerImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.cloudstack.api.command.LinkDomainToLdapCmd;
2929
import org.apache.cloudstack.api.response.LinkDomainToLdapResponse;
3030
import org.apache.cloudstack.ldap.dao.LdapTrustMapDao;
31+
import org.apache.commons.lang.Validate;
3132
import org.apache.log4j.Logger;
3233
import org.springframework.stereotype.Component;
3334

@@ -265,8 +266,14 @@ public List<LdapUser> searchUsers(final String username) throws NoLdapUserMatchi
265266

266267
@Override
267268
public LinkDomainToLdapResponse linkDomainToLdap(Long domainId, String type, String name, short accountType) {
268-
LdapTrustMapVO vo = _ldapTrustMapDao.persist(new LdapTrustMapVO(domainId, type, name, accountType));
269-
LinkDomainToLdapResponse response = new LinkDomainToLdapResponse(vo.getDomainId(), vo.getType(), vo.getName(), vo.getAccountType());
269+
Validate.notNull(type, "type cannot be null. It should either be GROUP or OU");
270+
Validate.notNull(domainId, "domainId cannot be null.");
271+
Validate.notEmpty(name, "GROUP or OU name cannot be empty");
272+
//Account type constants in com.cloud.user.Account
273+
Validate.isTrue(accountType>=0 && accountType<=5, "accountype should be a number from 0-5");
274+
LinkType linkType = LdapManager.LinkType.valueOf(type.toUpperCase());
275+
LdapTrustMapVO vo = _ldapTrustMapDao.persist(new LdapTrustMapVO(domainId, linkType, name, accountType));
276+
LinkDomainToLdapResponse response = new LinkDomainToLdapResponse(vo.getDomainId(), vo.getType().toString(), vo.getName(), vo.getAccountType());
270277
return response;
271278
}
272279

plugins/user-authenticators/ldap/src/org/apache/cloudstack/ldap/LdapTrustMapVO.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class LdapTrustMapVO implements InternalIdentity {
3737
private Long id;
3838

3939
@Column(name = "type")
40-
private String type;
40+
private LdapManager.LinkType type;
4141

4242
@Column(name = "name")
4343
private String name;
@@ -52,7 +52,7 @@ public class LdapTrustMapVO implements InternalIdentity {
5252
public LdapTrustMapVO() {
5353
}
5454

55-
public LdapTrustMapVO(long domainId, String type, String name, short accountType) {
55+
public LdapTrustMapVO(long domainId, LdapManager.LinkType type, String name, short accountType) {
5656
this.domainId = domainId;
5757
this.type = type;
5858
this.name = name;
@@ -64,7 +64,7 @@ public long getId() {
6464
return id;
6565
}
6666

67-
public String getType() {
67+
public LdapManager.LinkType getType() {
6868
return type;
6969
}
7070

@@ -79,4 +79,37 @@ public long getDomainId() {
7979
public short getAccountType() {
8080
return accountType;
8181
}
82+
83+
@Override
84+
public boolean equals(Object o) {
85+
if (this == o) {
86+
return true;
87+
}
88+
if (o == null || getClass() != o.getClass()) {
89+
return false;
90+
}
91+
92+
LdapTrustMapVO that = (LdapTrustMapVO) o;
93+
94+
if (domainId != that.domainId) {
95+
return false;
96+
}
97+
if (accountType != that.accountType) {
98+
return false;
99+
}
100+
if (type != that.type) {
101+
return false;
102+
}
103+
return name.equals(that.name);
104+
105+
}
106+
107+
@Override
108+
public int hashCode() {
109+
int result = type.hashCode();
110+
result = 31 * result + name.hashCode();
111+
result = 31 * result + (int) (domainId ^ (domainId >>> 32));
112+
result = 31 * result + (int) accountType;
113+
return result;
114+
}
82115
}

plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapManagerImplSpec.groovy

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import org.apache.cloudstack.api.command.LdapImportUsersCmd
2525
import org.apache.cloudstack.api.command.LdapListUsersCmd
2626
import org.apache.cloudstack.api.command.LdapUserSearchCmd
2727
import org.apache.cloudstack.api.command.LinkDomainToLdapCmd
28+
import org.apache.cloudstack.api.response.LinkDomainToLdapResponse
29+
import org.apache.cloudstack.ldap.dao.LdapTrustMapDao
2830

2931
import javax.naming.NamingException
3032
import javax.naming.ldap.InitialLdapContext
@@ -36,6 +38,8 @@ import org.apache.cloudstack.ldap.dao.LdapConfigurationDaoImpl
3638
import com.cloud.exception.InvalidParameterValueException
3739
import com.cloud.utils.Pair
3840

41+
import javax.naming.ldap.LdapContext
42+
3943
class LdapManagerImplSpec extends spock.lang.Specification {
4044
def "Test failing of getUser due to bind issue"() {
4145
given: "We have an LdapConfigurationDao, LdapContextFactory, LdapUserManager and LdapManager"
@@ -428,4 +432,154 @@ class LdapManagerImplSpec extends spock.lang.Specification {
428432
then: "A list greater of size one is returned"
429433
result.size() == 1;
430434
}
435+
436+
def "test linkDomainToLdap invalid ldap group type"() {
437+
def ldapManager = new LdapManagerImpl()
438+
LdapTrustMapDao ldapTrustMapDao = Mock(LdapTrustMapDao)
439+
ldapManager._ldapTrustMapDao = ldapTrustMapDao
440+
441+
def domainId = 1
442+
when:
443+
println("using type: " + type)
444+
LinkDomainToLdapResponse response = ldapManager.linkDomainToLdap(domainId, type, "CN=test,DC=CCP,DC=Citrix,DC=Com", (short)2)
445+
then:
446+
thrown(IllegalArgumentException)
447+
where:
448+
type << ["", null, "TEST", "TEST TEST"]
449+
}
450+
def "test linkDomainToLdap invalid domain"() {
451+
def ldapManager = new LdapManagerImpl()
452+
LdapTrustMapDao ldapTrustMapDao = Mock(LdapTrustMapDao)
453+
ldapManager._ldapTrustMapDao = ldapTrustMapDao
454+
455+
when:
456+
LinkDomainToLdapResponse response = ldapManager.linkDomainToLdap(null, "GROUP", "CN=test,DC=CCP,DC=Citrix,DC=Com", (short)2)
457+
then:
458+
thrown(IllegalArgumentException)
459+
}
460+
def "test linkDomainToLdap invalid ldap name"() {
461+
def ldapManager = new LdapManagerImpl()
462+
LdapTrustMapDao ldapTrustMapDao = Mock(LdapTrustMapDao)
463+
ldapManager._ldapTrustMapDao = ldapTrustMapDao
464+
465+
def domainId = 1
466+
when:
467+
println("using name: " + name)
468+
LinkDomainToLdapResponse response = ldapManager.linkDomainToLdap(domainId, "GROUP", name, (short)2)
469+
then:
470+
thrown(IllegalArgumentException)
471+
where:
472+
name << ["", null]
473+
}
474+
def "test linkDomainToLdap invalid accountType"(){
475+
476+
def ldapManager = new LdapManagerImpl()
477+
LdapTrustMapDao ldapTrustMapDao = Mock(LdapTrustMapDao)
478+
ldapManager._ldapTrustMapDao = ldapTrustMapDao
479+
480+
def domainId = 1
481+
when:
482+
println("using accountType: " + accountType)
483+
LinkDomainToLdapResponse response = ldapManager.linkDomainToLdap(domainId, "GROUP", "TEST", (short)accountType)
484+
then:
485+
thrown(IllegalArgumentException)
486+
where:
487+
accountType << [-1, 6, 20000, -500000]
488+
}
489+
def "test linkDomainToLdap when all is well"(){
490+
def ldapManager = new LdapManagerImpl()
491+
LdapTrustMapDao ldapTrustMapDao = Mock(LdapTrustMapDao)
492+
ldapManager._ldapTrustMapDao = ldapTrustMapDao
493+
494+
def domainId=1
495+
def type=LdapManager.LinkType.GROUP
496+
def name="CN=test,DC=CCP, DC=citrix,DC=com"
497+
short accountType=2
498+
499+
1 * ldapTrustMapDao.persist(new LdapTrustMapVO(domainId, type, name, accountType)) >> new LdapTrustMapVO(domainId, type, name, accountType)
500+
501+
when:
502+
LinkDomainToLdapResponse response = ldapManager.linkDomainToLdap(domainId, type.toString(), name, accountType)
503+
then:
504+
response.getDomainId() == domainId
505+
response.getType() == type.toString()
506+
response.getName() == name
507+
response.getAccountType() == accountType
508+
}
509+
510+
def "test getUser(username,type,group) when username disabled in ldap"(){
511+
def ldapUserManager = Mock(LdapUserManager)
512+
def ldapUserManagerFactory = Mock(LdapUserManagerFactory)
513+
ldapUserManagerFactory.getInstance(_) >> ldapUserManager
514+
def ldapContextFactory = Mock(LdapContextFactory)
515+
ldapContextFactory.createBindContext() >> Mock(LdapContext)
516+
def ldapConfiguration = Mock(LdapConfiguration)
517+
518+
def ldapManager = new LdapManagerImpl()
519+
ldapManager._ldapUserManagerFactory = ldapUserManagerFactory
520+
ldapManager._ldapContextFactory = ldapContextFactory
521+
ldapManager._ldapConfiguration = ldapConfiguration
522+
523+
def username = "admin"
524+
def type = "GROUP"
525+
def name = "CN=test,DC=citrix,DC=com"
526+
527+
ldapUserManager.getUser(username, type, name, _) >> new LdapUser(username, "email", "firstname", "lastname", "principal", "domain", true)
528+
529+
when:
530+
LdapUser user = ldapManager.getUser(username, type, name)
531+
then:
532+
user.getUsername() == username
533+
user.isDisabled() == true
534+
}
535+
536+
def "test getUser(username,type,group) when username doesnt exist in ldap"(){
537+
def ldapUserManager = Mock(LdapUserManager)
538+
def ldapUserManagerFactory = Mock(LdapUserManagerFactory)
539+
ldapUserManagerFactory.getInstance(_) >> ldapUserManager
540+
def ldapContextFactory = Mock(LdapContextFactory)
541+
ldapContextFactory.createBindContext() >> Mock(LdapContext)
542+
def ldapConfiguration = Mock(LdapConfiguration)
543+
544+
def ldapManager = new LdapManagerImpl()
545+
ldapManager._ldapUserManagerFactory = ldapUserManagerFactory
546+
ldapManager._ldapContextFactory = ldapContextFactory
547+
ldapManager._ldapConfiguration = ldapConfiguration
548+
549+
def username = "admin"
550+
def type = "GROUP"
551+
def name = "CN=test,DC=citrix,DC=com"
552+
553+
ldapUserManager.getUser(username, type, name, _) >> { throw new NamingException("Test naming exception") }
554+
555+
when:
556+
LdapUser user = ldapManager.getUser(username, type, name)
557+
then:
558+
thrown(NoLdapUserMatchingQueryException)
559+
}
560+
def "test getUser(username,type,group) when username is an active member of the group in ldap"(){
561+
def ldapUserManager = Mock(LdapUserManager)
562+
def ldapUserManagerFactory = Mock(LdapUserManagerFactory)
563+
ldapUserManagerFactory.getInstance(_) >> ldapUserManager
564+
def ldapContextFactory = Mock(LdapContextFactory)
565+
ldapContextFactory.createBindContext() >> Mock(LdapContext)
566+
def ldapConfiguration = Mock(LdapConfiguration)
567+
568+
def ldapManager = new LdapManagerImpl()
569+
ldapManager._ldapUserManagerFactory = ldapUserManagerFactory
570+
ldapManager._ldapContextFactory = ldapContextFactory
571+
ldapManager._ldapConfiguration = ldapConfiguration
572+
573+
def username = "admin"
574+
def type = "GROUP"
575+
def name = "CN=test,DC=citrix,DC=com"
576+
577+
ldapUserManager.getUser(username, type, name, _) >> new LdapUser(username, "email", "firstname", "lastname", "principal", "domain", false)
578+
579+
when:
580+
LdapUser user = ldapManager.getUser(username, type, name)
581+
then:
582+
user.getUsername() == username
583+
user.isDisabled() == false
584+
}
431585
}

0 commit comments

Comments
 (0)