@@ -25,6 +25,8 @@ import org.apache.cloudstack.api.command.LdapImportUsersCmd
2525import org.apache.cloudstack.api.command.LdapListUsersCmd
2626import org.apache.cloudstack.api.command.LdapUserSearchCmd
2727import org.apache.cloudstack.api.command.LinkDomainToLdapCmd
28+ import org.apache.cloudstack.api.response.LinkDomainToLdapResponse
29+ import org.apache.cloudstack.ldap.dao.LdapTrustMapDao
2830
2931import javax.naming.NamingException
3032import javax.naming.ldap.InitialLdapContext
@@ -36,6 +38,8 @@ import org.apache.cloudstack.ldap.dao.LdapConfigurationDaoImpl
3638import com.cloud.exception.InvalidParameterValueException
3739import com.cloud.utils.Pair
3840
41+ import javax.naming.ldap.LdapContext
42+
3943class 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