From 953bee549e730f55a80719f306afb9273bf1e745 Mon Sep 17 00:00:00 2001 From: Max Erenberg Date: Mon, 5 Sep 2022 17:34:28 -0400 Subject: [PATCH] fix tests --- tests/ceo/cli/test_webhosting.py | 3 ++ tests/ceod/api/test_webhosting.py | 4 ++- tests/ceod/model/test_webhosting.py | 44 +++++++++++++++++------------ tests/utils.py | 14 +++++++++ 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/tests/ceo/cli/test_webhosting.py b/tests/ceo/cli/test_webhosting.py index f4817c7..dba3ba5 100644 --- a/tests/ceo/cli/test_webhosting.py +++ b/tests/ceo/cli/test_webhosting.py @@ -5,6 +5,7 @@ from ceo_common.model import Term from tests.utils import ( create_php_file_for_club, reset_disable_club_conf, + create_website_config_for_club, set_datetime_in_app_process, restore_datetime_in_app_process, ) @@ -15,10 +16,12 @@ def test_disable_club_sites( new_club_gen, new_user_gen, g_admin_ctx, ldap_srv_session, ): runner = CliRunner() + sites_available_dir = webhosting_srv.sites_available_dir term = Term.current() clubs_home = cfg.get('clubs_home') with new_club_gen() as group, new_user_gen() as user: create_php_file_for_club(clubs_home, group.cn) + create_website_config_for_club(sites_available_dir, group.cn) user.add_non_member_terms([str(Term.current())]) group.add_member(user.uid) diff --git a/tests/ceod/api/test_webhosting.py b/tests/ceod/api/test_webhosting.py index db39f19..3345b2c 100644 --- a/tests/ceod/api/test_webhosting.py +++ b/tests/ceod/api/test_webhosting.py @@ -3,13 +3,14 @@ from unittest.mock import patch from ceo_common.model import Term import ceo_common.utils -from tests.utils import create_php_file_for_club, reset_disable_club_conf +from tests.utils import create_php_file_for_club, reset_disable_club_conf, create_website_config_for_club def test_disable_club_sites( cfg, client, webhosting_srv, webhosting_srv_resources, new_club_gen, new_user_gen, g_admin_ctx, ldap_srv_session, ): + sites_available_dir = webhosting_srv.sites_available_dir term = Term.current() clubs_home = cfg.get('clubs_home') with patch.object(ceo_common.utils, 'get_current_datetime') as now_mock: @@ -33,6 +34,7 @@ def test_disable_club_sites( assert group.members == [user.uid] reset_disable_club_conf(webhosting_srv) + create_website_config_for_club(sites_available_dir, group.cn) status, data = client.post('/api/webhosting/disableclubsites?remove_inactive_club_reps=true') assert status == 200 diff --git a/tests/ceod/model/test_webhosting.py b/tests/ceod/model/test_webhosting.py index e359c79..da7c89c 100644 --- a/tests/ceod/model/test_webhosting.py +++ b/tests/ceod/model/test_webhosting.py @@ -1,26 +1,11 @@ import datetime -import os import re import subprocess from unittest.mock import patch from ceo_common.model import Term import ceo_common.utils -from tests.utils import create_php_file_for_club - - -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: - fo.write(f""" - - ServerName {cn}.uwaterloo.internal - ServerAdmin {cn}@{cn}.uwaterloo.internal - DocumentRoot /users/{cn}/www/ - - """) +from tests.utils import create_php_file_for_club, reset_disable_club_conf, create_website_config_for_club def get_enabled_sites(webhosting_srv): @@ -67,6 +52,8 @@ def test_disable_inactive_club_sites( new_user_gen() as user2: create_website_config_for_club(sites_available_dir, group1.cn) create_website_config_for_club(sites_available_dir, group2.cn) + group1_email = f'{group1.cn}@{group1.cn}.uwaterloo.internal' + group2_email = f'{group2.cn}@{group2.cn}.uwaterloo.internal' create_php_file_for_club(clubs_home, group1.cn) with g_admin_ctx(): # group1 has no club reps so it should be disabled @@ -111,9 +98,14 @@ def test_disable_inactive_club_sites( # since each club had a ServerAdmin directive, they should both have received # notification emails assert len(mock_mail_server.messages) == 2 + recipients = set([ + *mock_mail_server.messages[0]['to'].split(','), + *mock_mail_server.messages[1]['to'].split(','), + ]) + assert group1_email in recipients + assert group2_email in recipients with open(webhosting_srv.conf_available_dir + '/disable-club.conf') as fi: disable_club_conf_content = fi.read() - print(disable_club_conf_content) for group in [group1, group2]: pat = re.compile( ( @@ -134,8 +126,9 @@ def test_disable_inactive_club_sites( def test_remove_inactive_club_reps( cfg, webhosting_srv, webhosting_srv_resources, g_admin_ctx, new_club_gen, - new_user_gen, ldap_srv_session, + new_user_gen, ldap_srv_session, mock_mail_server, ): + sites_available_dir = webhosting_srv.sites_available_dir term = Term.current() clubs_home = cfg.get('clubs_home') with patch.object(ceo_common.utils, 'get_current_datetime') as now_mock: @@ -144,15 +137,30 @@ def test_remove_inactive_club_reps( new_user_gen() as user1, \ new_user_gen() as user2: create_php_file_for_club(clubs_home, group.cn) + club_email = f'{group.cn}@{group.cn}.uwaterloo.internal' for user in [user1, user2]: user.add_non_member_terms([str(Term.current())]) group.add_member(user.uid) now_mock.return_value = (term + 4).to_datetime() + + # If the ServerAdmin directive wasn't specified, no email is sent and club reps + # aren't removed + mock_mail_server.messages.clear() + with g_admin_ctx(): + webhosting_srv.disable_sites_for_inactive_clubs(remove_inactive_club_reps=True) + group = ldap_srv_session.get_group(group.cn) + assert sorted(group.members) == [user1.uid, user2.uid] + assert mock_mail_server.messages == [] + + reset_disable_club_conf(webhosting_srv) + create_website_config_for_club(sites_available_dir, group.cn) with g_admin_ctx(): webhosting_srv.disable_sites_for_inactive_clubs(remove_inactive_club_reps=True) group = ldap_srv_session.get_group(group.cn) assert group.members == [] + assert len(mock_mail_server.messages) == 1 + assert club_email in mock_mail_server.messages[0]['to'].split(',') # Make sure that inactive club reps get removed even if the site # has already been disabled diff --git a/tests/utils.py b/tests/utils.py index 8eb5daf..62fedc6 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -81,3 +81,17 @@ 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: + fo.write(f""" + + ServerName {cn}.uwaterloo.internal + ServerAdmin {cn}@{cn}.uwaterloo.internal + DocumentRoot /users/{cn}/www/ + + """)