cubicweb/test/unittest_mail.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 14 Apr 2017 15:40:49 +0200
changeset 12164 8d1a7021c817
parent 11129 97095348b3ee
permissions -rw-r--r--
[pkg] Set version to 3.26.0.dev0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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/>.
11083
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
    19
"""unit tests for module cubicweb.mail"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
import os
10326
03d98646d57c [mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9149
diff changeset
    22
import re
11083
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
    23
from socket import gethostname
3153
93ce84956679 fix user login for windows
Aurelien Campeas
parents: 1977
diff changeset
    24
import sys
11083
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
    25
from unittest import TestCase
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
from logilab.common.umessage import message_from_string
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    29
from cubicweb.devtools.testlib import CubicWebTC
11083
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
    30
from cubicweb.mail import format_mail, construct_message_id, parse_message_id
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
def getlogin():
9149
31ed9dd946d1 [test] typo
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents: 6340
diff changeset
    34
    """avoid using os.getlogin() because of strange tty / stdin problems
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    (man 3 getlogin)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    Another solution would be to use $LOGNAME, $USER or $USERNAME
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    """
3153
93ce84956679 fix user login for windows
Aurelien Campeas
parents: 1977
diff changeset
    38
    if sys.platform != 'win32':
93ce84956679 fix user login for windows
Aurelien Campeas
parents: 1977
diff changeset
    39
        import pwd
93ce84956679 fix user login for windows
Aurelien Campeas
parents: 1977
diff changeset
    40
        return pwd.getpwuid(os.getuid())[0]
93ce84956679 fix user login for windows
Aurelien Campeas
parents: 1977
diff changeset
    41
    else:
93ce84956679 fix user login for windows
Aurelien Campeas
parents: 1977
diff changeset
    42
        return os.environ.get('USERNAME')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    43
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    45
class EmailTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    def test_format_mail(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        self.set_option('sender-addr', 'bim@boum.fr')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
        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
    50
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        mail = format_mail({'name': 'oim', 'email': 'oim@logilab.fr'},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
                           ['test@logilab.fr'], u'un petit cöucou', u'bïjour',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
                           config=self.config)
10326
03d98646d57c [mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9149
diff changeset
    54
        result = mail.as_string()
03d98646d57c [mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9149
diff changeset
    55
        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
    56
        self.assertMultiLineEqual(result, """\
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
MIME-Version: 1.0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
Content-Type: text/plain; charset="utf-8"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
Content-Transfer-Encoding: base64
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
Subject: =?utf-8?q?b=C3=AFjour?=
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
From: =?utf-8?q?oim?= <oim@logilab.fr>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
Reply-to: =?utf-8?q?oim?= <oim@logilab.fr>, =?utf-8?q?BimBam?= <bim@boum.fr>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
X-CW: data
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
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
    65
Date: now
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
dW4gcGV0aXQgY8O2dWNvdQ==
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
""")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        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
    70
        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
    71
        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
    72
        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
    73
        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
    74
        self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
    def test_format_mail_euro(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
        mail = format_mail({'name': u'oîm', 'email': u'oim@logilab.fr'},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
                           ['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
    79
        result = mail.as_string()
03d98646d57c [mail] include Date header in generated mails (closes #5271058)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9149
diff changeset
    80
        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
    81
        self.assertMultiLineEqual(result, """\
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
MIME-Version: 1.0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
Content-Type: text/plain; charset="utf-8"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
Content-Transfer-Encoding: base64
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
Subject: =?utf-8?b?YsOvam91ciDigqw=?=
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
From: =?utf-8?q?o=C3=AEm?= <oim@logilab.fr>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
Reply-to: =?utf-8?q?o=C3=AEm?= <oim@logilab.fr>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
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
    89
