author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 09 Mar 2010 10:53:55 +0100 | |
branch | stable |
changeset 4840 | c02a340cd1da |
parent 4252 | 6c4f109c2b03 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
2 |
"""unit tests for module cubicweb.mail |
1977
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 |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3153
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1977
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 |
|
3153 | 11 |
import sys |
0 | 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 |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
17 |
from cubicweb.mail import format_mail |
0 | 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 |
""" |
|
3153 | 25 |
if sys.platform != 'win32': |
26 |
import pwd |
|
27 |
return pwd.getpwuid(os.getuid())[0] |
|
28 |
else: |
|
29 |
return os.environ.get('USERNAME') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
30 |
|
0 | 31 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
32 |
class EmailTC(CubicWebTC): |
0 | 33 |
|
34 |
def test_format_mail(self): |
|
35 |
self.set_option('sender-addr', 'bim@boum.fr') |
|
36 |
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
|
37 |
|
0 | 38 |
mail = format_mail({'name': 'oim', 'email': 'oim@logilab.fr'}, |
39 |
['test@logilab.fr'], u'un petit cöucou', u'bïjour', |
|
40 |
config=self.config) |
|
41 |
self.assertLinesEquals(mail.as_string(), """\ |
|
42 |
MIME-Version: 1.0 |
|
43 |
Content-Type: text/plain; charset="utf-8" |
|
44 |
Content-Transfer-Encoding: base64 |
|
45 |
Subject: =?utf-8?q?b=C3=AFjour?= |
|
46 |
From: =?utf-8?q?oim?= <oim@logilab.fr> |
|
47 |
Reply-to: =?utf-8?q?oim?= <oim@logilab.fr>, =?utf-8?q?BimBam?= <bim@boum.fr> |
|
48 |
X-CW: data |
|
49 |
To: test@logilab.fr |
|
50 |
||
51 |
dW4gcGV0aXQgY8O2dWNvdQ== |
|
52 |
""") |
|
53 |
msg = message_from_string(mail.as_string()) |
|
54 |
self.assertEquals(msg.get('subject'), u'bïjour') |
|
55 |
self.assertEquals(msg.get('from'), u'oim <oim@logilab.fr>') |
|
56 |
self.assertEquals(msg.get('to'), u'test@logilab.fr') |
|
57 |
self.assertEquals(msg.get('reply-to'), u'oim <oim@logilab.fr>, BimBam <bim@boum.fr>') |
|
58 |
self.assertEquals(msg.get_payload(decode=True), u'un petit cöucou') |
|
59 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
60 |
|
0 | 61 |
def test_format_mail_euro(self): |
62 |
mail = format_mail({'name': u'oîm', 'email': u'oim@logilab.fr'}, |
|
63 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €') |
|
64 |
self.assertLinesEquals(mail.as_string(), """\ |
|
65 |
MIME-Version: 1.0 |
|
66 |
Content-Type: text/plain; charset="utf-8" |
|
67 |
Content-Transfer-Encoding: base64 |
|
68 |
Subject: =?utf-8?b?YsOvam91ciDigqw=?= |
|
69 |
From: =?utf-8?q?o=C3=AEm?= <oim@logilab.fr> |
|
70 |
Reply-to: =?utf-8?q?o=C3=AEm?= <oim@logilab.fr> |
|
71 |
To: test@logilab.fr |
|
72 |
||
73 |
dW4gcGV0aXQgY8O2dWNvdSDigqw= |
|
74 |
""") |
|
75 |
msg = message_from_string(mail.as_string()) |
|
76 |
self.assertEquals(msg.get('subject'), u'bïjour €') |
|
77 |
self.assertEquals(msg.get('from'), u'oîm <oim@logilab.fr>') |
|
78 |
self.assertEquals(msg.get('to'), u'test@logilab.fr') |
|
79 |
self.assertEquals(msg.get('reply-to'), u'oîm <oim@logilab.fr>') |
|
80 |
self.assertEquals(msg.get_payload(decode=True), u'un petit cöucou €') |
|
81 |
||
82 |
||
83 |
def test_format_mail_from_reply_to(self): |
|
84 |
# no sender-name, sender-addr in the configuration |
|
85 |
self.set_option('sender-name', '') |
|
86 |
self.set_option('sender-addr', '') |
|
87 |
msg = format_mail({'name': u'', 'email': u''}, |
|
88 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
89 |
config=self.config) |
|
90 |
self.assertEquals(msg.get('from'), u'') |
|
91 |
self.assertEquals(msg.get('reply-to'), None) |
|
92 |
msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'}, |
|
93 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
94 |
config=self.config) |
|
95 |
msg = message_from_string(msg.as_string()) |
|
96 |
self.assertEquals(msg.get('from'), u'tutu <tutu@logilab.fr>') |
|
97 |
self.assertEquals(msg.get('reply-to'), u'tutu <tutu@logilab.fr>') |
|
98 |
msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'}, |
|
99 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €') |
|
100 |
msg = message_from_string(msg.as_string()) |
|
101 |
self.assertEquals(msg.get('from'), u'tutu <tutu@logilab.fr>') |
|
102 |
self.assertEquals(msg.get('reply-to'), u'tutu <tutu@logilab.fr>') |
|
103 |
# set sender name and address as expected |
|
104 |
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
|
105 |
self.set_option('sender-addr', 'cubicweb-test@logilab.fr') |
0 | 106 |
# anonymous notification: no name and no email specified |
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 |
msg = message_from_string(msg.as_string()) |
|
111 |
self.assertEquals(msg.get('from'), u'cubicweb-test <cubicweb-test@logilab.fr>') |
|
112 |
self.assertEquals(msg.get('reply-to'), u'cubicweb-test <cubicweb-test@logilab.fr>') |
|
113 |
# anonymous notification: only email specified |
|
114 |
msg = format_mail({'email': u'tutu@logilab.fr'}, |
|
115 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
116 |
config=self.config) |
|
117 |
msg = message_from_string(msg.as_string()) |
|
118 |
self.assertEquals(msg.get('from'), u'cubicweb-test <tutu@logilab.fr>') |
|
119 |
self.assertEquals(msg.get('reply-to'), u'cubicweb-test <tutu@logilab.fr>, cubicweb-test <cubicweb-test@logilab.fr>') |
|
120 |
# anonymous notification: only name specified |
|
121 |
msg = format_mail({'name': u'tutu'}, |
|
122 |
['test@logilab.fr'], u'un petit cöucou €', u'bïjour €', |
|
123 |
config=self.config) |
|
124 |
msg = message_from_string(msg.as_string()) |
|
125 |
self.assertEquals(msg.get('from'), u'tutu <cubicweb-test@logilab.fr>') |
|
126 |
self.assertEquals(msg.get('reply-to'), u'tutu <cubicweb-test@logilab.fr>') |
|
127 |
||
128 |
||
129 |
||
130 |
if __name__ == '__main__': |
|
131 |
unittest_main() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
132 |