cubicweb/sobjects/test/unittest_email.py
changeset 11057 0b59724cb3f2
parent 9742 e02ebdf654c2
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    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/>.
       
    18 
       
    19 from cubicweb import Unauthorized
       
    20 from cubicweb.devtools.testlib import CubicWebTC
       
    21 
       
    22 class EmailAddressHooksTC(CubicWebTC):
       
    23 
       
    24     def test_use_email_set_primary_email(self):
       
    25         with self.admin_access.client_cnx() as cnx:
       
    26             cnx.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U use_email X WHERE U login "admin"')
       
    27             self.assertEqual(cnx.execute('Any A WHERE U primary_email X, U login "admin", X address A').rows,
       
    28                               [])
       
    29             cnx.commit()
       
    30             self.assertEqual(cnx.execute('Any A WHERE U primary_email X, U login "admin", X address A')[0][0],
       
    31                               'admin@logilab.fr')
       
    32             # having another email should'nt change anything
       
    33             cnx.execute('INSERT EmailAddress X: X address "a@logilab.fr", U use_email X WHERE U login "admin"')
       
    34             cnx.commit()
       
    35             self.assertEqual(cnx.execute('Any A WHERE U primary_email X, U login "admin", X address A')[0][0],
       
    36                               'admin@logilab.fr')
       
    37 
       
    38     def test_primary_email_set_use_email(self):
       
    39         with self.admin_access.client_cnx() as cnx:
       
    40             cnx.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U primary_email X WHERE U login "admin"')
       
    41             self.assertEqual(cnx.execute('Any A WHERE U use_email X, U login "admin", X address A').rows,
       
    42                               [])
       
    43             cnx.commit()
       
    44             self.assertEqual(cnx.execute('Any A WHERE U use_email X, U login "admin", X address A')[0][0],
       
    45                               'admin@logilab.fr')
       
    46 
       
    47     def test_cardinality_check(self):
       
    48         with self.admin_access.client_cnx() as cnx:
       
    49             email1 = cnx.execute('INSERT EmailAddress E: E address "client@client.com", U use_email E WHERE U login "admin"')[0][0]
       
    50             cnx.commit()
       
    51             cnx.execute('SET U primary_email E WHERE U login "anon", E address "client@client.com"')
       
    52             cnx.commit()
       
    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 
       
    56     def test_security_check(self):
       
    57         with self.admin_access.client_cnx() as cnx:
       
    58             self.create_user(cnx, 'toto')
       
    59             email1 = cnx.execute('INSERT EmailAddress E: E address "client@client.com", U use_email E WHERE U login "admin"')[0][0]
       
    60             cnx.commit()
       
    61         with self.new_access('toto').client_cnx() as cnx:
       
    62             self.assertRaises(Unauthorized,
       
    63                               cnx.execute, 'SET U primary_email E WHERE E eid %(e)s, U login "toto"',
       
    64                               {'e': email1})
       
    65 
       
    66 if __name__ == '__main__':
       
    67     from logilab.common.testlib import unittest_main
       
    68     unittest_main()