Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base moodle's file_exist method on the file content instead of headers #6841

Merged
merged 1 commit into from
Nov 6, 2024
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
19 changes: 11 additions & 8 deletions lms/services/moodle.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,17 @@ def list_files(self, course_id: int):
def file_exists(self, file_id) -> bool:
"""Check if the file exists in the course."""
# Moodle file IDs are URLs, but they need the token to be accessible
response = self._http.request("HEAD", f"{file_id}&token={self.token}")
# Moodle API doesn't use status codes, we can't rely on that.
# We don't want to download the full file so we'll do a HEAD request and assume:
# - JSON response, it's an error response
# - Anything else, it's the file we are after

LOG.info("Headers from Moodle file check %s", response.headers)
return not response.headers["content-type"].startswith("application/json")
response = self._http.request(
"GET", f"{file_id}&token={self.token}", headers={"Range": "bytes=0-1024"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue already existed before, but can we be certain that self.token will never contain characters that need to be escaped in a query param?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out my information about Adobe requiring "%PDF" in the first 1024 bytes of the file is out of date, per https://helpx.adobe.com/acrobat/kb/pdf-error-1015-11001-update.html. I expect other PDF viewers are still more liberal though.

)
# API doesn't use status codes, we can't rely on that.
if content_type := response.headers.get("content-type"):
# JSON response, assume it's an error response
return not content_type.startswith("application/json")

# If the Moodle server doesn't return the content-type header, check the first bytes of the response
# and check if the files is indeed a PDF.
return b"%PDF" in response.content

def page(self, course_id, page_id) -> dict | None:
url = self._api_url(Function.GET_PAGES)
Expand Down
23 changes: 14 additions & 9 deletions tests/unit/lms/services/moodle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,24 @@ def test_list_files(self, svc, http_service, contents):
}
]

@pytest.mark.parametrize("content", [b"some other content", b"%PDF-14"])
@pytest.mark.parametrize(
"header,expected",
[
("application/json", False),
("application/pdf", True),
],
"headers",
[{}, {"content-type": "application/json"}, {"content-type": "application/pdf"}],
)
def test_file_exists(self, svc, http_service, header, expected):
http_service.request.return_value = Mock(headers={"content-type": header})
def test_file_exists(self, svc, http_service, headers, content):
http_service.request.return_value = Mock(content=content, headers=headers)

assert svc.file_exists("URL") == expected
expected = (
not headers.get("content-type", "").startswith("application/json")
if headers.get("content-type")
else b"%PDF" in content
)

http_service.request.assert_called_once_with("HEAD", "URL&token=sentinel.token")
assert svc.file_exists("URL") == expected
http_service.request.assert_called_once_with(
"GET", "URL&token=sentinel.token", headers={"Range": "bytes=0-1024"}
)

def test_list_pages(self, svc, http_service, contents):
http_service.post.return_value.json.return_value = contents
Expand Down
Loading