Files
ARR-2.0-0918/tests/test_arr_web_preview.py

59 lines
2.5 KiB
Python

import json
import unittest
from arr_web.app import PortalApplication
from arr_web.preview import ReadOnlyPreview
class PreviewTests(unittest.TestCase):
def setUp(self):
self.preview = ReadOnlyPreview()
def test_actual_assets_and_explicit_preview_label(self):
page = self.preview.handle("GET", "/", {})
self.assertEqual(page.status, 200)
self.assertIn("本地只读预览", page.body.decode())
self.assertIn(b'id="arr-download-date"', page.body)
for path, content_type in (
("/assets/styles.css", "text/css"),
("/assets/preview.css", "text/css"),
("/assets/app.js", "text/javascript"),
("/assets/i18n.js", "text/javascript"),
):
with self.subTest(path=path):
response = self.preview.handle("GET", path, {})
self.assertEqual(response.status, 200)
self.assertTrue(response.content_type.startswith(content_type))
def test_preview_services_are_unavailable_and_history_empty(self):
def get(path):
response = self.preview.handle("GET", path, {})
self.assertEqual(response.status, 200)
return json.loads(response.body)["data"]
health = get("/api/health")
for key in ("database_ready", "processing_ready", "monthly_ready", "arr_download_ready"):
self.assertFalse(health[key])
self.assertFalse(get("/api/arr-downloads")["ready"])
self.assertEqual(get("/api/jobs?month=2026-09"), [])
self.assertEqual(get("/api/months"), [])
def test_all_mutations_blocked_before_dispatch(self):
for method in ("POST", "PATCH", "DELETE", "PUT"):
for path in ("/api/login", "/api/jobs", "/api/arr-downloads", "/api/logout"):
with self.subTest(method=method, path=path):
response = self.preview.handle(method, path, {}, b"{}")
self.assertEqual(response.status, 405)
self.assertEqual(json.loads(response.body)["error"]["code"], "PREVIEW_READ_ONLY")
def test_production_still_requires_authentication(self):
self.assertEqual(PortalApplication().handle("GET", "/api/session", {}).status, 401)
preview_session = self.preview.handle("GET", "/api/session", {})
self.assertEqual(preview_session.status, 200)
self.assertNotIn("Set-Cookie", preview_session.headers)
self.assertEqual(len(self.preview._sessions._sessions), 0)
if __name__ == "__main__":
unittest.main()