-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
794 lines (568 loc) · 22.3 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
"""Tests for Flask Cafe."""
import os
os.environ["DATABASE_URL"] = "postgresql:///flaskcafe_test"
import re
from unittest import TestCase
from flask import session
from app import app, CURR_USER_KEY
from models import db, Cafe, City, connect_db, User, Like
# Make Flask errors be real errors, rather than HTML pages with error info
app.config['TESTING'] = True
# This is a bit of hack, but don't use Flask DebugToolbar
app.config['DEBUG_TB_HOSTS'] = ['dont-show-debug-toolbar']
# Don't req CSRF for testing
app.config['WTF_CSRF_ENABLED'] = False
db.drop_all()
db.create_all()
#######################################
# helper functions for tests
def debug_html(response, label="DEBUGGING"): # pragma: no cover
"""Prints HTML response; useful for debugging tests."""
print("\n\n\n", "*********", label, "\n")
print(response.data.decode('utf8'))
print("\n\n")
def login_for_test(client, user_id):
"""Log in this user."""
with client.session_transaction() as sess:
sess[CURR_USER_KEY] = user_id
#######################################
# data to use for test objects / testing forms
CITY_DATA = dict(
code="sf",
name="San Francisco",
state="CA"
)
CAFE_DATA = dict(
name="Test Cafe",
description="Test description",
url="http://testcafe.com/",
address="500 Sansome St",
city_code="sf",
image_url="http://testcafeimg.com/"
)
CAFE_DATA_NEW = dict(
name="Another Cafe",
description="Another description",
url="http://anothercafe.com/",
address="500 Sansome St",
city_code="sf",
image_url="http://anothercafeimg.com/"
)
CAFE_DATA_EDIT = dict(
name="new-name",
description="new-description",
url="http://new-image.com/",
address="500 Sansome St",
city_code="sf",
image_url="http://new-image.com/"
)
TEST_USER_DATA = dict(
username="test",
first_name="Testy",
last_name="MacTest",
description="Test Description.",
email="[email protected]",
password="secret",
)
TEST_USER_DATA_EDIT = dict(
first_name="new-fn",
last_name="new-ln",
description="new-description",
email="[email protected]",
image_url="http://new-image.com",
)
TEST_USER_DATA_NEW = dict(
username="new-username",
first_name="new-fn",
last_name="new-ln",
description="new-description",
password="secret",
email="[email protected]",
image_url="http://new-image.com",
)
ADMIN_USER_DATA = dict(
username="admin",
first_name="Addie",
last_name="MacAdmin",
description="Admin Description.",
email="[email protected]",
password="secret",
admin=True,
)
#######################################
# homepage
class HomepageViewsTestCase(TestCase):
"""Tests about homepage."""
def test_homepage(self):
with app.test_client() as client:
resp = client.get("/")
self.assertIn(b'Where Coffee Dreams Come True', resp.data)
#######################################
# cities
class CityModelTestCase(TestCase):
"""Tests for City Model."""
def setUp(self):
"""Before all tests, add sample city & cafes"""
Cafe.query.delete()
City.query.delete()
sf = City(**CITY_DATA)
db.session.add(sf)
cafe = Cafe(**CAFE_DATA)
db.session.add(cafe)
db.session.commit()
self.cafe = cafe
def tearDown(self):
"""After each test, remove all cafes."""
Cafe.query.delete()
City.query.delete()
db.session.commit()
# depending on how you solve exercise, you may have things to test on
# the City model, so here's a good place to put that stuff.
def test_get_choices_cities(self):
self.assertEqual(City.get_choices_cities(), [('sf', 'San Francisco')])
#######################################
# cafes
class CafeModelTestCase(TestCase):
"""Tests for Cafe Model."""
def setUp(self):
"""Before all tests, add sample city & users"""
Cafe.query.delete()
City.query.delete()
sf = City(**CITY_DATA)
db.session.add(sf)
cafe = Cafe(**CAFE_DATA)
db.session.add(cafe)
db.session.commit()
self.cafe = cafe
def tearDown(self):
"""After each test, remove all cafes."""
Cafe.query.delete()
City.query.delete()
db.session.commit()
def test_get_city_state(self):
self.assertEqual(self.cafe.get_city_state(), "San Francisco, CA")
class CafeViewsTestCase(TestCase):
"""Tests for views on cafes."""
def setUp(self):
"""Before all tests, add sample city & users"""
Cafe.query.delete()
City.query.delete()
sf = City(**CITY_DATA)
db.session.add(sf)
cafe = Cafe(**CAFE_DATA)
db.session.add(cafe)
db.session.commit()
self.cafe_id = cafe.id
def tearDown(self):
"""After each test, remove all cafes."""
Cafe.query.delete()
City.query.delete()
db.session.commit()
def test_list(self):
with app.test_client() as client:
resp = client.get("/cafes")
self.assertEqual(resp.status_code, 200)
self.assertIn(b"Test Cafe", resp.data)
def test_detail(self):
with app.test_client() as client:
resp = client.get(f"/cafes/{self.cafe_id}")
self.assertEqual(resp.status_code, 200)
self.assertIn(b"Test Cafe", resp.data)
self.assertIn(b'testcafe.com', resp.data)
class CafeAdminViewsTestCase(TestCase):
"""Tests for add/edit views on cafes."""
def setUp(self):
"""Before each test, add sample city, users, and cafes"""
User.query.delete()
City.query.delete()
Cafe.query.delete()
sf = City(**CITY_DATA)
db.session.add(sf)
cafe = Cafe(**CAFE_DATA)
db.session.add(cafe)
user = User.register(**TEST_USER_DATA)
db.session.add(user)
admin = User.register(**ADMIN_USER_DATA)
db.session.add(admin)
db.session.commit()
self.cafe_id = cafe.id
self.user_id = user.id
self.admin_id = admin.id
def tearDown(self):
"""After each test, delete the cities, users, and cafes."""
User.query.delete()
Cafe.query.delete()
City.query.delete()
db.session.commit()
def test_dynamic_cities_vocab(self):
id = self.cafe_id
# the following is a regular expression for the HTML for the drop-down
# menu pattern we want to check for
choices_pattern = re.compile(
r'<select [^>]*name="city_code"[^>]*><option [^>]*value="sf">' +
r'San Francisco</option></select>')
with app.test_client() as client:
login_for_test(client, self.admin_id)
resp = client.get(f"/cafes/add")
self.assertRegex(resp.data.decode('utf8'), choices_pattern)
resp = client.get(f"/cafes/{id}/edit")
self.assertRegex(resp.data.decode('utf8'), choices_pattern)
def test_anon_add(self):
with app.test_client() as client:
resp = client.get(f"/cafes/add", follow_redirects=True)
self.assertIn(b'You are not logged in.', resp.data)
resp = client.post(
f"/cafes/add",
data=CAFE_DATA_NEW,
follow_redirects=True)
self.assertIn(b'You are not logged in.', resp.data)
def test_user_add(self):
with app.test_client() as client:
login_for_test(client, self.user_id)
resp = client.get(f"/cafes/add", follow_redirects=True)
self.assertIn(b'For administrators only.', resp.data)
resp = client.post(
f"/cafes/add",
data=CAFE_DATA_NEW,
follow_redirects=True)
self.assertIn(b'For administrators only.', resp.data)
def test_admin_add(self):
with app.test_client() as client:
login_for_test(client, self.admin_id)
resp = client.get(f"/cafes/add")
self.assertIn(b'Add Cafe', resp.data)
resp = client.post(
f"/cafes/add",
data=CAFE_DATA_NEW,
follow_redirects=True)
self.assertIn(b'added', resp.data)
self.assertIn(b'Another Cafe', resp.data)
def test_anon_edit(self):
id = self.cafe_id
with app.test_client() as client:
resp = client.get(f"/cafes/{id}/edit", follow_redirects=True)
self.assertIn(b'You are not logged in.', resp.data)
resp = client.post(
f"/cafes/{id}/edit",
data=CAFE_DATA_EDIT,
follow_redirects=True)
self.assertIn(b'You are not logged in.', resp.data)
def test_user_edit(self):
id = self.cafe_id
with app.test_client() as client:
login_for_test(client, self.user_id)
resp = client.get(f"/cafes/{id}/edit", follow_redirects=True)
self.assertIn(b'For administrators only.', resp.data)
resp = client.post(
f"/cafes/{id}/edit",
data=CAFE_DATA_EDIT,
follow_redirects=True)
self.assertIn(b'For administrators only.', resp.data)
def test_admin_edit(self):
id = self.cafe_id
with app.test_client() as client:
login_for_test(client, self.admin_id)
resp = client.get(f"/cafes/{id}/edit", follow_redirects=True)
self.assertIn(b'Edit Test Cafe', resp.data)
resp = client.post(
f"/cafes/{id}/edit",
data=CAFE_DATA_EDIT,
follow_redirects=True)
self.assertIn(b'edited', resp.data)
def test_edit_form_shows_curr_data(self):
id = self.cafe_id
with app.test_client() as client:
login_for_test(client, self.admin_id)
resp = client.get(f"/cafes/{id}/edit", follow_redirects=True)
self.assertIn(b'Test description', resp.data)
def test_anon_delete(self):
id = self.cafe_id
with app.test_client() as client:
resp = client.get(f"/cafes/{id}/delete", follow_redirects=True)
self.assertIn(b'You are not logged in.', resp.data)
resp = client.post(f"/cafes/{id}/delete", follow_redirects=True)
self.assertIn(b'You are not logged in.', resp.data)
def test_user_delete(self):
id = self.cafe_id
with app.test_client() as client:
login_for_test(client, self.user_id)
resp = client.get(f"/cafes/{id}/delete", follow_redirects=True)
self.assertIn(b'For administrators only.', resp.data)
resp = client.post(
f"/cafes/{id}/delete", follow_redirects=True)
self.assertIn(b'For administrators only.', resp.data)
def test_admin_delete(self):
id = self.cafe_id
with app.test_client() as client:
login_for_test(client, self.admin_id)
resp = client.get(f"/cafes/{id}/delete", follow_redirects=True)
self.assertIn(b'want to delete', resp.data)
resp = client.post(
f"/cafes/{id}/delete", follow_redirects=True)
self.assertIn(b'Deleted', resp.data)
self.assertNotIn(id, db.session.query(Cafe.id))
#######################################
# users
class UserModelTestCase(TestCase):
"""Tests for the user model."""
def setUp(self):
"""Before each test, add sample users."""
User.query.delete()
user = User.register(**TEST_USER_DATA)
db.session.add(user)
db.session.commit()
self.user = user
def tearDown(self):
"""After each test, remove all users."""
User.query.delete()
db.session.commit()
def test_authenticate(self):
rez = User.authenticate("test", "secret")
self.assertEqual(rez, self.user)
def test_authenticate_fail(self):
rez = User.authenticate("no-such-user", "secret")
self.assertFalse(rez)
rez = User.authenticate("test", "password")
self.assertFalse(rez)
def test_full_name(self):
self.assertEqual(self.user.get_full_name(), "Testy MacTest")
def test_register(self):
u = User.register(**TEST_USER_DATA)
# test that password gets bcrypt-hashed (all start w/$2b$)
self.assertEqual(u.hashed_password[:4], "$2b$")
db.session.rollback()
class AuthViewsTestCase(TestCase):
"""Tests for views on logging in/logging out/registration."""
def setUp(self):
"""Before each test, add sample users."""
User.query.delete()
user = User.register(**TEST_USER_DATA)
db.session.add(user)
db.session.commit()
self.user_id = user.id
def tearDown(self):
"""After each test, remove all users."""
User.query.delete()
db.session.commit()
def test_signup(self):
with app.test_client() as client:
resp = client.get("/signup")
self.assertIn(b'Sign Up', resp.data)
resp = client.post(
"/signup",
data=TEST_USER_DATA_NEW,
follow_redirects=True,
)
self.assertIn(b"You are signed up and logged in.", resp.data)
self.assertTrue(session.get(CURR_USER_KEY))
def test_signup_username_taken(self):
with app.test_client() as client:
resp = client.get("/signup")
self.assertIn(b'Sign Up', resp.data)
# signup with same data as the already-added user
resp = client.post(
"/signup",
data=TEST_USER_DATA,
follow_redirects=True,
)
self.assertIn(b"Username already taken", resp.data)
def test_login(self):
with app.test_client() as client:
resp = client.get("/login")
self.assertIn(b'Welcome Back!', resp.data)
resp = client.post(
"/login",
data={"username": "test", "password": "WRONG"},
follow_redirects=True,
)
self.assertIn(b"Invalid credentials", resp.data)
resp = client.post(
"/login",
data={"username": "test", "password": "secret"},
follow_redirects=True,
)
self.assertIn(b"Hello, test", resp.data)
self.assertEqual(session.get(CURR_USER_KEY), self.user_id)
def test_logout(self):
with app.test_client() as client:
login_for_test(client, self.user_id)
resp = client.post("/logout", follow_redirects=True)
self.assertIn(b"successfully logged out", resp.data)
self.assertEqual(session.get(CURR_USER_KEY), None)
class NavBarTestCase(TestCase):
"""Tests navigation bar."""
def setUp(self):
"""Before tests, add sample user."""
User.query.delete()
user = User.register(**TEST_USER_DATA)
db.session.add_all([user])
db.session.commit()
self.user_id = user.id
def tearDown(self):
"""After tests, remove all users."""
User.query.delete()
db.session.commit()
def test_anon_navbar(self):
with app.test_client() as client:
resp = client.get("/")
self.assertIn(b"Log In", resp.data)
self.assertIn(b"Sign Up", resp.data)
self.assertNotIn(b"Testy MacTest", resp.data)
def test_logged_in_navbar(self):
with app.test_client() as client:
login_for_test(client, self.user_id)
resp = client.get("/")
self.assertIn(b"Log Out", resp.data)
self.assertIn(b"Testy MacTest", resp.data)
class ProfileViewsTestCase(TestCase):
"""Tests for views on user profiles."""
def setUp(self):
"""Before each test, add sample user."""
User.query.delete()
user = User.register(**TEST_USER_DATA)
db.session.add(user)
db.session.commit()
self.user_id = user.id
def tearDown(self):
"""After each test, remove all users."""
User.query.delete()
db.session.commit()
def test_anon_profile(self):
with app.test_client() as client:
resp = client.get("/profile", follow_redirects=True)
self.assertIn(b"You are not logged in.", resp.data)
self.assertIn(b'Welcome Back!', resp.data)
def test_logged_in_profile(self):
with app.test_client() as client:
login_for_test(client, self.user_id)
resp = client.get("/profile")
self.assertIn(b"Testy MacTest", resp.data)
self.assertIn(b"Edit Your Profile", resp.data)
def test_anon_profile_edit(self):
with app.test_client() as client:
resp = client.get("/profile", follow_redirects=True)
self.assertIn(b"You are not logged in.", resp.data)
self.assertIn(b'Welcome Back!', resp.data)
def test_logged_in_profile_edit(self):
with app.test_client() as client:
login_for_test(client, self.user_id)
resp = client.get("/profile/edit")
self.assertIn(b"Edit Profile", resp.data)
self.assertIn(b"Testy", resp.data)
self.assertIn(b"MacTest", resp.data)
resp = client.post(
f"/profile/edit",
data=TEST_USER_DATA_EDIT,
follow_redirects=True)
self.assertIn(b'edited', resp.data)
self.assertIn(b'new-description', resp.data)
self.assertIn(b'http://new-image.com', resp.data)
#######################################
# likes
class LikeViewsTestCase(TestCase):
"""Tests for views on user's cafe likes."""
def setUp(self):
"""Before each test, add sample user and cafe."""
Like.query.delete()
User.query.delete()
Cafe.query.delete()
City.query.delete()
user = User.register(**TEST_USER_DATA)
db.session.add(user)
cafe = Cafe(**CAFE_DATA)
db.session.add(cafe)
city = City(**CITY_DATA)
db.session.add(city)
db.session.commit()
self.user = user
self.cafe = cafe
def tearDown(self):
"""After each test, remove all users and cafes."""
Like.query.delete()
User.query.delete()
Cafe.query.delete()
City.query.delete()
db.session.commit()
def test_profile_no_likes(self):
with app.test_client() as client:
login_for_test(client, self.user.id)
resp = client.get("/profile", follow_redirects=True)
self.assertIn(b"You have no liked cafes.", resp.data)
def test_profile_likes(self):
with app.test_client() as client:
login_for_test(client, self.user.id)
self.user.liked_cafes.append(self.cafe)
db.session.commit()
resp = client.get("/profile", follow_redirects=True)
self.assertIn(b"Test Cafe", resp.data)
# Tests for JSON API routes
def test_anon_check_if_like(self):
with app.test_client() as client:
resp = client.get("/api/likes",
query_string={"cafe_id": self.cafe.id})
data = resp.json
self.assertEqual({"error": "Not logged in"}, data)
def test_logged_in_check_if_like_not_liked(self):
with app.test_client() as client:
login_for_test(client, self.user.id)
resp = client.get("/api/likes",
query_string={"cafe_id": self.cafe.id})
data = resp.json
self.assertEqual({"likes": False}, data)
def test_logged_in_check_if_like_is_liked(self):
with app.test_client() as client:
login_for_test(client, self.user.id)
self.user.liked_cafes.append(self.cafe)
db.session.commit()
resp = client.get("/api/likes",
query_string={"cafe_id": self.cafe.id})
data = resp.json
self.assertEqual({"likes": True}, data)
def test_anon_add_like(self):
with app.test_client() as client:
resp = client.post("/api/like",
json={"cafe_id": self.cafe.id})
data = resp.json
self.assertEqual({"error": "Not logged in"}, data)
def test_logged_in_add_like(self):
with app.test_client() as client:
login_for_test(client, self.user.id)
resp = client.post("/api/like",
json={"cafe_id": self.cafe.id})
data = resp.json
self.assertEqual({"liked": self.cafe.id}, data)
self.assertEqual(self.cafe in self.user.liked_cafes, True)
def test_logged_in_add_like_duplicate(self):
with app.test_client() as client:
login_for_test(client, self.user.id)
self.user.liked_cafes.append(self.cafe)
db.session.commit()
resp = client.post("/api/like",
json={"cafe_id": self.cafe.id})
data = resp.json
self.assertEqual({"error": "Already in likes."}, data)
self.assertIsInstance(Cafe.query.one(), Cafe)
def test_anon_remove_like(self):
with app.test_client() as client:
resp = client.post("/api/unlike",
json={"cafe_id": self.cafe.id})
data = resp.json
self.assertEqual({"error": "Not logged in"}, data)
def test_logged_in_remove_like(self):
with app.test_client() as client:
login_for_test(client, self.user.id)
self.user.liked_cafes.append(self.cafe)
db.session.commit()
resp = client.post("/api/unlike",
json={"cafe_id": self.cafe.id})
data = resp.json
self.assertEqual({"unliked": self.cafe.id}, data)
self.assertEqual(self.cafe in self.user.liked_cafes, False)
def test_logged_in_remove_like_not_liked(self):
with app.test_client() as client:
login_for_test(client, self.user.id)
resp = client.post("/api/unlike",
json={"cafe_id": self.cafe.id})
data = resp.json
self.assertEqual({"error": "Not in your likes."}, data)
self.assertEqual(self.cafe in self.user.liked_cafes, False)