sobjects/test/unittest_register_user.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
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 """unittest for cubicweb.dbapi"""
       
    19 
       
    20 from cubicweb import ValidationError
       
    21 from cubicweb.web import Unauthorized
       
    22 from cubicweb.devtools.testlib import CubicWebTC
       
    23 
       
    24 
       
    25 class RegisterUserTC(CubicWebTC):
       
    26 
       
    27     def test_register_user_service(self):
       
    28         acc = self.admin_access
       
    29         with acc.client_cnx() as cnx:
       
    30             cnx.call_service('register_user', login=u'foo1', password=u'bar1',
       
    31                              email=u'foo1@bar1.com', firstname=u'Foo1',
       
    32                              surname=u'Bar1')
       
    33 
       
    34         acc = self.new_access('anon')
       
    35         with acc.client_cnx() as cnx:
       
    36             self.assertRaises(Unauthorized, cnx.call_service, 'register_user',
       
    37                               login=u'foo2', password=u'bar2',
       
    38                               email=u'foo2@bar2.com', firstname=u'Foo2', surname=u'Bar2')
       
    39 
       
    40         with self.repo.internal_cnx() as cnx:
       
    41             cnx.call_service('register_user', login=u'foo3',
       
    42                              password=u'bar3', email=u'foo3@bar3.com',
       
    43                              firstname=u'Foo3', surname=u'Bar3')
       
    44             # same login
       
    45             with self.assertRaises(ValidationError):
       
    46                 cnx.call_service('register_user', login=u'foo3',
       
    47                                  password=u'bar3')
       
    48 
       
    49     def test_register_user_attributes(self):
       
    50         with self.repo.internal_cnx() as cnx:
       
    51             cnx.call_service('register_user', login=u'foo3',
       
    52                              password=u'bar3', email=u'foo3@bar3.com',
       
    53                              firstname=u'Foo3', surname=u'Bar3')
       
    54             cnx.commit()
       
    55 
       
    56         with self.admin_access.client_cnx() as cnx:
       
    57             user = cnx.find('CWUser', login=u'foo3').one()
       
    58             self.assertEqual(user.firstname, u'Foo3')
       
    59             self.assertEqual(user.use_email[0].address, u'foo3@bar3.com')
       
    60 
       
    61     def test_register_user_groups(self):
       
    62         with self.repo.internal_cnx() as cnx:
       
    63             # default
       
    64             cnx.call_service('register_user', login=u'foo_user',
       
    65                              password=u'bar_user', email=u'foo_user@bar_user.com',
       
    66                              firstname=u'Foo_user', surname=u'Bar_user')
       
    67 
       
    68             # group kwarg
       
    69             cnx.call_service('register_user', login=u'foo_admin',
       
    70                              password=u'bar_admin', email=u'foo_admin@bar_admin.com',
       
    71                              firstname=u'Foo_admin', surname=u'Bar_admin',
       
    72                              groups=('managers', 'users'))
       
    73 
       
    74             # class attribute
       
    75             from cubicweb.sobjects import services
       
    76             services.RegisterUserService.default_groups = ('guests',)
       
    77             cnx.call_service('register_user', login=u'foo_guest',
       
    78                              password=u'bar_guest', email=u'foo_guest@bar_guest.com',
       
    79                              firstname=u'Foo_guest', surname=u'Bar_guest')
       
    80             cnx.commit()
       
    81 
       
    82         with self.admin_access.client_cnx() as cnx:
       
    83             user = cnx.find('CWUser', login=u'foo_user').one()
       
    84             self.assertEqual([g.name for g in user.in_group], ['users'])
       
    85 
       
    86             admin = cnx.find('CWUser', login=u'foo_admin').one()
       
    87             self.assertEqual(sorted(g.name for g in admin.in_group), ['managers', 'users'])
       
    88 
       
    89             guest = cnx.find('CWUser', login=u'foo_guest').one()
       
    90             self.assertEqual([g.name for g in guest.in_group], ['guests'])
       
    91 
       
    92 
       
    93 if __name__ == '__main__':
       
    94     from logilab.common.testlib import unittest_main
       
    95     unittest_main()