-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_home_view.py
More file actions
76 lines (50 loc) · 1.93 KB
/
test_home_view.py
File metadata and controls
76 lines (50 loc) · 1.93 KB
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
"""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 User, db, 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_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
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 HomeViewTestCase(TestCase):
def setUp(self):
User.query.delete()
u1 = User.signup("u1", "u1@email.com", "password", None)
db.session.add_all([u1])
db.session.commit()
self.u1_id = u1.id
def tearDown(self):
db.session.rollback()
def test_home_logged_in(self):
with app.test_client() as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.u1_id
resp = c.get(
"/",
follow_redirects=True,)
self.assertEqual(resp.status_code, 200)
self.assertIn("@u1", str(resp.data))
self.assertIn("Log out", str(resp.data))
def test_home_logged_out(self):
with app.test_client() as c:
resp = c.get(
"/",
follow_redirects=True,)
self.assertEqual(resp.status_code, 200)
self.assertIn("Sign up", str(resp.data))
self.assertIn("Log in", str(resp.data))
self.assertIn("Happening?", str(resp.data))