Files
LLeMbas/tests/test_settings.py
T
Jaroslav Beneš b8e7745311 An instance that can be somebody else's
A name, a tagline, a logo, a favicon and the launcher icons derived from it; the
Middle-earth strings as data; themes as token sets; and a stylesheet for what
none of that reaches. All four are on one page, in one settings group.

The snapshot is a Jinja global over a process-level cache, because render() has
no session and four render paths never reach it at all -- the sign-in page, the
error pages, the offline page and the SSE fragments. A context value would have
had to be threaded through every one and would still have missed those. It being
a global is also what lets mark() branch on an uploaded logo without any of its
six call sites learning about branding; the macro that renders the sidebar link
is called brandlink now, because a macro imported as `brand` shadows the global
for the whole template and took out every page at once.

Defaults in code and overrides in the database, as the prompt fragments do, with
one difference stated in the module: an empty fragment means off, an empty
flavour string means the shipped wording. And blanked rather than dropped --
settings_store.update merges, so an omitted key leaves what was stored last time
and "I typed the default back in" would store something different from "I changed
nothing".

A custom theme sets a handful of tokens and inherits the rest, and the
inheritance is a CSS fact: tokens.css matches [data-base="shire"] as well as
[data-theme="shire"], so a custom light theme lands on parchment rather than four
light colours on near-black. Values are validated on read rather than on save,
because a theme written straight into the settings table still has to produce a
stylesheet that parses -- a `}` in a value ends the rule and silently breaks
every rule after it. The soft variants are derived from the accent, or a changed
accent leaves focus rings in the old hue and reads as half-working.

/branding.css is a route, not an inline block: an external stylesheet has no HTML
context to escape from. The link carries a content hash, so a save is not left to
the browser's cache, and it is deliberately outside the service worker's precache
list, which is versioned by the release.

The instance name moved off /admin/general rather than being duplicated there.
An upgrade keeps it: the general row is read as a seed exactly while the branding
row has never mentioned the name, which is `key in row` and not `row[key] is
truthy` -- the two read alike would resurrect the old name underneath a cleared
one.

The theme list stops being a hard-coded pair in five places. Every failure mode
in that area is silent, so it is driven under a DOM stub as well as tested.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-06 15:42:25 +02:00

241 lines
8.3 KiB
Python

