refactor(user): confirm an email inside the single user update

pr-ready step 3 (simplify). The confirmation now rides the one
apply_user_update transaction, guarded by expected_email, so a later 400
or 403 rolls it back and the edit audit sees it without a post-write
patch. update_user resets trust whenever it writes an email the caller
doesn't vouch for, replacing trust_email. Tests share the dialog's apply,
save and emailField helpers, and drop two redundant local imports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-11-17 09:30:48 -05:00
parent cf46b01ff5
commit 012ae2fa1e
8 changed files with 139 additions and 72 deletions

View File

@@ -3,7 +3,7 @@ from datetime import datetime, timedelta, timezone
import pytest
from exceptions.database_exceptions import LastAdminError
from exceptions.database_exceptions import EmailChangedError, LastAdminError
from handler.database import db_user_handler
from handler.database.base_handler import sync_session
from models.user import Role, User
@@ -138,19 +138,40 @@ class TestUpdateLastActive:
assert executed_statements == []
class TestTrustEmail:
def test_trusts_the_email_the_user_still_holds(self, viewer_user: User):
class TestUpdateUserEmailTrust:
def test_a_new_email_is_untrusted_unless_the_caller_vouches(
self, viewer_user: User
):
db_user_handler.update_user(
viewer_user.id, {"email": "old@example.com", "email_trusted": True}
)
updated = db_user_handler.update_user(
viewer_user.id, {"email": "new@example.com"}
)
assert not updated.email_trusted
def test_writes_when_the_expected_email_still_matches(self, viewer_user: User):
db_user_handler.update_user(viewer_user.id, {"email": "viewer@example.com"})
assert db_user_handler.trust_email(viewer_user.id, "Viewer@Example.com")
updated = db_user_handler.update_user(
viewer_user.id,
{"email_trusted": True},
expected_email="Viewer@Example.com",
)
current = db_user_handler.get_user(viewer_user.id)
assert current and current.email_trusted
assert updated.email_trusted
def test_leaves_a_changed_email_untrusted(self, viewer_user: User):
def test_refuses_when_the_email_changed(self, viewer_user: User):
db_user_handler.update_user(viewer_user.id, {"email": "taken@example.com"})
assert not db_user_handler.trust_email(viewer_user.id, "viewer@example.com")
with pytest.raises(EmailChangedError):
db_user_handler.update_user(
viewer_user.id,
{"email_trusted": True},
expected_email="viewer@example.com",
)
current = db_user_handler.get_user(viewer_user.id)
assert current and not current.email_trusted