This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not 500 when upstream provider is unavailable
- Loading branch information
1 parent
2d8cf1f
commit 3ea70b3
Showing
4 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from test.factory.models.media import MediaFactory | ||
|
||
from catalog.api.models.image import Image | ||
|
||
|
||
class ImageFactory(MediaFactory): | ||
class Meta: | ||
model = Image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from test.factory.models.image import ImageFactory | ||
from unittest import mock | ||
from urllib.error import HTTPError | ||
|
||
from rest_framework.test import APIClient | ||
|
||
import pytest | ||
|
||
from catalog.api.models.image import Image | ||
|
||
|
||
@pytest.fixture | ||
def api_client() -> APIClient: | ||
return APIClient() | ||
|
||
|
||
@pytest.fixture | ||
def image() -> Image: | ||
return ImageFactory.create() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_thumb_error(api_client, image): | ||
error = None | ||
|
||
def urlopen_503_response(url, **kwargs): | ||
nonlocal error | ||
error = HTTPError(url, 503, "Bad error upstream whoops", {}, None) | ||
raise error | ||
|
||
with mock.patch( | ||
"catalog.api.views.media_views.urlopen" | ||
) as urlopen_mock, mock.patch( | ||
"catalog.api.views.media_views.capture_exception", autospec=True | ||
) as mock_capture_exception: | ||
urlopen_mock.side_effect = urlopen_503_response | ||
response = api_client.get(f"/v1/images/{image.identifier}/thumb/") | ||
|
||
assert response.status_code == 424 | ||
mock_capture_exception.assert_called_once_with(error) |