devtools/testlib.py
changeset 9440 6880674c1a26
parent 9363 d773589b6d46
parent 9427 9c13ebd45cb6
child 9478 2d7521881d3d
child 9594 e549ef4be945
equal deleted inserted replaced
9439:549c999d06d2 9440:6880674c1a26
     1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    82     return set(schema.entities()) - protected_entities
    82     return set(schema.entities()) - protected_entities
    83 
    83 
    84 class JsonValidator(object):
    84 class JsonValidator(object):
    85     def parse_string(self, data):
    85     def parse_string(self, data):
    86         return json.loads(data)
    86         return json.loads(data)
       
    87 
       
    88 @contextmanager
       
    89 def real_error_handling(app):
       
    90     """By default, CubicWebTC `app` attribute (ie the publisher) is monkey
       
    91     patched so that unexpected error are raised rather than going through the
       
    92     `error_handler` method.
       
    93 
       
    94     By using this context manager you disable this monkey-patching temporarily.
       
    95     Hence when publishihng a request no error will be raised, you'll get
       
    96     req.status_out set to an HTTP error status code and the generated page will
       
    97     usually hold a traceback as HTML.
       
    98 
       
    99     >>> with real_error_handling(app):
       
   100     >>>     page = app.handle_request(req)
       
   101     """
       
   102     # remove the monkey patched error handler
       
   103     fake_error_handler = app.error_handler
       
   104     del app.error_handler
       
   105     # return the app
       
   106     yield app
       
   107     # restore
       
   108     app.error_handler = fake_error_handler
    87 
   109 
    88 # email handling, to test emails sent by an application ########################
   110 # email handling, to test emails sent by an application ########################
    89 
   111 
    90 MAILBOX = []
   112 MAILBOX = []
    91 
   113 
   518     def assertItemsEqual(self, it1, it2, *args, **kwargs):
   540     def assertItemsEqual(self, it1, it2, *args, **kwargs):
   519         it1 = set(getattr(x, 'eid', x) for x in it1)
   541         it1 = set(getattr(x, 'eid', x) for x in it1)
   520         it2 = set(getattr(x, 'eid', x) for x in it2)
   542         it2 = set(getattr(x, 'eid', x) for x in it2)
   521         super(CubicWebTC, self).assertItemsEqual(it1, it2, *args, **kwargs)
   543         super(CubicWebTC, self).assertItemsEqual(it1, it2, *args, **kwargs)
   522 
   544 
   523     def assertMessageEqual(self, req, params, msg):
   545     def assertMessageEqual(self, req, params, expected_msg):
   524         msg = req.session.data[params['_cwmsgid']]
   546         msg = req.session.data[params['_cwmsgid']]
   525         self.assertEqual(msg, msg)
   547         self.assertEqual(expected_msg, msg)
   526 
   548 
   527     # workflow utilities #######################################################
   549     # workflow utilities #######################################################
   528 
   550 
   529     def assertPossibleTransitions(self, entity, expected):
   551     def assertPossibleTransitions(self, entity, expected):
   530         transitions = entity.cw_adapt_to('IWorkflowable').possible_transitions()
   552         transitions = entity.cw_adapt_to('IWorkflowable').possible_transitions()
   700             req.form.update(data)
   722             req.form.update(data)
   701         ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   723         ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   702         return self.ctrl_publish(req, ctrlid, rset)
   724         return self.ctrl_publish(req, ctrlid, rset)
   703 
   725 
   704     def http_publish(self, url, data=None):
   726     def http_publish(self, url, data=None):
   705         """like `url_publish`, except this returns a http response, even in case of errors"""
   727         """like `url_publish`, except this returns a http response, even in case
       
   728         of errors. You may give form parameters using the `data` argument.
       
   729         """
   706         req = self.req_from_url(url)
   730         req = self.req_from_url(url)
   707         if data is not None:
   731         if data is not None:
   708             req.form.update(data)
   732             req.form.update(data)
   709         # remove the monkey patched error handler
   733         with real_error_handling(self.app):
   710         fake_error_handler = self.app.error_handler
       
   711         del self.app.error_handler
       
   712         try:
       
   713             result = self.app_handle_request(req, req.relative_path(False))
   734             result = self.app_handle_request(req, req.relative_path(False))
   714         finally:
       
   715             self.app.error_handler = fake_error_handler
       
   716         return result, req
   735         return result, req
   717 
   736 
   718     @staticmethod
   737     @staticmethod
   719     def _parse_location(req, location):
   738     def _parse_location(req, location):
   720         try:
   739         try: