CLOUDSTACK-8647: formatted LdapAuthenticatorSpec

This commit is contained in:
Rajani Karuturi 2015-08-27 16:18:47 +05:30
parent 36340d97bd
commit c2b36cb705

View File

@ -27,80 +27,80 @@ import org.apache.cloudstack.ldap.LdapUser
class LdapAuthenticatorSpec extends spock.lang.Specification { class LdapAuthenticatorSpec extends spock.lang.Specification {
def "Test a failed authentication due to user not being found within cloudstack"() { 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) LdapManager ldapManager = Mock(LdapManager)
UserAccountDao userAccountDao = Mock(UserAccountDao) UserAccountDao userAccountDao = Mock(UserAccountDao)
userAccountDao.getUserAccount(_, _) >> null userAccountDao.getUserAccount(_, _) >> null
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao) def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "A user authentications" when: "A user authentications"
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null) def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
then: "their authentication fails" then: "their authentication fails"
result.first() == false result.first() == false
} }
def "Test failed authentication due to ldap bind being unsuccessful"() { def "Test failed authentication due to ldap bind being unsuccessful"() {
given: "We have an LdapManager, LdapConfiguration, userAccountDao and LdapAuthenticator" given: "We have an LdapManager, LdapConfiguration, userAccountDao and LdapAuthenticator"
def ldapManager = Mock(LdapManager) def ldapManager = Mock(LdapManager)
def ldapUser = Mock(LdapUser) def ldapUser = Mock(LdapUser)
ldapUser.isDisabled() >> false ldapUser.isDisabled() >> false
ldapManager.isLdapEnabled() >> true ldapManager.isLdapEnabled() >> true
ldapManager.getUser("rmurphy") >> ldapUser ldapManager.getUser("rmurphy") >> ldapUser
ldapManager.canAuthenticate(_, _) >> false ldapManager.canAuthenticate(_, _) >> false
UserAccountDao userAccountDao = Mock(UserAccountDao) UserAccountDao userAccountDao = Mock(UserAccountDao)
userAccountDao.getUserAccount(_, _) >> new UserAccountVO() userAccountDao.getUserAccount(_, _) >> new UserAccountVO()
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao) def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "The user authenticates with an incorrect password" when: "The user authenticates with an incorrect password"
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null) def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
then: "their authentication fails" then: "their authentication fails"
result.first() == false result.first() == false
} }
def "Test failed authentication due to ldap not being configured"() { def "Test failed authentication due to ldap not being configured"() {
given: "We have an LdapManager, A configured LDAP server, a userAccountDao and LdapAuthenticator" given: "We have an LdapManager, A configured LDAP server, a userAccountDao and LdapAuthenticator"
def ldapManager = Mock(LdapManager) def ldapManager = Mock(LdapManager)
ldapManager.isLdapEnabled() >> false ldapManager.isLdapEnabled() >> false
UserAccountDao userAccountDao = Mock(UserAccountDao) UserAccountDao userAccountDao = Mock(UserAccountDao)
userAccountDao.getUserAccount(_, _) >> new UserAccountVO() userAccountDao.getUserAccount(_, _) >> new UserAccountVO()
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao) def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "The user authenticates" when: "The user authenticates"
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null) def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
then: "their authentication fails" then: "their authentication fails"
result.first() == false result.first() == false
} }
def "Test successful authentication"() { def "Test successful authentication"() {
given: "We have an LdapManager, LdapConfiguration, userAccountDao and LdapAuthenticator" given: "We have an LdapManager, LdapConfiguration, userAccountDao and LdapAuthenticator"
def ldapManager = Mock(LdapManager) def ldapManager = Mock(LdapManager)
def ldapUser = Mock(LdapUser) def ldapUser = Mock(LdapUser)
ldapUser.isDisabled() >> false ldapUser.isDisabled() >> false
ldapManager.isLdapEnabled() >> true ldapManager.isLdapEnabled() >> true
ldapManager.canAuthenticate(_, _) >> true ldapManager.canAuthenticate(_, _) >> true
ldapManager.getUser("rmurphy") >> ldapUser ldapManager.getUser("rmurphy") >> ldapUser
UserAccountDao userAccountDao = Mock(UserAccountDao) UserAccountDao userAccountDao = Mock(UserAccountDao)
userAccountDao.getUserAccount(_, _) >> new UserAccountVO() userAccountDao.getUserAccount(_, _) >> new UserAccountVO()
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao) def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "The user authenticates with an incorrect password" when: "The user authenticates with an incorrect password"
def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null) def result = ldapAuthenticator.authenticate("rmurphy", "password", 0, null)
then: "their authentication passes" then: "their authentication passes"
result.first() == true result.first() == true
} }
def "Test that encode doesn't change the input"() { def "Test that encode doesn't change the input"() {
given: "We have an LdapManager, userAccountDao and LdapAuthenticator" given: "We have an LdapManager, userAccountDao and LdapAuthenticator"
LdapManager ldapManager = Mock(LdapManager) LdapManager ldapManager = Mock(LdapManager)
UserAccountDao userAccountDao = Mock(UserAccountDao) UserAccountDao userAccountDao = Mock(UserAccountDao)
def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao) def ldapAuthenticator = new LdapAuthenticator(ldapManager, userAccountDao)
when: "a users password is encoded" when: "a users password is encoded"
def result = ldapAuthenticator.encode("password") def result = ldapAuthenticator.encode("password")
then: "it doesn't change" then: "it doesn't change"
result == "password" result == "password"
} }
} }