/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_mail_client.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-26 19:15:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7055.
  • Revision ID: jelmer@jelmer.uk-20180726191527-wniq205k6tzfo1xx
Install fastimport from git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from .. import (
 
18
    errors,
 
19
    mail_client,
 
20
    tests,
 
21
    urlutils,
 
22
    osutils,
 
23
    )
 
24
 
 
25
 
 
26
class TestMutt(tests.TestCase):
 
27
 
 
28
    def test_commandline(self):
 
29
        mutt = mail_client.Mutt(None)
 
30
        commandline = mutt._get_compose_commandline(
 
31
            None, None, 'file%', body="hello")
 
32
        # The temporary filename is randomly generated, so it is not matched.
 
33
        self.assertEqual(['-a', 'file%', '-i'], commandline[:-1])
 
34
        commandline = mutt._get_compose_commandline('jrandom@example.org',
 
35
                                                     'Hi there!', None)
 
36
        self.assertEqual(['-s', 'Hi there!', '--', 'jrandom@example.org'],
 
37
                         commandline)
 
38
 
 
39
    def test_commandline_is_8bit(self):
 
40
        mutt = mail_client.Mutt(None)
 
41
        cmdline = mutt._get_compose_commandline(u'jrandom@example.org',
 
42
            u'Hi there!', u'file%')
 
43
        self.assertEqual(
 
44
            ['-s', 'Hi there!', '-a', 'file%', '--', 'jrandom@example.org'],
 
45
            cmdline)
 
46
        for item in cmdline:
 
47
            self.assertTrue(isinstance(item, str),
 
48
                'Command-line item %r is not a native string!' % item)
 
49
 
 
50
 
 
51
class TestThunderbird(tests.TestCase):
 
52
 
 
53
    def test_commandline(self):
 
54
        tbird = mail_client.Thunderbird(None)
 
55
        commandline = tbird._get_compose_commandline(None, None,
 
56
                                                     'file%')
 
57
        self.assertEqual(['-compose', "attachment='%s'" %
 
58
                          urlutils.local_path_to_url('file%')], commandline)
 
59
        commandline = tbird._get_compose_commandline('jrandom@example.org',
 
60
                                                     'Hi there!', None,
 
61
                                                     "bo'dy")
 
62
        self.assertEqual(['-compose', "body=bo%27dy,"
 
63
                                      "subject='Hi there!',"
 
64
                                      "to='jrandom@example.org'"],
 
65
                                      commandline)
 
66
 
 
67
    def test_commandline_is_8bit(self):
 
68
        # test for bug #139318
 
69
        tbird = mail_client.Thunderbird(None)
 
70
        cmdline = tbird._get_compose_commandline(u'jrandom@example.org',
 
71
            u'Hi there!', u'file%')
 
72
        self.assertEqual(['-compose',
 
73
            ("attachment='%s'," % urlutils.local_path_to_url('file%')) +
 
74
            "subject='Hi there!',to='jrandom@example.org'",
 
75
            ], cmdline)
 
76
        for item in cmdline:
 
77
            self.assertTrue(isinstance(item, str),
 
78
                'Command-line item %r is not a native string!' % item)
 
79
 
 
80
 
 
81
class TestEmacsMail(tests.TestCase):
 
82
 
 
83
    def test_commandline(self):
 
84
        eclient = mail_client.EmacsMail(None)
 
85
 
 
86
        commandline = eclient._get_compose_commandline(None, 'Hi there!', None)
 
87
        self.assertEqual(['--eval', '(compose-mail nil "Hi there!")'],
 
88
                         commandline)
 
89
 
 
90
        commandline = eclient._get_compose_commandline('jrandom@example.org',
 
91
                                                       'Hi there!', None)
 
92
        self.assertEqual(['--eval',
 
93
                          '(compose-mail "jrandom@example.org" "Hi there!")'],
 
94
                         commandline)
 
95
 
 
96
        # We won't be able to know the temporary file name at this stage
 
97
        # so we can't raise an assertion with assertEqual
 
98
        cmdline = eclient._get_compose_commandline(None, None, 'file%')
 
99
        if eclient.elisp_tmp_file is not None:
 
100
            self.addCleanup(osutils.delete_any, eclient.elisp_tmp_file)
 
101
        commandline = ' '.join(cmdline)
 
102
        self.assertContainsRe(commandline, '--eval')
 
103
        self.assertContainsRe(commandline, '(compose-mail nil nil)')
 
104
        self.assertContainsRe(commandline, '(load .*)')
 
105
        self.assertContainsRe(commandline, '(bzr-add-mime-att \"file%\")')
 
