141 lines
3.8 KiB
Python
141 lines
3.8 KiB
Python
import importlib.resources
|
|
import os
|
|
import shutil
|
|
|
|
import ldap
|
|
import pytest
|
|
import socket
|
|
from zope import component
|
|
|
|
from ceo_common.interfaces import IConfig, IKerberosService, ILDAPService, \
|
|
IFileService, IMailmanService, IHTTPClient
|
|
from ceo_common.model import Config, RemoteMailmanService, HTTPClient
|
|
from ceod.model import KerberosService, LDAPService, FileService, User, \
|
|
MailmanService
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session')
|
|
def cfg():
|
|
with importlib.resources.path('ceo_common.test', 'ceod_test_local.ini') as p:
|
|
config_file = p.__fspath__()
|
|
_cfg = Config(config_file)
|
|
component.provideUtility(_cfg, IConfig)
|
|
return _cfg
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session')
|
|
def krb_srv(cfg):
|
|
# we need to be root to read the keytab
|
|
assert os.geteuid() == 0
|
|
# this dance again... ugh
|
|
if socket.gethostname() == cfg.get('ceod_admin_host'):
|
|
principal = 'ceod/admin'
|
|
else:
|
|
principal = 'ceod/' + socket.getfqdn()
|
|
cache_file = '/tmp/ceod_test/krb5_cache'
|
|
if os.path.isfile(cache_file):
|
|
os.unlink(cache_file)
|
|
krb = KerberosService(principal, cache_file)
|
|
component.provideUtility(krb, IKerberosService)
|
|
yield krb
|
|
os.unlink(cache_file)
|
|
|
|
|
|
def recursively_delete_subtree(conn: ldap.ldapobject.LDAPObject, base_dn: str):
|
|
try:
|
|
records = conn.search_s(base_dn, ldap.SCOPE_ONELEVEL, attrlist=[''])
|
|
for dn, _ in records:
|
|
conn.delete_s(dn)
|
|
conn.delete_s(base_dn)
|
|
except ldap.NO_SUCH_OBJECT:
|
|
pass
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session')
|
|
def ldap_srv(cfg, krb_srv):
|
|
conn = ldap.initialize(cfg.get('ldap_server_url'))
|
|
conn.sasl_gssapi_bind_s()
|
|
users_base = cfg.get('ldap_users_base')
|
|
groups_base = cfg.get('ldap_groups_base')
|
|
|
|
recursively_delete_subtree(conn, users_base)
|
|
recursively_delete_subtree(conn, groups_base)
|
|
|
|
for base_dn in [users_base, groups_base]:
|
|
ou = base_dn.split(',', 1)[0].split('=')[1]
|
|
conn.add_s(base_dn, ldap.modlist.addModlist({
|
|
'objectClass': [b'organizationalUnit'],
|
|
'ou': [ou.encode()]
|
|
}))
|
|
_ldap_srv = LDAPService()
|
|
component.provideUtility(_ldap_srv, ILDAPService)
|
|
yield _ldap_srv
|
|
|
|
recursively_delete_subtree(conn, users_base)
|
|
recursively_delete_subtree(conn, groups_base)
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session')
|
|
def file_srv(cfg):
|
|
_file_srv = FileService()
|
|
component.provideUtility(_file_srv, IFileService)
|
|
members_home = cfg.get('members_home')
|
|
clubs_home = cfg.get('clubs_home')
|
|
|
|
shutil.rmtree(members_home, ignore_errors=True)
|
|
shutil.rmtree(clubs_home, ignore_errors=True)
|
|
yield _file_srv
|
|
shutil.rmtree(members_home, ignore_errors=True)
|
|
shutil.rmtree(clubs_home, ignore_errors=True)
|
|
|
|
|
|
@pytest.fixture
|
|
def simple_user():
|
|
return User(
|
|
uid='test_jdoe',
|
|
cn='John Doe',
|
|
program='Math',
|
|
terms=['s2021'],
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def simple_club():
|
|
return User(
|
|
uid='test_club1',
|
|
cn='Club One',
|
|
is_club=True,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def ldap_user(simple_user):
|
|
simple_user.add_to_ldap()
|
|
yield simple_user
|
|
simple_user.remove_from_ldap()
|
|
|
|
|
|
@pytest.fixture
|
|
def krb_user(simple_user):
|
|
simple_user.add_to_kerberos('krb5')
|
|
yield simple_user
|
|
simple_user.remove_from_kerberos()
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
def http_client():
|
|
client = HTTPClient()
|
|
component.provideUtility(client, IHTTPClient)
|
|
return
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session')
|
|
def mailman_srv(cfg, http_client):
|
|
if socket.gethostname() == cfg.get('ceod_mailman_host'):
|
|
# TODO: use a mock server on drone.io
|
|
mailman = MailmanService()
|
|
else:
|
|
mailman = RemoteMailmanService()
|
|
component.provideUtility(mailman, IMailmanService)
|
|
return mailman
|