Files
Evection/apps/accounts/tests.py
T
ksmithandClaude Sonnet 5 2e4674a18f Make the updater deploy tagged releases, like ds-chat's upgrade.sh
scripts/upgrade.sh now fetches tags and does a detached-HEAD checkout of
the latest one (git tag --sort=-creatordate | head -n1), matching the
ds-chat project's deploy/upgrade.sh exactly rather than a plain git pull
on whatever branch happened to be checked out. Errors clearly if the repo
has no tags yet (true for Evection right now — nothing to deploy until a
release is tagged and pushed), and no-ops cleanly if already on the
latest tag. scripts/install.sh does the same tag pin on first install
(falling back to whatever's checked out if no tags exist yet), so the
very first deploy is consistent with every upgrade after it.

Added a GET /health/ endpoint (apps.core.views.health) that checks DB
connectivity and is exempt from the first-run setup redirect, for the
upgrade script's post-restart verification (curled over the gunicorn
unix socket, same as ds-chat's curl-based check) and for any external
uptime monitor.

While wiring the setup-wizard exemption for /health/, found that
SetupWizardMiddleware._a_user_exists's reset fixture was scoped only to
apps/accounts/tests.py, so a test anywhere else that creates a user and
makes a request could leak a stale "a user exists" into later test files
with no reset — confirmed this concretely by temporarily breaking the new
exemption and watching the "works before setup" test still pass purely
from accounts/tests.py leaving the module-order-adjacent apps/articles
tests to poison the shared class attribute. Moved the fixture to a new
top-level conftest.py so it's autouse across every test file, which also
makes the new health-check test meaningful regardless of run order.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 22:15:37 -06:00

119 lines
3.8 KiB
Python

import pytest
from django.urls import reverse
from .models import User
# SetupWizardMiddleware's cache is reset around every test by the autouse
# fixture in the top-level conftest.py, not just here.
@pytest.mark.django_db
def test_root_redirects_to_setup_when_no_users_exist(client):
response = client.get("/", follow=False)
assert response.status_code == 302
assert response.url == reverse("accounts:setup")
@pytest.mark.django_db
def test_pwa_routes_bypass_the_setup_redirect(client):
for url_name in ["pwa:manifest", "pwa:service_worker", "pwa:offline"]:
response = client.get(reverse(url_name))
assert response.status_code == 200, url_name
@pytest.mark.django_db
def test_setup_creates_admin_and_logs_in(client):
response = client.post(
reverse("accounts:setup"),
{
"username": "keith",
"email": "keith@example.com",
"password": "correct-horse-battery",
"password_confirm": "correct-horse-battery",
},
)
assert response.status_code == 302
assert response.url == reverse("reader:stream")
user = User.objects.get(username="keith")
assert user.is_staff
assert user.is_superuser
stream_response = client.get(reverse("reader:stream"))
assert stream_response.status_code == 200
@pytest.mark.django_db
def test_setup_is_unreachable_once_a_user_exists(client):
User.objects.create_user("existing", password="irrelevant")
response = client.get(reverse("accounts:setup"))
assert response.status_code == 302
assert response.url == reverse("accounts:login")
@pytest.mark.django_db
def test_setup_rejects_mismatched_passwords(client):
response = client.post(
reverse("accounts:setup"),
{
"username": "keith",
"email": "keith@example.com",
"password": "correct-horse-battery",
"password_confirm": "does-not-match",
},
)
assert response.status_code == 200
assert not User.objects.exists()
@pytest.mark.django_db
def test_logout_via_post_logs_the_user_out(client):
User.objects.create_user("keith", password="pw-keith-123")
client.login(username="keith", password="pw-keith-123")
response = client.post(reverse("accounts:logout"))
assert response.status_code == 302
stream_response = client.get(reverse("reader:stream"))
assert stream_response.status_code == 302
assert stream_response.url.startswith(reverse("accounts:login"))
@pytest.mark.django_db
def test_logout_rejects_get(client):
# Django's LogoutView only accepts POST (see templates/core/_sidebar.html,
# which submits it as a form rather than a plain link).
User.objects.create_user("keith", password="pw-keith-123")
client.login(username="keith", password="pw-keith-123")
response = client.get(reverse("accounts:logout"))
assert response.status_code == 405
@pytest.mark.django_db
def test_non_admin_cannot_manage_users(client):
User.objects.create_user("regular", password="pw-regular-123")
client.login(username="regular", password="pw-regular-123")
response = client.get(reverse("accounts:manage_users"))
assert response.status_code == 302 # user_passes_test redirects non-staff
@pytest.mark.django_db
def test_admin_can_create_user(client):
admin = User.objects.create_user("admin", password="pw-admin-123", is_staff=True)
client.login(username="admin", password="pw-admin-123")
response = client.post(
reverse("accounts:create_user"),
{
"username": "newperson",
"email": "newperson@example.com",
"password": "pw-newperson-123",
},
)
assert response.status_code == 302
assert User.objects.filter(username="newperson").exists()
assert admin.pk # sanity: fixture user untouched