mirror of
https://github.com/Donkie/Spoolman.git
synced 2026-10-22 10:02:17 -05:00
101 tests covering the authentication system, run against a server with
SPOOLMAN_AUTH_ENABLED as a separate suite from the regression sweep.
Deliberately one SQLite variant rather than a fifth pass of the existing
four-database matrix. Nothing in spoolman/auth is database-specific, so
re-running the whole regression suite with auth on would quadruple the CI
bill to re-prove something none of those tests are about.
The tests live in tests_integration/tests_auth, a sibling of the regression
suite rather than a subdirectory of it, and the compose file mounts that
directory at the path the tester image's fixed entrypoint collects from. So
the auth suite runs exactly when asked and the regression runs can never
pick it up -- no entrypoint override, no collection guard, no way for the
two to interfere.
"sqlite-auth" is accepted by run.py but is not in the default target list,
so `poe itest` keeps meaning what it meant and `poe itest sqlite-auth` is
opt-in. CI gets its own small job for it, and the release gate now depends
on that job alongside the others.
Coverage:
test_unauthenticated every non-public route answers 401 without a
credential -- the black-box counterpart to the
import-time coverage assertion, and the test most
likely to catch a future route added without a gate
test_setup first claim succeeds, second is refused, username
stored lowercased
test_login success, wrong password, unknown user returning an
identical message, lockout with Retry-After, and
remember-me controlling cookie Max-Age
test_csrf cookie-authenticated writes need the header
test_levels the owner reaches read, edit and manage routes
test_anonymous_read the setting opens reads without opening writes, and
leaves the bulk exports closed
test_websocket rejection arrives as close code 4401, not as a failed
handshake -- the regression test for the uvicorn
behaviour that dictated the whole websocket design
test_password change requires the current password and takes effect
One test asserted that using 10 g leaves used_weight at 10. It is a running
total, and the spool was created with remaining_weight below the filament's
net weight, so it already stood at 500. Rewritten to assert the delta.
Verified by running both suites against a live server: 101 auth tests pass
with auth on, and the existing 372 regression tests still pass with auth off
and no test file edited.
89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
"""Integration tests: cross-site request forgery protection.
|
|
|
|
The session cookie is ambient -- the browser attaches it to any same-origin request,
|
|
including ones a hostile page caused. The defence is a double submit: a second, readable
|
|
cookie whose value must be echoed in a header. A cross-site page can make the browser
|
|
send the cookies but cannot read them, so it cannot construct the header.
|
|
|
|
The rejection is 403 and not 401 on purpose. The credential is valid; answering 401
|
|
would make a client treat a stale tab as a sign-out.
|
|
"""
|
|
|
|
import httpx
|
|
|
|
from .conftest import (
|
|
CSRF_COOKIE,
|
|
CSRF_HEADER,
|
|
assert_code,
|
|
signed_in,
|
|
)
|
|
|
|
|
|
def test_post_without_csrf_header_is_forbidden(setup_response: httpx.Response) -> None: # noqa: ARG001
|
|
"""A cookie-authenticated write with no CSRF header is refused."""
|
|
with signed_in() as (client, response):
|
|
assert_code(response, 200)
|
|
# signed_in attaches the header; drop it to model a request the client did not make.
|
|
del client.headers[CSRF_HEADER]
|
|
|
|
result = client.post("/vendor", json={"name": "CSRF test vendor"})
|
|
assert_code(result, 403)
|
|
assert "csrf" in result.json()["message"].lower()
|
|
|
|
|
|
def test_post_with_mismatched_csrf_header_is_forbidden(setup_response: httpx.Response) -> None: # noqa: ARG001
|
|
"""The header has to match the cookie, not merely be present."""
|
|
with signed_in() as (client, response):
|
|
assert_code(response, 200)
|
|
client.headers[CSRF_HEADER] = "not-the-token-from-the-cookie"
|
|
|
|
result = client.post("/vendor", json={"name": "CSRF test vendor"})
|
|
assert_code(result, 403)
|
|
|
|
|
|
def test_post_with_csrf_header_succeeds(setup_response: httpx.Response) -> None: # noqa: ARG001
|
|
"""With the cookie echoed into the header the write goes through."""
|
|
with signed_in() as (client, response):
|
|
assert_code(response, 200)
|
|
assert client.headers[CSRF_HEADER] == client.cookies.get(CSRF_COOKIE)
|
|
|
|
result = client.post("/vendor", json={"name": "CSRF test vendor"})
|
|
assert_code(result, 200)
|
|
vendor = result.json()
|
|
try:
|
|
assert vendor["name"] == "CSRF test vendor"
|
|
finally:
|
|
assert_code(client.delete(f"/vendor/{vendor['id']}"), 200)
|
|
|
|
|
|
def test_reads_do_not_need_a_csrf_token(setup_response: httpx.Response) -> None: # noqa: ARG001
|
|
"""Safe methods change nothing, so they are exempt.
|
|
|
|
Requiring a header on GET would break every plain link and bookmark for no gain.
|
|
"""
|
|
with signed_in() as (client, response):
|
|
assert_code(response, 200)
|
|
del client.headers[CSRF_HEADER]
|
|
|
|
assert_code(client.get("/spool"), 200)
|
|
assert_code(client.get("/vendor"), 200)
|
|
assert_code(client.get("/setting/"), 200)
|
|
|
|
|
|
def test_every_unsafe_method_is_checked(setup_response: httpx.Response) -> None: # noqa: ARG001
|
|
"""The check covers every state-changing method, not just POST."""
|
|
with signed_in() as (client, response):
|
|
assert_code(response, 200)
|
|
created = client.post("/vendor", json={"name": "CSRF delete test vendor"})
|
|
assert_code(created, 200)
|
|
vendor_id = created.json()["id"]
|
|
|
|
try:
|
|
del client.headers[CSRF_HEADER]
|
|
assert_code(client.delete(f"/vendor/{vendor_id}"), 403)
|
|
assert_code(client.patch(f"/vendor/{vendor_id}", json={"comment": "nope"}), 403)
|
|
assert_code(client.put("/spool/1/use", json={"use_weight": 1}), 403)
|
|
finally:
|
|
client.headers[CSRF_HEADER] = client.cookies.get(CSRF_COOKIE)
|
|
assert_code(client.delete(f"/vendor/{vendor_id}"), 200)
|