-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
65 lines (54 loc) · 2.27 KB
/
tests.py
File metadata and controls
65 lines (54 loc) · 2.27 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
import os
import server
import unittest
import tempfile
class ServerTestCase(unittest.TestCase):
"""A more complex test suite - https://github.com/pallets/flask/tree/master/examples/minitwit/"""
def setUp(self):
"""Create a new test client, init a new DB"""
self.db_fd, server.app.config['DATABASE'] = tempfile.mkstemp()
"""Disable error catching during request handling, better error reports
when performing test requests against the application"""
server.app.config['TESTING'] = True
self.app = server.app.test_client()
with server.app.app_context():
server.init_db()
def tearDown(self):
"""Deletes the DB after the test completes"""
os.close(self.db_fd)
os.unlink(server.app.config['DATABASE'])
def test_empty_db(self):
"""Assert that an empty database is empty"""
rv = self.app.get('/')
assert b'No entries here so far' in rv.data
def login(self, username, password):
"""Need to follow redirects (behaviour of login page)"""
return self.app.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)
def logout(self):
"""Need to follow redirects (behaviour of logout page)"""
return self.app.get('/logout', follow_redirects=True)
def test_login_logout(self):
"""Valid credentials should work, invalid credentials should not"""
rv = self.login('admin', 'default')
assert 'You were logged in' in rv.data
rv = self.logout()
assert 'You were logged out' in rv.data
rv = self.login('adminx', 'default')
assert 'Invalid username' in rv.data
rv = self.login('admin', 'defaultx')
assert 'Invalid password' in rv.data
def test_messages(self):
"""Check that posting a message works"""
self.login('admin', 'default')
rv = self.app.post('/add', data=dict(
title='<Hello>',
text='<strong>HTML</strong> allowed here'
), follow_redirects=True)
assert 'No entries here so far' not in rv.data
assert '<Hello>' in rv.data
assert '<strong>HTML</strong> allowed here' in rv.data
if __name__ == '__main__':
unittest.main()