web/test/unittest_views_baseforms.py
changeset 0 b97547f5f1fa
child 16 a70ece4d9d1a
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """cubicweb.web.views.baseforms unit tests"""
       
     2 
       
     3 from StringIO import StringIO
       
     4 import re
       
     5 
       
     6 from logilab.common.testlib import unittest_main
       
     7 from cubicweb.devtools.apptest import EnvBasedTC
       
     8 
       
     9 from cubicweb.entities import AnyEntity
       
    10 
       
    11 from mx.DateTime import DateTime
       
    12 from cubicweb.web import widgets
       
    13 orig_today = widgets.today
       
    14 orig_now = widgets.now
       
    15 
       
    16 def setup_module(options):
       
    17     def _today():
       
    18         return DateTime(0000, 1, 1)
       
    19     widgets.today = widgets.now = _today
       
    20 
       
    21 def teardown_module(options):
       
    22     widgets.today = orig_today
       
    23     widgets.now = orig_now
       
    24 
       
    25 
       
    26 def cleanup_text(text):
       
    27     return re.sub('\d\d:\d\d', 'hh:mm', re.sub('\d+/\d\d/\d\d', 'YYYY/MM/DD', '\n'.join(l.strip() for l in text.splitlines() if l.strip())))
       
    28 
       
    29 
       
    30 
       
    31 class EditionFormTC(EnvBasedTC):
       
    32 
       
    33     def setup_database(self):
       
    34         self.create_user('joe')
       
    35         
       
    36     def _build_creation_form(self, etype):
       
    37         req = self.request()
       
    38         req.next_tabindex()
       
    39         req.next_tabindex()
       
    40         req.del_page_data()
       
    41         req.form['etype'] = etype
       
    42         view = self.vreg.select_view('creation', req, None)
       
    43         entity = self.vreg.etype_class(etype)(req, None, None)
       
    44         buffer = StringIO()
       
    45         view.w = buffer.write
       
    46         view.edit_form(entity, {})
       
    47         return buffer.getvalue()
       
    48     
       
    49     def _test_view_for(self, etype, expected):
       
    50         self.assertTextEquals(expected, cleanup_text(self._build_creation_form(etype)))
       
    51         
       
    52     def test_base(self):
       
    53         self._test_view_for('EGroup', '''\
       
    54 <form id="entityForm" class="entityForm" cubicweb:target="eformframe"
       
    55 method="post" onsubmit="return freezeFormButtons('entityForm')" enctype="application/x-www-form-urlencoded" action="http://testing.fr/cubicweb/validateform">
       
    56 <div class="formTitle"><span>egroup (creation)</span></div>
       
    57 <div id="progress">validating...</div>
       
    58 <div class="iformTitle"><span>main informations</span></div>
       
    59 <div class="formBody"><fieldset>
       
    60 <input type="hidden" name="eid" value="A" />
       
    61 <input type="hidden" name="__type:A" value="EGroup" />
       
    62 <input type="hidden" name="__maineid" value="A" />
       
    63 <input id="errorurl" type="hidden" name="__errorurl" value="http://testing.fr/cubicweb/view?rql=Blop&amp;vid=blop" />
       
    64 <input type="hidden" name="__form_id" value="edition" />
       
    65 <input type="hidden" name="__message" value="element created" />
       
    66 <table id="entityFormA" class="attributeForm" style="width:100%;">
       
    67 <tr>
       
    68 <th class="labelCol"><label class="required" for="name:A">name</label></th>
       
    69 <td style="width:100%;">
       
    70 <input type="hidden" name="edits-name:A" value="__cubicweb_internal_field__"/>
       
    71 <input type="text" name="name:A" value="" accesskey="n" id="name:A" maxlength="64" size="40" tabindex="2"/>
       
    72 <br/>
       
    73 </td>
       
    74 </tr>
       
    75 </table>
       
    76 </fieldset>
       
    77 </div>
       
    78 <table width="100%">
       
    79 <tbody>
       
    80 <tr><td align="center">
       
    81 <input class="validateButton" type="submit" name="defaultsubmit" value="Button_ok" tabindex="3"/>
       
    82 </td><td style="align: right; width: 50%;">
       
    83 <input class="validateButton" type="button" onclick="postForm('__action_apply', 'Button_apply', 'entityForm')" value="Button_apply" tabindex="4"/>
       
    84 <input class="validateButton" type="button" onclick="postForm('__action_cancel', 'Button_cancel', 'entityForm')" value="Button_cancel" tabindex="5"/>
       
    85 </td></tr>
       
    86 </tbody>
       
    87 </table>
       
    88 </form>''')
       
    89 
       
    90     def test_with_inline_view(self):
       
    91         activated = self.execute('Any X WHERE X is State, X name "activated"')[0][0]
       
    92         self._test_view_for('EUser', '''<form id="entityForm" class="entityForm" cubicweb:target="eformframe"
       
    93 method="post" onsubmit="return freezeFormButtons('entityForm')" enctype="application/x-www-form-urlencoded" action="http://testing.fr/cubicweb/validateform">
       
    94 <div class="formTitle"><span>euser (creation)</span></div>
       
    95 <div id="progress">validating...</div>
       
    96 <div class="iformTitle"><span>main informations</span></div>
       
    97 <div class="formBody"><fieldset>
       
    98 <input type="hidden" name="eid" value="A" />
       
    99 <input type="hidden" name="__type:A" value="EUser" />
       
   100 <input type="hidden" name="__maineid" value="A" />
       
   101 <input id="errorurl" type="hidden" name="__errorurl" value="http://testing.fr/cubicweb/view?rql=Blop&amp;vid=blop" />
       
   102 <input type="hidden" name="__form_id" value="edition" />
       
   103 <input type="hidden" name="__message" value="element created" />
       
   104 <table id="entityFormA" class="attributeForm" style="width:100%%;">
       
   105 <tr>
       
   106 <th class="labelCol"><label class="required" for="login:A">login</label></th>
       
   107 <td style="width:100%%;">
       
   108 <input type="hidden" name="edits-login:A" value="__cubicweb_internal_field__"/>
       
   109 <input type="text" name="login:A" value="" accesskey="l" id="login:A" maxlength="64" size="40" tabindex="2"/>
       
   110 <br/>&nbsp;<span class="helper">unique identifier used to connect to the application</span>
       
   111 </td>
       
   112 </tr>
       
   113 <tr>
       
   114 <th class="labelCol"><label class="required" for="upassword:A">upassword</label></th>
       
   115 <td style="width:100%%;">
       
   116 <input type="hidden" name="edits-upassword:A" value="__cubicweb_internal_field__"/>
       
   117 <input type="password" name="upassword:A" value="" accesskey="u" id="upassword:A" tabindex="3"/><br/>
       
   118 <input type="password" name="upassword-confirm:A" id="upassword-confirm:A" tabindex="4"/>&nbsp;<span class="emphasis">(confirm password)</span>
       
   119 <br/>
       
   120 </td>
       
   121 </tr>
       
   122 <tr>
       
   123 <th class="labelCol"><label for="firstname:A">firstname</label></th>
       
   124 <td style="width:100%%;">
       
   125 <input type="hidden" name="edits-firstname:A" value="__cubicweb_internal_field__"/>
       
   126 <input type="text" name="firstname:A" value="" accesskey="f" id="firstname:A" maxlength="64" size="40" tabindex="5"/>
       
   127 <br/>
       
   128 </td>
       
   129 </tr>
       
   130 <tr>
       
   131 <th class="labelCol"><label for="surname:A">surname</label></th>
       
   132 <td style="width:100%%;">
       
   133 <input type="hidden" name="edits-surname:A" value="__cubicweb_internal_field__"/>
       
   134 <input type="text" name="surname:A" value="" accesskey="s" id="surname:A" maxlength="64" size="40" tabindex="6"/>
       
   135 <br/>
       
   136 </td>
       
   137 </tr>
       
   138 <tr>
       
   139 <th class="labelCol"><label class="required" for="in_group:A">in_group</label></th>
       
   140 <td style="width:100%%;">
       
   141 <input type="hidden" name="edits-in_group:A" value="__cubicweb_internal_field__"/>
       
   142 <select name="in_group:A" id="in_group:A" multiple="multiple" size="5" tabindex="7">
       
   143 <option value="3" >guests</option>
       
   144 <option value="1" >managers</option>
       
   145 <option value="2" >users</option>
       
   146 </select>
       
   147 <br/>&nbsp;<span class="helper">groups grant permissions to the user</span>
       
   148 </td>
       
   149 </tr>
       
   150 <tr>
       
   151 <th class="labelCol"><label class="required" for="in_state:A">in_state</label></th>
       
   152 <td style="width:100%%;">
       
   153 <input type="hidden" name="edits-in_state:A" value="__cubicweb_internal_field__"/>
       
   154 <select name="in_state:A" id="in_state:A" tabindex="8">
       
   155 <option value="%(activated)s" >activated</option>
       
   156 </select>
       
   157 <br/>&nbsp;<span class="helper">account state</span>
       
   158 </td>
       
   159 </tr>
       
   160 </table>
       
   161 <div id="inlineuse_emailslot">
       
   162 <div class="inlinedform" id="addNewEmailAddressuse_emailsubject:A" cubicweb:limit="true">
       
   163 <a class="addEntity" id="adduse_email:Alink" href="javascript: addInlineCreationForm('A', 'EUser', 'EmailAddress', 'use_email', 'subject')" >+ add a EmailAddress.</a>
       
   164 </div>
       
   165 <div class="trame_grise">&nbsp;</div>
       
   166 </div>
       
   167 </fieldset>
       
   168 </div>
       
   169 <table width="100%%">
       
   170 <tbody>
       
   171 <tr><td align="center">
       
   172 <input class="validateButton" type="submit" name="defaultsubmit" value="Button_ok" tabindex="9"/>
       
   173 </td><td style="align: right; width: 50%%;">
       
   174 <input class="validateButton" type="button" onclick="postForm('__action_apply', 'Button_apply', 'entityForm')" value="Button_apply" tabindex="10"/>
       
   175 <input class="validateButton" type="button" onclick="postForm('__action_cancel', 'Button_cancel', 'entityForm')" value="Button_cancel" tabindex="11"/>
       
   176 </td></tr>
       
   177 </tbody>
       
   178 </table>
       
   179 </form>''' % {'activated' : activated})
       
   180 
       
   181     def test_redirection_after_creation(self):
       
   182         req = self.request()
       
   183         req.form['etype'] = 'EUser'
       
   184         view = self.vreg.select_view('creation', req, None)
       
   185         self.assertEquals(view.redirect_url(), 'http://testing.fr/cubicweb/euser')
       
   186         req.form['__redirectrql'] = 'Any X WHERE X eid 3012'
       
   187         req.form['__redirectvid'] = 'avid'
       
   188         self.assertEquals(view.redirect_url(), 'http://testing.fr/cubicweb/view?rql=Any%20X%20WHERE%20X%20eid%203012&vid=avid')
       
   189 
       
   190 
       
   191     def test_need_multipart(self):
       
   192         req = self.request()
       
   193         class Salesterm(AnyEntity):
       
   194             id = 'Salesterm'
       
   195             __rtags__ = {'described_by_test' : 'inlineview'}
       
   196         vreg = self.vreg
       
   197         vreg.register_vobject_class(Salesterm)
       
   198         req.form['etype'] = 'Salesterm'
       
   199         entity = vreg.etype_class('Salesterm')(req, None, None)
       
   200         view = vreg.select_view('creation', req, None)
       
   201         self.failUnless(view.need_multipart(entity))
       
   202         
       
   203 
       
   204 
       
   205     def test_nonregr_check_add_permission_on_relation(self):
       
   206         from eblog.entities import BlogEntry
       
   207         class BlogEntryPlus(BlogEntry):
       
   208             __rtags__ = {'checked_by': 'primary'}
       
   209         self.vreg.register_vobject_class(BlogEntryPlus)
       
   210         # an admin should be able to edit the checked_by relation
       
   211         html = self._build_creation_form('BlogEntry')
       
   212         self.failUnless('name="edits-checked_by:A"' in html)
       
   213         # a regular user should not be able to see the relation
       
   214         self.login('joe')
       
   215         html = self._build_creation_form('BlogEntry')
       
   216         self.failIf('name="edits-checked_by:A"' in html)
       
   217         
       
   218 from cubicweb.devtools.testlib import WebTest
       
   219 from cubicweb.devtools.htmlparser import DTDValidator
       
   220 
       
   221 class CopyWebTest(WebTest):
       
   222 
       
   223     def setup_database(self):
       
   224         p = self.create_user("Doe")
       
   225         # do not try to skip 'primary_email' for this test
       
   226         e = self.add_entity('EmailAddress', address=u'doe@doe.com')
       
   227         self.execute('SET P use_email E, P primary_email E WHERE P eid %(p)s, E eid %(e)s',
       
   228                      {'p' : p.eid, 'e' : e.eid})
       
   229 
       
   230 
       
   231     def test_cloned_elements_in_copy_form(self):
       
   232         rset = self.execute('EUser P WHERE P login "Doe"')
       
   233         output = self.view('copy', rset)
       
   234         clones = [attrs for _, attrs in output.input_tags
       
   235                   if attrs.get('name', '').startswith('__cloned_eid')]
       
   236         # the only cloned entity should be the original person
       
   237         self.assertEquals(len(clones), 1)
       
   238         attrs = clones[0]
       
   239         self.assertEquals(attrs['name'], '__cloned_eid:A')
       
   240         self.assertEquals(int(attrs['value']), rset[0][0])
       
   241 
       
   242 
       
   243 if __name__ == '__main__':
       
   244     unittest_main()