diff -r 000000000000 -r b97547f5f1fa web/test/unittest_views_baseforms.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/unittest_views_baseforms.py Wed Nov 05 15:52:50 2008 +0100 @@ -0,0 +1,244 @@ +"""cubicweb.web.views.baseforms unit tests""" + +from StringIO import StringIO +import re + +from logilab.common.testlib import unittest_main +from cubicweb.devtools.apptest import EnvBasedTC + +from cubicweb.entities import AnyEntity + +from mx.DateTime import DateTime +from cubicweb.web import widgets +orig_today = widgets.today +orig_now = widgets.now + +def setup_module(options): + def _today(): + return DateTime(0000, 1, 1) + widgets.today = widgets.now = _today + +def teardown_module(options): + widgets.today = orig_today + widgets.now = orig_now + + +def cleanup_text(text): + 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()))) + + + +class EditionFormTC(EnvBasedTC): + + def setup_database(self): + self.create_user('joe') + + def _build_creation_form(self, etype): + req = self.request() + req.next_tabindex() + req.next_tabindex() + req.del_page_data() + req.form['etype'] = etype + view = self.vreg.select_view('creation', req, None) + entity = self.vreg.etype_class(etype)(req, None, None) + buffer = StringIO() + view.w = buffer.write + view.edit_form(entity, {}) + return buffer.getvalue() + + def _test_view_for(self, etype, expected): + self.assertTextEquals(expected, cleanup_text(self._build_creation_form(etype))) + + def test_base(self): + self._test_view_for('EGroup', '''\ +
+
egroup (creation)
+
validating...
+
main informations
+
+ + + + + + + + + + + +
+ + +
+
+
+
+ + + + +
+ + + + +
+
''') + + def test_with_inline_view(self): + activated = self.execute('Any X WHERE X is State, X name "activated"')[0][0] + self._test_view_for('EUser', '''
+
euser (creation)
+
validating...
+
main informations
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
 unique identifier used to connect to the application +
+ +
+ (confirm password) +
+
+ + +
+
+ + +
+
+ + +
 groups grant permissions to the user +
+ + +
 account state +
+ +
+
+ + + + +
+ + + + +
+
''' % {'activated' : activated}) + + def test_redirection_after_creation(self): + req = self.request() + req.form['etype'] = 'EUser' + view = self.vreg.select_view('creation', req, None) + self.assertEquals(view.redirect_url(), 'http://testing.fr/cubicweb/euser') + req.form['__redirectrql'] = 'Any X WHERE X eid 3012' + req.form['__redirectvid'] = 'avid' + self.assertEquals(view.redirect_url(), 'http://testing.fr/cubicweb/view?rql=Any%20X%20WHERE%20X%20eid%203012&vid=avid') + + + def test_need_multipart(self): + req = self.request() + class Salesterm(AnyEntity): + id = 'Salesterm' + __rtags__ = {'described_by_test' : 'inlineview'} + vreg = self.vreg + vreg.register_vobject_class(Salesterm) + req.form['etype'] = 'Salesterm' + entity = vreg.etype_class('Salesterm')(req, None, None) + view = vreg.select_view('creation', req, None) + self.failUnless(view.need_multipart(entity)) + + + + def test_nonregr_check_add_permission_on_relation(self): + from eblog.entities import BlogEntry + class BlogEntryPlus(BlogEntry): + __rtags__ = {'checked_by': 'primary'} + self.vreg.register_vobject_class(BlogEntryPlus) + # an admin should be able to edit the checked_by relation + html = self._build_creation_form('BlogEntry') + self.failUnless('name="edits-checked_by:A"' in html) + # a regular user should not be able to see the relation + self.login('joe') + html = self._build_creation_form('BlogEntry') + self.failIf('name="edits-checked_by:A"' in html) + +from cubicweb.devtools.testlib import WebTest +from cubicweb.devtools.htmlparser import DTDValidator + +class CopyWebTest(WebTest): + + def setup_database(self): + p = self.create_user("Doe") + # do not try to skip 'primary_email' for this test + e = self.add_entity('EmailAddress', address=u'doe@doe.com') + self.execute('SET P use_email E, P primary_email E WHERE P eid %(p)s, E eid %(e)s', + {'p' : p.eid, 'e' : e.eid}) + + + def test_cloned_elements_in_copy_form(self): + rset = self.execute('EUser P WHERE P login "Doe"') + output = self.view('copy', rset) + clones = [attrs for _, attrs in output.input_tags + if attrs.get('name', '').startswith('__cloned_eid')] + # the only cloned entity should be the original person + self.assertEquals(len(clones), 1) + attrs = clones[0] + self.assertEquals(attrs['name'], '__cloned_eid:A') + self.assertEquals(int(attrs['value']), rset[0][0]) + + +if __name__ == '__main__': + unittest_main()