~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_mail_client.py

  • Committer: Ian Clatworthy
  • Date: 2009-09-09 11:43:10 UTC
  • mto: (4634.37.2 prepare-2.0)
  • mto: This revision was merged to the branch mainline in revision 4689.
  • Revision ID: ian.clatworthy@canonical.com-20090909114310-glw7tv76i5gnx9pt
put rules back in Makefile supporting plain-style docs

Show diffs side-by-side

added added

removed removed

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