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

Make improvements to the oEmbed endpoint #934

Merged
merged 3 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/catalog/api/views/image_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ def oembed(self, request, *_, **__):
context = self.get_serializer_context()

url = params.validated_data["url"]
if url.endswith("/"):
url = url[:-1]
identifier = url.rsplit("/", 1)[1]
try:
image = self.get_queryset().get(identifier=identifier)
except Image.DoesNotExist:
return get_api_exception("Could not find image.", 404)
raise get_api_exception("Could not find image.", 404)
if not (image.height and image.width):
image_file = requests.get(image.url, headers=self.OEMBED_HEADERS)
width, height = PILImage.open(io.BytesIO(image_file.content)).size
Expand Down
26 changes: 26 additions & 0 deletions api/test/image_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ def test_audio_report(image_fixture):
report("images", image_fixture)


def test_oembed_endpoint_with_non_existent_image():
params = {
"url": "https://any.domain/any/path/00000000-0000-0000-0000-000000000000",
}
response = requests.get(
f"{API_URL}/v1/images/oembed?{urlencode(params)}", verify=False
)
assert response.status_code == 404


@pytest.mark.parametrize(
"url",
[
f"https://any.domain/any/path/{identifier}", # no trailing slash
f"https://any.domain/any/path/{identifier}/", # trailing slash
identifier, # just identifier instead of URL
],
)
def test_oembed_endpoint_with_fuzzy_input(url):
params = {"url": url}
response = requests.get(
f"{API_URL}/v1/images/oembed?{urlencode(params)}", verify=False
)
assert response.status_code == 200


def test_oembed_endpoint_for_json():
params = {
"url": f"https://any.domain/any/path/{identifier}",
Expand Down