Checkpointing.
[mailman.git] / src / mailman / app / tests / test_templates.py
blob7aab8f1f9660fd67483072e522d051816a9b3d0c
1 # Copyright (C) 2012-2015 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Test the template downloader API."""
20 __all__ = [
21 'TestTemplateLoader',
25 import os
26 import shutil
27 import tempfile
28 import unittest
30 from mailman.app.lifecycle import create_list
31 from mailman.config import config
32 from mailman.interfaces.languages import ILanguageManager
33 from mailman.interfaces.templates import ITemplateLoader
34 from mailman.testing.layers import ConfigLayer
35 from urllib.error import URLError
36 from zope.component import getUtility
40 class TestTemplateLoader(unittest.TestCase):
41 """Test the template downloader API."""
43 layer = ConfigLayer
45 def setUp(self):
46 self.var_dir = tempfile.mkdtemp()
47 config.push('template config', """\
48 [paths.testing]
49 var_dir: {0}
50 """.format(self.var_dir))
51 # Put a demo template in the site directory.
52 path = os.path.join(self.var_dir, 'templates', 'site', 'en')
53 os.makedirs(path)
54 with open(os.path.join(path, 'demo.txt'), 'w') as fp:
55 print('Test content', end='', file=fp)
56 self._loader = getUtility(ITemplateLoader)
57 getUtility(ILanguageManager).add('it', 'utf-8', 'Italian')
58 self._mlist = create_list('test@example.com')
60 def tearDown(self):
61 config.pop('template config')
62 shutil.rmtree(self.var_dir)
64 def test_mailman_internal_uris(self):
65 # mailman://demo.txt
66 content = self._loader.get('mailman:///demo.txt')
67 self.assertEqual(content, 'Test content')
69 def test_mailman_internal_uris_twice(self):
70 # mailman:///demo.txt
71 content = self._loader.get('mailman:///demo.txt')
72 self.assertEqual(content, 'Test content')
73 content = self._loader.get('mailman:///demo.txt')
74 self.assertEqual(content, 'Test content')
76 def test_mailman_uri_with_language(self):
77 content = self._loader.get('mailman:///en/demo.txt')
78 self.assertEqual(content, 'Test content')
80 def test_mailman_uri_with_english_fallback(self):
81 content = self._loader.get('mailman:///it/demo.txt')
82 self.assertEqual(content, 'Test content')
84 def test_mailman_uri_with_list_name(self):
85 content = self._loader.get('mailman:///test@example.com/demo.txt')
86 self.assertEqual(content, 'Test content')
88 def test_mailman_full_uri(self):
89 content = self._loader.get('mailman:///test@example.com/en/demo.txt')
90 self.assertEqual(content, 'Test content')
92 def test_mailman_full_uri_with_english_fallback(self):
93 content = self._loader.get('mailman:///test@example.com/it/demo.txt')
94 self.assertEqual(content, 'Test content')
96 def test_uri_not_found(self):
97 with self.assertRaises(URLError) as cm:
98 self._loader.get('mailman:///missing.txt')
99 self.assertEqual(cm.exception.reason, 'No such file')
101 def test_shorter_url_error(self):
102 with self.assertRaises(URLError) as cm:
103 self._loader.get('mailman:///')
104 self.assertEqual(cm.exception.reason, 'No template specified')
106 def test_short_url_error(self):
107 with self.assertRaises(URLError) as cm:
108 self._loader.get('mailman://')
109 self.assertEqual(cm.exception.reason, 'No template specified')
111 def test_bad_language(self):
112 with self.assertRaises(URLError) as cm:
113 self._loader.get('mailman:///xx/demo.txt')
114 self.assertEqual(cm.exception.reason, 'Bad language or list name')
116 def test_bad_mailing_list(self):
117 with self.assertRaises(URLError) as cm:
118 self._loader.get('mailman:///missing@example.com/demo.txt')
119 self.assertEqual(cm.exception.reason, 'Bad language or list name')
121 def test_too_many_path_components(self):
122 with self.assertRaises(URLError) as cm:
123 self._loader.get('mailman:///missing@example.com/en/foo/demo.txt')
124 self.assertEqual(cm.exception.reason, 'No such file')
126 def test_non_ascii(self):
127 # mailman://demo.txt with non-ascii content.
128 test_text = b'\xe4\xb8\xad'
129 path = os.path.join(self.var_dir, 'templates', 'site', 'it')
130 os.makedirs(path)
131 with open(os.path.join(path, 'demo.txt'), 'wb') as fp:
132 fp.write(test_text)
133 content = self._loader.get('mailman:///it/demo.txt')
134 self.assertIsInstance(content, str)
135 self.assertEqual(content, test_text.decode('utf-8'))