skip file bodies during Moodle activity scans

This commit is contained in:
Meghdad committed 2026-11-22 17:57:04 +03:30
1 parent 9d277383ac
commit 45392c14b9
14 files changed
+184 -30

No files matched your search

+17
View File
@@ -109,6 +109,23 @@ class CourseAssessmentTests(unittest.TestCase):
self.assertEqual(item["kind"], "assessment")
self.assertEqual(item["bundle_path"], "assessments/final-exam")
def test_local_exam_pdf_remains_available_after_repeated_scans(self) -> None:
course = self.write_course(56)
self.write_overlay(course, filename="final-exam.pdf")
attachment = course / "assessments" / "final-exam" / "files" / "final-exam.pdf"
attachment.parent.mkdir(parents=True)
attachment.write_bytes(b"exam PDF attachment")
scan_offline(self.public, [56])
scan_offline(self.public, [56])
manifest = json.loads((course / "manifest.json").read_text(encoding="utf-8"))
item = manifest["course"]["sections"][-1]["items"][0]
self.assertEqual(item["state"]["download"], "complete")
self.assertEqual(item["extension"], "pdf")
self.assertEqual(item["mimetype"], "application/pdf")
self.assertEqual(item["local_url"], "courses/56/assessments/final-exam/files/final-exam.pdf")
def test_question_import_uses_the_local_assessment_bundle(self) -> None:
course = self.write_course(51)
self.write_overlay(course)
+16 -2
View File
@@ -11,12 +11,26 @@ from types import SimpleNamespace
from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'src'))
from ravin.auth import _capture_browser_session, _authenticated_browser_session, _select_login_tab
from ravin.auth import (
_authenticate_browser_session,
_authenticated_browser_session,
_capture_browser_session,
_select_login_tab,
)
from ravin.client import MoodleClient
from ravin.models import MoodleError
from ravin.models import MoodleConnectionError, MoodleError
class BrowserUpdateTests(unittest.TestCase):
def test_network_failure_does_not_refresh_saved_browser_session(self):
args = SimpleNamespace(site="https://training.example", refresh_session=False)
with patch.object(
MoodleClient, "authenticate", side_effect=MoodleConnectionError("DNS failed")
), patch("ravin.auth._capture_browser_session") as capture:
with self.assertRaisesRegex(MoodleConnectionError, "DNS failed"):
_authenticate_browser_session(args, ("Synthetic Browser", "MoodleSession=synthetic"))
capture.assert_not_called()
def capture(self, *, captcha=False, existing_session=False, new_tab=False, invalid_key=False, use_zen=False):
test = self
+61 -4
View File
@@ -12,7 +12,7 @@ from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from ravin import Course, FileItem, MoodleClient
from ravin import Course, FileItem, MoodleClient, MoodleError
from ravin.auth import (
_browser_login_url,
_capture_browser_session,
@@ -38,9 +38,14 @@ from ravin.scan import format_scan, scan_offline, scan_remote
class _FakeResponse:
def __init__(self, chunks, *, status=200, content_range=""):
def __init__(
self, chunks, *, status=200, content_range="",
url="https://training.example/pluginfile.php/file.bin",
):
self.chunks = list(chunks)
self.status = status
self.length = None
self.url = url
self.headers = email.message.Message()
self.headers["Content-Disposition"] = 'attachment; filename="file.bin"'
if content_range:
@@ -56,9 +61,9 @@ class _FakeResponse:
return self.status
def geturl(self):
return "https://training.example/pluginfile.php/file.bin"
return self.url
def read(self, _size):
def read(self, _size=None):
if not self.chunks:
return b""
value = self.chunks.pop(0)
@@ -692,6 +697,58 @@ class ParserTests(unittest.TestCase):
self.assertEqual(destination.read_bytes(), b"abcdexyz!!")
self.assertEqual(client.request_headers, [{}, {"Range": "bytes=8-"}])
def test_module_inspection_does_not_read_redirected_file_body(self):
course_url = "https://training.example/course/view.php?id=44"
course_page = b"""
<li id="section-1" data-for="section" data-id="940" data-sectionid="1">
<li class="modtype_resource" data-for="cmitem" data-id="4940">
<a href="/mod/resource/view.php?id=4940">Lesson</a></li></li>
"""
response = _FakeResponse([AssertionError("inspection read the media body")])
response.headers["Content-Type"] = "video/mp4"
response.headers.replace_header(
"Content-Disposition", 'inline; filename="lesson.mp4"'
)
response.headers["Content-Length"] = "607191032"
client = _FakeDownloadClient([_FakeResponse([course_page], url=course_url), response])
with tempfile.TemporaryDirectory() as directory, patch.object(
client, "list_courses", return_value=[Course(44, "Course")]
):
public = Path(directory)
scan_remote(client, public, [44])
manifest_path = public / "courses" / "44" / "manifest.json"
original = manifest_path.read_bytes()
item = json.loads(original)["course"]["sections"][0]["items"][0]
self.assertEqual(item["filename"], "lesson.mp4")
self.assertEqual(item["mimetype"], "video/mp4")
self.assertEqual(item["remote_size"], 607191032)
client.responses.append(_FakeResponse([course_page], url=course_url))
for _ in range(4):
interrupted = _FakeResponse([http.client.IncompleteRead(b"<html>")])
del interrupted.headers["Content-Disposition"]
interrupted.headers["Content-Type"] = "text/html"
client.responses.append(interrupted)
with patch("ravin.client.time.sleep"), self.assertRaisesRegex(
MoodleError, "connection interrupted while reading"
):
scan_remote(client, public, [44])
self.assertEqual(manifest_path.read_bytes(), original)
def test_module_inspection_retries_incomplete_html(self):
first = _FakeResponse([http.client.IncompleteRead(b"<html>")])
second = _FakeResponse([b"<html>complete</html>"])
for response in (first, second):
del response.headers["Content-Disposition"]
response.headers["Content-Type"] = "text/html; charset=utf-8"
client = _FakeDownloadClient([first, second])
with patch("ravin.client.time.sleep"):
page, _, _ = client._read_text("/mod/folder/view.php?id=1", inspect_module=True)
self.assertEqual(page, "<html>complete</html>")
def test_existing_partial_download_is_resumed(self):
response = _FakeResponse([b"67890"], status=206, content_range="bytes 5-9/10")
client = _FakeDownloadClient([response])