summaryrefslogtreecommitdiff
path: root/src/mailman/app/tests/test_templates.py
blob: 33b728325f48263a26b4f8b8498aa7644b0cffaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright (C) 2012-2015 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Test the template downloader API."""

__all__ = [
    'TestTemplateLoader',
    ]


import os
import shutil
import tempfile
import unittest

from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.interfaces.templates import ITemplateLoader
from mailman.testing.layers import ConfigLayer
from urllib.error import URLError
from zope.component import getUtility



class TestTemplateLoader(unittest.TestCase):
    """Test the template downloader API."""

    layer = ConfigLayer

    def setUp(self):
        self.var_dir = tempfile.mkdtemp()
        config.push('template config', """\
        [paths.testing]
        var_dir: {0}
        """.format(self.var_dir))
        # Put a demo template in the site directory.
        path = os.path.join(self.var_dir, 'templates', 'site', 'en')
        os.makedirs(path)
        with open(os.path.join(path, 'demo.txt'), 'w') as fp:
            print('Test content', end='', file=fp)
        self._loader = getUtility(ITemplateLoader)
        self._mlist = create_list('test@example.com')

    def tearDown(self):
        config.pop('template config')
        shutil.rmtree(self.var_dir)

    def test_mailman_internal_uris(self):
        # mailman://demo.txt
        content = self._loader.get('mailman:///demo.txt')
        self.assertEqual(content, 'Test content')

    def test_mailman_internal_uris_twice(self):
        # mailman:///demo.txt
        content = self._loader.get('mailman:///demo.txt')
        self.assertEqual(content, 'Test content')
        content = self._loader.get('mailman:///demo.txt')
        self.assertEqual(content, 'Test content')

    def test_mailman_uri_with_language(self):
        content = self._loader.get('mailman:///en/demo.txt')
        self.assertEqual(content, 'Test content')

    def test_mailman_uri_with_english_fallback(self):
        content = self._loader.get('mailman:///it/demo.txt')
        self.assertEqual(content, 'Test content')

    def test_mailman_uri_with_list_name(self):
        content = self._loader.get('mailman:///test@example.com/demo.txt')
        self.assertEqual(content, 'Test content')

    def test_mailman_full_uri(self):
        content = self._loader.get('mailman:///test@example.com/en/demo.txt')
        self.assertEqual(content, 'Test content')

    def test_mailman_full_uri_with_english_fallback(self):
        content = self._loader.get('mailman:///test@example.com/it/demo.txt')
        self.assertEqual(content, 'Test content')

    def test_uri_not_found(self):
        with self.assertRaises(URLError) as cm:
            self._loader.get('mailman:///missing.txt')
        self.assertEqual(cm.exception.reason, 'No such file')

    def test_shorter_url_error(self):
        with self.assertRaises(URLError) as cm:
            self._loader.get('mailman:///')
        self.assertEqual(cm.exception.reason, 'No template specified')

    def test_short_url_error(self):
        with self.assertRaises(URLError) as cm:
            self._loader.get('mailman://')
        self.assertEqual(cm.exception.reason, 'No template specified')

    def test_bad_language(self):
        with self.assertRaises(URLError) as cm:
            self._loader.get('mailman:///xx/demo.txt')
        self.assertEqual(cm.exception.reason, 'Bad language or list name')

    def test_bad_mailing_list(self):
        with self.assertRaises(URLError) as cm:
            self._loader.get('mailman:///missing@example.com/demo.txt')
        self.assertEqual(cm.exception.reason, 'Bad language or list name')

    def test_too_many_path_components(self):
        with self.assertRaises(URLError) as cm:
            self._loader.get('mailman:///missing@example.com/en/foo/demo.txt')
        self.assertEqual(cm.exception.reason, 'No such file')

    def test_non_ascii(self):
        # mailman://demo.txt with non-ascii content.
        test_text = b'\xe4\xb8\xad'
        path = os.path.join(self.var_dir, 'templates', 'site', 'it')
        os.makedirs(path)
        with open(os.path.join(path, 'demo.txt'), 'wb') as fp:
            fp.write(test_text)
        content = self._loader.get('mailman:///it/demo.txt')
        self.assertIsInstance(content, str)
        self.assertEqual(content, test_text.decode('utf-8'))