author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 08 Dec 2015 17:59:54 +0100 | |
changeset 10953 | 8b8193046600 |
parent 10326 | 03d98646d57c |
child 10907 | 9ae707db5265 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
2 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
3 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
4 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
5 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
6 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
7 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
8 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
9 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
10 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
11 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
12 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
14 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
15 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
16 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
17 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
18 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
19 |
"""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
|
20 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
21 |
""" |
0 | 22 |
|
23 |
import os |
|
10326
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9149
diff
changeset
|
24 |
import re |
3153 | 25 |
import sys |
0 | 26 |
|
27 |
from logilab.common.testlib import unittest_main |
|
28 |
from logilab.common.umessage import message_from_string |
|
29 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
30 |
from cubicweb.devtools.testlib import CubicWebTC |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
31 |
from cubicweb.mail import format_mail |
0 | 32 |
|
33 |
||
34 |
def getlogin(): |
|
9149
31ed9dd946d1
[test] typo
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents:
6340
diff
changeset
|
35 |
"""avoid using os.getlogin() because of strange tty / stdin problems |
0 | 36 |
(man 3 getlogin) |
37 |
Another solution would be to use $LOGNAME, $USER or $USERNAME |
|
38 |
""" |
|
3153 | 39 |
if sys.platform != 'win32': |
40 |
import pwd |
|
41 |
return pwd.getpwuid(os.getuid())[0] |
|
42 |
else: |
|
43 |
return os.environ.get('USERNAME') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
44 |
|
0 | 45 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
46 |
class EmailTC(CubicWebTC): |
0 | 47 |
|
48 |
def test_format_mail(self): |
|
49 |
self.set_option('sender-addr', 'bim@boum.fr') |
|
50 |
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
|
51 |
|
0 | 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) |
|
10326
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9149
diff
changeset
|
55 |
result = mail.as_string() |
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9149
diff
changeset
|
56 |
result = re.sub('^Date: .*$', 'Date: now', result, flags=re.MULTILINE) |
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9149
diff
changeset
|
57 |
self.assertMultiLineEqual(result, """\ |
0 | 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 |
|
10326
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9149
diff
changeset
|
66 |
Date: now |
0 | 67 |
|
68 |
dW4gcGV0aXQgY8O2dWNvdQ== |
|
69 |
""") |
|
70 |
msg = message_from_string(mail.as_string()) |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
71 |
self.assertEqual(msg.get('subject'), u'bïjour') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
72 |
self.assertEqual(msg.get('from'), u'oim <oim@logilab.fr>') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
73 |
self.assertEqual(msg.get('to'), u'test@logilab.fr') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
74 |
self.assertEqual(msg.get('reply-to'), u'oim <oim@logilab.fr>, BimBam <bim@boum.fr>') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
75 |
self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou') |
0 | 76 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
77 |
|
0 | 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 €') |
|
10326
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9149
diff
changeset
|
81 |
result = mail.as_string() |
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9149
diff
changeset
|
82 |
result = re.sub('^Date: .*$', 'Date: now', result, flags=re.MULTILINE) |
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9149
diff
changeset
|
83 |
self.assertMultiLineEqual(result, """\ |
0 | 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 |
|
10326
03d98646d57c
[mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9149
diff
changeset
|
91 |
Date: now |
0 | 92 |
|
93 |
dW4gcGV0aXQgY8O2dWNvdSDigqw= |
|
94 |
""") |
|
95 |
msg = message_from_string(mail.as_string()) |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
96 |
self.assertEqual(msg.get('subject'), u'bïjour €') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
97 |
self.assertEqual(msg.get('from'), u'oîm <oim@logilab.fr>') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
98 |
self.assertEqual(msg.get('to'), u'test@logilab.fr') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
99 |
self.assertEqual(msg.get('reply-to'), u'oîm <oim@logilab.fr>') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
100 |
self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou €') |
0 | 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) |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
110 |
self.assertEqual(msg.get('from'), u'') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
111 |
self.assertEqual(msg.get('reply-to'), None) |
0 | 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()) |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
116 |
self.assertEqual(msg.get('from'), u'tutu <tutu@logilab.fr>') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
117 |
self.assertEqual(msg.get('reply-to'), u'tutu <tutu@logilab.fr>') |
0 | 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()) |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
121 |
self.assertEqual(msg.get('from'), u'tutu <tutu@logilab.fr>') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
122 |
self.assertEqual(msg.get('reply-to'), u'tutu <tutu@logilab.fr>') |
0 | 123 |
# set sender name and address as expected |
124 |
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
|
125 |
self.set_option('sender-addr', 'cubicweb-test@logilab.fr') |
0 | 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()) |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
131 |
self.assertEqual(msg.get('from'), u'cubicweb-test <cubicweb-test@logilab.fr>') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
132 |
self.assertEqual(msg.get('reply-to'), u'cubicweb-test <cubicweb-test@logilab.fr>') |
0 | 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()) |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
138 |
self.assertEqual(msg.get('from'), u'cubicweb-test <tutu@logilab.fr>') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
139 |
self.assertEqual(msg.get('reply-to'), u'cubicweb-test <tutu@logilab.fr>, cubicweb-test <cubicweb-test@logilab.fr>') |
0 | 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()) |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
145 |
self.assertEqual(msg.get('from'), u'tutu <cubicweb-test@logilab.fr>') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
146 |
self.assertEqual(msg.get('reply-to'), u'tutu <cubicweb-test@logilab.fr>') |
0 | 147 |
|
148 |
||
149 |
||
150 |
if __name__ == '__main__': |
|
151 |
unittest_main() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
152 |