This repository has been archived by the owner on Dec 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
conftest.py
108 lines (78 loc) · 2.39 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
import pytest
import jwt
from flask import current_app
from app import create_app
from db import db
from data.seed import Seed
from tests.factory_fixtures import * # noqa: F401, F403
@pytest.fixture(autouse=True)
def environment(monkeypatch):
monkeypatch.setenv("FLASK_ENV", "testing")
@pytest.fixture
def app(monkeypatch):
monkeypatch.setenv("FLASK_ENV", "testing")
app = create_app("testing")
return app
@pytest.fixture(scope="session", autouse=True)
def db_setup():
app = create_app("testing")
with app.app_context():
db.drop_all()
db.create_all()
@pytest.fixture(autouse=True)
def app_context(app):
Seed().destroy_all()
@pytest.fixture
def valid_header(admin_header):
return admin_header
def _user_claims(user):
return {
"sub": user.id,
"email": user.email,
"phone": user.phone,
"firstName": user.firstName,
"lastName": user.lastName,
"role": user.role.value,
}
@pytest.fixture
def header():
def _header(user):
token = jwt.encode(
_user_claims(user),
current_app.secret_key,
algorithm="HS256",
)
return {"Authorization": f"Bearer {token}"}
yield _header
@pytest.fixture
def admin_header(header, create_admin_user):
return header(create_admin_user())
@pytest.fixture
def staff_header(header, create_join_staff):
def _staff_header(staff=None):
return header(staff or create_join_staff())
yield _staff_header
@pytest.fixture
def pm_header(header, create_property_manager):
def _pm_header(pm=None):
return header(pm or create_property_manager())
yield _pm_header
# ------------- NON-FIXTURE FUNCTIONS --------------------
def has_valid_headers(response):
if response.content_type != "application/json":
return False
elif "*" not in response.access_control_allow_origin:
return False
return True
def is_valid(response, expected_status_code):
if not has_valid_headers(response):
return False
if response.status_code != expected_status_code:
return False
return True
# A debug function that prints useful response data
# Be sure to run "pytest -s" to allow console prints
def log(response):
print(f"\n\nResponse Status: {response.status}")
print(f"Response JSON: {response.json}")
print(f"Response headers:\n\n{response.headers}")