web/test/unittest_views_editforms.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 from logilab.common.testlib import unittest_main, mock_object
       
    19 
       
    20 from cubicweb.devtools.testlib import CubicWebTC
       
    21 from cubicweb.web.views import uicfg
       
    22 from cubicweb.web.formwidgets import AutoCompletionWidget
       
    23 from cubicweb.schema import RRQLExpression
       
    24 
       
    25 
       
    26 AFFK = uicfg.autoform_field_kwargs
       
    27 AFS = uicfg.autoform_section
       
    28 
       
    29 def rbc(entity, formtype, section):
       
    30     if section in ('attributes', 'metadata', 'hidden'):
       
    31         permission = 'update'
       
    32     else:
       
    33         permission = 'add'
       
    34     return [(rschema.type, x)
       
    35             for rschema, tschemas, x in AFS.relations_by_section(entity,
       
    36                                                                  formtype,
       
    37                                                                  section,
       
    38                                                                  permission)]
       
    39 
       
    40 class AutomaticEntityFormTC(CubicWebTC):
       
    41 
       
    42     def test_custom_widget(self):
       
    43         with self.admin_access.web_request() as req:
       
    44             AFFK.tag_subject_of(('CWUser', 'login', '*'),
       
    45                                 {'widget': AutoCompletionWidget(autocomplete_initfunc='get_logins')})
       
    46             form = self.vreg['forms'].select('edition', req, entity=req.user)
       
    47             field = form.field_by_name('login', 'subject')
       
    48             self.assertIsInstance(field.widget, AutoCompletionWidget)
       
    49             AFFK.del_rtag('CWUser', 'login', '*', 'subject')
       
    50 
       
    51 
       
    52     def test_cwuser_relations_by_category(self):
       
    53         with self.admin_access.web_request() as req:
       
    54             e = self.vreg['etypes'].etype_class('CWUser')(req)
       
    55             # see custom configuration in views.cwuser
       
    56             self.assertEqual(rbc(e, 'main', 'attributes'),
       
    57                              [('login', 'subject'),
       
    58                               ('upassword', 'subject'),
       
    59                               ('firstname', 'subject'),
       
    60                               ('surname', 'subject'),
       
    61                               ('in_group', 'subject'),
       
    62                               ])
       
    63             self.assertEqual(rbc(e, 'muledit', 'attributes'),
       
    64                                   [('login', 'subject'),
       
    65                                    ('upassword', 'subject'),
       
    66                                    ('in_group', 'subject'),
       
    67                                    ])
       
    68             self.assertCountEqual(rbc(e, 'main', 'metadata'),
       
    69                                   [('last_login_time', 'subject'),
       
    70                                    ('cw_source', 'subject'),
       
    71                                    ('creation_date', 'subject'),
       
    72                                    ('modification_date', 'subject'),
       
    73                                    ('created_by', 'subject'),
       
    74                                    ('owned_by', 'subject'),
       
    75                                    ('bookmarked_by', 'object'),
       
    76                                    ])
       
    77             # XXX skip 'tags' relation here and in the hidden category because
       
    78             # of some test interdependancy when pytest is launched on whole cw
       
    79             # (appears here while expected in hidden
       
    80             self.assertCountEqual([x for x in rbc(e, 'main', 'relations')
       
    81                                    if x != ('tags', 'object')],
       
    82                                   [('connait', 'subject'),
       
    83                                    ('custom_workflow', 'subject'),
       
    84                                    ('primary_email', 'subject'),
       
    85                                    ('checked_by', 'object'),
       
    86                                    ])
       
    87             self.assertListEqual(rbc(e, 'main', 'inlined'),
       
    88                                   [('use_email', 'subject'),
       
    89                                    ])
       
    90             # owned_by is defined both as subject and object relations on CWUser
       
    91             self.assertListEqual(sorted(x for x in rbc(e, 'main', 'hidden')
       
    92                                          if x != ('tags', 'object')),
       
    93                                   sorted([('for_user', 'object'),
       
    94                                           ('created_by', 'object'),
       
    95                                           ('wf_info_for', 'object'),
       
    96                                           ('owned_by', 'object'),
       
    97                                           ]))
       
    98 
       
    99     def test_inlined_view(self):
       
   100         self.assertIn('main_inlined',
       
   101                       AFS.etype_get('CWUser', 'use_email', 'subject', 'EmailAddress'))
       
   102         self.assertNotIn('main_inlined',
       
   103                          AFS.etype_get('CWUser', 'primary_email', 'subject', 'EmailAddress'))
       
   104         self.assertIn('main_relations',
       
   105                       AFS.etype_get('CWUser', 'primary_email', 'subject', 'EmailAddress'))
       
   106 
       
   107     def test_personne_relations_by_category(self):
       
   108         with self.admin_access.web_request() as req:
       
   109             e = self.vreg['etypes'].etype_class('Personne')(req)
       
   110             self.assertListEqual(rbc(e, 'main', 'attributes'),
       
   111                                   [('nom', 'subject'),
       
   112                                    ('prenom', 'subject'),
       
   113                                    ('sexe', 'subject'),
       
   114                                    ('promo', 'subject'),
       
   115                                    ('titre', 'subject'),
       
   116                                    ('ass', 'subject'),
       
   117                                    ('web', 'subject'),
       
   118                                    ('tel', 'subject'),
       
   119                                    ('fax', 'subject'),
       
   120                                    ('datenaiss', 'subject'),
       
   121                                    ('test', 'subject'),
       
   122                                    ('description', 'subject'),
       
   123                                    ('salary', 'subject'),
       
   124                                    ])
       
   125             self.assertListEqual(rbc(e, 'muledit', 'attributes'),
       
   126                                   [('nom', 'subject'),
       
   127                                    ])
       
   128             self.assertCountEqual(rbc(e, 'main', 'metadata'),
       
   129                                   [('cw_source', 'subject'),
       
   130                                    ('creation_date', 'subject'),
       
   131                                    ('modification_date', 'subject'),
       
   132                                    ('created_by', 'subject'),
       
   133                                    ('owned_by', 'subject'),
       
   134                                    ])
       
   135             self.assertCountEqual(rbc(e, 'main', 'relations'),
       
   136                                   [('travaille', 'subject'),
       
   137                                    ('manager', 'object'),
       
   138                                    ('connait', 'object'),
       
   139                                    ])
       
   140             self.assertListEqual(rbc(e, 'main', 'hidden'),
       
   141                                   [])
       
   142 
       
   143     def test_edition_form(self):
       
   144         with self.admin_access.web_request() as req:
       
   145             rset = req.execute('CWUser X LIMIT 1')
       
   146             form = self.vreg['forms'].select('edition', req, rset=rset, row=0, col=0)
       
   147             # should be also selectable by specifying entity
       
   148             self.vreg['forms'].select('edition', req, entity=rset.get_entity(0, 0))
       
   149             self.assertFalse(any(f for f in form.fields if f is None))
       
   150 
       
   151     def test_edition_form_with_action(self):
       
   152         with self.admin_access.web_request() as req:
       
   153             rset = req.execute('CWUser X LIMIT 1')
       
   154             form = self.vreg['forms'].select('edition', req, rset=rset, row=0,
       
   155                                              col=0, action='my_custom_action')
       
   156             self.assertEqual(form.form_action(), 'my_custom_action')
       
   157 
       
   158     def test_attribute_add_permissions(self):
       
   159         # https://www.cubicweb.org/ticket/4342844
       
   160         with self.admin_access.repo_cnx() as cnx:
       
   161             self.create_user(cnx, u'toto')
       
   162             cnx.commit()
       
   163         with self.new_access(u'toto').web_request() as req:
       
   164             e = self.vreg['etypes'].etype_class('Personne')(req)
       
   165             cform = self.vreg['forms'].select('edition', req, entity=e)
       
   166             self.assertIn('sexe',
       
   167                           [rschema.type
       
   168                            for rschema, _ in cform.editable_attributes()])
       
   169             with self.new_access(u'toto').repo_cnx() as cnx:
       
   170                 person_eid = cnx.create_entity('Personne', nom=u'Robert').eid
       
   171                 cnx.commit()
       
   172             person = req.entity_from_eid(person_eid)
       
   173             mform = self.vreg['forms'].select('edition', req, entity=person)
       
   174             self.assertNotIn('sexe',
       
   175                              [rschema.type
       
   176                               for rschema, _ in mform.editable_attributes()])
       
   177 
       
   178     def test_inlined_relations(self):
       
   179         with self.admin_access.web_request() as req:
       
   180             with self.temporary_permissions(EmailAddress={'add': ()}):
       
   181                 autoform = self.vreg['forms'].select('edition', req, entity=req.user)
       
   182                 self.assertEqual(list(autoform.inlined_form_views()), [])
       
   183 
       
   184     def test_check_inlined_rdef_permissions(self):
       
   185         # try to check permissions when creating an entity ('user' below is a
       
   186         # fresh entity without an eid)
       
   187         with self.admin_access.web_request() as req:
       
   188             ttype = 'EmailAddress'
       
   189             rschema = self.schema['use_email']
       
   190             rdef =  rschema.rdefs[('CWUser', ttype)]
       
   191             tschema = self.schema[ttype]
       
   192             role = 'subject'
       
   193             with self.temporary_permissions((rdef, {'add': ()})):
       
   194                 user = self.vreg['etypes'].etype_class('CWUser')(req)
       
   195                 autoform = self.vreg['forms'].select('edition', req, entity=user)
       
   196                 self.assertFalse(autoform.check_inlined_rdef_permissions(rschema, role,
       
   197                                                                          tschema, ttype))
       
   198             # we actually don't care about the actual expression,
       
   199             # may_have_permission only checks the presence of such expressions
       
   200             expr = RRQLExpression('S use_email O')
       
   201             with self.temporary_permissions((rdef, {'add': (expr,)})):
       
   202                 user = self.vreg['etypes'].etype_class('CWUser')(req)
       
   203                 autoform = self.vreg['forms'].select('edition', req, entity=user)
       
   204                 self.assertTrue(autoform.check_inlined_rdef_permissions(rschema, role,
       
   205                                                                         tschema, ttype))
       
   206 
       
   207 
       
   208 class FormViewsTC(CubicWebTC):
       
   209 
       
   210     def test_delete_conf_formview(self):
       
   211         with self.admin_access.web_request() as req:
       
   212             rset = req.execute('CWGroup X')
       
   213             self.view('deleteconf', rset, template=None, req=req).source
       
   214 
       
   215     def test_automatic_edition_formview(self):
       
   216         with self.admin_access.web_request() as req:
       
   217             rset = req.execute('CWUser X')
       
   218             self.view('edition', rset, row=0, template=None, req=req).source
       
   219 
       
   220     def test_automatic_edition_copyformview(self):
       
   221         with self.admin_access.web_request() as req:
       
   222             rset = req.execute('CWUser X')
       
   223             self.view('copy', rset, row=0, template=None, req=req).source
       
   224 
       
   225     def test_automatic_creation_formview(self):
       
   226         with self.admin_access.web_request() as req:
       
   227             self.view('creation', None, etype='CWUser', template=None, req=req).source
       
   228 
       
   229     def test_automatic_muledit_formview(self):
       
   230         with self.admin_access.web_request() as req:
       
   231             rset = req.execute('CWUser X')
       
   232             self.view('muledit', rset, template=None, req=req).source
       
   233 
       
   234     def test_automatic_reledit_formview(self):
       
   235         with self.admin_access.web_request() as req:
       
   236             rset = req.execute('CWUser X')
       
   237             self.view('reledit', rset, row=0, rtype='login', template=None, req=req).source
       
   238 
       
   239     def test_automatic_inline_edit_formview(self):
       
   240         with self.admin_access.web_request() as req:
       
   241             geid = req.execute('CWGroup X LIMIT 1')[0][0]
       
   242             rset = req.execute('CWUser X LIMIT 1')
       
   243             self.view('inline-edition', rset, row=0, col=0, rtype='in_group',
       
   244                       peid=geid, role='object', i18nctx='', pform=MOCKPFORM,
       
   245                       template=None, req=req).source
       
   246 
       
   247     def test_automatic_inline_creation_formview(self):
       
   248         with self.admin_access.web_request() as req:
       
   249             geid = req.execute('CWGroup X LIMIT 1')[0][0]
       
   250             self.view('inline-creation', None, etype='CWUser', rtype='in_group',
       
   251                       peid=geid, petype='CWGroup', i18nctx='', role='object', pform=MOCKPFORM,
       
   252                       template=None, req=req)
       
   253 
       
   254 MOCKPFORM = mock_object(form_previous_values={}, form_valerror=None)
       
   255 
       
   256 if __name__ == '__main__':
       
   257     unittest_main()