Files
romm/backend/tests/models/test_user.py
Georges-Antoine Assi ff9627ae70 fix(auth): require the current password to change your own username, password or email (#5351)
* feat(auth): require the current password to change your own password

PUT /api/users/{id} set a new password or email for the caller with
nothing more than a me.write credential, so a leaked client token or a
hijacked session could take the account over for good.

A self-edit that changes the password or the email now needs
current_password, checked with bcrypt off the event loop (400 when
missing, 403 when wrong). Admins editing another user and the
forgot/reset-password flow are unchanged. Accounts with no local
password (OIDC-provisioned placeholders) can set a first one only from a
sign-in session, not from a bearer or client token.

UserSchema gains has_password so the UI asks for the current password
only when the backend will: the change-password dialog, the profile
email field, and the edit-user dialog when editing yourself.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* docs(user): shorten _verify_account_holder docstring

Addresses Greptile review on #5351.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(user): treat a case-only email edit as no change (code-review)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(user): simplify current-password checks and tests (simplify)

Hash the new password off the event loop, reuse errorMessage and the
viewer token fixture, and table-drive the needsCurrentPassword tests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(user): require the current password to change your own username

Login is by username and a rename revokes every session, so a stolen
me.write token could lock the owner out without touching the password.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat(user): lock the email of OIDC accounts to the identity provider

The provider overwrites a linked account's email at every sign-in, and
finds an unlinked password-less account by it, so a self-edit is either
lost or breaks SSO sign-in. Admins can still change it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat(RTextField): give read-only fields a muted style

Dashed chrome, no fill and muted text, with no hover or pointer-focus
halo, so a locked value no longer looks editable. Keyboard and gamepad
focus keep their outline.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-26 11:12:27 -04:00

50 lines
1.4 KiB
Python

import pytest
from handler.auth.constants import EDIT_SCOPES, FULL_SCOPES, WRITE_SCOPES
from models.user import User
def test_admin(admin_user: User):
assert admin_user.oauth_scopes == FULL_SCOPES
def test_editor(editor_user: User):
assert editor_user.oauth_scopes == EDIT_SCOPES
def test_user(viewer_user: User):
assert viewer_user.oauth_scopes == WRITE_SCOPES
def test_a_bcrypt_hash_is_a_usable_password(viewer_user: User):
assert viewer_user.has_password
@pytest.mark.parametrize(
"hashed_password", [None, "", "8f14e45f-ceea-467a-9575-1a9b2e6c3d70"]
)
def test_a_missing_or_placeholder_hash_is_no_password(hashed_password: str | None):
assert not User(username="oidc_user", hashed_password=hashed_password).has_password
@pytest.mark.parametrize(
"oidc_enabled, oidc_sub, hashed_password, expected",
[
(True, "provider-sub", "$2b$12$hash", True),
(True, None, "8f14e45f-ceea-467a-9575-1a9b2e6c3d70", True),
(True, None, "$2b$12$hash", False),
(False, "provider-sub", "$2b$12$hash", False),
],
)
def test_email_managed_by_oidc(
mocker,
oidc_enabled: bool,
oidc_sub: str | None,
hashed_password: str,
expected: bool,
):
mocker.patch("models.user.OIDC_ENABLED", oidc_enabled)
user = User(username="u", oidc_sub=oidc_sub, hashed_password=hashed_password)
assert user.email_managed_by_oidc is expected