forked from fossasia/open-event-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
109 lines (75 loc) · 2.27 KB
/
conftest.py
File metadata and controls
109 lines (75 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import pytest
from flask import _app_ctx_stack as ctx_stack
from flask.globals import request
from flask_jwt_extended.utils import create_access_token
from app.models import db as _db
from tests.all.integration.setup_database import (
Environment,
Setup,
create_app,
set_settings,
)
from tests.factories.user import UserFactory
@pytest.fixture(scope='module')
def app():
app = create_app()
with app.test_request_context():
yield app
@pytest.fixture(scope='module')
def client(app):
return app.test_client()
@pytest.fixture(scope='module')
def database(app):
_db.create_all()
set_settings(app_name='Open Event', app_environment=Environment.TESTING)
yield _db
Setup.drop_db()
@pytest.fixture(scope='module')
def connection(database):
with database.engine.connect() as conn:
yield conn
@pytest.fixture(scope='function')
def db(database, connection):
transaction = connection.begin()
options = dict(bind=connection, binds={})
session = database.create_scoped_session(options=options)
old_session = database._session
# For proxying session in factories and flask-json-restapi
database._session = session
yield database
session.remove()
database._session = old_session
transaction.rollback()
@pytest.fixture
def user(db):
user = UserFactory(is_admin=False, is_verified=False)
db.session.commit()
return user
@pytest.fixture
def admin_user(db):
user = UserFactory(is_admin=True)
db.session.commit()
return user
@pytest.fixture
def jwt(user):
return {'Authorization': "JWT " + create_access_token(user.id, fresh=True)}
@pytest.fixture
def admin_jwt(admin_user):
return {'Authorization': "JWT " + create_access_token(admin_user.id, fresh=True)}
@pytest.fixture
def login_context(client):
"Use only when above methods don't work"
yield ctx_stack
ctx_stack.top.jwt = {}
ctx_stack.top.jwt_user = None
request.headers = {}
@pytest.fixture
def user_login(login_context, user, jwt):
login_context.top.jwt = {'identity': user.id}
request.headers = jwt
yield user
@pytest.fixture
def admin_login(login_context, admin_user, admin_jwt):
login_context.top.jwt = {'identity': admin_user.id}
request.headers = admin_jwt
yield admin_user