# HG changeset patch # User Martin Bornhold # Date 2016-10-07 07:15:43 # Node ID eb8b2333864d92728ca31a46274031db6825ef98 # Parent cc8792380b0a94526c85b8543868546b4aca7a5c tests: Add a way to deactivate exception re-raising in tests. diff --git a/rhodecode/lib/middleware/error_handling.py b/rhodecode/lib/middleware/error_handling.py --- a/rhodecode/lib/middleware/error_handling.py +++ b/rhodecode/lib/middleware/error_handling.py @@ -20,7 +20,6 @@ import logging - from pyramid import httpexceptions from pyramid.httpexceptions import HTTPError, HTTPInternalServerError from pyramid.threadlocal import get_current_request @@ -40,7 +39,7 @@ class PylonsErrorHandlingMiddleware(obje def __init__(self, app, error_view, reraise=False): self.app = app self.error_view = error_view - self.reraise = reraise + self._reraise = reraise def __call__(self, environ, start_response): # We need to use the pyramid request here instead of creating a custom @@ -63,6 +62,9 @@ class PylonsErrorHandlingMiddleware(obje return (self.is_http_error(response) and not self.is_vcs_response(response)) + def reraise(self): + return self._reraise + def handle_request(self, request): """ Calls the underlying WSGI app (typically the old RhodeCode pylons app) @@ -83,7 +85,7 @@ class PylonsErrorHandlingMiddleware(obje except Exception as e: log.exception(e) - if self.reraise: + if self.reraise(): raise if isinstance(e, VCSCommunicationError): diff --git a/rhodecode/tests/lib/middleware/test_vcs_unavailable.py b/rhodecode/tests/lib/middleware/test_vcs_unavailable.py --- a/rhodecode/tests/lib/middleware/test_vcs_unavailable.py +++ b/rhodecode/tests/lib/middleware/test_vcs_unavailable.py @@ -20,8 +20,10 @@ import mock import pytest -import rhodecode import rhodecode.lib.vcs.client as client +from rhodecode.lib.middleware.error_handling import ( + PylonsErrorHandlingMiddleware) + @pytest.mark.usefixtures('autologin_user', 'app') def test_vcs_available_returns_summary_page(app, backend): @@ -35,13 +37,14 @@ def test_vcs_available_returns_summary_p def test_vcs_unavailable_returns_vcs_error_page(app, backend): url = '/{repo_name}'.format(repo_name=backend.repo.repo_name) - try: - rhodecode.disable_error_handler = False - with mock.patch.object(client, '_get_proxy_method') as p: - p.side_effect = client.exceptions.PyroVCSCommunicationError() + # Path the get proxy method to raise an exception instead of making a RPC + # call to the vcsserver. + with mock.patch.object(client, '_get_proxy_method') as p: + p.side_effect = client.exceptions.PyroVCSCommunicationError() + # Patch pylons error handling middleware to not re-raise exceptions. + with mock.patch.object(PylonsErrorHandlingMiddleware, 'reraise') as r: + r.return_value = False response = app.get(url, expect_errors=True) - finally: - rhodecode.disable_error_handler = True assert response.status_code == 502 assert 'Could not connect to VCS Server' in response.body