-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
169 lines (115 loc) · 4.98 KB
/
tests.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
169
"""
Routes covered by this file:
/
/new_user
/login
/logout
/reviews
"""
from unittest import TestCase
from server import app, session # import app and session from server
from model import db, User, Landlord, Building, Review, HPDViolation, HPDRegistration, HPDContact, connect_to_db
import os
class FlaskTestsBasic(TestCase):
"""Set up and run Flask tests"""
def setUp(self):
"""Stuff to do before every test"""
# Get the Flask test client
self.client = app.test_client()
# Show Flask errors that happen during tests
app.config['TESTING'] = True
# Set up secret key in order to use "session"
app.config['SECRET_KEY'] = 'key'
def test_index(self):
"""Test homepage."""
result = self.client.get("/")
self.assertIn(b"Renters", result.data)
class FlaskTestsNewUSer(TestCase):
"""Test new user creation route."""
def setUp(self):
"""Stuff to do before every test: get Flask test client, show errors,
set up secret key"""
self.client = app.test_client()
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'key'
def tearDown(self):
"""Stuff to do at end of every test."""
# Remove user from database that was created in test_new_user_route
user = User.query.filter(User.email == "newuser@gmail.com").first()
if user:
db.session.delete(user)
db.session.commit()
def test_new_user_route(self):
"""Check that a new user can be created and stored in 'users' database."""
with self.client as c:
result = c.post("/new_user",
data={"email": "newuser@gmail.com", "username": "test_user",
"password": "pwd543"}, follow_redirects=True)
# Database query to "Users" that returns user object that matches "newuser@gmail.com"
db_query = User.query.filter(User.email == "newuser@gmail.com").first()
self.assertEqual("newuser@gmail.com", db_query.email)
class FlaskTestsLogInLogOut(TestCase):
"""Test user log in and log out."""
def setUp(self):
"""Stuff to do before every test: get Flask test client, show errors,
set up secret key"""
self.client = app.test_client()
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'key'
# Create new user in database
user = User(email="email@gmail.com", username="user_name", password="password123")
db.session.add(user)
db.session.commit()
def tearDown(self):
"""Stuff to do at end of every test."""
# Remove user from database that was created in setUp
user = User.query.filter(User.email == "email@gmail.com").first()
db.session.delete(user)
db.session.commit()
def test_login_route(self):
"""Check that the login route adds user to session for user already in DB."""
with self.client as c:
result = c.post("/login",
data={"email": "email@gmail.com", "password": "password123"},
follow_redirects=True)
self.assertEqual(session["user_email"], "email@gmail.com")
def test_logout_route(self):
"""Check that logout route removes user from sesssion for logged in user
who is already in DB."""
with self.client as c:
with c.session_transaction() as sess:
sess["user_email"] = "email@gmail.com"
result = self.client.get("/logout", follow_redirects=True)
self.assertNotIn(b"user_email", session)
class FlaskTestsLoggedIn(TestCase):
"""Flask tests with user logged in to session."""
def setUp(self):
"""Stuff to do before every test."""
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'key'
self.client = app.test_client()
with self.client as c:
with c.session_transaction() as sess:
sess['user_email'] = "user4@test.com"
def test_write_review_form(self):
"""Check that the write review form shows up in the all_reviews page
for users that are logged in."""
result = self.client.get("/reviews")
self.assertIn(b"Write a Review", result.data)
class FlaskTestsLoggedOut(TestCase):
"""Flask tests with no user logged in to session."""
def setUp(self):
"""Stuff to do before every test."""
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'key'
self.client = app.test_client()
def test_no_review_form(self):
"""Check that the write review form does not show up in the all_reviews
page for users that are not logged in."""
result = self.client.get("/reviews")
self.assertNotIn(b"Write a Review", result.data)
if __name__ == '__main__':
import unittest
# Connect to app from server
connect_to_db(app)
unittest.main()