1 # -*- coding: utf-8 -*- |
|
2 """unit tests for module cubicweb.common.mail |
|
3 |
|
4 :organization: Logilab |
|
5 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
|
6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
7 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
|
8 """ |
|
9 |
|
10 import os |
|
11 import sys |
|
12 |
|
13 from logilab.common.testlib import unittest_main |
|
14 from logilab.common.umessage import message_from_string |
|
15 |
|
16 from cubicweb.devtools.apptest import EnvBasedTC |
|
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 if sys.platform != 'win32': |
|
26 import pwd |
|
27 return pwd.getpwuid(os.getuid())[0] |
|
28 else: |
|
29 return os.environ.get('USERNAME') |
|
30 |
|
31 |
|
32 class EmailTC(EnvBasedTC): |
|
33 |
|
34 def test_format_mail(self): |
|
35 self.set_option('sender-addr', 'bim@boum.fr') |
|
36 self.set_option('sender-name', 'BimBam') |
|
37 |
|
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 |
|
60 |
|
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') |
|
105 self.set_option('sender-addr', 'cubicweb-test@logilab.fr') |
|
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() |
|
132 |
|