Skip to content

Commit f24d730

Browse files
committed
Add error logging and fix tests
1 parent f9f4e69 commit f24d730

File tree

3 files changed

+12
-70
lines changed

3 files changed

+12
-70
lines changed

django/thunderstore/frontend/services/thumbnail.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from django.conf import settings
55
from easy_thumbnails.files import get_thumbnailer
66

7+
from thunderstore.core.utils import capture_exception
8+
79

810
@dataclass
911
class Thumbnail:
@@ -30,5 +32,6 @@ def get_or_create_thumbnail(
3032
else:
3133
thumbnail = thumbnailer.get_thumbnail(thumbnail_options, generate=True)
3234
return Thumbnail(storage_path=thumbnail.name, url=thumbnail.url)
33-
except Exception:
35+
except Exception as e:
36+
capture_exception(e)
3437
return None

django/thunderstore/frontend/tests/test_views.py

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -134,77 +134,14 @@ def test_views_disabled_for_auth_exclusive_host(
134134
assert response.status_code == 302
135135

136136

137-
@pytest.mark.django_db
138-
def test_thumbnail_redirect_success(dummy_cover_image, client, community_site):
139-
community = community_site.community
140-
community.cover_image = dummy_cover_image
141-
community.save()
142-
143-
url = reverse("cdn_thumb_redirect", kwargs={"path": community.cover_image.name})
144-
params = {"width": 100, "height": 100}
145-
146-
response = client.get(url, params, HTTP_HOST=community_site.site.domain)
147-
148-
assert response.status_code == 302
149-
assert response["Location"].endswith(".jpg")
150-
assert "Cache-Control" in response
151-
assert "max-age=86400" in response["Cache-Control"]
152-
153-
154-
@pytest.mark.django_db
155-
def test_thumbnail_redirect_exception(dummy_cover_image, community_site, client):
156-
community = community_site.community
157-
community.cover_image = dummy_cover_image
158-
community.save()
159-
160-
url = reverse("cdn_thumb_redirect", kwargs={"path": community.cover_image.name})
161-
params = {"width": 100, "height": 100}
162-
163-
path = "thunderstore.frontend.views.get_or_create_thumbnail"
164-
with patch(path) as mock_get_thumbnail:
165-
mock_get_thumbnail.return_value = None
166-
response = client.get(url, params, HTTP_HOST=community_site.site.domain)
167-
168-
assert response.status_code == 404
169-
assert "Cache-Control" in response
170-
assert "max-age=300" in response["Cache-Control"]
171-
172-
173-
@pytest.mark.django_db
174-
@pytest.mark.parametrize(
175-
"params",
176-
[
177-
{"width": "abc", "height": "100"},
178-
{"width": "100", "height": "abc"},
179-
{"width": "0", "height": "100"},
180-
{"width": "100", "height": "0"},
181-
{"width": "-1", "height": "100"},
182-
{},
183-
],
184-
)
185-
def test_thumbnail_redirect_invalid_params_returns_fallback(
186-
params, dummy_cover_image, client, community_site
187-
):
188-
community = community_site.community
189-
community.cover_image = dummy_cover_image
190-
community.save()
191-
192-
url = reverse("cdn_thumb_redirect", kwargs={"path": community.cover_image.name})
193-
response = client.get(url, params, HTTP_HOST=community_site.site.domain)
194-
195-
assert response.status_code == 404
196-
assert "Cache-Control" in response
197-
assert "max-age=300" in response["Cache-Control"]
198-
199-
200137
@pytest.mark.django_db
201138
def test_thumbnail_serve_success(dummy_cover_image, client, community_site):
202139
community = community_site.community
203140
community.cover_image = dummy_cover_image
204141
community.save()
205142

206143
url = reverse("cdn_thumb_serve", kwargs={"path": community.cover_image.name})
207-
params = {"width": 100, "height": 100}
144+
params = {"width": 64, "height": 64}
208145

209146
response = client.get(url, params, HTTP_HOST=community_site.site.domain)
210147
assert response.status_code == 200
@@ -235,7 +172,7 @@ def test_thumbnail_serve_exception(params, dummy_cover_image, client, community_
235172
url = reverse("cdn_thumb_serve", kwargs={"path": community.cover_image.name})
236173

237174
response = client.get(url, params, HTTP_HOST=community_site.site.domain)
238-
assert response.status_code == 404
175+
assert response.status_code == 403
239176
assert response.get("Cache-Control") == "max-age=300, public"
240177

241178

@@ -246,7 +183,7 @@ def test_thumbnail_serve_invalid(client, community_site):
246183
community.save()
247184

248185
url = reverse("cdn_thumb_serve", kwargs={"path": community.cover_image.name})
249-
params = {"width": 100, "height": 100}
186+
params = {"width": 64, "height": 64}
250187

251188
response = client.get(url, params, HTTP_HOST=community_site.site.domain)
252189
assert response.status_code == 404

django/thunderstore/frontend/views.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from django.views.decorators.csrf import ensure_csrf_cookie
1313
from django.views.generic import RedirectView, TemplateView, View
1414

15+
from thunderstore.core.utils import capture_exception
1516
from thunderstore.frontend.services.thumbnail import get_or_create_thumbnail
1617
from thunderstore.plugins.registry import plugin_registry
1718

@@ -78,8 +79,8 @@ def get(self, request, *args, **kwargs):
7879

7980
max_age = 300 # 5 minutes
8081

81-
if not asset_path or width <= 0 or height <= 0:
82-
response = HttpResponseNotFound("Invalid request parameters.")
82+
if not asset_path:
83+
response = HttpResponseNotFound("Thumbnail not found.")
8384
else:
8485
thumbnail = get_or_create_thumbnail(asset_path, width, height)
8586
thumbnail_path = thumbnail.storage_path if thumbnail else None
@@ -90,7 +91,8 @@ def get(self, request, *args, **kwargs):
9091
file = default_storage.open(thumbnail_path, "rb")
9192
response = FileResponse(file, content_type=mime_type)
9293
max_age = 86400 # 24h
93-
except FileNotFoundError:
94+
except FileNotFoundError as e:
95+
capture_exception(e)
9496
response = HttpResponseNotFound("Thumbnail not found.")
9597
else:
9698
response = HttpResponseNotFound("Invalid request.")

0 commit comments

Comments
 (0)