106
 
 
107
    def test_commandline_is_8bit(self):
 
108
        eclient = mail_client.EmacsMail(None)
 
109
        commandline = eclient._get_compose_commandline(u'jrandom@example.org',
 
110
            u'Hi there!', u'file%')
 
111
        if eclient.elisp_tmp_file is not None:
 
112
            self.addCleanup(osutils.delete_any, eclient.elisp_tmp_file)
 
113
        for item in commandline:
 
114
            self.assertTrue(isinstance(item, str),
 
115
                'Command-line item %r is not a native string!' % item)
 
116
 
 
117
 
 
118
class TestXDGEmail(tests.TestCase):
 
119
 
 
120
    def test_commandline(self):
 
121
        xdg_email = mail_client.XDGEmail(None)
 
122
        self.assertRaises(mail_client.NoMailAddressSpecified,
 
123
                          xdg_email._get_compose_commandline,
 
124
                          None, None, 'file%')
 
125
        commandline = xdg_email._get_compose_commandline(
 
126
            'jrandom@example.org', None, 'file%')
 
127
        self.assertEqual(['jrandom@example.org', '--attach', 'file%'],
 
128
                         commandline)
 
129
        commandline = xdg_email._get_compose_commandline(
 
130
            'jrandom@example.org', 'Hi there!', None, "bo'dy")
 
131
        self.assertEqual(['jrandom@example.org', '--subject', 'Hi there!',
 
132
                          '--body', "bo'dy"], commandline)
 
133
 
 
134
    def test_commandline_is_8bit(self):
 
135
        xdg_email = mail_client.XDGEmail(None)
 
136
        cmdline = xdg_email._get_compose_commandline(u'jrandom@example.org',
 
137
            u'Hi there!', u'file%')
 
138
        self.assertEqual(
 
139
            ['jrandom@example.org', '--subject', 'Hi there!',
 
140
             '--attach', 'file%'],
 
141
            cmdline)
 
142
        for item in cmdline:
 
143
            self.assertTrue(isinstance(item, str),
 
144
                'Command-line item %r is not a native string!' % item)
 
145
 
 
146
 
 
147
class TestEvolution(tests.TestCase):
 
148
 
 
149
    def test_commandline(self):
 
150
        evo = mail_client.Evolution(None)
 
151
        commandline = evo._get_compose_commandline(None, None, 'file%')
 
152
        self.assertEqual(['mailto:?attach=file%25'], commandline)
 
153
        commandline = evo._get_compose_commandline('jrandom@example.org',
 
154
                                                   'Hi there!', None, 'bo&dy')
 
155
        self.assertEqual(['mailto:jrandom@example.org?body=bo%26dy&'
 
156
                          'subject=Hi%20there%21'], commandline)
 
157
 
 
158
    def test_commandline_is_8bit(self):
 
159
        evo = mail_client.Evolution(None)
 
160
        cmdline = evo._get_compose_commandline(u'jrandom@example.org',
 
161
            u'Hi there!', u'file%')
 
162
        self.assertEqual(
 
163
            ['mailto:jrandom@example.org?attach=file%25&subject=Hi%20there%21'
 
164
            ],
 
165
            cmdline)
 
166
        for item in cmdline:
 
167
            self.assertTrue(isinstance(item, str),
 
168
                'Command-line item %r is not a native string!' % item)
 
169
 
 
170
 
 
171
class TestKMail(tests.TestCase):
 
172
 
 
173
    def test_commandline(self):
 
174
        kmail = mail_client.KMail(None)
 
175
        commandline = kmail._get_compose_commandline(None, None, 'file%')
 
176
        self.assertEqual(['--attach', 'file%'], commandline)
 
177
        commandline = kmail._get_compose_commandline('jrandom@example.org',
 
178
                                                     'Hi there!', None)
 
179
        self.assertEqual(['-s', 'Hi there!', 'jrandom@example.org'],
 
180
                         commandline)
 
181
 
 
182
    def test_commandline_is_8bit(self):
 
183
        kmail = mail_client.KMail(None)
 
184
        cmdline = kmail._get_compose_commandline(u'jrandom@example.org',
 
185
            u'Hi there!', u'file%')
 
186
        self.assertEqual(
 
187
            ['-s', 'Hi there!', '--attach', 'file%', 'jrandom@example.org'],
 
188
            cmdline)
 
189
        for item in cmdline:
 
190
            self.assertTrue(isinstance(item, str),
 
191
                'Command-line item %r is not a native string!' % item)
 
192
 
 
193
 
 
194
class TestClaws(tests.TestCase):
 
