-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonkey_tests.py
More file actions
151 lines (135 loc) · 5.59 KB
/
monkey_tests.py
File metadata and controls
151 lines (135 loc) · 5.59 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
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
# -*- coding: utf-8 -*-
"""
monkey Tests
~~~~~~~~~~~~
Tests the monkey application.
:Original falskr micro blogging
:copyright: (c) 2014 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
Edited by Paavo Kivistö to monkey database
"""
import os
import monkey
import unittest
import tempfile
class monkeyTestCase(unittest.TestCase):
def setUp(self):
"""Before each test, set up a blank database"""
self.db_fd, monkey.app.config['DATABASE'] = tempfile.mkstemp()
monkey.app.config['TESTING'] = True
self.app = monkey.app.test_client()
monkey.init_db()
def tearDown(self):
"""Get rid of the database again after each test."""
os.close(self.db_fd)
os.unlink(monkey.app.config['DATABASE'])
def login(self, username, password):
return self.app.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)
def logout(self):
return self.app.get('/logout', follow_redirects=True)
# testing functions
def test_empty_db(self):
"""Start with a blank database."""
rv = self.app.get('/')
assert b'No monkies here so far' in rv.data
def test_login_logout(self):
"""Make sure login and logout works"""
rv = self.login(monkey.app.config['USERNAME'],
monkey.app.config['PASSWORD'])
assert b'You were logged in' in rv.data
rv = self.logout()
assert b'You were logged out' in rv.data
rv = self.login(monkey.app.config['USERNAME'] + 'x',
monkey.app.config['PASSWORD'])
assert b'Invalid username' in rv.data
rv = self.login(monkey.app.config['USERNAME'],
monkey.app.config['PASSWORD'] + 'x')
assert b'Invalid password' in rv.data
def test_messages(self):
"""Test that messages work"""
self.login(monkey.app.config['USERNAME'],
monkey.app.config['PASSWORD'])
rv = self.app.post('/add', data=dict(
name='Paavo',
age=28,
mail='paavo.kivisto@gmail.com',
text='<strong>Kova</strong> koodari!'
), follow_redirects=True)
assert b'No monkies here so far' not in rv.data
assert b'Paavo' in rv.data
assert b'paavo.kivisto@gmail.com' in rv.data
assert b'<strong>Kova</strong> koodari!' in rv.data
def test_edit(self):
"""Test that profile editing works"""
self.login(monkey.app.config['USERNAME'],
monkey.app.config['PASSWORD'])
rv = self.app.post('/add', data=dict(
name='Paavo',
age=28,
mail='paavo.kivisto@gmail.com',
text='<strong>Kova</strong> koodari!'
), follow_redirects=True)
assert b'No monkies here so far' not in rv.data
assert b'Paavo' in rv.data
assert b'paavo.kivisto@gmail.com' in rv.data
assert b'<strong>Kova</strong> koodari!' in rv.data
rv = self.app.post('/edit', data=dict(
name='Paavo',
age=28,
mail='paavo.kivisto@aalto.fi',
text='<strong>Tosi kova</strong> koodari!',
oldmail='paavo.kivisto@gmail.com'
), follow_redirects=True)
assert b'No monkies here so far' not in rv.data
assert b'Paavo' in rv.data
assert b'paavo.kivisto@gmail.com' not in rv.data
assert b'paavo.kivisto@aalto.fi' in rv.data
assert b'<strong>Tosi kova</strong> koodari!' in rv.data
def test_friends(self):
"""Test that friending works"""
self.login(monkey.app.config['USERNAME'],
monkey.app.config['PASSWORD'])
self.app.post('/add', data=dict(
name='Paavo',
age=28,
mail='paavo.kivisto@gmail.com',
text='<strong>Kova</strong> koodari!'
))
self.app.post('/add', data=dict(
name='Kamu',
age=27,
mail='kamu.kiva@monkey.fi',
text='Apina!'
))
# Add friendship
rv = self.app.get('/friends/kamu.kiva%40monkey.fi&paavo.kivisto%40gmail.com', follow_redirects=True)
assert b'has no friends.' not in rv.data
assert b'<strong>Friends: </strong>1' in rv.data
rv = self.app.get('/')
assert b'<strong>Friends: </strong>0' not in rv.data
assert b'<strong>Friends: </strong>1' in rv.data
# Paavo thinks that Kamu is his best friend
rv = self.app.get('/best/kamu.kiva%40monkey.fi&paavo.kivisto%40gmail.com', follow_redirects=True)
assert b'<strong>Best Friend: </strong>None' not in rv.data
assert b'<strong>Best Friend: </strong>kamu.kiva@monkey.fi' in rv.data
# Remove friendship. That should remove the best friend, too.
rv = self.app.get('/delfriends/kamu.kiva%40monkey.fi&paavo.kivisto%40gmail.com', follow_redirects=True)
assert b'<strong>Best Friend: </strong>None' in rv.data
assert b'has no friends.' in rv.data
assert b'<strong>Friends: </strong>0' in rv.data
rv = self.app.get('/')
assert b'<strong>Friends: </strong>0' in rv.data
assert b'<strong>Friends: </strong>1' not in rv.data
# Add friendship and best friend status back
self.app.get('/friends/kamu.kiva%40monkey.fi&paavo.kivisto%40gmail.com')
self.app.get('/best/kamu.kiva%40monkey.fi&paavo.kivisto%40gmail.com')
# Remove monkey to see that you don't have deleted friends
rv = self.app.post('/delete', data=dict(mail='kamu.kiva@monkey.fi'), follow_redirects=True)
assert b'<strong>Best friend: </strong>None' in rv.data
assert b'<strong>Friends: </strong>0' in rv.data
assert b'<strong>Friends: </strong>1' not in rv.data
if __name__ == '__main__':
unittest.main()