mirror of
https://github.com/DRYTRIX/TimeTracker.git
synced 2026-05-17 01:49:35 -05:00
c46fb360cb
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