QUA-002, premiere moitie. **Ce commit ne fait que reformater** : aucun changement de comportement, aucune ligne de logique touchee. 72 fichiers, 4 restaient deja conformes. Il est isole exprès, pour que `git log -p` sur les commits voisins reste lisible. `quote-style = "preserve"` etait deja pose dans pyproject.toml, ce qui evite le brassage guillemets simples / doubles : le diff porte sur les retours a la ligne, l indentation des appels longs et les virgules finales, pas sur le style de chaine. Verification : 263 tests passent avant et apres, ruff check propre. L activation en CI arrive dans le commit suivant, separement, pour que ce diff-ci ne contienne rien d autre. Co-Authored-By: Claude Opus 5 <[email protected]>
116 lines
3.9 KiB
Python
116 lines
3.9 KiB
Python
"""Changing a user's role — ARCH-008.
|
|
|
|
`role` is the polymorphic discriminator, and SQLAlchemy fixes an instance's
|
|
class at load time. edit_user therefore writes the column with a
|
|
statement-level UPDATE and re-reads the row, which is correct. What it did
|
|
next was not: it called db.session.remove(), throwing away the whole
|
|
session, and it committed the role on its own before the rest of the edit
|
|
had been applied.
|
|
|
|
The two consequences these tests pin down:
|
|
|
|
- everything the request still held became detached, current_user
|
|
included. The route only survived because it had copied the actor's
|
|
name and id into locals beforehand — a workaround, in place of the fix.
|
|
- the role change was already committed when the rest of the edit ran, so
|
|
a failure afterwards left the account promoted and nothing else
|
|
applied.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from app.extensions import db
|
|
from app.models import Coach, Player, User
|
|
|
|
|
|
def _edit(client, user_id, **overrides):
|
|
payload = {
|
|
'full_name': 'Promoted Person',
|
|
'email': '[email protected]',
|
|
'role': 'coach',
|
|
'is_active_account': 'on',
|
|
}
|
|
payload.update(overrides)
|
|
return client.post(f'/users/{user_id}/edit', data=payload, follow_redirects=True)
|
|
|
|
|
|
class TestPolymorphicIdentity:
|
|
def test_the_account_takes_its_new_class(self, app, client, as_role, make_user):
|
|
target_id = make_user('player')
|
|
as_role('admin')
|
|
|
|
_edit(client, target_id)
|
|
|
|
with app.app_context():
|
|
assert isinstance(db.session.get(User, target_id), Coach)
|
|
|
|
def test_the_new_role_grants_its_pages_right_away(self, app, client, as_role, make_user, login):
|
|
"""isinstance() is how this application authorises; a stale class in
|
|
the identity map is a stale permission set."""
|
|
target_id = make_user('player')
|
|
as_role('admin')
|
|
_edit(client, target_id)
|
|
|
|
with app.app_context():
|
|
username = db.session.get(User, target_id).username
|
|
client.post('/auth/logout')
|
|
login(username)
|
|
|
|
# Coach-only, and it is a coach's own page rather than a redirect.
|
|
assert client.get('/users/notes-dashboard').status_code == 200
|
|
|
|
def test_the_other_fields_of_the_edit_land_too(self, app, client, as_role, make_user):
|
|
target_id = make_user('player')
|
|
as_role('admin')
|
|
|
|
_edit(client, target_id, full_name='Renamed Too', phone='555-0100')
|
|
|
|
with app.app_context():
|
|
user = db.session.get(User, target_id)
|
|
assert user.full_name == 'Renamed Too'
|
|
assert user.phone == '555-0100'
|
|
|
|
|
|
class TestTheActingPresident:
|
|
"""db.session.remove() detached current_user mid-request."""
|
|
|
|
def test_the_president_keeps_their_session(self, client, as_role, make_user):
|
|
target_id = make_user('player')
|
|
as_role('admin')
|
|
|
|
_edit(client, target_id)
|
|
|
|
assert client.get('/users').status_code == 200
|
|
|
|
def test_the_response_renders(self, client, as_role, make_user):
|
|
target_id = make_user('player')
|
|
as_role('admin')
|
|
|
|
response = _edit(client, target_id)
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
class TestAtomicity:
|
|
def test_a_failure_after_the_update_leaves_the_role_alone(
|
|
self, app, client, as_role, make_user, monkeypatch
|
|
):
|
|
"""The role used to be committed on its own, before the rest of the
|
|
edit ran. A failure afterwards left an account promoted, and no
|
|
interface reported it."""
|
|
target_id = make_user('player')
|
|
as_role('admin')
|
|
|
|
def _explode(user, selected_games):
|
|
raise RuntimeError('storage unavailable')
|
|
|
|
monkeypatch.setattr('app.routes.users.update_user_gamertags', _explode)
|
|
|
|
with pytest.raises(RuntimeError):
|
|
_edit(client, target_id)
|
|
|
|
with app.app_context():
|
|
user = db.session.get(User, target_id)
|
|
assert isinstance(user, Player)
|
|
assert user.role == 'player'
|