1616from pathlib import Path
1717from pyquery import PyQuery
1818from urllib .parse import urlparse , parse_qs
19- from tempfile import NamedTemporaryFile
2019from collections import defaultdict
2120from zoneinfo import ZoneInfo
2221
5150 DraftAliasGenerator ,
5251 generate_idnits2_rfc_status ,
5352 generate_idnits2_rfcs_obsoleted ,
53+ get_doc_email_aliases ,
5454)
5555from ietf .group .models import Group , Role
5656from ietf .group .factories import GroupFactory , RoleFactory
@@ -2169,24 +2169,6 @@ def test_references(self):
21692169 self .assertContains (r , doc1 .name )
21702170
21712171class GenerateDraftAliasesTests (TestCase ):
2172- def setUp (self ):
2173- super ().setUp ()
2174- self .doc_aliases_file = NamedTemporaryFile (delete = False , mode = "w+" )
2175- self .doc_aliases_file .close ()
2176- self .doc_virtual_file = NamedTemporaryFile (delete = False , mode = "w+" )
2177- self .doc_virtual_file .close ()
2178- self .saved_draft_aliases_path = settings .DRAFT_ALIASES_PATH
2179- self .saved_draft_virtual_path = settings .DRAFT_VIRTUAL_PATH
2180- settings .DRAFT_ALIASES_PATH = self .doc_aliases_file .name
2181- settings .DRAFT_VIRTUAL_PATH = self .doc_virtual_file .name
2182-
2183- def tearDown (self ):
2184- settings .DRAFT_ALIASES_PATH = self .saved_draft_aliases_path
2185- settings .DRAFT_VIRTUAL_PATH = self .saved_draft_virtual_path
2186- os .unlink (self .doc_aliases_file .name )
2187- os .unlink (self .doc_virtual_file .name )
2188- super ().tearDown ()
2189-
21902172 @override_settings (TOOLS_SERVER = "tools.example.org" , DRAFT_ALIAS_DOMAIN = "draft.example.org" )
21912173 def test_generator_class (self ):
21922174 """The DraftAliasGenerator should generate the same lists as the old mgmt cmd"""
@@ -2286,6 +2268,28 @@ def test_generator_class(self):
22862268 {k : sorted (v ) for k , v in expected_dict .items ()},
22872269 )
22882270
2271+ # check single name
2272+ output = [(alias , alist ) for alias , alist in DraftAliasGenerator (Document .objects .filter (name = doc1 .name ))]
2273+ alias_dict = dict (output )
2274+ self .assertEqual (len (alias_dict ), len (output )) # no duplicate aliases
2275+ expected_dict = {
2276+ doc1 .name : [author1 .email_address ()],
2277+ doc1 .name + ".ad" : [ad .email_address ()],
2278+ doc1 .name + ".authors" : [author1 .email_address ()],
2279+ doc1 .name + ".shepherd" : [shepherd .email_address ()],
2280+ doc1 .name
2281+ + ".all" : [
2282+ author1 .email_address (),
2283+ ad .email_address (),
2284+ shepherd .email_address (),
2285+ ],
2286+ }
2287+ # Sort lists for comparison
2288+ self .assertEqual (
2289+ {k : sorted (v ) for k , v in alias_dict .items ()},
2290+ {k : sorted (v ) for k , v in expected_dict .items ()},
2291+ )
2292+
22892293 @override_settings (TOOLS_SERVER = "tools.example.org" , DRAFT_ALIAS_DOMAIN = "draft.example.org" )
22902294 def test_get_draft_notify_emails (self ):
22912295 ad = PersonFactory ()
@@ -2336,37 +2340,20 @@ def setUp(self):
23362340 WgDraftFactory (name = 'draft-ietf-mars-test' ,group__acronym = 'mars' )
23372341 WgDraftFactory (name = 'draft-ietf-ames-test' ,group__acronym = 'ames' )
23382342 RoleFactory (group__type_id = 'review' , group__acronym = 'yangdoctors' , name_id = 'secr' )
2339- self .doc_alias_file = NamedTemporaryFile (delete = False , mode = 'w+' )
2340- self .doc_alias_file .write ("""# Generated by hand at 2015-02-12_16:26:45
2341- virtual.ietf.org anything
2342- draft-ietf-mars-test@ietf.org xfilter-draft-ietf-mars-test
2343- expand-draft-ietf-mars-test@virtual.ietf.org mars-author@example.com, mars-collaborator@example.com
2344- draft-ietf-mars-test.authors@ietf.org xfilter-draft-ietf-mars-test.authors
2345- expand-draft-ietf-mars-test.authors@virtual.ietf.org mars-author@example.mars, mars-collaborator@example.mars
2346- draft-ietf-mars-test.chairs@ietf.org xfilter-draft-ietf-mars-test.chairs
2347- expand-draft-ietf-mars-test.chairs@virtual.ietf.org mars-chair@example.mars
2348- draft-ietf-mars-test.all@ietf.org xfilter-draft-ietf-mars-test.all
2349- expand-draft-ietf-mars-test.all@virtual.ietf.org mars-author@example.mars, mars-collaborator@example.mars, mars-chair@example.mars
2350- draft-ietf-ames-test@ietf.org xfilter-draft-ietf-ames-test
2351- expand-draft-ietf-ames-test@virtual.ietf.org ames-author@example.com, ames-collaborator@example.com
2352- draft-ietf-ames-test.authors@ietf.org xfilter-draft-ietf-ames-test.authors
2353- expand-draft-ietf-ames-test.authors@virtual.ietf.org ames-author@example.ames, ames-collaborator@example.ames
2354- draft-ietf-ames-test.chairs@ietf.org xfilter-draft-ietf-ames-test.chairs
2355- expand-draft-ietf-ames-test.chairs@virtual.ietf.org ames-chair@example.ames
2356- draft-ietf-ames-test.all@ietf.org xfilter-draft-ietf-ames-test.all
2357- expand-draft-ietf-ames-test.all@virtual.ietf.org ames-author@example.ames, ames-collaborator@example.ames, ames-chair@example.ames
2358-
2359- """ )
2360- self .doc_alias_file .close ()
2361- self .saved_draft_virtual_path = settings .DRAFT_VIRTUAL_PATH
2362- settings .DRAFT_VIRTUAL_PATH = self .doc_alias_file .name
2363-
2364- def tearDown (self ):
2365- settings .DRAFT_VIRTUAL_PATH = self .saved_draft_virtual_path
2366- os .unlink (self .doc_alias_file .name )
2367- super ().tearDown ()
2368-
2369- def testAliases (self ):
2343+
2344+
2345+ @mock .patch ("ietf.doc.views_doc.get_doc_email_aliases" )
2346+ def testAliases (self , mock_get_aliases ):
2347+ mock_get_aliases .return_value = [
2348+ {"doc_name" : "draft-ietf-mars-test" , "alias_type" : "" , "expansion" : "mars-author@example.mars, mars-collaborator@example.mars" },
2349+ {"doc_name" : "draft-ietf-mars-test" , "alias_type" : ".authors" , "expansion" : "mars-author@example.mars, mars-collaborator@example.mars" },
2350+ {"doc_name" : "draft-ietf-mars-test" , "alias_type" : ".chairs" , "expansion" : "mars-chair@example.mars" },
2351+ {"doc_name" : "draft-ietf-mars-test" , "alias_type" : ".all" , "expansion" : "mars-author@example.mars, mars-collaborator@example.mars, mars-chair@example.mars" },
2352+ {"doc_name" : "draft-ietf-ames-test" , "alias_type" : "" , "expansion" : "ames-author@example.ames, ames-collaborator@example.ames" },
2353+ {"doc_name" : "draft-ietf-ames-test" , "alias_type" : ".authors" , "expansion" : "ames-author@example.ames, ames-collaborator@example.ames" },
2354+ {"doc_name" : "draft-ietf-ames-test" , "alias_type" : ".chairs" , "expansion" : "ames-chair@example.ames" },
2355+ {"doc_name" : "draft-ietf-ames-test" , "alias_type" : ".all" , "expansion" : "ames-author@example.ames, ames-collaborator@example.ames, ames-chair@example.ames" },
2356+ ]
23702357 PersonFactory (user__username = 'plain' )
23712358 url = urlreverse ('ietf.doc.urls.redirect.document_email' , kwargs = dict (name = "draft-ietf-mars-test" ))
23722359 r = self .client .get (url )
@@ -2376,16 +2363,70 @@ def testAliases(self):
23762363 login_testing_unauthorized (self , "plain" , url )
23772364 r = self .client .get (url )
23782365 self .assertEqual (r .status_code , 200 )
2366+ self .assertEqual (mock_get_aliases .call_args , mock .call ())
23792367 self .assertTrue (all ([x in unicontent (r ) for x in ['mars-test@' ,'mars-test.authors@' ,'mars-test.chairs@' ]]))
23802368 self .assertTrue (all ([x in unicontent (r ) for x in ['ames-test@' ,'ames-test.authors@' ,'ames-test.chairs@' ]]))
23812369
2382- def testExpansions (self ):
2370+
2371+ @mock .patch ("ietf.doc.views_doc.get_doc_email_aliases" )
2372+ def testExpansions (self , mock_get_aliases ):
2373+ mock_get_aliases .return_value = [
2374+ {"doc_name" : "draft-ietf-mars-test" , "alias_type" : "" , "expansion" : "mars-author@example.mars, mars-collaborator@example.mars" },
2375+ {"doc_name" : "draft-ietf-mars-test" , "alias_type" : ".authors" , "expansion" : "mars-author@example.mars, mars-collaborator@example.mars" },
2376+ {"doc_name" : "draft-ietf-mars-test" , "alias_type" : ".chairs" , "expansion" : "mars-chair@example.mars" },
2377+ {"doc_name" : "draft-ietf-mars-test" , "alias_type" : ".all" , "expansion" : "mars-author@example.mars, mars-collaborator@example.mars, mars-chair@example.mars" },
2378+ ]
23832379 url = urlreverse ('ietf.doc.views_doc.document_email' , kwargs = dict (name = "draft-ietf-mars-test" ))
23842380 r = self .client .get (url )
2381+ self .assertEqual (mock_get_aliases .call_args , mock .call ("draft-ietf-mars-test" ))
23852382 self .assertEqual (r .status_code , 200 )
23862383 self .assertContains (r , 'draft-ietf-mars-test.all@ietf.org' )
23872384 self .assertContains (r , 'iesg_ballot_saved' )
2385+
2386+ @mock .patch ("ietf.doc.utils.DraftAliasGenerator" )
2387+ def test_get_doc_email_aliases (self , mock_alias_gen_cls ):
2388+ mock_alias_gen_cls .return_value = [
2389+ ("draft-something-or-other.some-type" , ["somebody@example.com" ]),
2390+ ("draft-something-or-other" , ["somebody@example.com" ]),
2391+ ("draft-nothing-at-all" , ["nobody@example.com" ]),
2392+ ("draft-nothing-at-all.some-type" , ["nobody@example.com" ]),
2393+ ]
2394+ # order is important in the response - should be sorted by doc name and otherwise left
2395+ # in order
2396+ self .assertEqual (
2397+ get_doc_email_aliases (),
2398+ [
2399+ {
2400+ "doc_name" : "draft-nothing-at-all" ,
2401+ "alias_type" : "" ,
2402+ "expansion" : "nobody@example.com" ,
2403+ },
2404+ {
2405+ "doc_name" : "draft-nothing-at-all" ,
2406+ "alias_type" : ".some-type" ,
2407+ "expansion" : "nobody@example.com" ,
2408+ },
2409+ {
2410+ "doc_name" : "draft-something-or-other" ,
2411+ "alias_type" : ".some-type" ,
2412+ "expansion" : "somebody@example.com" ,
2413+ },
2414+ {
2415+ "doc_name" : "draft-something-or-other" ,
2416+ "alias_type" : "" ,
2417+ "expansion" : "somebody@example.com" ,
2418+ },
2419+ ],
2420+ )
2421+ self .assertEqual (mock_alias_gen_cls .call_args , mock .call (None ))
23882422
2423+ # Repeat with a name, no need to re-test that the alias list is actually passed through, just
2424+ # check that the DraftAliasGenerator is called correctly
2425+ draft = WgDraftFactory ()
2426+ get_doc_email_aliases (draft .name )
2427+ self .assertQuerySetEqual (mock_alias_gen_cls .call_args [0 ][0 ], Document .objects .filter (pk = draft .pk ))
2428+
2429+
23892430class DocumentMeetingTests (TestCase ):
23902431
23912432 def setUp (self ):
0 commit comments