Date: now
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
dW4gcGV0aXQgY8O2dWNvdSDigqw=
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
""")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        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
    94
        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
    95
        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
    96
        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
    97
        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
    98
        self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou €')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    def test_format_mail_from_reply_to(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        # no sender-name, sender-addr in the configuration
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        self.set_option('sender-name', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        self.set_option('sender-addr', '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        msg = format_mail({'name': u'', 'email': u''},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
                          ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
                          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
   107
        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
   108
        self.assertEqual(msg.get('reply-to'), None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
        msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
                          ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
                          config=self.config)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
        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
   113
        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
   114
        self.assertEqual(msg.get('reply-to'), u'tutu <tutu@logilab.fr>')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        msg = format_mail({'name': u'tutu', 'email': u'tutu@logilab.fr'},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
                          ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        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
   118
        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
   119
        self.assertEqual(msg.get('reply-to'), u'tutu <tutu@logilab.fr>')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        # set sender name and address as expected
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        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
   122
        self.set_option('sender-addr', 'cubicweb-test@logilab.fr')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
        # anonymous notification: no name and no email specified
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
        msg = format_mail({'name': u'', 'email': u''},
11083
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   125
                          ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   126
                          config=self.config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
        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
   128
        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
   129
        self.assertEqual(msg.get('reply-to'), u'cubicweb-test <cubicweb-test@logilab.fr>')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        # anonymous notification: only email specified
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
        msg = format_mail({'email': u'tutu@logilab.fr'},
11083
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   132
                          ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   133
                          config=self.config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
        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
   135
        self.assertEqual(msg.get('from'), u'cubicweb-test <tutu@logilab.fr>')
11083
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   136
        self.assertEqual(
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   137
            msg.get('reply-to'),
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   138
            u'cubicweb-test <tutu@logilab.fr>, cubicweb-test <cubicweb-test@logilab.fr>')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
        # anonymous notification: only name specified
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        msg = format_mail({'name': u'tutu'},
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
                          ['test@logilab.fr'], u'un petit cöucou €', u'bïjour €',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
                          config=self.config)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
        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
   144
        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
   145
        self.assertEqual(msg.get('reply-to'), u'tutu <cubicweb-test@logilab.fr>')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
11083
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   148
class MessageIdTC(TestCase):
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   149
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   150
    def test_base(self):
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   151
        msgid1 = construct_message_id('testapp', 21)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   152
        msgid2 = construct_message_id('testapp', 21)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   153
        self.assertNotEqual(msgid1, msgid2)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   154
        self.assertNotIn('&', msgid1)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   155
        self.assertNotIn('=', msgid1)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   156
        self.assertNotIn('/', msgid1)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   157
        self.assertNotIn('+', msgid1)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   158
        values = parse_message_id(msgid1, 'testapp')
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   159
        self.assertTrue(values)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   160
        # parse_message_id should work with or without surrounding <>
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   161
        self.assertEqual(values, parse_message_id(msgid1[1:-1], 'testapp'))
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   162
        self.assertEqual(values['eid'], '21')
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   163
        self.assertIn('timestamp', values)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   164
        self.assertEqual(parse_message_id(msgid1[1:-1], 'anotherapp'), None)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   165
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   166
    def test_notimestamp(self):
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   167
        msgid1 = construct_message_id('testapp', 21, False)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   168
        construct_message_id('testapp', 21, False)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   169
        values = parse_message_id(msgid1, 'testapp')
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   170
        self.assertEqual(values, {'eid': '21'})
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   171
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   172
    def test_parse_message_doesnt_raise(self):
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   173
        self.assertEqual(parse_message_id('oijioj@bla.bla', 'tesapp'), None)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   174
        self.assertEqual(parse_message_id('oijioj@bla', 'tesapp'), None)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   175
        self.assertEqual(parse_message_id('oijioj', 'tesapp'), None)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   176
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   177
    def test_nonregr_empty_message_id(self):
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   178
        for eid in (1, 12, 123, 1234):
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   179
            msgid1 = construct_message_id('testapp', eid, 12)
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   180
            self.assertNotEqual(msgid1, '<@testapp.%s>' % gethostname())
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   181
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
if __name__ == '__main__':
11083
4b18b1027d02 [test] Gather tests about mail module
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10907
diff changeset
   184
    from logilab.common.testlib import unittest_main
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
    unittest_main()