-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user_views.py
98 lines (66 loc) · 3.09 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
"""User View tests."""
# run these tests like:
#
# FLASK_DEBUG=False python -m unittest test_message_views.py
import os
from unittest import TestCase
from models import db, Message, User, connect_db
# BEFORE we import our app, let's set an environmental variable
# to use a different database for tests (we need to do this
# before we import our app, since that will have already
# connected to the database
os.environ['DATABASE_URL'] = "postgresql:///warbler_test"
# Now we can import app
from app import app, CURR_USER_KEY
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
# This is a bit of hack, but don't use Flask DebugToolbar
app.config['DEBUG_TB_HOSTS'] = ['dont-show-debug-toolbar']
# Create our tables (we do this here, so we only create the tables
# once for all tests --- in each test, we'll delete the data
# and create fresh new clean test data
connect_db(app)
db.drop_all()
db.create_all()
# Don't have WTForms use CSRF at all, since it's a pain to test
app.config['WTF_CSRF_ENABLED'] = False
class UserBaseViewTestCase(TestCase):
def setUp(self):
User.query.delete()
db.session.flush()
db.session.commit()
self.u1_id = u1.id
self.u2_id = u2.id
self.u3_id = u3.id
self.u4_id = u4.id
self.u1 = u1
self.u2 = u2
self.u3 = u3
self.u4 = u4
self.client = app.test_client()
class UserFollowUnfollowTestCase(UserBaseViewTestCase):
def test_see_followers_page(self):
"""Test to go to other user's followers page when logged in"""
with self.client as client:
with client.session_transaction() as sess:
sess[CURR_USER_KEY] = self.u1_id
resp_follow = client.post(f"/users/follow/{self.u2_id}", follow_redirects=True)
self.assertEqual(resp_follow.status_code, 200)
resp_followers_page = client.get(f"/users/{self.u2_id}/followers")
resp_followers_page_html = resp_followers_page.get_data(as_text=True)
self.assertIn(self.u1.username, resp_followers_page_html)
self.assertEqual(resp_followers_page.status_code, 200)
def test_cant_followers_page_not_logged_in(self):
"""test to not be able to go to other user's followers page when not logged in"""
with self.client as client:
resp = client.get(f"/users/{self.u2_id}/followers", follow_redirects=True)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.request.path, "/")
# TODO: ADD TEST FOR FOLLOWING PAGES
# TODO: ADD TEST FOR LIKING THINGS.
# LEAVE TODOS IN GITHUB HOSTED CODE FOR EMPLOYERS TO SEE SHOWING FUTURE THOUGHTS
# // When you’re logged in, can you see the follower / following pages for any user?
# // When you’re logged out, are you disallowed from visiting a user’s follower / following pages?