195
 
 
196
    def test_commandline(self):
 
197
        claws = mail_client.Claws(None)
 
198
        commandline = claws._get_compose_commandline(
 
199
            'jrandom@example.org', None, 'file%')
 
200
        self.assertEqual(
 
201
            ['--compose', 'mailto:jrandom@example.org?', '--attach', 'file%'],
 
202
            commandline)
 
203
        commandline = claws._get_compose_commandline(
 
204
            'jrandom@example.org', 'Hi there!', None)
 
205
        self.assertEqual(
 
206
            ['--compose',
 
207
             'mailto:jrandom@example.org?subject=Hi%20there%21'],
 
208
            commandline)
 
209
 
 
210
    def test_commandline_is_8bit(self):
 
211
        claws = mail_client.Claws(None)
 
212
        cmdline = claws._get_compose_commandline(
 
213
            u'jrandom@example.org', u'\xb5cosm of fun!', u'file%')
 
214
        subject_string = urlutils.quote(
 
215
            u'\xb5cosm of fun!'.encode(osutils.get_user_encoding(), 'replace'))
 
216
        self.assertEqual(
 
217
            ['--compose',
 
218
             'mailto:jrandom@example.org?subject=%s' % subject_string,
 
219
             '--attach',
 
220
             'file%'],
 
221
            cmdline)
 
222
        for item in cmdline:
 
223
            self.assertTrue(isinstance(item, str),
 
224
                'Command-line item %r is not a native string!' % item)
 
225
 
 
226
    def test_with_from(self):
 
227
        claws = mail_client.Claws(None)
 
228
        cmdline = claws._get_compose_commandline(
 
229
            u'jrandom@example.org', None, None, None, u'qrandom@example.com')
 
230
        self.assertEqual(
 
231
            ['--compose',
 
232
             'mailto:jrandom@example.org?from=qrandom%40example.com'],
 
233
            cmdline)
 
234
 
 
235
    def test_to_required(self):
 
236
        claws = mail_client.Claws(None)
 
237
        self.assertRaises(mail_client.NoMailAddressSpecified,
 
238
                          claws._get_compose_commandline,
 
239
                          None, None, 'file%')
 
240
 
 
241
    def test_with_body(self):
 
242
        claws = mail_client.Claws(None)
 
243
        cmdline = claws._get_compose_commandline(
 
244
            u'jrandom@example.org', None, None, 'This is some body text')
 
245
        self.assertEqual(
 
246
            ['--compose',
 
247
             'mailto:jrandom@example.org?body=This%20is%20some%20body%20text'],
 
248
            cmdline)
 
249
 
 
250
 
 
251
class TestEditor(tests.TestCase):
 
252
 
 
253
    def test_get_merge_prompt_unicode(self):
 
254
        """Prompt, to and subject are unicode, the attachement is binary"""
 
255
        editor = mail_client.Editor(None)
 
256
        prompt = editor._get_merge_prompt(u'foo\u1234',
 
257
                                        u'bar\u1234',
 
258
                                        u'baz\u1234',
 
259
                                        u'qux\u1234'.encode('utf-8'))
 
260
        self.assertContainsRe(prompt, u'foo\u1234(.|\n)*bar\u1234'
 
261
                              u'(.|\n)*baz\u1234(.|\n)*qux\u1234')
 
262
        editor._get_merge_prompt(u'foo', u'bar', u'baz', b'qux\xff')
 
263
 
 
264
 
 
265
class DummyMailClient(object):
 
266
 
 
267
    def compose_merge_request(self, *args, **kwargs):
 
268
        self.args = args
 
269
        self.kwargs = kwargs
 
270
 
 
271
 
 
272
class DefaultMailDummyClient(mail_client.DefaultMail):
 
273
 
 
274
    def __init__(self):
 
275
        self.client = DummyMailClient()
 
276
 
 
277
    def _mail_client(self):
 
278
        return self.client
 
279
 
 
280
 
 
281
class TestDefaultMail(tests.TestCase):
 
282
 
 
283
    def test_compose_merge_request(self):
 
284
        client = DefaultMailDummyClient()
 
285
        to = "a@b.com"
 
286
        subject = "[MERGE]"
 
287
        directive = "directive",
 
288
        basename = "merge"
 
289
        client.compose_merge_request(to, subject, directive,
 
290
                                     basename=basename)
 
291
        dummy_client = client.client
 
292
        self.assertEqual(dummy_client.args, (to, subject, directive))
 
293
        self.assertEqual(dummy_client.kwargs,
 
294
                         {"basename": basename, 'body': None})