Files
Evection/apps/pwa/tests.py
T
ksmithandClaude Sonnet 5 d7b433e580 Purge the service worker's authenticated-page cache on logout (S3)
PAGES_CACHE held every cached navigation response (including
authenticated article pages) with no logout-time purge, so a later
offline navigation on a shared or account-switched device could still
serve another user's cached pages. Logout is a plain POST, so the
fetch handler now special-cases that request: it deletes PAGES_CACHE
and lets the real logout request through, with no race against the
navigation that follows since it's synchronous with the same request.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:03:10 -06:00

69 lines
2.5 KiB
Python

import json
import pytest
from django.urls import reverse
@pytest.mark.django_db
def test_manifest_is_valid_json_with_expected_fields(client):
response = client.get(reverse("pwa:manifest"))
assert response.status_code == 200
assert response["Content-Type"] == "application/manifest+json"
manifest = json.loads(response.content)
assert manifest["name"] == "Evection"
assert manifest["display"] == "standalone"
assert manifest["scope"] == "/"
sizes = {icon["sizes"] for icon in manifest["icons"]}
assert {"192x192", "512x512"} <= sizes
assert any(icon.get("purpose") == "maskable" for icon in manifest["icons"])
@pytest.mark.django_db
def test_service_worker_is_served_at_root_with_correct_type_and_scope(client):
response = client.get(reverse("pwa:service_worker"))
assert response.status_code == 200
assert response["Content-Type"] == "application/javascript"
assert response["Service-Worker-Allowed"] == "/"
body = response.content.decode()
assert 'self.addEventListener("install"' in body
assert 'self.addEventListener("fetch"' in body
# Mutations must stay network-only, never intercepted.
assert 'request.method !== "GET"' in body
# Logging out must purge PAGES_CACHE so a later offline navigation on
# a shared/account-switched device can't serve another user's
# previously cached authenticated pages.
assert "caches.delete(PAGES_CACHE)" in body
assert reverse("accounts:logout") in body
@pytest.mark.django_db
def test_offline_page_renders(client):
response = client.get(reverse("pwa:offline"))
assert response.status_code == 200
assert b"offline" in response.content.lower()
@pytest.mark.django_db
def test_manifest_and_sw_do_not_require_login(client):
# The service worker fetches these itself, unauthenticated, before any
# user necessarily has a session.
assert client.get(reverse("pwa:manifest")).status_code == 200
assert client.get(reverse("pwa:service_worker")).status_code == 200
assert client.get(reverse("pwa:offline")).status_code == 200
@pytest.mark.django_db
def test_base_template_links_manifest_and_registers_service_worker(client, django_user_model):
django_user_model.objects.create_user("alice", password="pw-alice-123")
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
content = response.content.decode()
assert 'rel="manifest"' in content
assert "register-sw.js" in content
assert "apple-touch-icon" in content