fix tests
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
0334e7e667
commit
953bee549e
|
@ -5,6 +5,7 @@ from ceo_common.model import Term
|
||||||
from tests.utils import (
|
from tests.utils import (
|
||||||
create_php_file_for_club,
|
create_php_file_for_club,
|
||||||
reset_disable_club_conf,
|
reset_disable_club_conf,
|
||||||
|
create_website_config_for_club,
|
||||||
set_datetime_in_app_process,
|
set_datetime_in_app_process,
|
||||||
restore_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,
|
new_club_gen, new_user_gen, g_admin_ctx, ldap_srv_session,
|
||||||
):
|
):
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
|
sites_available_dir = webhosting_srv.sites_available_dir
|
||||||
term = Term.current()
|
term = Term.current()
|
||||||
clubs_home = cfg.get('clubs_home')
|
clubs_home = cfg.get('clubs_home')
|
||||||
with new_club_gen() as group, new_user_gen() as user:
|
with new_club_gen() as group, new_user_gen() as user:
|
||||||
create_php_file_for_club(clubs_home, group.cn)
|
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())])
|
user.add_non_member_terms([str(Term.current())])
|
||||||
group.add_member(user.uid)
|
group.add_member(user.uid)
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,14 @@ from unittest.mock import patch
|
||||||
|
|
||||||
from ceo_common.model import Term
|
from ceo_common.model import Term
|
||||||
import ceo_common.utils
|
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(
|
def test_disable_club_sites(
|
||||||
cfg, client, webhosting_srv, webhosting_srv_resources, new_club_gen,
|
cfg, client, webhosting_srv, webhosting_srv_resources, new_club_gen,
|
||||||
new_user_gen, g_admin_ctx, ldap_srv_session,
|
new_user_gen, g_admin_ctx, ldap_srv_session,
|
||||||
):
|
):
|
||||||
|
sites_available_dir = webhosting_srv.sites_available_dir
|
||||||
term = Term.current()
|
term = Term.current()
|
||||||
clubs_home = cfg.get('clubs_home')
|
clubs_home = cfg.get('clubs_home')
|
||||||
with patch.object(ceo_common.utils, 'get_current_datetime') as now_mock:
|
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]
|
assert group.members == [user.uid]
|
||||||
|
|
||||||
reset_disable_club_conf(webhosting_srv)
|
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')
|
status, data = client.post('/api/webhosting/disableclubsites?remove_inactive_club_reps=true')
|
||||||
assert status == 200
|
assert status == 200
|
||||||
|
|
|
@ -1,26 +1,11 @@
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from ceo_common.model import Term
|
from ceo_common.model import Term
|
||||||
import ceo_common.utils
|
import ceo_common.utils
|
||||||
from tests.utils import create_php_file_for_club
|
from tests.utils import create_php_file_for_club, reset_disable_club_conf, create_website_config_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"""
|
|
||||||
<VirtualHost *:80>
|
|
||||||
ServerName {cn}.uwaterloo.internal
|
|
||||||
ServerAdmin {cn}@{cn}.uwaterloo.internal
|
|
||||||
DocumentRoot /users/{cn}/www/
|
|
||||||
</VirtualHost>
|
|
||||||
""")
|
|
||||||
|
|
||||||
|
|
||||||
def get_enabled_sites(webhosting_srv):
|
def get_enabled_sites(webhosting_srv):
|
||||||
|
@ -67,6 +52,8 @@ def test_disable_inactive_club_sites(
|
||||||
new_user_gen() as user2:
|
new_user_gen() as user2:
|
||||||
create_website_config_for_club(sites_available_dir, group1.cn)
|
create_website_config_for_club(sites_available_dir, group1.cn)
|
||||||
create_website_config_for_club(sites_available_dir, group2.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)
|
create_php_file_for_club(clubs_home, group1.cn)
|
||||||
with g_admin_ctx():
|
with g_admin_ctx():
|
||||||
# group1 has no club reps so it should be disabled
|
# 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
|
# since each club had a ServerAdmin directive, they should both have received
|
||||||
# notification emails
|
# notification emails
|
||||||
assert len(mock_mail_server.messages) == 2
|
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:
|
with open(webhosting_srv.conf_available_dir + '/disable-club.conf') as fi:
|
||||||
disable_club_conf_content = fi.read()
|
disable_club_conf_content = fi.read()
|
||||||
print(disable_club_conf_content)
|
|
||||||
for group in [group1, group2]:
|
for group in [group1, group2]:
|
||||||
pat = re.compile(
|
pat = re.compile(
|
||||||
(
|
(
|
||||||
|
@ -134,8 +126,9 @@ def test_disable_inactive_club_sites(
|
||||||
|
|
||||||
def test_remove_inactive_club_reps(
|
def test_remove_inactive_club_reps(
|
||||||
cfg, webhosting_srv, webhosting_srv_resources, g_admin_ctx, new_club_gen,
|
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()
|
term = Term.current()
|
||||||
clubs_home = cfg.get('clubs_home')
|
clubs_home = cfg.get('clubs_home')
|
||||||
with patch.object(ceo_common.utils, 'get_current_datetime') as now_mock:
|
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 user1, \
|
||||||
new_user_gen() as user2:
|
new_user_gen() as user2:
|
||||||
create_php_file_for_club(clubs_home, group.cn)
|
create_php_file_for_club(clubs_home, group.cn)
|
||||||
|
club_email = f'{group.cn}@{group.cn}.uwaterloo.internal'
|
||||||
|
|
||||||
for user in [user1, user2]:
|
for user in [user1, user2]:
|
||||||
user.add_non_member_terms([str(Term.current())])
|
user.add_non_member_terms([str(Term.current())])
|
||||||
group.add_member(user.uid)
|
group.add_member(user.uid)
|
||||||
now_mock.return_value = (term + 4).to_datetime()
|
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():
|
with g_admin_ctx():
|
||||||
webhosting_srv.disable_sites_for_inactive_clubs(remove_inactive_club_reps=True)
|
webhosting_srv.disable_sites_for_inactive_clubs(remove_inactive_club_reps=True)
|
||||||
group = ldap_srv_session.get_group(group.cn)
|
group = ldap_srv_session.get_group(group.cn)
|
||||||
assert group.members == []
|
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
|
# Make sure that inactive club reps get removed even if the site
|
||||||
# has already been disabled
|
# has already been disabled
|
||||||
|
|
|
@ -81,3 +81,17 @@ def create_php_file_for_club(clubs_home, club_name):
|
||||||
www = f'{clubs_home}/{club_name}/www'
|
www = f'{clubs_home}/{club_name}/www'
|
||||||
os.makedirs(www, exist_ok=True)
|
os.makedirs(www, exist_ok=True)
|
||||||
open(www + '/index.php', 'w').close()
|
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"""
|
||||||
|
<VirtualHost *:80>
|
||||||
|
ServerName {cn}.uwaterloo.internal
|
||||||
|
ServerAdmin {cn}@{cn}.uwaterloo.internal
|
||||||
|
DocumentRoot /users/{cn}/www/
|
||||||
|
</VirtualHost>
|
||||||
|
""")
|
||||||
|
|
Loading…
Reference in New Issue