test/unittest_entity.py
changeset 8161 6f4229eb8178
parent 7973 64639bc94e25
child 8216 99ff746e8de8
equal deleted inserted replaced
8159:f8678956bd05 8161:6f4229eb8178
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     4 #
     4 #
     5 # This file is part of CubicWeb.
     5 # This file is part of CubicWeb.
     6 #
     6 #
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
    17 # You should have received a copy of the GNU Lesser General Public License along
    17 # You should have received a copy of the GNU Lesser General Public License along
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    19 """unit tests for cubicweb.web.views.entities module"""
    19 """unit tests for cubicweb.web.views.entities module"""
    20 
    20 
    21 from datetime import datetime
    21 from datetime import datetime
       
    22 
    22 from logilab.common import tempattr
    23 from logilab.common import tempattr
       
    24 from logilab.common.decorators import clear_cache
       
    25 
    23 from cubicweb import Binary, Unauthorized
    26 from cubicweb import Binary, Unauthorized
    24 from cubicweb.devtools.testlib import CubicWebTC
    27 from cubicweb.devtools.testlib import CubicWebTC
    25 from cubicweb.mttransforms import HAS_TAL
    28 from cubicweb.mttransforms import HAS_TAL
    26 from cubicweb.entities import fetch_config
    29 from cubicweb.entities import fetch_config
    27 from cubicweb.uilib import soup2xhtml
    30 from cubicweb.uilib import soup2xhtml
   312         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   315         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   313         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   316         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   314                          'WHERE NOT S use_email O, O eid %(x)s, S is CWUser, '
   317                          'WHERE NOT S use_email O, O eid %(x)s, S is CWUser, '
   315                          'S login AA, S firstname AB, S surname AC, S modification_date AD')
   318                          'S login AA, S firstname AB, S surname AC, S modification_date AD')
   316         self.login('anon')
   319         self.login('anon')
   317         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   320         rperms = self.schema['EmailAddress'].permissions['read']
   318         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   321         clear_cache(self.schema['EmailAddress'], 'get_groups')
   319         self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
   322         clear_cache(self.schema['EmailAddress'], 'get_rqlexprs')
   320                          'WHERE NOT S use_email O, O eid %(x)s, S is CWUser, '
   323         self.schema['EmailAddress'].permissions['read'] = ('managers', 'users', 'guests',)
   321                          'S login AA, S firstname AB, S surname AC, S modification_date AD, '
   324         try:
   322                          'AE eid %(AF)s, EXISTS(S identity AE, NOT AE in_group AG, AG name "guests", AG is CWGroup)')
   325             email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
       
   326             rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
       
   327             self.assertEqual(rql, 'Any S,AA,AB,AC,AD ORDERBY AA '
       
   328                              'WHERE NOT S use_email O, O eid %(x)s, S is CWUser, '
       
   329                              'S login AA, S firstname AB, S surname AC, S modification_date AD, '
       
   330                              'AE eid %(AF)s, EXISTS(S identity AE, NOT AE in_group AG, AG name "guests", AG is CWGroup)')
       
   331         finally:
       
   332             clear_cache(self.schema['EmailAddress'], 'get_groups')
       
   333             clear_cache(self.schema['EmailAddress'], 'get_rqlexprs')
       
   334             self.schema['EmailAddress'].permissions['read'] = rperms
       
   335 
   323 
   336 
   324     def test_unrelated_rql_security_nonexistant(self):
   337     def test_unrelated_rql_security_nonexistant(self):
   325         self.login('anon')
   338         self.login('anon')
   326         email = self.vreg['etypes'].etype_class('EmailAddress')(self.request())
   339         email = self.vreg['etypes'].etype_class('EmailAddress')(self.request())
   327         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   340         rql = email.cw_unrelated_rql('use_email', 'CWUser', 'object')[0]
   455         req.create_entity('Personne', nom=u'thenault', prenom=u'sylvain')
   468         req.create_entity('Personne', nom=u'thenault', prenom=u'sylvain')
   456         self.assertEqual(len(e.unrelated('tags', 'Personne', 'subject', limit=1)),
   469         self.assertEqual(len(e.unrelated('tags', 'Personne', 'subject', limit=1)),
   457                           1)
   470                           1)
   458 
   471 
   459     def test_unrelated_security(self):
   472     def test_unrelated_security(self):
   460         email = self.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0)
   473         rperms = self.schema['EmailAddress'].permissions['read']
   461         rset = email.unrelated('use_email', 'CWUser', 'object')
   474         clear_cache(self.schema['EmailAddress'], 'get_groups')
   462         self.assertEqual([x.login for x in rset.entities()], [u'admin', u'anon'])
   475         clear_cache(self.schema['EmailAddress'], 'get_rqlexprs')
   463         user = self.request().user
   476         self.schema['EmailAddress'].permissions['read'] = ('managers', 'users', 'guests',)
   464         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   477         try:
   465         self.assertEqual([x.address for x in rset.entities()], [u'hop'])
   478             email = self.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0)
   466         req = self.request()
   479             rset = email.unrelated('use_email', 'CWUser', 'object')
   467         self.create_user(req, 'toto')
   480             self.assertEqual([x.login for x in rset.entities()], [u'admin', u'anon'])
   468         self.login('toto')
   481             user = self.request().user
   469         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   482             rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   470         rset = email.unrelated('use_email', 'CWUser', 'object')
   483             self.assertEqual([x.address for x in rset.entities()], [u'hop'])
   471         self.assertEqual([x.login for x in rset.entities()], ['toto'])
   484             req = self.request()
   472         user = self.request().user
   485             self.create_user(req, 'toto')
   473         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   486             self.login('toto')
   474         self.assertEqual([x.address for x in rset.entities()], ['hop'])
   487             email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   475         user = self.execute('Any X WHERE X login "admin"').get_entity(0, 0)
   488             rset = email.unrelated('use_email', 'CWUser', 'object')
   476         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   489             self.assertEqual([x.login for x in rset.entities()], ['toto'])
   477         self.assertEqual([x.address for x in rset.entities()], [])
   490             user = self.request().user
   478         self.login('anon')
   491             rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   479         email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
   492             self.assertEqual([x.address for x in rset.entities()], ['hop'])
   480         rset = email.unrelated('use_email', 'CWUser', 'object')
   493             user = self.execute('Any X WHERE X login "admin"').get_entity(0, 0)
   481         self.assertEqual([x.login for x in rset.entities()], [])
   494             rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   482         user = self.request().user
   495             self.assertEqual([x.address for x in rset.entities()], [])
   483         rset = user.unrelated('use_email', 'EmailAddress', 'subject')
   496             self.login('anon')
   484         self.assertEqual([x.address for x in rset.entities()], [])
   497             email = self.execute('Any X WHERE X eid %(x)s', {'x': email.eid}).get_entity(0, 0)
       
   498             rset = email.unrelated('use_email', 'CWUser', 'object')
       
   499             self.assertEqual([x.login for x in rset.entities()], [])
       
   500             user = self.request().user
       
   501             rset = user.unrelated('use_email', 'EmailAddress', 'subject')
       
   502             self.assertEqual([x.address for x in rset.entities()], [])
       
   503         finally:
       
   504             clear_cache(self.schema['EmailAddress'], 'get_groups')
       
   505             clear_cache(self.schema['EmailAddress'], 'get_rqlexprs')
       
   506             self.schema['EmailAddress'].permissions['read'] = rperms
   485 
   507 
   486     def test_unrelated_new_entity(self):
   508     def test_unrelated_new_entity(self):
   487         e = self.vreg['etypes'].etype_class('CWUser')(self.request())
   509         e = self.vreg['etypes'].etype_class('CWUser')(self.request())
   488         unrelated = [r[0] for r in e.unrelated('in_group', 'CWGroup', 'subject')]
   510         unrelated = [r[0] for r in e.unrelated('in_group', 'CWGroup', 'subject')]
   489         # should be default groups but owners, i.e. managers, users, guests
   511         # should be default groups but owners, i.e. managers, users, guests