author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 02 Sep 2009 10:22:47 +0200 | |
changeset 3083 | 3084bc9ccc64 |
parent 2773 | b2530e3e0afb |
child 3163 | edfe43ceaa35 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
"""unit tests for module cubicweb.common.mail |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
8 |
""" |
0 | 9 |
|
10 |
import os |
|
11 |
import pwd |
|
12 |
||
13 |
from logilab.common.testlib import unittest_main |
|
14 |
from logilab.common.umessage import message_from_string |
|
15 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
16 |
from cubicweb.devtools.testlib import CubicWebTC |
0 | 17 |
from cubicweb.common.mail import format_mail |
18 |
||
19 |
||
20 |
def getlogin(): |
|
21 |
"""avoid usinng os.getlogin() because of strange tty / stdin problems |
|
22 |
(man 3 getlogin) |
|
23 |
Another solution would be to use $LOGNAME, $USER or $USERNAME |
|
24 |
""" |
|
25 |
return pwd.getpwuid(os.getuid())[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
26 |
|
0 | 27 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
28 |
class EmailTC(CubicWebTC): |
0 | 29 |
|
30 |
def test_format_mail(self): |
|
31 |
self.set_option('sender-addr', 'bim@boum.fr') |
|
32 |
self.set_option('sender-name', 'BimBam') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
33 |
|
0 | 34 |
mail = format_mail({'name': 'oim', 'email': 'oim@logilab.fr'}, |
35 |
['test@logilab.fr'], u'un petit cöucou', u'bïjour', |
|
36 |
config=self.config) |
|
37 |
self.assertLinesEquals(mail.as_string(), """\ |
|
38 |
MIME-Version: 1.0 |
|
39 |
Content-Type: text/plain; charset="utf-8" |
|
40 |
Content-Transfer-Encoding: base64 |
|
41 |
Subject: =?utf-8?q?b=C3=AFjour?= |
|
42 |
From: =?utf-8?q?oim?= <oim@logilab.fr> |
|
43 |
Reply-to: =?utf-8?q?oim?= <oim@logilab.fr>, =?utf-8?q?BimBam?= <bim@boum.fr> |
|
44 |
X-CW: data |
|
45 |
To: test@logilab.fr |
|
46 |
||
47 |
dW4gcGV0aXQgY8O2dWNvdQ== |
|
48 |
""") |
|
49 |
msg = message_from_string(mail.as_string()) |
|
50 |
self.assertEquals(msg.get('subject'), u'bïjour') |
|
51 |
self.assertEquals(msg.get('from'), u'oim <oim@logilab.fr>') |
|
52 |
self.assertEquals(msg.get('to'), u'test@logilab.fr') |
|
53 |
self.assertEquals(msg.get('reply-to'), u'oim <oim@logilab.fr>, BimBam <bim@boum.fr>') |
|
54 |
self.assertEquals(msg.get_payload(decode=True), u'un petit cöucou') |
|
55 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
56 |
|
0 | 57 |
def test_format_mail_euro(self): |
58 |
mail = format_mail({'name': u'oîm', 'email': u'oim@logilab.fr'}, |
|
59 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €') |
|
60 |
self.assertLinesEquals(mail.as_string(), """\ |
|
61 |
MIME-Version: 1.0 |
|
62 |
Content-Type: text/plain; charset="utf-8" |
|
63 |
Content-Transfer-Encoding: base64 |
|
64 |
Subject: =?utf-8?b?YsOvam91ciDigqw=?= |
|
65 |
From: =?utf-8?q?o=C3=AEm?= <oim@logilab.fr> |
|
66 |
Reply-to: =?utf-8?q?o=C3=AEm?= <oim@logilab.fr> |
|
67 |
To: test@logilab.fr |
|
68 |
||
69 |
dW4gcGV0aXQgY8O2dWNvdSDigqw= |
|
70 |
""") |
|
71 |
msg = message_from_string(mail.as_string()) |
|
72 |
self.assertEquals(msg.get('subject'), u'bïjour €') |
|
73 |
self.assertEquals(msg.get('from'), u'oîm <oim@logilab.fr>') |
|
74 |
self.assertEquals(msg.get('to'), u'test@logilab.fr') |
|
75 |
self.assertEquals(msg.get('reply-to'), u'oîm <oim@logilab.fr>') |
|
76 |
self.assertEquals(msg.get_payload(decode=True), u'un petit cöucou €') |
|
77 |
||
78 |
||
79 |
def test_format_mail_from_reply_to(self): |
|
80 |
# no sender-name, sender-addr in the configuration |
|
81 |
self.set_option('sender-name', '') |
|
82 |
self.set_option('sender-addr', '') |
|
83 |
msg = format_mail({'name': u'', 'email': u''}, |
|
84 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
85 |
config=self.config) |
|
86 |
self.assertEquals(msg.get('from'), u'') |
|
87 |
self.assertEquals(msg.get('reply-to'), None) |
|
88 |
msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'}, |
|
89 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
90 |
config=self.config) |
|
91 |
msg = message_from_string(msg.as_string()) |
|
92 |
self.assertEquals(msg.get('from'), u'tutu <tutu@logilab.fr>') |
|
93 |
self.assertEquals(msg.get('reply-to'), u'tutu <tutu@logilab.fr>') |
|
94 |
msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'}, |
|
95 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €') |
|
96 |
msg = message_from_string(msg.as_string()) |
|
97 |
self.assertEquals(msg.get('from'), u'tutu <tutu@logilab.fr>') |
|
98 |
self.assertEquals(msg.get('reply-to'), u'tutu <tutu@logilab.fr>') |
|
99 |
# set sender name and address as expected |
|
100 |
self.set_option('sender-name', 'cubicweb-test') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
101 |
self.set_option('sender-addr', 'cubicweb-test@logilab.fr') |
0 | 102 |
# anonymous notification: no name and no email specified |
103 |
msg = format_mail({'name': u'', 'email': u''}, |
|
104 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
105 |
config=self.config) |
|
106 |
msg = message_from_string(msg.as_string()) |
|
107 |
self.assertEquals(msg.get('from'), u'cubicweb-test <cubicweb-test@logilab.fr>') |
|
108 |
self.assertEquals(msg.get('reply-to'), u'cubicweb-test <cubicweb-test@logilab.fr>') |
|
109 |
# anonymous notification: only email specified |
|
110 |
msg = format_mail({'email': u'tutu@logilab.fr'}, |
|
111 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
112 |
config=self.config) |
|
113 |
msg = message_from_string(msg.as_string()) |
|
114 |
self.assertEquals(msg.get('from'), u'cubicweb-test <tutu@logilab.fr>') |
|
115 |
self.assertEquals(msg.get('reply-to'), u'cubicweb-test <tutu@logilab.fr>, cubicweb-test <cubicweb-test@logilab.fr>') |
|
116 |
# anonymous notification: only name specified |
|
117 |
msg = format_mail({'name': u'tutu'}, |
|
118 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
119 |
config=self.config) |
|
120 |
msg = message_from_string(msg.as_string()) |
|
121 |
self.assertEquals(msg.get('from'), u'tutu <cubicweb-test@logilab.fr>') |
|
122 |
self.assertEquals(msg.get('reply-to'), u'tutu <cubicweb-test@logilab.fr>') |
|
123 |
||
124 |
||
125 |
||
126 |
if __name__ == '__main__': |
|
127 |
unittest_main() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
128 |