Files
Evection/apps/pwa/tests.py
T
ksmithandClaude Sonnet 5 4c20558525 Fix CSP blocking the service worker registration and tag colors
script-src 'self' blocks any inline <script> block outright, which would
have silently broken the service-worker registration snippet in base.html
in production — the exact feature this whole project exists to support.
Moved it to static/js/register-sw.js, loaded via <script src>, so script-src
can stay strict with no 'unsafe-inline' exception. style-src needed
'unsafe-inline' regardless, since tag pills set a per-tag color via a
dynamic inline style attribute.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 20:11:55 -06:00

64 lines
2.2 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
@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