Files
Evection/apps/pwa/tests.py
T
ksmithandClaude Sonnet 5.5 aacb41d1d3 Purge the PWA page cache on every login/logout/expiry transition (R2-S3, #35)
The service worker cached authenticated pages in one origin-wide cache
and only purged it when the logout request itself passed through it —
not on session expiry, externally cleared cookies, or an account
switch, so a shared device could show the previous session's pages
offline.

Every HTML response now carries X-Evection-Auth: an opaque HMAC of the
user (or "anon"), via a small middleware. The service worker remembers
the marker its cached pages belong to and, whenever a navigation brings
a different one, deletes the pages cache before storing anything; "anon"
responses (the login page) are never cached. So the cache always holds
exactly one session's pages, and any transition the worker can observe
online clears it. (What no design can detect is a session ending while
the device stays offline.)

Verified in a real browser: pages cached after login; expiring the
session server-side (no logout request) and loading a page left the
cache deleted and the marker "anon"; logging in as a second user
afterward cached only that user's pages, none of the first user's.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 12:05:45 -07:00

108 lines
4.1 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
@pytest.mark.django_db
def test_html_responses_are_stamped_with_who_they_were_rendered_for(client, django_user_model):
"""R2-S3: the service worker purges its page cache whenever this marker
changes, so it has to be stable per user, different between users,
and "anon" when logged out."""
django_user_model.objects.create_user("alice", password="pw-alice-123")
django_user_model.objects.create_user("bob", password="pw-bob-123")
anon = client.get(reverse("accounts:login"))["X-Evection-Auth"]
assert anon == "anon"
client.login(username="alice", password="pw-alice-123")
alice_1 = client.get(reverse("reader:stream"))["X-Evection-Auth"]
alice_2 = client.get(reverse("reader:stream"))["X-Evection-Auth"]
client.logout()
client.login(username="bob", password="pw-bob-123")
bob = client.get(reverse("reader:stream"))["X-Evection-Auth"]
assert alice_1 == alice_2
assert len({anon, alice_1, bob}) == 3
assert "alice" not in alice_1 and not alice_1.isdigit()
@pytest.mark.django_db
def test_non_html_responses_are_not_stamped(client, django_user_model):
django_user_model.objects.create_user("alice", password="pw-alice-123")
client.login(username="alice", password="pw-alice-123")
assert "X-Evection-Auth" not in client.get(reverse("core:health"))
def test_service_worker_purges_its_page_cache_when_the_auth_marker_changes(client):
body = client.get(reverse("pwa:service_worker")).content.decode()
assert "X-Evection-Auth" in body
assert "caches.delete(PAGES_CACHE)" in body
assert "previous !== marker" in body