This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathideawheel_tests.py
247 lines (208 loc) · 8.11 KB
/
ideawheel_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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import flask
import ideawheel
import os
import tempfile
import unittest
class IdeawheelTests(unittest.TestCase):
def setUp(self):
self.db_fd, ideawheel.app.config['DATABASE'] = tempfile.mkstemp()
ideawheel.app.config['TESTING'] = True
self.app = ideawheel.app.test_client()
ideawheel.init_db()
ideawheel.app.secret_key = os.urandom(12)
def tearDown(self):
os.close(self.db_fd)
os.unlink(ideawheel.app.config['DATABASE'])
def create_user(self, username='TestUser', password='TestPassword',
email='[email protected]'):
return self.app.post('/register',
data=dict(
username=username,
password=password,
password2=password,
email=email,
hp=''
), follow_redirects=True)
def make_admin(self, username):
with ideawheel.app.test_request_context('/'):
ideawheel.app.preprocess_request()
flask.g.db.execute('update auth_users set user_type=2 where '
'username=?', [username])
flask.g.db.commit()
def make_staff(self, username):
with ideawheel.app.test_request_context('/'):
ideawheel.app.preprocess_request()
flask.g.db.execute('update auth_users set user_type=1 where '
'username=?', [username])
flask.g.db.commit()
def login(self, username='TestUser', password='TestPassword'):
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)
def create_stub(self, stub_text, link=None):
with ideawheel.app.test_request_context('/'):
ideawheel.app.preprocess_request()
flask.g.db.execute('insert into idea_stubs (stub, link) values '
'(?, ?)', [stub_text, link])
flask.g.db.commit()
class UserManagementTestCase(IdeawheelTests):
def test_register(self):
# Mismatched passwords
result = self.app.post('/register', data=dict(
username='Test',
password='TestPassword',
password2='No Match',
email='[email protected]',
hp=''
), follow_redirects=True)
self.assertIn(b'All fields are required', result.data)
# Missing field
result = self.app.post('/register', data=dict(
username='Test',
password='TestPassword',
password2='TestPassword',
email='',
hp=''
), follow_redirects=True)
self.assertIn(b'All fields are required', result.data)
# Honeypot with data
result = self.app.post('/register', data=dict(
username='Test',
password='TestPassword',
password2='TestPassword',
email='[email protected]',
hp='Actually a spammer'
), follow_redirects=True)
self.assertIn(b'All fields are required', result.data)
# Success
result = self.create_user()
self.assertIn(b'Logged in as <a href="/user/TestUser">TestUser</a>',
result.data)
# Duplicate user
result = self.create_user()
self.assertIn(b'That username or email is already in use', result.data)
def test_login(self):
self.create_user()
# Success
result = self.login()
self.assertIn(b'Logout', result.data)
# Bad user/pass
result = self.login('Bad', 'User')
self.assertIn(b'Incorrect username or password', result.data)
def test_logout(self):
self.create_user()
self.login()
# Success
result = self.logout()
self.assertIn(b'Login', result.data)
def test_edit_and_view_profile(self):
self.create_user()
# Default data
self.login()
result = self.app.get('/user/TestUser')
expected = [
b'~TestUser',
b'thinker of grand ideas',
b'user',
b'No information provided'
]
for e in expected:
self.assertIn(e, result.data)
# New data
result = self.app.post('/user/TestUser/edit', data=dict(
display_name='New name',
blurb='New blurb',
artist_type='New artist type',
email='[email protected]',
password='TestPassword',
new_password='',
new_password2=''
), follow_redirects=True)
expected = [
b'New name',
b'New artist type',
b'user',
b'New blurb'
]
for e in expected:
self.assertIn(e, result.data)
self.logout()
# Admin editing
self.create_user(username='Admin', password='Admin',
email='[email protected]')
self.make_admin('Admin')
self.login('Admin', 'Admin')
result = self.app.post('/user/TestUser/edit', data=dict(
display_name='New name',
blurb='Edited by admin',
artist_type='New artist type',
email='[email protected]',
password='TestPassword',
new_password='',
new_password2=''
), follow_redirects=True)
self.assertIn(b'Edited by admin', result.data)
self.logout()
# Permission denied editing someone else's profile
self.login()
result = self.app.post('/user/Admin/edit', data=dict(
display_name='New name',
blurb='Edited by non-admin',
artist_type='New artist type',
email='[email protected]',
password='TestPassword',
new_password='',
new_password2=''
), follow_redirects=True)
self.assertEqual(result.status_code, 403)
result = self.app.get('/user/Admin')
self.assertNotIn(b'Edited by non-admin', result.data)
self.logout()
# Permission denied when logged out
result = self.app.get('/user/TestUser/edit')
self.assertEqual(result.status_code, 403)
class IdeaStubTestCase(IdeawheelTests):
def test_create_stub(self):
# Staff only action - hit /stubs/create to make a new one
# Normal user bounced
self.login()
result = self.app.post('/stub/create', data=dict(
text='Sample stub text',
title='Sample stub'
), follow_redirects=True)
self.assertEqual(result.status_code, 403)
# TODO pull into helper
self.create_user(username='Admin', password='Admin',
email='[email protected]')
self.make_admin('Admin')
self.login('Admin', 'Admin')
result = self.app.post('/stub/create', data=dict(
stub_text='Sample stub text',
), follow_redirects=True)
self.assertIn(b'Stub created', result.data)
self.create_user(username='Staff', password='Staff',
email='[email protected]')
self.make_staff('Staff')
self.login('Staff', 'Staff')
result = self.app.post('/stub/create', data=dict(
stub_text='Sample stub text',
), follow_redirects=True)
self.assertIn(b'Stub created', result.data)
def test_random_stub(self):
# This assumes that the randomness of SQLite works, focusing instead on
# displaying the random stub.
self.login()
self.create_stub('A stub without a link')
result = self.app.get('/idea')
self.assertIn(b'A stub without a link', result.data)
def test_random_stub_with_link(self):
self.login()
self.create_stub('A stub with a link', link='the link itself')
result = self.app.get('/idea')
self.assertIn(b'A stub with a link', result.data)
self.assertIn(b'the link itself', result.data)
if __name__ == '__main__':
unittest.main()