Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Add tests for thumbnail requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed Aug 16, 2022
1 parent fece7ce commit bad1118
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions api/test/unit/views/media_views_test.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"]
)

0 comments on commit bad1118

Please sign in to comment.