25 |
25 |
26 from cubicweb import Binary |
26 from cubicweb import Binary |
27 from cubicweb.devtools.testlib import CubicWebTC |
27 from cubicweb.devtools.testlib import CubicWebTC |
28 from cubicweb.predicates import (is_instance, adaptable, match_kwargs, match_user_groups, |
28 from cubicweb.predicates import (is_instance, adaptable, match_kwargs, match_user_groups, |
29 multi_lines_rset, score_entity, is_in_state, |
29 multi_lines_rset, score_entity, is_in_state, |
30 rql_condition, relation_possible) |
30 rql_condition, relation_possible, match_form_params) |
31 from cubicweb.selectors import on_transition # XXX on_transition is deprecated |
31 from cubicweb.selectors import on_transition # XXX on_transition is deprecated |
32 from cubicweb.view import EntityAdapter |
32 from cubicweb.view import EntityAdapter |
33 from cubicweb.web import action |
33 from cubicweb.web import action |
34 |
34 |
35 |
35 |
389 with self.admin_access.web_request() as req: |
389 with self.admin_access.web_request() as req: |
390 selector = adaptable('IWhatever') |
390 selector = adaptable('IWhatever') |
391 rset = req.execute('Any X WHERE X is IN(CWGroup, CWUser)') |
391 rset = req.execute('Any X WHERE X is IN(CWGroup, CWUser)') |
392 self.assertTrue(selector(None, req, rset=rset)) |
392 self.assertTrue(selector(None, req, rset=rset)) |
393 |
393 |
|
394 |
|
395 class MatchFormParamsTC(CubicWebTC): |
|
396 """tests for match_form_params predicate""" |
|
397 |
|
398 def test_keyonly_match(self): |
|
399 """test standard usage: ``match_form_params('param1', 'param2')`` |
|
400 |
|
401 ``param1`` and ``param2`` must be specified in request's form. |
|
402 """ |
|
403 web_request = self.admin_access.web_request |
|
404 vid_selector = match_form_params('vid') |
|
405 vid_subvid_selector = match_form_params('vid', 'subvid') |
|
406 # no parameter => KO,KO |
|
407 with web_request() as req: |
|
408 self.assertEqual(vid_selector(None, req), 0) |
|
409 self.assertEqual(vid_subvid_selector(None, req), 0) |
|
410 # one expected parameter found => OK,KO |
|
411 with web_request(vid='foo') as req: |
|
412 self.assertEqual(vid_selector(None, req), 1) |
|
413 self.assertEqual(vid_subvid_selector(None, req), 0) |
|
414 # all expected parameters found => OK,OK |
|
415 with web_request(vid='foo', subvid='bar') as req: |
|
416 self.assertEqual(vid_selector(None, req), 1) |
|
417 self.assertEqual(vid_subvid_selector(None, req), 2) |
|
418 |
|
419 def test_keyvalue_match_one_parameter(self): |
|
420 """test dict usage: ``match_form_params(param1=value1)`` |
|
421 |
|
422 ``param1`` must be specified in the request's form and its value |
|
423 must be ``value1``. |
|
424 """ |
|
425 web_request = self.admin_access.web_request |
|
426 # test both positional and named parameters |
|
427 vid_selector = match_form_params(vid='foo') |
|
428 # no parameter => should fail |
|
429 with web_request() as req: |
|
430 self.assertEqual(vid_selector(None, req), 0) |
|
431 # expected parameter found with expected value => OK |
|
432 with web_request(vid='foo', subvid='bar') as req: |
|
433 self.assertEqual(vid_selector(None, req), 1) |
|
434 # expected parameter found but value is incorrect => KO |
|
435 with web_request(vid='bar') as req: |
|
436 self.assertEqual(vid_selector(None, req), 0) |
|
437 |
|
438 def test_keyvalue_match_two_parameters(self): |
|
439 """test dict usage: ``match_form_params(param1=value1, param2=value2)`` |
|
440 |
|
441 ``param1`` and ``param2`` must be specified in the request's form and |
|
442 their respective value must be ``value1`` and ``value2``. |
|
443 """ |
|
444 web_request = self.admin_access.web_request |
|
445 vid_subvid_selector = match_form_params(vid='list', subvid='tsearch') |
|
446 # missing one expected parameter => KO |
|
447 with web_request(vid='list') as req: |
|
448 self.assertEqual(vid_subvid_selector(None, req), 0) |
|
449 # expected parameters found but values are incorrect => KO |
|
450 with web_request(vid='list', subvid='foo') as req: |
|
451 self.assertEqual(vid_subvid_selector(None, req), 0) |
|
452 # expected parameters found and values are correct => OK |
|
453 with web_request(vid='list', subvid='tsearch') as req: |
|
454 self.assertEqual(vid_subvid_selector(None, req), 2) |
|
455 |
|
456 def test_keyvalue_multiple_match(self): |
|
457 """test dict usage with multiple values |
|
458 |
|
459 i.e. as in ``match_form_params(param1=('value1', 'value2'))`` |
|
460 |
|
461 ``param1`` must be specified in the request's form and its value |
|
462 must be either ``value1`` or ``value2``. |
|
463 """ |
|
464 web_request = self.admin_access.web_request |
|
465 vid_subvid_selector = match_form_params(vid='list', subvid=('tsearch', 'listitem')) |
|
466 # expected parameters found and values correct => OK |
|
467 with web_request(vid='list', subvid='tsearch') as req: |
|
468 self.assertEqual(vid_subvid_selector(None, req), 2) |
|
469 with web_request(vid='list', subvid='listitem') as req: |
|
470 self.assertEqual(vid_subvid_selector(None, req), 2) |
|
471 # expected parameters found but values are incorrect => OK |
|
472 with web_request(vid='list', subvid='foo') as req: |
|
473 self.assertEqual(vid_subvid_selector(None, req), 0) |
|
474 |
|
475 def test_invalid_calls(self): |
|
476 """checks invalid calls raise a ValueError""" |
|
477 # mixing named and positional arguments should fail |
|
478 with self.assertRaises(ValueError) as cm: |
|
479 match_form_params('list', x='1', y='2') |
|
480 self.assertEqual(str(cm.exception), |
|
481 "match_form_params() can't be called with both " |
|
482 "positional and named arguments") |
|
483 # using a dict as first and unique argument should fail |
|
484 with self.assertRaises(ValueError) as cm: |
|
485 match_form_params({'x': 1}) |
|
486 self.assertEqual(str(cm.exception), |
|
487 "match_form_params() positional arguments must be strings") |
|
488 |
|
489 |
394 if __name__ == '__main__': |
490 if __name__ == '__main__': |
395 unittest_main() |
491 unittest_main() |
396 |
492 |