-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_message_views.py
201 lines (151 loc) · 6.25 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
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
"""Message View tests."""
# run these tests like:
#
# FLASK_ENV=production python -m unittest test_message_views.py
import os
from unittest import TestCase
from models import db, connect_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
# 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.create_all()
# Don't have WTForms use CSRF at all, since it's a pain to test
app.config['WTF_CSRF_ENABLED'] = False
class MessageViewTestCase(TestCase):
"""Test views for messages."""
def setUp(self):
"""Create test client, add sample data."""
User.query.delete()
Message.query.delete()
self.client = app.test_client()
testuser = User(
username="testuser",
email="test@test.com",
password="testuser",
image_url=None
)
db.session.add(testuser)
db.session.commit()
self.test_id = testuser.id
test_message_like_user = User(
username="test2",
email="test2@test.com",
password="testuser",
image_url=None
)
db.session.add(test_message_like_user)
testmsg = Message(text="test_liked_message")
test_message_like_user.messages.append(testmsg)
db.session.commit()
testuser.messages_liked.append(testmsg)
db.session.commit()
self.test_user_likes_id = test_message_like_user.id
def test_message_add(self):
"""Can user add a message?"""
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.test_id
resp = c.post(
"/messages/new",
data={"text": "Hello"},
)
# Make sure it redirects
self.assertEqual(resp.status_code, 302)
msg = Message.query.filter_by(text="Hello").first()
self.assertEqual(msg.text, "Hello")
resp = c.post(
"/messages/new",
data={"text": "Hello"},
follow_redirects=True,
)
html = resp.get_data(as_text=True)
# Make sure redirect follows through
self.assertEqual(resp.status_code, 200)
self.assertIn('id="users-show-page"', html)
self.assertIn("Hello", html)
def test_message_show(self):
""" Does message show? """
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.test_id
testmsg = Message(text="test_message")
testuser = User.query.get(self.test_id)
testuser.messages.append(testmsg)
db.session.commit()
resp = c.get(f"/messages/{testmsg.id}")
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('test_message', html)
self.assertNotIn('Hello', html)
self.assertIn('id="message-show-page"', html)
msg = Message.query.get(testmsg.id)
self.assertEqual(msg.text, "test_message")
def test_message_destroy(self):
""" Does message destroy? """
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.test_id
testmsg = Message(text="test_message")
testuser = User.query.get(self.test_id)
testuser.messages.append(testmsg)
db.session.commit()
msg = Message.query.all()
self.assertEqual(len(msg), 2)
resp = c.post(f"/messages/{testmsg.id}/delete")
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 302)
msg = Message.query.all()
self.assertEqual(len(msg), 1)
testmsg2 = Message(text="test_message")
testuser = User.query.get(self.test_id)
testuser.messages.append(testmsg2)
db.session.commit()
resp = c.post(
f"/messages/{testmsg2.id}/delete",
follow_redirects=True,
)
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('id="users-show-page"', html)
self.assertNotIn("test_message", html)
def test_messages_liked(self):
""" Does messages liked show """
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.test_id
resp = c.get('/messages/liked')
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('id="liked-messages-page"', html)
self.assertIn('test_liked_message', html)
self.assertNotIn('test_message', html)
def test_message_like(self):
""" Does message like work? """
with self.client as c:
with c.session_transaction() as sess:
sess[CURR_USER_KEY] = self.test_id
testmsg = Message(text="new_message_liked")
test_user2 = User.query.get(self.test_user_likes_id)
test_user2.messages.append(testmsg)
db.session.commit()
resp = c.post(f"/messages/{testmsg.id}/like")
self.assertEqual(resp.status_code, 302)
curr_user = User.query.get(self.test_id)
self.assertIn(testmsg, curr_user.messages_liked)
resp = c.post(
f"/messages/{testmsg.id}/like",
follow_redirects=True
)
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('id="liked-messages-page"', html)
self.assertIn('new_message_liked', html)
self.assertIn('test_liked_message', html)
self.assertNotIn('test_message', html)