Commit 92a512cf authored by Lorenz Steinert's avatar Lorenz Steinert

added more ldap tests and reworked some

parent 9735feac
......@@ -5,15 +5,23 @@ import os
import unittest
import configparser
import ldap3 as ldap
from esme import FsrLdap, get_fallback_fsr, get_fallback_fsr_extern, get_sprecher, get_protokollant, get_config, get_share, get_path
from esme import FsrLdap, get_sprecher, get_protokollant, get_config, get_share, get_path, Error
class FsrLdapTest(unittest.TestCase):
"""test the functions with ldap"""
def logging_warning(self, arg):
self.warning += [arg]
def logging_info(self, arg):
self.info += [arg]
def setUp(self):
"""setup for the tests"""
self.test_ldap_server = FsrLdap('test_server', 'dc=test,dc=de')
conn = ldap.Connection(self.test_ldap_server.server, client_strategy=ldap.MOCK_SYNC)
share_dir = "/tmp/python3-esme-testing"
self.test_ldap_server = FsrLdap('test_server', 'dc=test,dc=de', share_dir, testing=True)
conn = self.test_ldap_server.conn
conn.strategy.add_entry('ou=people,'+self.test_ldap_server.base, {})
conn.strategy.add_entry('ou=group,'+self.test_ldap_server.base, {})
conn.strategy.add_entry('cn=intern,ou=group,'+self.test_ldap_server.base,
......@@ -33,36 +41,76 @@ class FsrLdapTest(unittest.TestCase):
conn.strategy.add_entry('uid=test6,ou=people,'+self.test_ldap_server.base,
{'displayName': 'test6, testing'})
os.mkdir("/tmp/python3-esme-testing")
open("/tmp/python3-esme-testing/fsr", 'a').close()
open("/tmp/python3-esme-testing/fsr_extern", 'a').close()
with open("/tmp/python3-esme-testing/fsr", 'w') as fobj:
fobj.write("testing test1\n")
with open("/tmp/python3-esme-testing/fsr_extern", 'w') as fobj:
fobj.write("testing test2\n")
self.sharedir = "/tmp/python3-esme-testing"
self.info = []
self.warning = []
@staticmethod
def ldap_search_fail(*arg, attributes=None):
return False
@staticmethod
def ldap_exception(*arg, attributes=None):
raise ldap.core.exceptions.LDAPException
def test_get_fsr_ldap(self):
"""test the get_fsr_ldap function"""
self.assertEqual(self.test_ldap_server.get_fsr_ldap(self.sharedir, testing=True),
self.assertEqual(self.test_ldap_server._get_fsr_ldap(),
['testing test1', 'testing test2', 'testing test3'])
def test_get_fsr_extern_ldap(self):
"""test the get_fsr_extern_ldap function"""
self.assertEqual(self.test_ldap_server.get_fsr_extern_ldap(self.sharedir, testing=True),
self.assertEqual(self.test_ldap_server._get_fsr_extern_ldap(),
['testing test4', 'testing test5', 'testing test6'])
def test_get_fallback_fsr(self):
"""test the get_fallback_fsr function"""
sharedir = "/tmp/python3-esme-testing"
self.assertEqual(self.test_ldap_server._get_fallback_fsr(sharedir), ['testing test1'])
os.remove("/tmp/python3-esme-testing/fsr")
self.assertEqual(self.test_ldap_server._get_fallback_fsr(sharedir), Error.NO_FILE)
def test_get_fallback_fsr_extern(self):
"""test the get_fallback_fsr_extern function"""
sharedir = "/tmp/python3-esme-testing"
self.assertEqual(self.test_ldap_server._get_fallback_fsr_extern(sharedir), ['testing test2'])
os.remove("/tmp/python3-esme-testing/fsr_extern")
self.assertEqual(self.test_ldap_server._get_fallback_fsr_extern(sharedir), Error.NO_FILE)
def test_get_fsr_ldap_no_server(self):
"""test the get_fsr_ldap function without ldap connection"""
self.test_ldap_server.server = ""
self.assertEqual(self.test_ldap_server.get_fsr_ldap(self.sharedir, testing=True),
[])
self.test_ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.test_ldap_server._get_fsr_ldap(),
Error.UNSEC_SEARCH)
def test_get_fsr_extern_ldap_no_server(self):
"""test the get_fst_extern_ldap function without ldap connection"""
self.test_ldap_server.server = ""
self.assertEqual(self.test_ldap_server.get_fsr_extern_ldap(self.sharedir, testing=True),
[])
self.test_ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.test_ldap_server._get_fsr_extern_ldap(),
Error.UNSEC_SEARCH)
def test_get_fsr_ldap_ldap_exception(self):
"""test the get_fsr_ldap function with a LDAPExcepton"""
self.test_ldap_server.conn.search = self.ldap_exception
self.assertEqual(self.test_ldap_server._get_fsr_ldap()[0],
Error.LDAP_FAIL)
def test_get_fsr_extern_ldap_ldap_exception(self):
"""test the get_fsr_extern_ldap function with a LDAPExcepton"""
self.test_ldap_server.conn.search = self.ldap_exception
self.assertEqual(self.test_ldap_server._get_fsr_extern_ldap()[0],
Error.LDAP_FAIL)
def tearDown(self):
"""cleanup after the tests"""
os.remove("/tmp/python3-esme-testing/fsr")
os.remove("/tmp/python3-esme-testing/fsr_extern")
if os.path.isfile("/tmp/python3-esme-testing/fsr"):
os.remove("/tmp/python3-esme-testing/fsr")
if os.path.isfile("/tmp/python3-esme-testing/fsr_extern"):
os.remove("/tmp/python3-esme-testing/fsr_extern")
os.rmdir("/tmp/python3-esme-testing")
......@@ -111,20 +159,6 @@ class GetFuncTest(unittest.TestCase):
"""override for the std input method"""
return ""
def test_get_fallback_fsr(self):
"""test the get_fallback_fsr function"""
sharedir = "/tmp/python3-esme-testing"
self.assertEqual(get_fallback_fsr(sharedir), ['testing test1'])
os.remove("/tmp/python3-esme-testing/fsr")
self.assertEqual(get_fallback_fsr(sharedir), [])
def test_get_fallback_fsr_extern(self):
"""test the get_fallback_fsr_extern function"""
sharedir = "/tmp/python3-esme-testing"
self.assertEqual(get_fallback_fsr_extern(sharedir), ['testing test2'])
os.remove("/tmp/python3-esme-testing/fsr_extern")
self.assertEqual(get_fallback_fsr_extern(sharedir), [])
def test_get_sprcher(self):
"""test the get_sprecher function"""
self.assertEqual(get_sprecher(self.config, self.input_test), "testinginput")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment