sobjects/test/unittest_email.py
changeset 9742 e02ebdf654c2
parent 7791 31bb51ea5485
equal deleted inserted replaced
9739:5efd4a1abc66 9742:e02ebdf654c2
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """
       
    19 
       
    20 """
       
    21 
    18 
    22 from cubicweb import Unauthorized
    19 from cubicweb import Unauthorized
    23 from cubicweb.devtools.testlib import CubicWebTC
    20 from cubicweb.devtools.testlib import CubicWebTC
    24 
    21 
    25 class EmailAddressHooksTC(CubicWebTC):
    22 class EmailAddressHooksTC(CubicWebTC):
    26 
    23 
    27     def test_use_email_set_primary_email(self):
    24     def test_use_email_set_primary_email(self):
    28         self.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U use_email X WHERE U login "admin"')
    25         with self.admin_access.client_cnx() as cnx:
    29         self.assertEqual(self.execute('Any A WHERE U primary_email X, U login "admin", X address A').rows,
    26             cnx.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U use_email X WHERE U login "admin"')
    30                           [])
    27             self.assertEqual(cnx.execute('Any A WHERE U primary_email X, U login "admin", X address A').rows,
    31         self.commit()
    28                               [])
    32         self.assertEqual(self.execute('Any A WHERE U primary_email X, U login "admin", X address A')[0][0],
    29             cnx.commit()
    33                           'admin@logilab.fr')
    30             self.assertEqual(cnx.execute('Any A WHERE U primary_email X, U login "admin", X address A')[0][0],
    34         # having another email should'nt change anything
    31                               'admin@logilab.fr')
    35         self.execute('INSERT EmailAddress X: X address "a@logilab.fr", U use_email X WHERE U login "admin"')
    32             # having another email should'nt change anything
    36         self.commit()
    33             cnx.execute('INSERT EmailAddress X: X address "a@logilab.fr", U use_email X WHERE U login "admin"')
    37         self.assertEqual(self.execute('Any A WHERE U primary_email X, U login "admin", X address A')[0][0],
    34             cnx.commit()
    38                           'admin@logilab.fr')
    35             self.assertEqual(cnx.execute('Any A WHERE U primary_email X, U login "admin", X address A')[0][0],
       
    36                               'admin@logilab.fr')
    39 
    37 
    40     def test_primary_email_set_use_email(self):
    38     def test_primary_email_set_use_email(self):
    41         self.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U primary_email X WHERE U login "admin"')
    39         with self.admin_access.client_cnx() as cnx:
    42         self.assertEqual(self.execute('Any A WHERE U use_email X, U login "admin", X address A').rows,
    40             cnx.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U primary_email X WHERE U login "admin"')
    43                           [])
    41             self.assertEqual(cnx.execute('Any A WHERE U use_email X, U login "admin", X address A').rows,
    44         self.commit()
    42                               [])
    45         self.assertEqual(self.execute('Any A WHERE U use_email X, U login "admin", X address A')[0][0],
    43             cnx.commit()
    46                           'admin@logilab.fr')
    44             self.assertEqual(cnx.execute('Any A WHERE U use_email X, U login "admin", X address A')[0][0],
       
    45                               'admin@logilab.fr')
    47 
    46 
    48     def test_cardinality_check(self):
    47     def test_cardinality_check(self):
    49         email1 = self.execute('INSERT EmailAddress E: E address "client@client.com", U use_email E WHERE U login "admin"')[0][0]
    48         with self.admin_access.client_cnx() as cnx:
    50         self.commit()
    49             email1 = cnx.execute('INSERT EmailAddress E: E address "client@client.com", U use_email E WHERE U login "admin"')[0][0]
    51         self.execute('SET U primary_email E WHERE U login "anon", E address "client@client.com"')
    50             cnx.commit()
    52         self.commit()
    51             cnx.execute('SET U primary_email E WHERE U login "anon", E address "client@client.com"')
    53         rset = self.execute('Any X WHERE X use_email E, E eid %(e)s', {'e': email1})
    52             cnx.commit()
    54         self.assertFalse(rset.rowcount != 1, rset)
    53             rset = cnx.execute('Any X WHERE X use_email E, E eid %(e)s', {'e': email1})
       
    54             self.assertFalse(rset.rowcount != 1, rset)
    55 
    55 
    56     def test_security_check(self):
    56     def test_security_check(self):
    57         req = self.request()
    57         with self.admin_access.client_cnx() as cnx:
    58         self.create_user(req, 'toto')
    58             self.create_user(cnx, 'toto')
    59         email1 = self.execute('INSERT EmailAddress E: E address "client@client.com", U use_email E WHERE U login "admin"')[0][0]
    59             email1 = cnx.execute('INSERT EmailAddress E: E address "client@client.com", U use_email E WHERE U login "admin"')[0][0]
    60         self.commit()
    60             cnx.commit()
    61         cnx = self.login('toto')
    61         with self.new_access('toto').client_cnx() as cnx:
    62         cu = cnx.cursor()
    62             self.assertRaises(Unauthorized,
    63         self.assertRaises(Unauthorized,
    63                               cnx.execute, 'SET U primary_email E WHERE E eid %(e)s, U login "toto"',
    64                           cu.execute, 'SET U primary_email E WHERE E eid %(e)s, U login "toto"',
    64                               {'e': email1})
    65                           {'e': email1})
       
    66         cnx.close()
       
    67 
    65 
    68 if __name__ == '__main__':
    66 if __name__ == '__main__':
    69     from logilab.common.testlib import unittest_main
    67     from logilab.common.testlib import unittest_main
    70     unittest_main()
    68     unittest_main()