|
|
|
@ -8,23 +8,27 @@ import socket |
|
|
|
|
from zope import component |
|
|
|
|
|
|
|
|
|
from ceo_common.interfaces import IConfig, IKerberosService, ILDAPService, \ |
|
|
|
|
IFileService, IMailmanService, IHTTPClient, IUWLDAPService |
|
|
|
|
IFileService, IMailmanService, IHTTPClient, IUWLDAPService, IMailService |
|
|
|
|
from ceo_common.model import Config, RemoteMailmanService, HTTPClient |
|
|
|
|
from ceod.api import create_app |
|
|
|
|
from ceod.model import KerberosService, LDAPService, FileService, User, \ |
|
|
|
|
MailmanService, Group, UWLDAPService, UWLDAPRecord |
|
|
|
|
MailmanService, Group, UWLDAPService, UWLDAPRecord, MailService |
|
|
|
|
from ceod.model.utils import strings_to_bytes |
|
|
|
|
from .MockSMTPServer import MockSMTPServer |
|
|
|
|
from .MockMailmanServer import MockMailmanServer |
|
|
|
|
from .conftest_ceod_api import * |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session') |
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def cfg(): |
|
|
|
|
with importlib.resources.path('tests_common', 'ceod_test_local.ini') as p: |
|
|
|
|
with importlib.resources.path('tests', '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') |
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def krb_srv(cfg): |
|
|
|
|
# we need to be root to read the keytab |
|
|
|
|
assert os.geteuid() == 0 |
|
|
|
@ -33,13 +37,12 @@ def krb_srv(cfg): |
|
|
|
|
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) |
|
|
|
|
cache_dir = '/tmp/ceod_test/krb5_cache' |
|
|
|
|
shutil.rmtree(cache_dir, ignore_errors=True) |
|
|
|
|
krb = KerberosService(principal, cache_dir) |
|
|
|
|
component.provideUtility(krb, IKerberosService) |
|
|
|
|
yield krb |
|
|
|
|
os.unlink(cache_file) |
|
|
|
|
shutil.rmtree(cache_dir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def recursively_delete_subtree(conn: ldap.ldapobject.LDAPObject, base_dn: str): |
|
|
|
@ -52,7 +55,7 @@ def recursively_delete_subtree(conn: ldap.ldapobject.LDAPObject, base_dn: str): |
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session') |
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def ldap_srv(cfg, krb_srv): |
|
|
|
|
conn = ldap.initialize(cfg.get('ldap_server_url')) |
|
|
|
|
conn.sasl_gssapi_bind_s() |
|
|
|
@ -76,7 +79,7 @@ def ldap_srv(cfg, krb_srv): |
|
|
|
|
recursively_delete_subtree(conn, groups_base) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session') |
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def file_srv(cfg): |
|
|
|
|
_file_srv = FileService() |
|
|
|
|
component.provideUtility(_file_srv, IFileService) |
|
|
|
@ -90,6 +93,82 @@ def file_srv(cfg): |
|
|
|
|
shutil.rmtree(clubs_home, ignore_errors=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def http_client(cfg): |
|
|
|
|
client = HTTPClient() |
|
|
|
|
component.provideUtility(client, IHTTPClient) |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def mock_mailman_server(): |
|
|
|
|
server = MockMailmanServer() |
|
|
|
|
server.start() |
|
|
|
|
yield server |
|
|
|
|
server.stop() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def mailman_srv(mock_mailman_server, cfg, http_client): |
|
|
|
|
# TODO: test the RemoteMailmanService as well |
|
|
|
|
mailman = MailmanService() |
|
|
|
|
component.provideUtility(mailman, IMailmanService) |
|
|
|
|
return mailman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def uwldap_srv(cfg, ldap_srv): |
|
|
|
|
conn = ldap.initialize(cfg.get('uwldap_server_url')) |
|
|
|
|
conn.sasl_gssapi_bind_s() |
|
|
|
|
base_dn = cfg.get('uwldap_base') |
|
|
|
|
ou = base_dn.split(',', 1)[0].split('=')[1] |
|
|
|
|
|
|
|
|
|
recursively_delete_subtree(conn, base_dn) |
|
|
|
|
|
|
|
|
|
conn.add_s(base_dn, ldap.modlist.addModlist({ |
|
|
|
|
'objectClass': [b'organizationalUnit'], |
|
|
|
|
'ou': [ou.encode()] |
|
|
|
|
})) |
|
|
|
|
_uwldap_srv = UWLDAPService() |
|
|
|
|
component.provideUtility(_uwldap_srv, IUWLDAPService) |
|
|
|
|
yield _uwldap_srv |
|
|
|
|
|
|
|
|
|
recursively_delete_subtree(conn, base_dn) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def mock_mail_server(): |
|
|
|
|
mock_server = MockSMTPServer() |
|
|
|
|
mock_server.start() |
|
|
|
|
yield mock_server |
|
|
|
|
mock_server.stop() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def mail_srv(cfg, mock_mail_server): |
|
|
|
|
_mail_srv = MailService() |
|
|
|
|
component.provideUtility(_mail_srv, IMailService) |
|
|
|
|
return _mail_srv |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session') |
|
|
|
|
def app( |
|
|
|
|
cfg, |
|
|
|
|
krb_srv, |
|
|
|
|
ldap_srv, |
|
|
|
|
file_srv, |
|
|
|
|
mailman_srv, |
|
|
|
|
uwldap_srv, |
|
|
|
|
mail_srv, |
|
|
|
|
): |
|
|
|
|
# need to be root to read keytab |
|
|
|
|
assert os.geteuid() == 0 |
|
|
|
|
app = create_app({ |
|
|
|
|
'TESTING': True, |
|
|
|
|
}) |
|
|
|
|
return app |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
|
def simple_user(): |
|
|
|
|
return User( |
|
|
|
@ -123,24 +202,6 @@ def krb_user(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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
|
def simple_group(): |
|
|
|
|
return Group( |
|
|
|
@ -156,26 +217,6 @@ def ldap_group(simple_group): |
|
|
|
|
simple_group.remove_from_ldap() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session') |
|
|
|
|
def uwldap_srv(cfg, ldap_srv): |
|
|
|
|
conn = ldap.initialize(cfg.get('uwldap_server_url')) |
|
|
|
|
conn.sasl_gssapi_bind_s() |
|
|
|
|
base_dn = cfg.get('uwldap_base') |
|
|
|
|
ou = base_dn.split(',', 1)[0].split('=')[1] |
|
|
|
|
|
|
|
|
|
recursively_delete_subtree(conn, base_dn) |
|
|
|
|
|
|
|
|
|
conn.add_s(base_dn, ldap.modlist.addModlist({ |
|
|
|
|
'objectClass': [b'organizationalUnit'], |
|
|
|
|
'ou': [ou.encode()] |
|
|
|
|
})) |
|
|
|
|
_uwldap_srv = UWLDAPService() |
|
|
|
|
component.provideUtility(_uwldap_srv, IUWLDAPService) |
|
|
|
|
yield _uwldap_srv |
|
|
|
|
|
|
|
|
|
recursively_delete_subtree(conn, base_dn) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
|
def uwldap_user(cfg, uwldap_srv): |
|
|
|
|
conn = ldap.initialize(cfg.get('uwldap_server_url')) |