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