Files
TimeTracker/tests
MacJediWizard c46fb360cb fix(test): re-query user inside session to avoid detached Role error
Three tests in `test_timer_edit_own_time_entries.py` failed with
`sqlalchemy.exc.InvalidRequestError: Object '<Role at 0x...>' is not
attached to a Session` when the suite ran after v5.5.6's fixture
refresh.

Root cause: the `user` fixture commits the user and calls
`db.session.refresh(user)`, leaving the instance bound to the
fixture's scoped session. Each test then enters a fresh
`with app.app_context():` block before calling
`_ensure_edit_own_permission(user)`. Flask-SQLAlchemy's scoped session
in the new context is a *different* session than the one that owns
`user`, so when `user.add_role(role)` tries to associate a freshly
loaded `Role` with the cross-session `user`, the resulting commit
detaches the role and the next attribute access fails.

Fix: have `_ensure_edit_own_permission` take a `user_id` instead of the
ORM instance and re-query the user from the active session
(`User.query.get(user_id)`). All three objects (user, role, permission)
now live in the same identity map. Drop the trailing
`db.session.refresh(user)` — route handlers re-load the user via their
own request-scoped sessions and see the persisted association.

Test plan
- pytest tests/test_timer_edit_own_time_entries.py
2026-05-14 16:48:43 -04:00
..
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00
2026-03-08 06:50:42 +01:00