forked from codeforboston/police-data-trust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
139 lines (111 loc) · 3.14 KB
/
conftest.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import psycopg2.errors
import pytest
from backend.api import create_app
from backend.auth import user_manager
from backend.config import TestingConfig
from backend.database import User, UserRole, db
from pytest_postgresql.janitor import DatabaseJanitor
example_email = "[email protected]"
admin_email = "[email protected]"
contributor_email = "[email protected]"
example_password = "my_password"
@pytest.fixture(scope="session")
def database():
cfg = TestingConfig()
janitor = DatabaseJanitor(
cfg.POSTGRES_USER,
cfg.POSTGRES_HOST,
cfg.POSTGRES_PORT,
cfg.POSTGRES_DB,
9.6,
cfg.POSTGRES_PASSWORD,
)
try:
janitor.init()
except psycopg2.errors.lookup("42P04"):
pass
yield
janitor.drop()
@pytest.fixture(scope="session")
def app(database):
app = create_app(config="testing")
# The app should be ready! Provide the app instance here.
# Use the app context to make testing easier.
# The main time where providing app context can cause false positives is
# when testing CLI commands that don't pass the app context.
with app.app_context():
yield app
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def example_user(db_session):
user = User(
email=example_email,
password=user_manager.hash_password(example_password),
role=UserRole.PUBLIC,
first_name="first",
last_name="last",
phone_number="(012) 345-6789",
)
db_session.add(user)
db_session.commit()
return user
@pytest.fixture
def admin_user(db_session):
user = User(
email=admin_email,
password=user_manager.hash_password(example_password),
role=UserRole.ADMIN,
first_name="admin",
last_name="last",
)
db_session.add(user)
db_session.commit()
return user
@pytest.fixture
def contributor_user(db_session):
user = User(
email=contributor_email,
password=user_manager.hash_password(example_password),
role=UserRole.CONTRIBUTOR,
first_name="contributor",
last_name="last",
)
db_session.add(user)
db_session.commit()
return user
@pytest.fixture
def access_token(client, example_user):
res = client.post(
"api/v1/auth/login",
json={
"email": example_email,
"password": example_password,
},
)
assert res.status_code == 200
return res.json["access_token"]
@pytest.fixture
def contributor_access_token(client, contributor_user):
res = client.post(
"api/v1/auth/login",
json={
"email": contributor_email,
"password": example_password,
},
)
assert res.status_code == 200
return res.json["access_token"]
@pytest.fixture
def cli_runner(app):
return app.test_cli_runner()
@pytest.fixture(scope="session")
def _db(app):
"""See this:
https://github.com/jeancochrane/pytest-flask-sqlalchemy
Basically, this '_db' fixture is required for the above extension to work.
We use this extension to allow for easy testing of the database.
"""
db.create_all()
yield db