sobjects/test/unittest_email.py
author Julien Cristau <julien.cristau@logilab.fr>
Tue, 06 Oct 2015 11:19:37 +0200
changeset 10781 e2ab20561932
parent 9742 e02ebdf654c2
permissions -rw-r--r--
[devtools] make httptest module importable under py3k
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9742
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4673
diff changeset
     2
# 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: 4673
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4673
diff changeset
     4
# 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: 4673
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4673
diff changeset
     6
# 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: 4673
diff changeset
     7
# 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: 4673
diff changeset
     8
# 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: 4673
diff changeset
     9
# 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: 4673
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# 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: 4673
diff changeset
    12
# 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: 4673
diff changeset
    13
# 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: 4673
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4673
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4673
diff changeset
    16
# 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: 4673
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
3293
69c0ba095536 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2773 3253
diff changeset
    18
3253
905e269ff853 email hooks tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    19
from cubicweb import Unauthorized
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    20
from cubicweb.devtools.testlib import CubicWebTC
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    22
class EmailAddressHooksTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
    def test_use_email_set_primary_email(self):
9742
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    25
        with self.admin_access.client_cnx() as cnx:
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    26
            cnx.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U use_email X WHERE U login "admin"')
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    27
            self.assertEqual(cnx.execute('Any A WHERE U primary_email X, U login "admin", X address A').rows,
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    28
                              [])
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    29
            cnx.commit()
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    30
            self.assertEqual(cnx.execute('Any A WHERE U primary_email X, U login "admin", X address A')[0][0],
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    31
                              'admin@logilab.fr')
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    32
            # having another email should'nt change anything
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    33
            cnx.execute('INSERT EmailAddress X: X address "a@logilab.fr", U use_email X WHERE U login "admin"')
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    34
            cnx.commit()
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    35
            self.assertEqual(cnx.execute('Any A WHERE U primary_email X, U login "admin", X address A')[0][0],
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    36
                              'admin@logilab.fr')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
    def test_primary_email_set_use_email(self):
9742
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    39
        with self.admin_access.client_cnx() as cnx:
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    40
            cnx.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U primary_email X WHERE U login "admin"')
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    41
            self.assertEqual(cnx.execute('Any A WHERE U use_email X, U login "admin", X address A').rows,
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    42
                              [])
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    43
            cnx.commit()
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    44
            self.assertEqual(cnx.execute('Any A WHERE U use_email X, U login "admin", X address A')[0][0],
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    45
                              'admin@logilab.fr')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    46
3253
905e269ff853 email hooks tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    47
    def test_cardinality_check(self):
9742
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    48
        with self.admin_access.client_cnx() as cnx:
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    49
            email1 = cnx.execute('INSERT EmailAddress E: E address "client@client.com", U use_email E WHERE U login "admin"')[0][0]
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    50
            cnx.commit()
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    51
            cnx.execute('SET U primary_email E WHERE U login "anon", E address "client@client.com"')
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    52
            cnx.commit()
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    53
            rset = cnx.execute('Any X WHERE X use_email E, E eid %(e)s', {'e': email1})
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    54
            self.assertFalse(rset.rowcount != 1, rset)
3253
905e269ff853 email hooks tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    55
905e269ff853 email hooks tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    56
    def test_security_check(self):
9742
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    57
        with self.admin_access.client_cnx() as cnx:
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    58
            self.create_user(cnx, 'toto')
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    59
            email1 = cnx.execute('INSERT EmailAddress E: E address "client@client.com", U use_email E WHERE U login "admin"')[0][0]
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    60
            cnx.commit()
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    61
        with self.new_access('toto').client_cnx() as cnx:
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    62
            self.assertRaises(Unauthorized,
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    63
                              cnx.execute, 'SET U primary_email E WHERE E eid %(e)s, U login "toto"',
e02ebdf654c2 [test] update sobjects/test/unittest_email to cw 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
    64
                              {'e': email1})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    from logilab.common.testlib import unittest_main
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
    unittest_main()