37 from cubicweb.entities.authobjs import CWUser |
37 from cubicweb.entities.authobjs import CWUser |
38 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes |
38 from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes |
39 from cubicweb.web.views.basecontrollers import JSonController, xhtmlize, jsonize |
39 from cubicweb.web.views.basecontrollers import JSonController, xhtmlize, jsonize |
40 from cubicweb.web.views.ajaxcontroller import ajaxfunc, AjaxFunction |
40 from cubicweb.web.views.ajaxcontroller import ajaxfunc, AjaxFunction |
41 import cubicweb.transaction as tx |
41 import cubicweb.transaction as tx |
|
42 from cubicweb.server.hook import Hook, Operation |
|
43 from cubicweb.predicates import is_instance |
42 |
44 |
43 u = unicode |
45 u = unicode |
44 |
46 |
45 def req_form(user): |
47 def req_form(user): |
46 return {'eid': [str(user.eid)], |
48 return {'eid': [str(user.eid)], |
285 } |
287 } |
286 with self.assertRaises(ValidationError) as cm: |
288 with self.assertRaises(ValidationError) as cm: |
287 self.ctrl_publish(req) |
289 self.ctrl_publish(req) |
288 cm.exception.translate(unicode) |
290 cm.exception.translate(unicode) |
289 self.assertEqual(cm.exception.errors, {'amount-subject': 'value 110 must be <= 100'}) |
291 self.assertEqual(cm.exception.errors, {'amount-subject': 'value 110 must be <= 100'}) |
|
292 |
290 req = self.request(rollbackfirst=True) |
293 req = self.request(rollbackfirst=True) |
291 req.form = {'eid': ['X'], |
294 req.form = {'eid': ['X'], |
292 '__type:X': 'Salesterm', |
295 '__type:X': 'Salesterm', |
293 '_cw_entity_fields:X': 'amount-subject,described_by_test-subject', |
296 '_cw_entity_fields:X': 'amount-subject,described_by_test-subject', |
294 'amount-subject:X': u'10', |
297 'amount-subject:X': u'10', |
298 # should be redirected on the created |
301 # should be redirected on the created |
299 #eid = params['rql'].split()[-1] |
302 #eid = params['rql'].split()[-1] |
300 e = self.execute('Salesterm X').get_entity(0, 0) |
303 e = self.execute('Salesterm X').get_entity(0, 0) |
301 self.assertEqual(e.amount, 10) |
304 self.assertEqual(e.amount, 10) |
302 |
305 |
|
306 def test_interval_bound_constraint_validateform(self): |
|
307 """Test the FormValidatorController controller on entity with |
|
308 constrained attributes""" |
|
309 feid = self.execute('INSERT File X: X data_name "toto.txt", X data %(data)s', |
|
310 {'data': Binary('yo')})[0][0] |
|
311 seid = self.request().create_entity('Salesterm', amount=0, described_by_test=feid).eid |
|
312 self.commit() |
|
313 |
|
314 # ensure a value that violate a constraint is properly detected |
|
315 req = self.request(rollbackfirst=True) |
|
316 req.form = {'eid': [unicode(seid)], |
|
317 '__type:%s'%seid: 'Salesterm', |
|
318 '_cw_entity_fields:%s'%seid: 'amount-subject', |
|
319 'amount-subject:%s'%seid: u'-10', |
|
320 } |
|
321 self.assertEqual('''<script type="text/javascript"> |
|
322 window.parent.handleFormValidationResponse('entityForm', null, null, [false, [%s, {"amount-subject": "value -10 must be >= 0"}], null], null); |
|
323 </script>'''%seid, self.ctrl_publish(req, 'validateform')) |
|
324 |
|
325 # ensure a value that comply a constraint is properly processed |
|
326 req = self.request(rollbackfirst=True) |
|
327 req.form = {'eid': [unicode(seid)], |
|
328 '__type:%s'%seid: 'Salesterm', |
|
329 '_cw_entity_fields:%s'%seid: 'amount-subject', |
|
330 'amount-subject:%s'%seid: u'20', |
|
331 } |
|
332 self.assertEqual('''<script type="text/javascript"> |
|
333 window.parent.handleFormValidationResponse('entityForm', null, null, [true, "http://testing.fr/cubicweb/view", null], null); |
|
334 </script>''', self.ctrl_publish(req, 'validateform')) |
|
335 self.assertEqual(20, self.execute('Any V WHERE X amount V, X eid %(eid)s', {'eid': seid})[0][0]) |
|
336 |
|
337 req = self.request(rollbackfirst=True) |
|
338 req.form = {'eid': ['X'], |
|
339 '__type:X': 'Salesterm', |
|
340 '_cw_entity_fields:X': 'amount-subject,described_by_test-subject', |
|
341 'amount-subject:X': u'0', |
|
342 'described_by_test-subject:X': u(feid), |
|
343 } |
|
344 |
|
345 # ensure a value that is modified in an operation on a modify |
|
346 # hook works as it should (see |
|
347 # https://www.cubicweb.org/ticket/2509729 ) |
|
348 class MyOperation(Operation): |
|
349 def precommit_event(self): |
|
350 self.entity.cw_set(amount=-10) |
|
351 class ValidationErrorInOpAfterHook(Hook): |
|
352 __regid__ = 'valerror-op-after-hook' |
|
353 __select__ = Hook.__select__ & is_instance('Salesterm') |
|
354 events = ('after_add_entity',) |
|
355 def __call__(self): |
|
356 MyOperation(self._cw, entity=self.entity) |
|
357 |
|
358 with self.temporary_appobjects(ValidationErrorInOpAfterHook): |
|
359 self.assertEqual('''<script type="text/javascript"> |
|
360 window.parent.handleFormValidationResponse('entityForm', null, null, [false, ["X", {"amount-subject": "value -10 must be >= 0"}], null], null); |
|
361 </script>''', self.ctrl_publish(req, 'validateform')) |
|
362 |
|
363 self.assertEqual('''<script type="text/javascript"> |
|
364 window.parent.handleFormValidationResponse('entityForm', null, null, [true, "http://testing.fr/cubicweb/view", null], null); |
|
365 </script>''', self.ctrl_publish(req, 'validateform')) |
|
366 |
303 def test_req_pending_insert(self): |
367 def test_req_pending_insert(self): |
304 """make sure req's pending insertions are taken into account""" |
368 """make sure req's pending insertions are taken into account""" |
305 tmpgroup = self.request().create_entity('CWGroup', name=u"test") |
369 tmpgroup = self.request().create_entity('CWGroup', name=u"test") |
306 user = self.user() |
370 user = self.user() |
307 req = self.request(**req_form(user)) |
371 req = self.request(**req_form(user)) |
309 path, params = self.expect_redirect_handle_request(req, 'edit') |
373 path, params = self.expect_redirect_handle_request(req, 'edit') |
310 usergroups = [gname for gname, in |
374 usergroups = [gname for gname, in |
311 self.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})] |
375 self.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})] |
312 self.assertItemsEqual(usergroups, ['managers', 'test']) |
376 self.assertItemsEqual(usergroups, ['managers', 'test']) |
313 self.assertEqual(get_pending_inserts(req), []) |
377 self.assertEqual(get_pending_inserts(req), []) |
314 |
|
315 |
378 |
316 def test_req_pending_delete(self): |
379 def test_req_pending_delete(self): |
317 """make sure req's pending deletions are taken into account""" |
380 """make sure req's pending deletions are taken into account""" |
318 user = self.user() |
381 user = self.user() |
319 groupeid = self.execute('INSERT CWGroup G: G name "test", U in_group G WHERE U eid %(x)s', |
382 groupeid = self.execute('INSERT CWGroup G: G name "test", U in_group G WHERE U eid %(x)s', |