Commit 35342fd8 authored by Lorenz Steinert's avatar Lorenz Steinert

added more get tests and changed to mocking functions

parent c32351a3
......@@ -2,36 +2,30 @@
# -*- coding: utf-8 -*-
"""test the esme module"""
import os
import time
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_fallback_fsr, get_fallback_fsr_extern, get_sprecher, get_protokollant, get_config, get_share, get_path, get_ldap_base, get_ldap_server, get_server_timeout, get_editor, get_date
import esme
class FsrLdapTest(unittest.TestCase):
"""test the functions with ldap"""
def setUp(self):
"""setup for the tests"""
self.test_ldap_server = FsrLdap('test_server', 'dc=test,dc=de')
self.test_ldap_server = esme.FsrLdap('test_server', 'dc=test,dc=de')
conn = ldap.Connection(self.test_ldap_server.server, client_strategy=ldap.MOCK_SYNC)
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,
{'memberUid': ['test1', 'test2', 'test3']})
{'memberUid': 'test1'})
conn.strategy.add_entry('cn=extern,ou=group,'+self.test_ldap_server.base,
{'memberUid': ['test4', 'test5', 'test6']})
{'memberUid': 'test2'})
conn.strategy.add_entry('uid=test1,ou=people,'+self.test_ldap_server.base,
{'displayName': 'test1, testing'})
conn.strategy.add_entry('uid=test2,ou=people,'+self.test_ldap_server.base,
{'displayName': 'test2, testing'})
conn.strategy.add_entry('uid=test3,ou=people,'+self.test_ldap_server.base,
{'displayName': 'test3, testing'})
conn.strategy.add_entry('uid=test4,ou=people,'+self.test_ldap_server.base,
{'displayName': 'test4, testing'})
conn.strategy.add_entry('uid=test5,ou=people,'+self.test_ldap_server.base,
{'displayName': 'test5, testing'})
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()
......@@ -39,26 +33,38 @@ class FsrLdapTest(unittest.TestCase):
def test_get_fsr_ldap(self):
"""test the get_fsr_ldap function"""
ldap.Connection = lambda x: ldap.Connection(self.test_ldap_server, client_strategy=ldap.MOCK_SYNC)
self.assertEqual(self.test_ldap_server.get_fsr_ldap(self.sharedir, testing=True),
['testing test1', 'testing test2', 'testing test3'])
['testing test1'])
def test_get_fsr_extern_ldap(self):
"""test the get_fsr_extern_ldap function"""
ldap.Connection = lambda x: ldap.Connection(self.test_ldap_server, client_strategy=ldap.MOCK_SYNC)
self.assertEqual(self.test_ldap_server.get_fsr_extern_ldap(self.sharedir, testing=True),
['testing test4', 'testing test5', 'testing test6'])
['testing test2'])
def test_get_protokollant_ldap(self):
ldap.Connection = lambda x: ldap.Connection(self.test_ldap_server, client_strategy=ldap.MOCK_SYNC)
esme.current_user = lambda: 'testing test1'
self.assertEqual(self.test_ldap_server.get_protokollant_ldap({'PROTOKOLL': {'Protokollant': 'testing test3'}}), 'testing test1')
def test_get_fsr_ldap_no_server(self):
"""test the get_fsr_ldap function without ldap connection"""
self.test_ldap_server.server = ""
ldap.Connection = lambda x: ldap.Connection('', client_strategy=ldap.MOCK_SYNC)
self.assertEqual(self.test_ldap_server.get_fsr_ldap(self.sharedir, testing=True),
[])
def test_get_fsr_extern_ldap_no_server(self):
"""test the get_fst_extern_ldap function without ldap connection"""
self.test_ldap_server.server = ""
ldap.Connection = lambda x: ldap.Connection('', client_strategy=ldap.MOCK_SYNC)
self.assertEqual(self.test_ldap_server.get_fsr_extern_ldap(self.sharedir, testing=True),
[])
def test_get_protokollant_ldap_no_server(self):
ldap.Connection = lambda x: ldap.Connection('', client_strategy=ldap.MOCK_SYNC)
esme.current_user = lambda: 'testing test1'
self.assertEqual(self.test_ldap_server.get_protokollant_ldap({'PROTOKOLL': {'Protokollant': 'testing test3'}}), 'testing test3')
def tearDown(self):
"""cleanup after the tests"""
os.remove("/tmp/python3-esme-testing/fsr")
......@@ -71,6 +77,7 @@ class GetFuncTest(unittest.TestCase):
def setUp(self):
"""setup for the tests"""
self.time_save = time.localtime
os.mkdir("/tmp/python3-esme-testing")
os.mkdir("/tmp/python3-esme-testing/etc")
os.mkdir("/tmp/python3-esme-testing/etc/test")
......@@ -86,7 +93,7 @@ class GetFuncTest(unittest.TestCase):
'path': '/tmp/python3-esme-testing'}
self.config['SERVER'] = {'name': 'test_server',
'base': 'dc=test,dc=de',
'connect_timeout': 10}
'connect_timeout': 3}
with open("/tmp/python3-esme-testing/etc/proto.ini", 'w') as fobj:
self.config.write(fobj)
self.config1 = configparser.ConfigParser()
......@@ -96,71 +103,106 @@ class GetFuncTest(unittest.TestCase):
'path': '/tmp/python3-esme-testing'}
self.config1['SERVER'] = {'name': 'test_server',
'base': 'dc=test,dc=de',
'connect_timeout': 10}
'connect_timeout': 3}
self.config1['DEFAULT'] = {'tmp': 'tmp'}
with open("/tmp/python3-esme-testing/etc/proto.ini.default", 'w') as fobj:
self.config1.write(fobj)
@staticmethod
def input_test(arg):
"""override for the std input method"""
return "testinginput"
@staticmethod
def input_test2(arg):
"""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'])
self.assertEqual(esme.get_fallback_fsr(sharedir), ['testing test1'])
os.remove("/tmp/python3-esme-testing/fsr")
self.assertEqual(get_fallback_fsr(sharedir), [])
self.assertEqual(esme.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'])
self.assertEqual(esme.get_fallback_fsr_extern(sharedir), ['testing test2'])
os.remove("/tmp/python3-esme-testing/fsr_extern")
self.assertEqual(get_fallback_fsr_extern(sharedir), [])
self.assertEqual(esme.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")
self.assertEqual(get_sprecher(self.config, self.input_test2), "testsprecher")
self.assertEqual(get_sprecher(getinput=self.input_test), "testinginput")
self.assertEqual(get_sprecher(getinput=self.input_test2), "")
esme.input = lambda x: ''
self.assertEqual(esme.get_sprecher(), "")
self.assertEqual(esme.get_sprecher(self.config), "testsprecher")
esme.input = lambda x: 'testinput'
self.assertEqual(esme.get_sprecher(), "testinput")
self.assertEqual(esme.get_sprecher(self.config), "testinput")
def test_get_protokollant(self):
"""test the get_protokollant function"""
self.assertEqual(get_protokollant(self.config, self.input_test), "testinginput")
self.assertEqual(get_protokollant(self.config, self.input_test2), "testprotokollant")
self.assertEqual(get_protokollant(getinput=self.input_test), "testinginput")
self.assertEqual(get_protokollant(getinput=self.input_test2), "")
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")
def test_get_config(self):
"""test the get_config function"""
self.assertEqual(get_config("", "/tmp/python3-esme-testing/etc/proto.ini"), self.config)
self.assertEqual(get_config("/tmp/python3-esme-testing/etc"), 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(get_config("/tmp/python3-esme-testing/etc"), self.config1)
self.assertEqual(get_config("/tmp/python3-esme-testing/etc", default=True), self.config1)
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)
def test_get_share(self):
"""test the get_share function"""
self.assertEqual(get_share("", self.config), '/tmp/python3-esme-testing/share')
self.assertEqual(os.path.abspath(get_share("/tmp/python3-esme-testing/share")), '/tmp/python3-esme-testing/share')
self.assertEqual(os.path.abspath(get_share("", self.config, '/tmp/python3-esme-testing/share')), '/tmp/python3-esme-testing/share')
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')), '/tmp/python3-esme-testing/share')
def test_get_path(self):
"""test the get_path function"""
self.assertEqual(get_path("", self.config), '/tmp/python3-esme-testing')
self.assertEqual(os.path.abspath(get_path("/tmp/python3-esme-testing/etc/test")), '/tmp/python3-esme-testing')
self.assertEqual(os.path.abspath(get_path("", self.config, '/tmp/python3-esme-testing')), '/tmp/python3-esme-testing')
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')), '/tmp/python3-esme-testing')
def test_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')
def test_get_ldap_base(self):
"""test the get_ldap_base function"""
self.assertEqual(esme.get_ldap_base(self.config), 'dc=test,dc=de')
self.assertEqual(esme.get_ldap_base(base='dc=testing,dc=de'), 'dc=testing,dc=de')
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')
def test_get_server_timeout(self):
"""test the get_server_timeout function"""
self.assertEqual(esme.get_server_timeout(self.config), 3)
self.assertEqual(esme.get_server_timeout(test=True), 1)
self.assertEqual(esme.get_server_timeout(self.config, True), 1)
self.assertEqual(esme.get_server_timeout(), 10)
def test_get_editor(self):
"""test the get_editor function"""
esme.input = lambda x: 'testinput'
self.assertEqual(esme.get_editor('vim'), 'testinput')
esme.input = lambda x: ''
self.assertEqual(esme.get_editor('vim'), 'vim')
def test_get_date(self):
"""test the get_date function"""
time.localtime = lambda: (2001, 1, 1)
esme.input = lambda x: ''
self.assertEqual(esme.get_date(), ('01.01.2001', '2001', '01', '01'))
self.assertEqual(esme.get_date(True), ('01.01.2000', '2000', '01', '01'))
esme.input = lambda x: '01.01.2002'
self.assertEqual(esme.get_date(), ('01.01.2002', '2002', '01', '01'))
self.assertEqual(esme.get_date(True), ('01.01.2002', '2002', '01', '01'))
def tearDown(self):
"""cleanup after tests"""
esme.input = input
time.localtime = self.time_save
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'):
......
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