/
test.py
262 lines (204 loc) · 10.4 KB
/
test.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import unittest
import controller
# from controller import app
from model import db, connect_to_db
from seed_testdata_csv import (populate_business, populate_users, populate_animals,
populate_people, populate_services,
populate_reservations)
# write tests as we go
class ControllerTests(unittest.TestCase):
"""Flask tests that use the database."""
def setUp(self):
"""Stuff to do before every test."""
# Get the Flask test client
self.client = controller.app.test_client()
# Show Flask errors that happen during tests
controller.app.config['TESTING'] = True
# Connect to test database
# @todo set from environment config
connect_to_db(controller.app, 'postgresql:///dogwalker')
# # Create tables and add sample data
db.drop_all()
db.create_all()
# # Populate the test data
populate_business()
populate_users()
populate_animals()
populate_people()
populate_services()
populate_reservations()
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
def test_sees_homepage(self):
"""Confirm user sees login button."""
result = self.client.get('/')
self.assertIn('Log Me In', result.data)
self.assertNotIn('Logout', result.data)
def test_login(self):
"""Confirm user can log in."""
result = self.client.post('/login',
data={'user_name': 'mary',
'password': '1234'},
follow_redirects=True)
self.assertIn('Logout', result.data)
self.assertNotIn('Log Me In', result.data)
def test_logout(self):
"""Confirm user can logout."""
result = self.client.post('/login',
data={'user_name': 'mary',
'password': '1234'},
follow_redirects=True)
result = self.client.get('/logout', follow_redirects=True)
self.assertIn('Log Me In', result.data)
self.assertNotIn('Logout', result.data)
def test_register_duplicate_username(self):
"""Confirm user is unable to register duplicate username."""
result = self.client.post('/register',
data={'register_user_name': 'mary',
'register_password': '1234',
'register_first_name': 'frank',
'register_last_name': 'franklast',
'register_email': 'frank@frank',
'register_business_name': ''},
follow_redirects=True)
self.assertIn('Please select another username.', result.data)
def test_register_no_bizname(self):
"""Confirm user can register without adding business name."""
result = self.client.post('/register',
data={'register_user_name': 'frank',
'register_password': '1234',
'register_first_name': 'frank',
'register_last_name': 'franklast',
'register_email': 'frank@frank',
'register_business_name': ''},
follow_redirects=True)
self.assertIn('Add Business Details', result.data)
def test_register_later_bizname(self):
"""Confirm user can register and seperately add business name."""
result = self.client.post('/register',
data={'register_user_name': 'frank',
'register_password': '1234',
'register_first_name': 'frank',
'register_last_name': 'franklast',
'register_email': 'frank@frank',
'register_business_name': ''},
follow_redirects=True)
self.assertIn('Add Business Details', result.data)
result = self.client.post('/business/add',
data={'business_name': 'franksdogs',
'street': '683 Sutter Street',
'city': 'San Francisco',
'state': 'CA',
'zip': '94103',
'phone': '4155555555',
'url': 'franksurl',
'license': 'frankslicense'},
follow_redirects=True)
self.assertIn('Edit Business Details', result.data)
self.assertIn('franksdogs', result.data)
self.assertIn('683 Sutter Street', result.data)
self.assertIn('San Francisco', result.data)
self.assertIn('CA', result.data)
self.assertIn('94103', result.data)
self.assertIn('4155555555', result.data)
self.assertIn('franksurl', result.data)
self.assertIn('frankslicense', result.data)
def test_register(self):
"""User can register with a business name."""
result = self.client.post('/register',
data={'register_user_name': 'sally',
'register_password': '1234',
'register_first_name': 'Sally',
'register_last_name': 'Sallylast',
'register_email': 'sally@sally',
'register_business_name': 'Sally\'s Pets'
},
follow_redirects=True
)
self.assertIn('Sally's Pets', result.data)
self.assertIn('Welcome Sally', result.data)
self.assertIn('Edit Business Details', result.data)
def test_business_update(self):
"""Confirm user can update business details."""
result = self.client.post('/login',
data={'user_name': 'mary',
'password': '1234'},
follow_redirects=True)
self.assertIn('Logout', result.data)
self.assertNotIn('Log Me In', result.data)
result = self.client.post('/business/update',
data={'business_name': 'franksdogs',
'street': '683 Sutter Street',
'city': 'San Francisco',
'state': 'CA',
'zip': '94103',
'phone': '4155555555',
'url': 'franksurl',
'license': 'frankslicense'},
follow_redirects=True)
self.assertIn('franksdogs', result.data)
self.assertIn('683 Sutter Street', result.data)
self.assertIn('San Francisco', result.data)
self.assertIn('CA', result.data)
self.assertIn('94103', result.data)
self.assertIn('4155555555', result.data)
self.assertIn('franksurl', result.data)
self.assertIn('frankslicense', result.data)
def test_unauthorized_login(self):
"""Unauthorized user/pass cannot login."""
result = self.client.post('/login',
data={'user_name': 'mary',
'password': '12345'},
follow_redirects=True)
self.assertIn('did not match a registered user', result.data)
def test_route_blocked(self):
"""Unauthorized user/pass cannot view route requiring login."""
result = self.client.get('/business', follow_redirects=True)
self.assertIn('Unauthorized', result.data)
def test_add_animal(self):
"""adding a person at same time you add a dog from business.html"""
result = self.client.post('/login',
data={'user_name': 'mary',
'password': '1234'},
follow_redirects=True)
self.assertIn('Logout', result.data)
self.assertNotIn('Log Me In', result.data)
result = self.client.post('/animal/add',
data={'business_id': '1',
'name': 'Julius',
'species': 'Dog',
'breed': 'German Shepard',
'birthday': '3/1/2005',
'vet': 'Four Seasons Pet Hospital',
'note': 'Needs muzzle on trail',
'fullname': 'Sally Brown',
'street': '200 Irving Street',
'city': 'San Francisco',
'state': 'CA',
'zipcode': '94122',
'phone': '3101111111',
'email': 'sally@brown'
},
follow_redirects=True
)
self.assertIn('Julius', result.data)
self.assertIn('Sally Brown', result.data)
# JAVASCRIPT clicking on edit enables all business form fields
# adding a dog from business.html
# adding a service
# edit a service
# person telephone number longer than 10 isn't allowed
# add a reservation
# edit a reservation
# add a person to a dog
# edit an animal
# edit a person
# add a dog to a household
# @todo -- how to control what data a user can view
# @todo http://pythonhosted.org/dictalchemy/#utilities
# coverage run --source=. --omit=env/*,seed_testdata_csv.py test.py
# coverage report -m
if __name__ == "__main__":
unittest.main()