web/test/unittest_views_basecontrollers.py
changeset 10016 984505da8b89
parent 9846 67c1b77b44be
child 10075 136b5f995f8e
--- a/web/test/unittest_views_basecontrollers.py	Wed Apr 09 16:58:58 2014 +0200
+++ b/web/test/unittest_views_basecontrollers.py	Thu Apr 25 10:16:25 2013 +0200
@@ -23,7 +23,11 @@
     from urlparse import parse_qs as url_parse_query
 except ImportError:
     from cgi import parse_qs as url_parse_query
+
+import lxml
+
 from logilab.common.testlib import unittest_main
+
 from logilab.common.decorators import monkeypatch
 
 from cubicweb import Binary, NoSelectableObject, ValidationError
@@ -81,6 +85,49 @@
                 self.assertEqual({'login-subject': 'the value "admin" is already used, use another one'},
                                  cm.exception.errors)
 
+    def test_simultaneous_edition_only_one_commit(self):
+        """ Allow two simultaneous edit view of the same entity as long as only one commits
+        """
+        with self.admin_access.web_request() as req:
+            e = req.create_entity('BlogEntry', title=u'cubicweb.org', content=u"hop")
+            expected_path = e.rest_path()
+            req.cnx.commit()
+            form = self.vreg['views'].select('edition', req, rset=e.as_rset(), row=0)
+            html_form = lxml.html.fromstring(form.render(w=None, action='edit')).forms[0]
+
+        with self.admin_access.web_request() as req2:
+            form2 = self.vreg['views'].select('edition', req, rset=e.as_rset(), row=0)
+
+        with self.admin_access.web_request(**dict(html_form.form_values())) as req:
+            path, args = self.expect_redirect_handle_request(req, path='edit')
+            self.assertEqual(path, expected_path)
+
+    def test_simultaneous_edition_refuse_second_commit(self):
+        """ Disallow committing changes to an entity edited in between """
+        with self.admin_access.web_request() as req:
+            e = req.create_entity('BlogEntry', title=u'cubicweb.org', content=u"hop")
+            eid = e.eid
+            req.cnx.commit()
+            form = self.vreg['views'].select('edition', req, rset=e.as_rset(), row=0)
+            html_form = lxml.html.fromstring(form.render(w=None, action='edit')).forms[0]
+
+        with self.admin_access.web_request() as req2:
+            e = req2.entity_from_eid(eid)
+            e.cw_set(content = u"hip")
+            req2.cnx.commit()
+
+        form_field_name = "content-subject:%d" % eid
+        form_values = dict(html_form.form_values())
+        assert form_field_name in form_values
+        form_values[form_field_name] = u'yep'
+        with self.admin_access.web_request(**form_values) as req:
+            with self.assertRaises(ValidationError) as cm:
+                self.ctrl_publish(req)
+            reported_eid, dict_info = cm.exception.args
+            self.assertEqual(reported_eid, eid)
+            self.assertIn(None, dict_info)
+            self.assertIn("has changed since you started to edit it.", dict_info[None])
+
     def test_user_editing_itself(self):
         """checking that a manager user can edit itself
         """