forked from codeforpdx/dwellinglybackend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
114 lines (93 loc) · 3.7 KB
/
conftest.py
File metadata and controls
114 lines (93 loc) · 3.7 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
110
111
112
113
114
import pytest
import os
from app import create_app
from db import db
from data.seedData import seedData
from models.user import UserModel
from models.property import PropertyModel
newPropertyName = "test1"
newPropertyAddress = "123 NE FLanders St"
# Note: this repo uses the "pytest-flask" plugin which exposes the following fixtures for use in tests:
# client: an instance of flask's app.test_client - for making requests i.e. client.get('/')
@pytest.fixture
def app():
app = create_app()
return app
@pytest.fixture
def admin_user():
adminUser = UserModel(email="user4@dwellingly.org", password="1234", firstName="user4", lastName="admin", phone="555-867-5309", role="admin", archived=0)
return adminUser
@pytest.fixture
def new_user():
newUser = UserModel(email="someone@domain.com", password="1234", firstName="user2", lastName="tester", phone="1-888-cal-saul", role="", archived=0)
return newUser
@pytest.fixture
def property_manager_user():
return UserModel(email="manager@domain.com", password="1234", firstName="Leslie", lastName="Knope", phone="505-503-4455", role="property_manager", archived=0)
#Returns an object with authorization headers for users of all roles (admin, property-manager, pending)
@pytest.fixture
def auth_headers(client, test_database, admin_user, new_user, property_manager_user):
admin_auth_header = get_auth_header(client, admin_user)
pm_auth_header = get_auth_header(client, property_manager_user)
pending_auth_header = get_auth_header(client, new_user)
return {
"admin": admin_auth_header,
"pm": pm_auth_header,
"pending": pending_auth_header
}
@pytest.fixture
def new_property():
newProperty = PropertyModel( name=newPropertyName
, address=newPropertyAddress
, city="Portland"
, unit="101"
, state="OR"
, zipcode="97207"
, propertyManager=5
, tenants=3
, dateAdded="2020-04-12"
, archived=0
)
return newProperty
@pytest.fixture
def empty_database():
if(os.path.isfile("./data.db")):
os.remove("./data.db")
@pytest.fixture
def test_database(app, admin_user, new_user, property_manager_user):
db.create_all()
seedData()
db.session.add(admin_user)
db.session.add(new_user)
db.session.add(property_manager_user)
db.session.commit()
yield db
db.drop_all()
# ------------- NON-FIXTURE FUNCTIONS --------------------
# Logs a user in and returns their auth header
# To log a user in, you must also load the "test_database" fixture
def get_auth_header(client, userModel):
login_response = client.post("/api/login", json={
"email": userModel.email,
"password": userModel.password
})
auth_header = {"Authorization": f"Bearer {login_response.json['access_token']}"}
return auth_header
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}')