-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_post.py
54 lines (34 loc) · 1.4 KB
/
test_post.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
# """Post View tests."""
# import os
# from unittest import TestCase
# from models import db, connect_db, Post, User
# os.environ['DATABASE_URL'] = "postgresql:///spotify"
# from app import app, CURR_USER_KEY
# db.create_all()
app.config['WTF_CSRF_ENABLED'] = False
class PostViewTestCase(TestCase):
"""Test views for posts."""
def setUp(self):
"""Create test client, add sample data."""
User.query.delete()
Post.query.delete()
self.client = app.test_client()
self.testuser = User.signup(username="testuser",
email="[email protected]",
password="testuser",
image_url=None)
db.session.commit()
def test_add_post(self):
"""Can user add a post?"""
# 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.testuser.id
# Now, that session setting is saved, so we can have
# the rest of ours test
resp = c.post("/posts/new", data={"title":"Hello"})
# Make sure it redirects
self.assertEqual(resp.status_code, 302)
post = Post.query.one()
self.assertEqual(post.title, "Hello")