"""Instance settings (registration toggle) and changing your own password."""
from __future__ import annotations
from fastapi.testclient import TestClient
from sqlalchemy import select
from lembas.db.models import Session as SessionRow
from lembas.db.models import User
from lembas.services import settings_store
# --- Registration toggle -----------------------------------------------------
def test_registration_is_open_by_default(client: TestClient, db, registered):
assert settings_store.signup_allowed(db) is True
def test_closing_registration_blocks_new_accounts(client: TestClient, db, registered):
client.post("/admin/general", data={"instance_name": "LLeMbas"}, follow_redirects=False)
assert settings_store.signup_allowed(db) is False
response = client.post(
"/auth/register",
data={"name": "Uninvited", "email": "no@thanks.test", "password": "let-me-in-please"},
follow_redirects=False,
)
assert response.status_code == 403
assert "Registration is closed" in response.text
assert db.scalar(select(User).where(User.email == "no@thanks.test")) is None
def test_closed_registration_hides_the_create_account_link(
client: TestClient, db, registered
):
client.post("/admin/general", data={"instance_name": "LLeMbas"}, follow_redirects=False)
client.post("/auth/logout", follow_redirects=False)
page = client.get("/auth/login")
assert "/auth/register" not in page.text
def test_open_registration_shows_the_link(client: TestClient, db, registered):
client.post(
"/admin/general",
data={"instance_name": "LLeMbas", "allow_signup": "true"},
follow_redirects=False,
)
client.post("/auth/logout", follow_redirects=False)
assert "/auth/register" in client.get("/auth/login").text
def test_existing_users_can_still_sign_in_when_registration_is_closed(
client: TestClient, db, registered
):
client.post("/admin/general", data={"instance_name": "LLeMbas"}, follow_redirects=False)
client.post("/auth/logout", follow_redirects=False)
response = client.post(
"/auth/login",
data={"email": registered["email"], "password": registered["password"]},
follow_redirects=False,
)
assert response.status_code == 303
def test_stored_setting_beats_the_environment_default(client: TestClient, db, registered):
"""A toggle that silently reverted on restart would be worse than none."""
from lembas.config import settings as env_settings
assert env_settings.allow_signup is True
settings_store.update(db, {"allow_signup": False})
assert settings_store.signup_allowed(db) is False
def test_ordinary_users_cannot_change_instance_settings(client: TestClient, db, registered):
client.post("/auth/logout", follow_redirects=False)
client.post(
"/auth/register",
data={"name": "Sam", "email": "sam@shire.test", "password": "potatoes-po-ta-toes"},
follow_redirects=False,
)
assert client.post("/admin/general", data={"system_prompt": "Pwned"}).status_code == 403
assert settings_store.get(db, "system_prompt") == ""
# And the page the identity moved to is guarded the same way.
assert (
client.post(
"/admin/customization/identity", data={"instance_name": "Pwned"}
).status_code
== 403
)
# --- Password change ---------------------------------------------------------
def test_password_change_works(client: TestClient, db, registered):
response = client.post(
"/api/preferences/password",
data={
"current_password": registered["password"],
"new_password": "a-much-better-password",
"confirm_password": "a-much-better-password",
},
follow_redirects=False,
)
assert response.status_code == 303
assert "saved=" in response.headers["location"]
client.post("/auth/logout", follow_redirects=False)
assert (
client.post(
"/auth/login",
data={"email": registered["email"], "password": "a-much-better-password"},
follow_redirects=False,
).status_code
== 303
)
def test_old_password_stops_working(client: TestClient, db, registered):
client.post(
"/api/preferences/password",
data={
"current_password": registered["password"],
"new_password": "a-much-better-password",
"confirm_password": "a-much-better-password",
},
follow_redirects=False,
)
client.post("/auth/logout", follow_redirects=False)
assert (
client.post(
"/auth/login",
data={"email": registered["email"], "password": registered["password"]},
follow_redirects=False,
).status_code
== 401
)
def test_wrong_current_password_is_refused(client: TestClient, db, registered):
response = client.post(
"/api/preferences/password",
data={
"current_password": "not-my-password",
"new_password": "a-much-better-password",
"confirm_password": "a-much-better-password",
},
follow_redirects=False,
)
assert "error=" in response.headers["location"]
user = db.scalar(select(User).where(User.email == registered["email"]))
db.refresh(user)
from lembas.security.passwords import verify_password
assert verify_password(registered["password"], user.password_hash)
def test_mismatched_confirmation_is_refused(client: TestClient, db, registered):
response = client.post(
"/api/preferences/password",
data={
"current_password": registered["password"],
"new_password": "a-much-better-password",
"confirm_password": "a-different-password",
},
follow_redirects=False,
)
assert "do%20not%20match" in response.headers["location"]
def test_short_new_password_is_refused(client: TestClient, db, registered):
response = client.post(
"/api/preferences/password",
data={
"current_password": registered["password"],
"new_password": "short",
"confirm_password": "short",
},
follow_redirects=False,
)
assert "8%20characters" in response.headers["location"]
def test_other_sessions_are_revoked_but_this_one_survives(client: TestClient, db, registered):
"""If the reason for changing a password is that someone else knows it,
leaving their session alive defeats the point."""
other = TestClient(client.app)
other.post(
"/auth/login",
data={"email": registered["email"], "password": registered["password"]},
follow_redirects=False,
)
assert other.get("/chat", follow_redirects=False).status_code == 200
assert db.scalar(select(SessionRow)) is not None
client.post(
"/api/preferences/password",
data={
"current_password": registered["password"],
"new_password": "a-much-better-password",
"confirm_password": "a-much-better-password",
},
follow_redirects=False,
)
# The other browser is out...
assert other.get("/chat", follow_redirects=False).status_code == 303
# ...and the tab that made the change is still in.
assert client.get("/chat", follow_redirects=False).status_code == 200
# --- Adding a column to a table that already has rows -------------------------
def test_a_nullable_column_is_added_without_a_default(db):
"""Backfilling one would give existing rows a value the model does not treat
as absent: an added foreign key arrives as "" rather than NULL, and every
"is this set?" check downstream is then wrong about the old rows."""
from sqlalchemy import Column, String
from lembas.db.migrations import _add_column_sql
from lembas.db.models import Document
sql = _add_column_sql(
Document.__table__, Column("later", String(32), nullable=True), db.bind.dialect
)
assert "DEFAULT" not in sql
def test_a_not_null_column_still_gets_one(db):
"""SQLite refuses NOT NULL with no default, so there it is unavoidable."""
from sqlalchemy import Column, String
from lembas.db.migrations import _add_column_sql
from lembas.db.models import Document
sql = _add_column_sql(
Document.__table__,
Column("later", String(32), nullable=False, default=""),
db.bind.dialect,
)
assert "NOT NULL" in sql and "DEFAULT" in sql