|
1 # -*- coding: utf-8 -*- |
|
2 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
4 # |
|
5 # This file is part of CubicWeb. |
|
6 # |
|
7 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
8 # terms of the GNU Lesser General Public License as published by the Free |
|
9 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
10 # any later version. |
|
11 # |
|
12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
15 # details. |
|
16 # |
|
17 # You should have received a copy of the GNU Lesser General Public License along |
|
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
19 """unit tests for module cubicweb.mail |
|
20 |
|
21 """ |
|
22 |
|
23 import os |
|
24 import re |
|
25 import sys |
|
26 |
|
27 from logilab.common.testlib import unittest_main |
|
28 from logilab.common.umessage import message_from_string |
|
29 |
|
30 from cubicweb.devtools.testlib import CubicWebTC |
|
31 from cubicweb.mail import format_mail |
|
32 |
|
33 |
|
34 def getlogin(): |
|
35 """avoid using os.getlogin() because of strange tty / stdin problems |
|
36 (man 3 getlogin) |
|
37 Another solution would be to use $LOGNAME, $USER or $USERNAME |
|
38 """ |
|
39 if sys.platform != 'win32': |
|
40 import pwd |
|
41 return pwd.getpwuid(os.getuid())[0] |
|
42 else: |
|
43 return os.environ.get('USERNAME') |
|
44 |
|
45 |
|
46 class EmailTC(CubicWebTC): |
|
47 |
|
48 def test_format_mail(self): |
|
49 self.set_option('sender-addr', 'bim@boum.fr') |
|
50 self.set_option('sender-name', 'BimBam') |
|
51 |
|
52 mail = format_mail({'name': 'oim', 'email': 'oim@logilab.fr'}, |
|
53 ['test@logilab.fr'], u'un petit cöucou', u'bïjour', |
|
54 config=self.config) |
|
55 result = mail.as_string() |
|
56 result = re.sub('^Date: .*$', 'Date: now', result, flags=re.MULTILINE) |
|
57 self.assertMultiLineEqual(result, """\ |
|
58 MIME-Version: 1.0 |
|
59 Content-Type: text/plain; charset="utf-8" |
|
60 Content-Transfer-Encoding: base64 |
|
61 Subject: =?utf-8?q?b=C3=AFjour?= |
|
62 From: =?utf-8?q?oim?= <oim@logilab.fr> |
|
63 Reply-to: =?utf-8?q?oim?= <oim@logilab.fr>, =?utf-8?q?BimBam?= <bim@boum.fr> |
|
64 X-CW: data |
|
65 To: test@logilab.fr |
|
66 Date: now |
|
67 |
|
68 dW4gcGV0aXQgY8O2dWNvdQ== |
|
69 """) |
|
70 msg = message_from_string(mail.as_string()) |
|
71 self.assertEqual(msg.get('subject'), u'bïjour') |
|
72 self.assertEqual(msg.get('from'), u'oim <oim@logilab.fr>') |
|
73 self.assertEqual(msg.get('to'), u'test@logilab.fr') |
|
74 self.assertEqual(msg.get('reply-to'), u'oim <oim@logilab.fr>, BimBam <bim@boum.fr>') |
|
75 self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou') |
|
76 |
|
77 |
|
78 def test_format_mail_euro(self): |
|
79 mail = format_mail({'name': u'oîm', 'email': u'oim@logilab.fr'}, |
|
80 ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €') |
|
81 result = mail.as_string() |
|
82 result = re.sub('^Date: .*$', 'Date: now', result, flags=re.MULTILINE) |
|
83 self.assertMultiLineEqual(result, """\ |
|
84 MIME-Version: 1.0 |
|
85 Content-Type: text/plain; charset="utf-8" |
|
86 Content-Transfer-Encoding: base64 |
|
87 Subject: =?utf-8?b?YsOvam91ciDigqw=?= |
|
88 From: =?utf-8?q?o=C3=AEm?= <oim@logilab.fr> |
|
89 Reply-to: =?utf-8?q?o=C3=AEm?= <oim@logilab.fr> |
|
90 To: test@logilab.fr |
|
91 Date: now |
|
92 |
|
93 dW4gcGV0aXQgY8O2dWNvdSDigqw= |
|
94 """) |
|
95 msg = message_from_string(mail.as_string()) |
|
96 self.assertEqual(msg.get('subject'), u'bïjour €') |
|
97 self.assertEqual(msg.get('from'), u'oîm <oim@logilab.fr>') |
|
98 self.assertEqual(msg.get('to'), u'test@logilab.fr') |
|
99 self.assertEqual(msg.get('reply-to'), u'oîm <oim@logilab.fr>') |
|
100 self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou €') |
|
101 |
|
102 |
|
103 def test_format_mail_from_reply_to(self): |
|
104 # no sender-name, sender-addr in the configuration |
|
105 self.set_option('sender-name', '') |
|
106 self.set_option('sender-addr', '') |
|
107 msg = format_mail({'name': u'', 'email': u''}, |
|
108 ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
109 config=self.config) |
|
110 self.assertEqual(msg.get('from'), u'') |
|
111 self.assertEqual(msg.get('reply-to'), None) |
|
112 msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'}, |
|
113 ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
114 config=self.config) |
|
115 msg = message_from_string(msg.as_string()) |
|
116 self.assertEqual(msg.get('from'), u'tutu <tutu@logilab.fr>') |
|
117 self.assertEqual(msg.get('reply-to'), u'tutu <tutu@logilab.fr>') |
|
118 msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'}, |
|
119 ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €') |
|
120 msg = message_from_string(msg.as_string()) |
|
121 self.assertEqual(msg.get('from'), u'tutu <tutu@logilab.fr>') |
|
122 self.assertEqual(msg.get('reply-to'), u'tutu <tutu@logilab.fr>') |
|
123 # set sender name and address as expected |
|
124 self.set_option('sender-name', 'cubicweb-test') |
|
125 self.set_option('sender-addr', 'cubicweb-test@logilab.fr') |
|
126 # anonymous notification: no name and no email specified |
|
127 msg = format_mail({'name': u'', 'email': u''}, |
|
128 ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
129 config=self.config) |
|
130 msg = message_from_string(msg.as_string()) |
|
131 self.assertEqual(msg.get('from'), u'cubicweb-test <cubicweb-test@logilab.fr>') |
|
132 self.assertEqual(msg.get('reply-to'), u'cubicweb-test <cubicweb-test@logilab.fr>') |
|
133 # anonymous notification: only email specified |
|
134 msg = format_mail({'email': u'tutu@logilab.fr'}, |
|
135 ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
136 config=self.config) |
|
137 msg = message_from_string(msg.as_string()) |
|
138 self.assertEqual(msg.get('from'), u'cubicweb-test <tutu@logilab.fr>') |
|
139 self.assertEqual(msg.get('reply-to'), u'cubicweb-test <tutu@logilab.fr>, cubicweb-test <cubicweb-test@logilab.fr>') |
|
140 # anonymous notification: only name specified |
|
141 msg = format_mail({'name': u'tutu'}, |
|
142 ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
143 config=self.config) |
|
144 msg = message_from_string(msg.as_string()) |
|
145 self.assertEqual(msg.get('from'), u'tutu <cubicweb-test@logilab.fr>') |
|
146 self.assertEqual(msg.get('reply-to'), u'tutu <cubicweb-test@logilab.fr>') |
|
147 |
|
148 |
|
149 |
|
150 if __name__ == '__main__': |
|
151 unittest_main() |