Commit 73663d97 authored by Lorenz Steinert's avatar Lorenz Steinert

test corrections and style changes

parent afceae75
......@@ -7,32 +7,35 @@ import logging
import unittest
import configparser
import ldap3 as ldap
from esme import FsrLdap, get_sprecher, get_protokollant, get_config, get_share, get_path, get_ldap_base, get_ldap_server, get_server_timeout, get_editor, get_date, Error
import esme
class FsrLdapTest(unittest.TestCase):
"""test the functions with ldap"""
def logging_warning(self, arg):
"""write log warings to variable"""
self.warning += [arg]
def logging_info(self, arg):
"""write log info to wariable"""
self.info += [arg]
def setUp(self):
"""setup for the tests"""
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,
config = {'PROTOKOLL': {'Protokollant': 'testing test3'}}
self.ldap_server = esme.FsrLdap('test_server', 'dc=test,dc=de',
share_dir, config, testing=True)
conn = self.ldap_server.conn
conn.strategy.add_entry('ou=people,'+self.ldap_server.base, {})
conn.strategy.add_entry('ou=group,'+self.ldap_server.base, {})
conn.strategy.add_entry('cn=intern,ou=group,'+self.ldap_server.base,
{'memberUid': 'test1'})
conn.strategy.add_entry('cn=extern,ou=group,'+self.test_ldap_server.base,
conn.strategy.add_entry('cn=extern,ou=group,'+self.ldap_server.base,
{'memberUid': 'test2'})
conn.strategy.add_entry('uid=test1,ou=people,'+self.test_ldap_server.base,
conn.strategy.add_entry('uid=test1,ou=people,'+self.ldap_server.base,
{'displayName': 'test1, testing'})
conn.strategy.add_entry('uid=test2,ou=people,'+self.test_ldap_server.base,
conn.strategy.add_entry('uid=test2,ou=people,'+self.ldap_server.base,
{'displayName': 'test2, testing'})
os.mkdir("/tmp/python3-esme-testing")
with open("/tmp/python3-esme-testing/fsr", 'w') as fobj:
......@@ -50,98 +53,118 @@ class FsrLdapTest(unittest.TestCase):
@staticmethod
def ldap_search_fail(*arg, attributes=None):
"""mok th conn.search funktion for server timeout"""
return False
@staticmethod
def ldap_exception(*arg, attributes=None):
"""mok the conn.search funktion for excepton"""
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.assertEqual(self.ldap_server._get_fsr_ldap(),
['testing test1'])
self.assertEqual(self.info, ['Fetching FSR from LDAP ...', 'Done'])
self.assertEqual(self.warning, [])
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.assertEqual(self.ldap_server._get_fsr_extern_ldap(),
['testing test2'])
self.assertEqual(self.info, ['Fetching EFSR from LDAP ...', 'Done'])
self.assertEqual(self.warning, [])
def test_get_protokollant_ldap(self):
"""test the get_protokollant_ldap function"""
esme.current_user = lambda: 'test1'
esme.input = lambda x: ""
self.assertEqual(self.test_ldap_server.get_protokollant_ldap({'PROTOKOLL': {'Protokollant': 'testing test3'}}), 'testing test1')
self.assertEqual(self.ldap_server._get_protokollant_ldap(), 'testing test1')
self.assertEqual(self.info, ['Fetching Protokollant from LDAP ...', 'Done'])
self.assertEqual(self.warning, [])
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'])
self.assertEqual(self.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)
self.assertEqual(self.ldap_server._get_fallback_fsr(sharedir), esme.Error.NO_FILE)
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
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'])
self.assertEqual(self.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)
self.assertEqual(self.ldap_server._get_fallback_fsr_extern(sharedir), esme.Error.NO_FILE)
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_fallback_protokollant(self):
"""test the get_fallback_protokollant function"""
config = {'PROTOKOLL': {'Protokollant': 'testprotokollant'}}
esme.input = lambda x: 'testinput'
self.assertEqual(self.ldap_server._get_fallback_protokollant(self.ldap_server.config),
"testinput")
self.assertEqual(self.ldap_server._get_fallback_protokollant(config), "testinput")
self.assertEqual(self.ldap_server._get_fallback_protokollant(), "testinput")
esme.input = lambda x: ''
self.assertEqual(self.ldap_server._get_fallback_protokollant(self.ldap_server.config),
"testing test3")
self.assertEqual(self.ldap_server._get_fallback_protokollant(config),
"testprotokollant")
self.assertEqual(self.ldap_server._get_fallback_protokollant(), '')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_fsr_ldap_no_server(self):
"""test the get_fsr_ldap function without ldap connection"""
self.test_ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.test_ldap_server._get_fsr_ldap(),
Error.UNSEC_SEARCH)
self.ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.ldap_server._get_fsr_ldap(),
esme.Error.UNSEC_SEARCH)
self.assertEqual(self.info, ['Fetching FSR from LDAP ...'])
self.assertEqual(self.warning, [])
def test_get_fsr_extern_ldap_no_server(self):
"""test the get_fst_extern_ldap function without ldap connection"""
self.test_ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.test_ldap_server._get_fsr_extern_ldap(),
Error.UNSEC_SEARCH)
self.ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.ldap_server._get_fsr_extern_ldap(),
esme.Error.UNSEC_SEARCH)
self.assertEqual(self.info, ['Fetching EFSR from LDAP ...'])
self.assertEqual(self.warning, [])
def test_get_protokollant_ldap_no_server(self):
"""test the get_protokollant_ldap function without ldap connection"""
esme.current_user = lambda: 'testing test1'
esme.input = lambda x: ""
self.test_ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.test_ldap_server.get_protokollant_ldap({'PROTOKOLL': {'Protokollant': 'testing test3'}}),
'testing test3')
self.ldap_server.conn.search = self.ldap_search_fail
self.assertEqual(self.ldap_server._get_protokollant_ldap(),
esme.Error.UNSEC_SEARCH)
self.assertEqual(self.info, ['Fetching Protokollant from LDAP ...'])
self.assertEqual(self.warning, [])
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)
"""test the get_fsr_ldap function with a LDAPException"""
self.ldap_server.conn.search = self.ldap_exception
self.assertEqual(self.ldap_server._get_fsr_ldap()[0],
esme.Error.LDAP_FAIL)
self.assertEqual(self.info, ['Fetching FSR from LDAP ...'])
self.assertEqual(self.warning, ["Couldn't Connect to ldap. Using fallback File."])
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)
"""test the get_fsr_extern_ldap function with a LDAPException"""
self.ldap_server.conn.search = self.ldap_exception
self.assertEqual(self.ldap_server._get_fsr_extern_ldap()[0],
esme.Error.LDAP_FAIL)
self.assertEqual(self.info, ['Fetching EFSR from LDAP ...'])
self.assertEqual(self.warning, ["Couldn't Connect to ldap. Using fallback File."])
def test_get_protokollant_ldap_ldap_exception(self):
"""test the get_protokollant_ldap function with a LDAPException"""
esme.current_user = lambda: 'testing test1'
esme.input = lambda x: ""
self.test_ldap_server.conn.search = self.ldap_exception
self.assertEqual(self.test_ldap_server.get_protokollant_ldap({'PROTOKOLL': {'Protokollant': 'testing test3'}}),
'testing test3')
self.ldap_server.conn.search = self.ldap_exception
self.assertEqual(self.ldap_server._get_protokollant_ldap(),
esme.Error.LDAP_FAIL)
self.assertEqual(self.info, ['Fetching Protokollant from LDAP ...'])
self.assertEqual(self.warning, ["Couldn't Connect to ldap. Using fallback default."])
......@@ -160,9 +183,11 @@ class GetFuncTest(unittest.TestCase):
"""test case for the get methods without ldap"""
def logging_warning(self, arg):
"""write log warings to variable"""
self.warning += [arg]
def logging_info(self, arg):
"""write log info to variable"""
self.info += [arg]
def setUp(self):
......@@ -215,24 +240,15 @@ class GetFuncTest(unittest.TestCase):
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_protokollant(self):
"""test the get_protokollant function"""
esme.input= lambda x: 'testinput'
self.assertEqual(esme.get_protokollant(), "testinput")
self.assertEqual(esme.get_protokollant(self.config), "testinput")
esme.input= lambda x: ''
self.assertEqual(esme.get_protokollant(), "")
self.assertEqual(esme.get_protokollant(self.config), "testprotokollant")
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_get_config(self):
"""test the get_config function"""
self.assertEqual(esme.get_config("", "/tmp/python3-esme-testing/etc/proto.ini"), self.config)
self.assertEqual(esme.get_config("", "/tmp/python3-esme-testing/etc/proto.ini"),
self.config)
self.assertEqual(esme.get_config("/tmp/python3-esme-testing/etc"), self.config)
os.remove("/tmp/python3-esme-testing/etc/proto.ini")
self.assertEqual(esme.get_config("/tmp/python3-esme-testing/etc"), self.config1)
self.assertEqual(esme.get_config("/tmp/python3-esme-testing/etc", default=True), self.config1)
self.assertEqual(esme.get_config("/tmp/python3-esme-testing/etc", default=True),
self.config1)
self.assertEqual(self.info, [])
self.assertEqual(self.warning, ['No non default ini file found using default file.'])
......@@ -241,7 +257,8 @@ class GetFuncTest(unittest.TestCase):
self.assertEqual(esme.get_share("", self.config), '/tmp/python3-esme-testing/share')
self.assertEqual(os.path.abspath(esme.get_share("/tmp/python3-esme-testing/share")),
'/tmp/python3-esme-testing/share')
self.assertEqual(os.path.abspath(esme.get_share("", self.config, '/tmp/python3-esme-testing/share')),
self.assertEqual(os.path.abspath(esme.get_share("", self.config,
'/tmp/python3-esme-testing/share')),
'/tmp/python3-esme-testing/share')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
......@@ -251,19 +268,21 @@ class GetFuncTest(unittest.TestCase):
self.assertEqual(esme.get_path("", self.config), '/tmp/python3-esme-testing')
self.assertEqual(os.path.abspath(esme.get_path("/tmp/python3-esme-testing/etc/test")),
'/tmp/python3-esme-testing')
self.assertEqual(os.path.abspath(esme.get_path("", self.config, '/tmp/python3-esme-testing')),
self.assertEqual(os.path.abspath(esme.get_path("", self.config,
'/tmp/python3-esme-testing')),
'/tmp/python3-esme-testing')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, [])
def test_ldap_server(self):
def test_get_ldap_server(self):
"""test the get_ldap_server function"""
self.assertEqual(esme.get_ldap_server(self.config), 'test_server')
self.assertEqual(esme.get_ldap_server(server='testing'), 'testing')
self.assertEqual(esme.get_ldap_server(self.config, 'testing'), 'testing')
self.assertEqual(esme.get_ldap_server(), 'rincewind.fs.physik.uni-kl.de')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, ['No LDAP Server supplied. Triing to use "rincwind.fs.physik.uni-kl.de".'])
self.assertEqual(self.warning,
['No LDAP Server supplied. Triing to use "rincwind.fs.physik.uni-kl.de".'])
def test_get_ldap_base(self):
"""test the get_ldap_base function"""
......@@ -272,7 +291,8 @@ class GetFuncTest(unittest.TestCase):
self.assertEqual(esme.get_ldap_base(self.config, 'dc=testing,dc=de'), 'dc=testing,dc=de')
self.assertEqual(esme.get_ldap_base(), 'dc=fs,dc=physik,dc=uni-kl,dc=de')
self.assertEqual(self.info, [])
self.assertEqual(self.warning, ['No LDAP search base supplied. Triing to use "dc=fs,dc=physik,dc=uni-kl,dc=de".'])
self.assertEqual(self.warning, ['No LDAP search base supplied. ' \
+ 'Triing to use "dc=fs,dc=physik,dc=uni-kl,dc=de".'])
def test_get_server_timeout(self):
"""test the get_server_timeout function"""
......
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