ldapcherry/tests/test_BackendLdap.py

100 lines
3.2 KiB
Python
Raw Normal View History

2015-05-21 08:33:56 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
from __future__ import unicode_literals
import pytest
import sys
from sets import Set
2015-05-21 08:54:28 +02:00
from ldapcherry.backend.backendLdap import Backend
from ldapcherry import syslog_error
2015-05-21 08:33:56 +02:00
from ldapcherry.exceptions import *
import cherrypy
from ldap import SERVER_DOWN
2015-05-21 08:33:56 +02:00
cfg = {
'module' : 'ldapcherry.backend.ldap',
'groupdn' : 'ou=group,dc=example,dc=org',
'userdn' : 'ou=People,dc=example,dc=org',
'binddn' : 'cn=dnscherry,dc=example,dc=org',
2015-05-21 08:33:56 +02:00
'password' : 'password',
'uri' : 'ldap://ldap.ldapcherry.org:390',
'ca' : './tests/test_env/etc/ldapcherry/TEST-cacert.pem',
'starttls' : 'off',
2015-05-21 08:33:56 +02:00
'checkcert' : 'off',
'user_filter_tmpl' : '(uid=%(username)s)',
'group_filter_tmpl' : '(member=%(userdn)s)',
2015-05-21 08:33:56 +02:00
}
cherrypy.log.error = syslog_error
2015-05-22 10:27:46 +02:00
attr = ['shell', 'cn', 'uid', 'uidNumber', 'gidNumber', 'home', 'userPassword', 'givenName', 'email', 'sn']
2015-05-21 08:33:56 +02:00
class TestError(object):
def testNominal(self):
2015-05-22 10:27:46 +02:00
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
2015-05-21 08:33:56 +02:00
return True
def testConnect(self):
2015-05-22 10:27:46 +02:00
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
ldap = inv._connect()
ldap.simple_bind_s(inv.binddn, inv.bindpassword)
2015-05-21 08:33:56 +02:00
return True
def testConnectSSL(self):
cfg2 = cfg.copy()
cfg2['uri'] = 'ldaps://ldap.ldapcherry.org:637'
cfg2['checkcert'] = 'on'
2015-05-22 10:27:46 +02:00
inv = Backend(cfg2, cherrypy.log, 'ldap', attr)
ldap = inv._connect()
ldap.simple_bind_s(inv.binddn, inv.bindpassword)
2015-05-21 08:33:56 +02:00
def testConnectSSLWrongCA(self):
cfg2 = cfg.copy()
cfg2['uri'] = 'ldaps://ldap.ldapcherry.org:637'
cfg2['checkcert'] = 'on'
cfg2['ca'] = './cfg/wrong_ca.crt'
2015-05-22 10:27:46 +02:00
inv = Backend(cfg2, cherrypy.log, 'ldap', attr)
ldapc = inv._connect()
try:
ldapc.simple_bind_s(inv.binddn, inv.bindpassword)
except SERVER_DOWN as e:
assert e[0]['info'] == 'TLS: hostname does not match CN in peer certificate'
# def testConnectSSLNoCheck(self):
# cfg2 = cfg.copy()
# cfg2['uri'] = 'ldaps://ldap.ldapcherry.org:637'
# cfg2['checkcert'] = 'off'
2015-05-22 10:27:46 +02:00
# inv = Backend(cfg2, cherrypy.log, 'ldap', attr)
# ldap = inv._connect()
# ldap.simple_bind_s(inv.binddn, inv.bindpassword)
2015-05-21 08:33:56 +02:00
def testAuthSuccess(self):
2015-05-22 10:27:46 +02:00
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
2015-05-21 08:33:56 +02:00
return True
def testAuthSuccess(self):
2015-05-22 10:27:46 +02:00
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
2015-05-22 01:33:15 +02:00
ret = inv.auth('jwatson', 'passwordwatson')
assert ret == True
2015-05-21 08:33:56 +02:00
def testAuthFailure(self):
2015-05-22 10:27:46 +02:00
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
2015-05-22 01:33:15 +02:00
res = inv.auth('notauser', 'password') or inv.auth('jwatson', 'notapassword')
assert res == False
2015-05-21 08:33:56 +02:00
def testMissingParam(self):
cfg2 = {}
return True
try:
2015-05-22 10:27:46 +02:00
inv = Backend(cfg2, cherrypy.log, 'ldap', attr)
2015-05-21 08:33:56 +02:00
except MissingKey:
return
else:
raise AssertionError("expected an exception")
def testGetUser(self):
2015-05-22 10:27:46 +02:00
inv = Backend(cfg, cherrypy.log, 'ldap', attr)
2015-05-21 08:33:56 +02:00
return True