aboutsummaryrefslogtreecommitdiff
path: root/tests/conftest.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py32
1 files changed, 26 insertions, 6 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index a91d2f4..8f7f77c 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -9,6 +9,8 @@ import pytest
import transaction
import webtest
+from sqlalchemy import delete, select
+
from fietsboek import main
from fietsboek import models
from fietsboek.models.meta import Base
@@ -145,7 +147,7 @@ def route_path(app_request):
@pytest.fixture()
-def logged_in(dbsession, testapp, route_path):
+def logged_in(testapp, route_path, dbsession, tm):
"""
A fixture that represents a logged in state.
@@ -153,14 +155,32 @@ def logged_in(dbsession, testapp, route_path):
Returns the user that was logged in.
"""
- user = models.User(email='foo@bar.com', is_verified=True)
- user.set_password("foobar")
- dbsession.add(user)
+ tm.abort()
+
+ with tm:
+ user = models.User(email='foo@barre.com', is_verified=True)
+ user.set_password("foobar")
+ dbsession.add(user)
+ dbsession.flush()
+ user_id = user.id
+
+ tm.begin()
+ tm.doom()
login = testapp.get(route_path('login'))
form = login.form
- form['email'] = 'foo@bar.com'
+ form['email'] = 'foo@barre.com'
form['password'] = 'foobar'
response = form.submit()
assert response.status_code == 302
- return user
+
+ try:
+ # Make sure to return an object that is not bound to the wrong db
+ # session by re-fetching it with the proper fixture session:
+ yield dbsession.execute(select(models.User).filter_by(id=user_id)).scalar_one()
+ finally:
+ tm.abort()
+ with tm:
+ dbsession.execute(delete(models.User).filter_by(id=user_id))
+ tm.begin()
+ tm.doom()