-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user_views.py
168 lines (117 loc) · 5.67 KB
/
test_user_views.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""User View tests."""
# run these tests like:
#
# FLASK_ENV=production python -m unittest test_user_views.py
from unittest import TestCase
from models import db, User, SavedSearch
from app import app, CURR_USER_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///restroom-finder-test'
db.create_all()
app.config['WTF_CSRF_ENABLED'] = False
class UserViewTestCase(TestCase):
"""Test views for user."""
def setUp(self):
"""Create test client, add sample data."""
SavedSearch.query.delete()
User.query.delete()
self.client = app.test_client()
self.testuser = User.register(
email="[email protected]",
password="test_password",
firstname="Tom",
lastname="Jones"
)
db.session.commit()
def test_home_logged_in_redirect(self):
"""Does the home route display correct the html if the user is logged in?
The user should be redirected to the Search Page
"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
resp = c.get("/", follow_redirects=True)
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('<div id="search-container" class="col mt-3 px-0">', html)
def test_home_logged_out(self):
"""Does the home route display correct the html if the user is logged in"""
with self.client as c:
resp = self.client.get("/")
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('id="landing-container"', html)
def test_display_register_form(self):
"""Does the register route display correct the html"""
with self.client as c:
resp = self.client.get("/register")
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('<form id="register-form" class="user-form" method="POST">', html)
def test_add_user(self):
"""Can user add a new user and be redirected to the search paged?"""
with self.client as c:
u = {"email": "[email protected]",
"password": "testpassword",
"firstname": "Jon",
"lastname": "Snow"}
resp = c.post("/register", data=u, follow_redirects=True)
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('<div id="search-container" class="col mt-3 px-0">', html)
def test_login_form(self):
"""Does the add login route display correct the html"""
with self.client as c:
resp = self.client.get("/login")
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('<form id="login-form" class="user-form" method="POST">', html)
def test_login_user(self):
"""Can user login?
The user will be redirected to the search page on successful login
"""
with self.client as c:
u = {"email": "[email protected]",
"password": "test_password"}
resp = c.post("/login", data=u, follow_redirects=True)
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('<div id="search-container" class="col mt-3 px-0">', html)
def test_logout(self):
"""Can user logout?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
resp = c.get("/logout", follow_redirects=True)
html = resp.get_data(as_text=True)
# Does the user get redirected to the login page after logging out?
self.assertEqual(resp.status_code, 200)
self.assertIn('<form id="login-form" class="user-form" method="POST">', html)
def test_display_user_profile(self):
"""Does the user profile route display correct the html"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
resp = self.client.get(f"/users/{self.testuser.id}")
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('<form id="edit-user-form" class="user-form" method="POST">', html)
def test_update_user(self):
"""Can user update their profile info?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
firstname = "Newfirst"
lastname = "Newlast"
u = {"firstname": firstname,
"lastname": lastname}
# update test user using the update route
resp = c.post(f"/users/{self.testuser.id}", data=u, follow_redirects=True)
html = resp.get_data(as_text=True)
# check response and redirect to correct html for user profile with updated info
self.assertEqual(resp.status_code, 200)
self.assertIn(f'<input class="form-control" id="firstname" name="firstname" required type="text" value="{firstname}">', html)
# check testuser's info has been updated correctly
test_user = User.query.get(self.testuser.id)
self.assertEqual(test_user.email,"[email protected]")
self.assertEqual(test_user.firstname, firstname)
self.assertEqual(test_user.lastname, lastname)