Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_middlewares.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from hypothesis import given | |||||
import pytest | import pytest | ||||
from django.test import modify_settings | from django.test import modify_settings | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.strategies import snapshot | |||||
@modify_settings( | @modify_settings( | ||||
MIDDLEWARE={"remove": ["swh.web.common.middlewares.ExceptionMiddleware"]} | MIDDLEWARE={"remove": ["swh.web.common.middlewares.ExceptionMiddleware"]} | ||||
) | ) | ||||
@given(snapshot()) | |||||
def test_exception_middleware_disabled(client, mocker, snapshot): | def test_exception_middleware_disabled(client, mocker, snapshot): | ||||
mock_browse_snapshot_directory = mocker.patch( | mock_browse_snapshot_directory = mocker.patch( | ||||
"swh.web.browse.views.snapshot.browse_snapshot_directory" | "swh.web.browse.views.snapshot.browse_snapshot_directory" | ||||
) | ) | ||||
mock_browse_snapshot_directory.side_effect = Exception("Something went wrong") | mock_browse_snapshot_directory.side_effect = Exception("Something went wrong") | ||||
url = reverse("browse-snapshot-directory", url_args={"snapshot_id": snapshot}) | url = reverse("browse-snapshot-directory", url_args={"snapshot_id": snapshot}) | ||||
with pytest.raises(Exception, match="Something went wrong"): | with pytest.raises(Exception, match="Something went wrong"): | ||||
client.get(url) | client.get(url) | ||||
@given(snapshot()) | |||||
def test_exception_middleware_enabled(client, mocker, snapshot): | def test_exception_middleware_enabled(client, mocker, snapshot): | ||||
mock_browse_snapshot_directory = mocker.patch( | mock_browse_snapshot_directory = mocker.patch( | ||||
"swh.web.browse.views.snapshot.browse_snapshot_directory" | "swh.web.browse.views.snapshot.browse_snapshot_directory" | ||||
) | ) | ||||
mock_browse_snapshot_directory.side_effect = Exception("Something went wrong") | mock_browse_snapshot_directory.side_effect = Exception("Something went wrong") | ||||
url = reverse("browse-snapshot-directory", url_args={"snapshot_id": snapshot}) | url = reverse("browse-snapshot-directory", url_args={"snapshot_id": snapshot}) | ||||
resp = client.get(url) | resp = client.get(url) | ||||
assert resp.status_code == 500 | assert resp.status_code == 500 | ||||
assert hasattr(resp, "traceback") | assert hasattr(resp, "traceback") | ||||
assert "Traceback" in getattr(resp, "traceback") | assert "Traceback" in getattr(resp, "traceback") |