-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: don't use filesystem for group aliases (#7556)
* refactor: generate group aliases on the fly * chore: remove group alias file check * chore: drop group alias settings, fix lint * refactor: rename var to hint it's ignored * test: update tests * refactor: move utility to utils * test: add test --------- Co-authored-by: Robert Sparks <[email protected]>
- Loading branch information
1 parent
6338f45
commit 0ac2ae1
Showing
7 changed files
with
129 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
# Copyright The IETF Trust 2013-2020, All Rights Reserved | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import datetime | ||
import json | ||
import mock | ||
|
||
from tempfile import NamedTemporaryFile | ||
|
||
from django.conf import settings | ||
from django.urls import reverse as urlreverse | ||
from django.db.models import Q | ||
from django.test import Client | ||
|
@@ -22,6 +19,7 @@ | |
get_group_role_emails, | ||
get_child_group_role_emails, | ||
get_group_ad_emails, | ||
get_group_email_aliases, | ||
GroupAliasGenerator, | ||
role_holder_emails, | ||
) | ||
|
@@ -132,24 +130,6 @@ def test_group_document_dependencies(self): | |
|
||
|
||
class GenerateGroupAliasesTests(TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.doc_aliases_file = NamedTemporaryFile(delete=False, mode='w+') | ||
self.doc_aliases_file.close() | ||
self.doc_virtual_file = NamedTemporaryFile(delete=False, mode='w+') | ||
self.doc_virtual_file.close() | ||
self.saved_draft_aliases_path = settings.GROUP_ALIASES_PATH | ||
self.saved_draft_virtual_path = settings.GROUP_VIRTUAL_PATH | ||
settings.GROUP_ALIASES_PATH = self.doc_aliases_file.name | ||
settings.GROUP_VIRTUAL_PATH = self.doc_virtual_file.name | ||
|
||
def tearDown(self): | ||
settings.GROUP_ALIASES_PATH = self.saved_draft_aliases_path | ||
settings.GROUP_VIRTUAL_PATH = self.saved_draft_virtual_path | ||
os.unlink(self.doc_aliases_file.name) | ||
os.unlink(self.doc_virtual_file.name) | ||
super().tearDown() | ||
|
||
def test_generator_class(self): | ||
"""The GroupAliasGenerator should generate the same lists as the old mgmt cmd""" | ||
# clean out test fixture group roles we don't need for this test | ||
|
@@ -208,6 +188,66 @@ def test_generator_class(self): | |
{k: (sorted(doms), sorted(addrs)) for k, (doms, addrs) in expected_dict.items()}, | ||
) | ||
|
||
@mock.patch("ietf.group.utils.GroupAliasGenerator") | ||
def test_get_group_email_aliases(self, mock_alias_gen_cls): | ||
GroupFactory(name="agroup", type_id="rg") | ||
GroupFactory(name="bgroup") | ||
GroupFactory(name="cgroup", type_id="rg") | ||
GroupFactory(name="dgroup") | ||
|
||
mock_alias_gen_cls.return_value = [ | ||
("bgroup-chairs", ["ietf"], ["[email protected]", "[email protected]"]), | ||
("agroup-ads", ["ietf", "irtf"], ["[email protected]"]), | ||
("bgroup-ads", ["ietf"], ["[email protected]"]), | ||
] | ||
# order is important - should be by acronym, otherwise left in order returned by generator | ||
self.assertEqual( | ||
get_group_email_aliases(None, None), | ||
[ | ||
{ | ||
"acronym": "agroup", | ||
"alias_type": "-ads", | ||
"expansion": "[email protected]", | ||
}, | ||
{ | ||
"acronym": "bgroup", | ||
"alias_type": "-chairs", | ||
"expansion": "[email protected], [email protected]", | ||
}, | ||
{ | ||
"acronym": "bgroup", | ||
"alias_type": "-ads", | ||
"expansion": "[email protected]", | ||
}, | ||
], | ||
) | ||
self.assertQuerySetEqual( | ||
mock_alias_gen_cls.call_args[0][0], | ||
Group.objects.all(), | ||
ordered=False, | ||
) | ||
|
||
# test other parameter combinations but we already checked that the alias generator's | ||
# output will be passed through, so don't re-test the processing | ||
get_group_email_aliases("agroup", None) | ||
self.assertQuerySetEqual( | ||
mock_alias_gen_cls.call_args[0][0], | ||
Group.objects.filter(acronym="agroup"), | ||
ordered=False, | ||
) | ||
get_group_email_aliases(None, "wg") | ||
self.assertQuerySetEqual( | ||
mock_alias_gen_cls.call_args[0][0], | ||
Group.objects.filter(type_id="wg"), | ||
ordered=False, | ||
) | ||
get_group_email_aliases("agroup", "wg") | ||
self.assertQuerySetEqual( | ||
mock_alias_gen_cls.call_args[0][0], | ||
Group.objects.none(), | ||
ordered=False, | ||
) | ||
|
||
|
||
class GroupRoleEmailTests(TestCase): | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,15 @@ | |
# -*- coding: utf-8 -*- | ||
|
||
|
||
import os | ||
import calendar | ||
import datetime | ||
import io | ||
import bleach | ||
import mock | ||
|
||
from unittest.mock import call, patch | ||
from pathlib import Path | ||
from pyquery import PyQuery | ||
from tempfile import NamedTemporaryFile | ||
|
||
import debug # pyflakes:ignore | ||
|
||
|
@@ -1925,58 +1924,72 @@ def setUp(self): | |
PersonFactory(user__username='plain') | ||
GroupFactory(acronym='mars',parent=GroupFactory(type_id='area')) | ||
GroupFactory(acronym='ames',parent=GroupFactory(type_id='area')) | ||
self.group_alias_file = NamedTemporaryFile(delete=False) | ||
self.group_alias_file.write(b"""# Generated by hand at 2015-02-12_16:30:52 | ||
virtual.ietf.org anything | ||
[email protected] xfilter-mars-ads | ||
[email protected] [email protected] | ||
[email protected] xfilter-mars-chairs | ||
[email protected] [email protected] | ||
[email protected] xfilter-mars-ads | ||
[email protected] [email protected] | ||
[email protected] xfilter-mars-chairs | ||
[email protected] [email protected] | ||
""") | ||
self.group_alias_file.close() | ||
self.saved_group_virtual_path = settings.GROUP_VIRTUAL_PATH | ||
settings.GROUP_VIRTUAL_PATH = self.group_alias_file.name | ||
|
||
def tearDown(self): | ||
settings.GROUP_VIRTUAL_PATH = self.saved_group_virtual_path | ||
os.unlink(self.group_alias_file.name) | ||
super().tearDown() | ||
|
||
def testAliases(self): | ||
|
||
@mock.patch("ietf.group.views.get_group_email_aliases") | ||
def testAliases(self, mock_get_aliases): | ||
url = urlreverse('ietf.group.urls_info_details.redirect.email', kwargs=dict(acronym="mars")) | ||
r = self.client.get(url) | ||
self.assertEqual(r.status_code, 302) | ||
|
||
mock_get_aliases.return_value = [ | ||
{"acronym": "mars", "alias_type": "-ads", "expansion": "[email protected]"}, | ||
{"acronym": "mars", "alias_type": "-chairs", "expansion": "[email protected]"}, | ||
] | ||
for testdict in [dict(acronym="mars"),dict(acronym="mars",group_type="wg")]: | ||
url = urlreverse('ietf.group.urls_info_details.redirect.email', kwargs=testdict) | ||
r = self.client.get(url,follow=True) | ||
self.assertEqual( | ||
mock_get_aliases.call_args, | ||
mock.call(testdict.get("acronym", None), testdict.get("group_type", None)), | ||
) | ||
self.assertTrue(all([x in unicontent(r) for x in ['mars-ads@','mars-chairs@']])) | ||
self.assertFalse(any([x in unicontent(r) for x in ['ames-ads@','ames-chairs@']])) | ||
|
||
url = urlreverse('ietf.group.views.email_aliases', kwargs=dict()) | ||
login_testing_unauthorized(self, "plain", url) | ||
|
||
mock_get_aliases.return_value = [ | ||
{"acronym": "mars", "alias_type": "-ads", "expansion": "[email protected]"}, | ||
{"acronym": "mars", "alias_type": "-chairs", "expansion": "[email protected]"}, | ||
{"acronym": "ames", "alias_type": "-ads", "expansion": "[email protected]"}, | ||
{"acronym": "ames", "alias_type": "-chairs", "expansion": "[email protected]"}, | ||
] | ||
r = self.client.get(url) | ||
self.assertTrue(r.status_code,200) | ||
self.assertEqual(mock_get_aliases.call_args, mock.call(None, None)) | ||
self.assertTrue(all([x in unicontent(r) for x in ['mars-ads@','mars-chairs@','ames-ads@','ames-chairs@']])) | ||
|
||
url = urlreverse('ietf.group.views.email_aliases', kwargs=dict(group_type="wg")) | ||
mock_get_aliases.return_value = [ | ||
{"acronym": "mars", "alias_type": "-ads", "expansion": "[email protected]"}, | ||
{"acronym": "mars", "alias_type": "-chairs", "expansion": "[email protected]"}, | ||
{"acronym": "ames", "alias_type": "-ads", "expansion": "[email protected]"}, | ||
{"acronym": "ames", "alias_type": "-chairs", "expansion": "[email protected]"}, | ||
] | ||
r = self.client.get(url) | ||
self.assertEqual(r.status_code,200) | ||
self.assertEqual(mock_get_aliases.call_args, mock.call(None, "wg")) | ||
self.assertContains(r, 'mars-ads@') | ||
|
||
url = urlreverse('ietf.group.views.email_aliases', kwargs=dict(group_type="rg")) | ||
mock_get_aliases.return_value = [] | ||
r = self.client.get(url) | ||
self.assertEqual(r.status_code,200) | ||
self.assertEqual(mock_get_aliases.call_args, mock.call(None, "rg")) | ||
self.assertNotContains(r, 'mars-ads@') | ||
|
||
def testExpansions(self): | ||
@mock.patch("ietf.group.views.get_group_email_aliases") | ||
def testExpansions(self, mock_get_aliases): | ||
mock_get_aliases.return_value = [ | ||
{"acronym": "mars", "alias_type": "-ads", "expansion": "[email protected]"}, | ||
{"acronym": "mars", "alias_type": "-chairs", "expansion": "[email protected]"}, | ||
{"acronym": "ames", "alias_type": "-ads", "expansion": "[email protected]"}, | ||
{"acronym": "ames", "alias_type": "-chairs", "expansion": "[email protected]"}, | ||
] | ||
url = urlreverse('ietf.group.views.email', kwargs=dict(acronym="mars")) | ||
r = self.client.get(url) | ||
self.assertEqual(r.status_code,200) | ||
self.assertEqual(mock_get_aliases.call_args, mock.call("mars", None)) | ||
self.assertContains(r, 'Email aliases') | ||
self.assertContains(r, '[email protected]') | ||
self.assertContains(r, 'group_personnel_change') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.