forked from cewing/training.sample-flask-app
-
Notifications
You must be signed in to change notification settings - Fork 18
/
microblog_tests.py
116 lines (96 loc) · 3.92 KB
/
microblog_tests.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
import os
import tempfile
import unittest
from flask import session
import microblog
class MicroblogTestCase(unittest.TestCase):
def setUp(self):
db_fd = tempfile.mkstemp()
self.db_fd, microblog.app.config['DATABASE'] = db_fd
microblog.app.config['TESTING'] = True
self.client = microblog.app.test_client()
self.app = microblog.app
microblog.init_db()
def tearDown(self):
os.close(self.db_fd)
os.unlink(microblog.app.config['DATABASE'])
def login(self, username, password):
return self.client.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)
def logout(self):
return self.client.get('/logout',
follow_redirects=True)
def test_database_setup(self):
con = microblog.connect_db()
cur = con.execute('PRAGMA table_info(entries);')
rows = cur.fetchall()
self.assertEquals(len(rows), 3)
def test_write_entry(self):
expected = ("My Title", "My Text")
with self.app.test_request_context('/'):
microblog.write_entry(*expected)
con = microblog.connect_db()
cur = con.execute("select * from entries;")
rows = cur.fetchall()
self.assertEquals(len(rows), 1)
for val in expected:
self.assertTrue(val in rows[0])
def test_get_all_entries_empty(self):
with self.app.test_request_context('/'):
entries = microblog.get_all_entries()
self.assertEquals(len(entries), 0)
def test_get_all_entries(self):
expected = ("My Title", "My Text")
with self.app.test_request_context('/'):
microblog.write_entry(*expected)
entries = microblog.get_all_entries()
self.assertEquals(len(entries), 1)
for entry in entries:
self.assertEquals(expected[0], entry['title'])
self.assertEquals(expected[1], entry['text'])
def test_empty_listing(self):
response = self.client.get('/')
assert 'No entries here so far' in response.data
def test_listing(self):
expected = ("My Title", "My Text")
with self.app.test_request_context('/'):
microblog.write_entry(*expected)
response = self.client.get('/')
for value in expected:
assert value in response.data
def test_login_passes(self):
with self.app.test_request_context('/'):
microblog.do_login(microblog.app.config['USERNAME'],
microblog.app.config['PASSWORD'])
self.assertTrue(session.get('logged_in', False))
def test_login_fails(self):
with self.app.test_request_context('/'):
self.assertRaises(ValueError,
microblog.do_login,
microblog.app.config['USERNAME'],
'incorrectpassword')
def test_login_logout(self):
# verify we can log in
response = self.login('admin', 'secret')
assert 'You were logged in' in response.data
# verify we can log back out
response = self.logout()
assert 'You were logged out' in response.data
# verify that incorrect credentials get a proper message
response = self.login('notadmin', 'secret')
assert 'Invalid Login' in response.data
response = self.login('admin', 'notsosecret')
assert 'Invalid Login' in response.data
def test_add_entries(self):
self.login('admin', 'secret')
response = self.client.post('/add', data=dict(
title='Hello',
text='This is a post'
), follow_redirects=True)
assert 'No entries here so far' not in response.data
assert 'Hello' in response.data
assert 'This is a post' in response.data
if __name__ == '__main__':
unittest.main()