-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_message_views.py
73 lines (47 loc) · 1.97 KB
/
test_message_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
"""Message 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
# 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
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 MessageBaseViewTestCase(TestCase):
def setUp(self):
User.query.delete()
u1 = User.signup("u1", "u1@email.com", "password", None)
db.session.flush()
m1 = Message(text="m1-text", user_id=u1.id)
db.session.add_all([m1])
db.session.commit()
self.u1_id = u1.id
self.m1_id = m1.id
self.client = app.test_client()
class MessageAddViewTestCase(MessageBaseViewTestCase):
def test_add_message(self):
# Since we need to change the session to mimic logging in,
# we need to use the changing-session trick:
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.u1_id
# Now, that session setting is saved, so we can have
# the rest of ours test
resp = c.post("/messages/new", data={"text": "Hello"})
self.assertEqual(resp.status_code, 302)
Message.query.filter_by(text="Hello").one()