Python CSC Electronic Office
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.8 KiB

import ceod.utils as ceod_utils
import contextlib
import os
import shutil
import subprocess
from subprocess import DEVNULL
import tempfile
from unittest.mock import patch
import gssapi
import pytest
# map principals to GSSAPI credentials
_cache = {}
def gssapi_token_ctx(principal: str):
Temporarily set KRB5CCNAME to a ccache storing credentials
for the specified user, and yield the GSSAPI credentials.
old_krb5ccname = os.environ['KRB5CCNAME']
if principal not in _cache:
f = tempfile.NamedTemporaryFile()
os.environ['KRB5CCNAME'] = 'FILE:' +
args = ['kinit', principal]
if principal == 'ceod/admin':
args = ['kinit', '-k', principal]
args, stdout=DEVNULL, text=True, input='krb5', check=True)
creds = gssapi.Credentials(name=gssapi.Name(principal), usage='initiate')
# Keep the credential cache files around as long as the creds are
# used, otherwise we get a "Invalid credential was supplied" error
_cache[principal] = creds, f
creds, f = _cache[principal]
os.environ['KRB5CCNAME'] = 'FILE:' +
yield creds.export()
os.environ['KRB5CCNAME'] = old_krb5ccname
@pytest.fixture(scope='session', autouse=True)
def ccache_cleanup():
"""Make sure the ccache files get deleted at the end of the tests."""
def gen_password_mock_ctx():
with patch.object(ceod_utils, 'gen_password') as mock:
mock.return_value = 'krb5'
def mocks_for_create_user_ctx():
with gen_password_mock_ctx():
def set_datetime_in_app_process(app_process, value):
pipe = app_process
pipe.recv() # wait for ACK
def restore_datetime_in_app_process(app_process):
set_datetime_in_app_process(app_process, None)
def reset_disable_club_conf(webhosting_srv):
shutil.copy('tests/resources/apache2/disable-club.conf', webhosting_srv.conf_available_dir)
def create_php_file_for_club(clubs_home, club_name):
www = f'{clubs_home}/{club_name}/www'
os.makedirs(www, exist_ok=True)
open(www + '/index.php', 'w').close()
def create_website_config_for_club(sites_available_dir, cn, filename=None):
if filename is None:
filename = f'club-{cn}.conf'
filepath = os.path.join(sites_available_dir, filename)
with open(filepath, 'w') as fo:
<VirtualHost *:80>
ServerName {cn}.uwaterloo.internal
ServerAdmin {cn}@{cn}.uwaterloo.internal
DocumentRoot /users/{cn}/www/