diff --git a/api/test/unit/views/media_views_test.py b/api/test/unit/views/media_views_test.py index d97a5b040..1953233dc 100644 --- a/api/test/unit/views/media_views_test.py +++ b/api/test/unit/views/media_views_test.py @@ -1,3 +1,6 @@ +import json +from http.client import HTTPResponse +from pathlib import Path from test.factory.models.image import ImageFactory from unittest import mock from urllib.error import HTTPError @@ -7,6 +10,12 @@ import pytest from catalog.api.models.image import Image +from catalog.api.views.media_views import MediaViewSet + + +_MOCK_IMAGE_PATH = Path(__file__).parent / ".." / ".." / "factory" +_MOCK_IMAGE_BYTES = (_MOCK_IMAGE_PATH / "sample-image.jpg").read_bytes() +_MOCK_IMAGE_INFO = json.loads((_MOCK_IMAGE_PATH / "sample-image-info.json").read_text()) @pytest.fixture @@ -38,3 +47,21 @@ def urlopen_503_response(url, **kwargs): assert response.status_code == 424 mock_capture_exception.assert_called_once_with(error) + + +@pytest.mark.django_db +def test_thumb_sends_ua_header(api_client, image): + with mock.patch("catalog.api.views.media_views.urlopen") as urlopen_mock: + mock_res = mock.MagicMock(spec=HTTPResponse) + mock_res.status = 200 + mock_res.headers = {} + urlopen_mock.return_value = mock_res + res = api_client.get(f"/v1/images/{image.identifier}/thumb/") + + assert res.status_code == 200 + + urlopen_mock.assert_called_once() + assert ( + urlopen_mock.call_args[0][0].headers["User-agent"] + == MediaViewSet.THUMBNAIL_PROXY_COMM_HEADERS["User-Agent"] + )