devtools/testlib.py
changeset 9990 c84ad981fc4a
parent 9933 3674f249ab1d
child 10017 58c7a075c793
equal deleted inserted replaced
9974:b240b33c7125 9990:c84ad981fc4a
   294     requestcls = fake.FakeRequest
   294     requestcls = fake.FakeRequest
   295     tags = TestCase.tags | Tags('cubicweb', 'cw_repo')
   295     tags = TestCase.tags | Tags('cubicweb', 'cw_repo')
   296     test_db_id = DEFAULT_EMPTY_DB_ID
   296     test_db_id = DEFAULT_EMPTY_DB_ID
   297     _cnxs = set() # establised connection
   297     _cnxs = set() # establised connection
   298                   # stay on connection for leak detection purpose
   298                   # stay on connection for leak detection purpose
       
   299 
       
   300     # anonymous is logged by default in cubicweb test cases
       
   301     anonymous_allowed = True
   299 
   302 
   300     def __init__(self, *args, **kwargs):
   303     def __init__(self, *args, **kwargs):
   301         self._admin_session = None
   304         self._admin_session = None
   302         self._admin_clt_cnx = None
   305         self._admin_clt_cnx = None
   303         self._current_session = None
   306         self._current_session = None
   530         # web resources
   533         # web resources
   531         try:
   534         try:
   532             config.global_set_option('embed-allowed', re.compile('.*'))
   535             config.global_set_option('embed-allowed', re.compile('.*'))
   533         except Exception: # not in server only configuration
   536         except Exception: # not in server only configuration
   534             pass
   537             pass
       
   538         config.set_anonymous_allowed(cls.anonymous_allowed)
   535 
   539 
   536     @property
   540     @property
   537     def vreg(self):
   541     def vreg(self):
   538         return self.repo.vreg
   542         return self.repo.vreg
   539 
   543 
   608 
   612 
   609     def setup_database(self):
   613     def setup_database(self):
   610         """add your database setup code by overriding this method"""
   614         """add your database setup code by overriding this method"""
   611 
   615 
   612     @classmethod
   616     @classmethod
   613     def pre_setup_database(cls, session, config):
   617     def pre_setup_database(cls, cnx, config):
   614         """add your pre database setup code by overriding this method
   618         """add your pre database setup code by overriding this method
   615 
   619 
   616         Do not forget to set the cls.test_db_id value to enable caching of the
   620         Do not forget to set the cls.test_db_id value to enable caching of the
   617         result.
   621         result.
   618         """
   622         """
   877         except web.Redirect:
   881         except web.Redirect:
   878             req.cnx.commit()
   882             req.cnx.commit()
   879             raise
   883             raise
   880         return result
   884         return result
   881 
   885 
       
   886     @deprecated('[3.19] use .admin_request_from_url instead')
   882     def req_from_url(self, url):
   887     def req_from_url(self, url):
   883         """parses `url` and builds the corresponding CW-web request
   888         """parses `url` and builds the corresponding CW-web request
   884 
   889 
   885         req.form will be setup using the url's query string
   890         req.form will be setup using the url's query string
   886         """
   891         """
   890         querystring = urlparse.urlparse(url)[-2]
   895         querystring = urlparse.urlparse(url)[-2]
   891         params = urlparse.parse_qs(querystring)
   896         params = urlparse.parse_qs(querystring)
   892         req.setup_params(params)
   897         req.setup_params(params)
   893         return req
   898         return req
   894 
   899 
       
   900     @contextmanager
       
   901     def admin_request_from_url(self, url):
       
   902         """parses `url` and builds the corresponding CW-web request
       
   903 
       
   904         req.form will be setup using the url's query string
       
   905         """
       
   906         with self.admin_access.web_request(url=url) as req:
       
   907             if isinstance(url, unicode):
       
   908                 url = url.encode(req.encoding) # req.setup_params() expects encoded strings
       
   909             querystring = urlparse.urlparse(url)[-2]
       
   910             params = urlparse.parse_qs(querystring)
       
   911             req.setup_params(params)
       
   912             yield req
       
   913 
   895     def url_publish(self, url, data=None):
   914     def url_publish(self, url, data=None):
   896         """takes `url`, uses application's app_resolver to find the appropriate
   915         """takes `url`, uses application's app_resolver to find the appropriate
   897         controller and result set, then publishes the result.
   916         controller and result set, then publishes the result.
   898 
   917 
   899         To simulate post of www-form-encoded data, give a `data` dictionary
   918         To simulate post of www-form-encoded data, give a `data` dictionary
   900         containing desired key/value associations.
   919         containing desired key/value associations.
   901 
   920 
   902         This should pretty much correspond to what occurs in a real CW server
   921         This should pretty much correspond to what occurs in a real CW server
   903         except the apache-rewriter component is not called.
   922         except the apache-rewriter component is not called.
   904         """
   923         """
   905         req = self.req_from_url(url)
   924         with self.admin_request_from_url(url) as req:
   906         if data is not None:
   925             if data is not None:
   907             req.form.update(data)
   926                 req.form.update(data)
   908         ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   927             ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
   909         return self.ctrl_publish(req, ctrlid, rset)
   928             return self.ctrl_publish(req, ctrlid, rset)
   910 
   929 
   911     def http_publish(self, url, data=None):
   930     def http_publish(self, url, data=None):
   912         """like `url_publish`, except this returns a http response, even in case
   931         """like `url_publish`, except this returns a http response, even in case
   913         of errors. You may give form parameters using the `data` argument.
   932         of errors. You may give form parameters using the `data` argument.
   914         """
   933         """
   915         req = self.req_from_url(url)
   934         with self.admin_request_from_url(url) as req:
   916         if data is not None:
   935             if data is not None:
   917             req.form.update(data)
   936                 req.form.update(data)
   918         with real_error_handling(self.app):
   937             with real_error_handling(self.app):
   919             result = self.app_handle_request(req, req.relative_path(False))
   938                 result = self.app_handle_request(req, req.relative_path(False))
   920         return result, req
   939             return result, req
   921 
   940 
   922     @staticmethod
   941     @staticmethod
   923     def _parse_location(req, location):
   942     def _parse_location(req, location):
   924         try:
   943         try:
   925             path, params = location.split('?', 1)
   944             path, params = location.split('?', 1)