forked from NYUCCL/psiTurk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_psiturk.py
438 lines (350 loc) · 15.2 KB
/
test_psiturk.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# -*- coding: utf-8 -*-
""" This module tests the psiTurk suite. """
import os
import unittest
import tempfile
import psiturk
import json
from faker import Faker
fake = Faker() # Fake data generator
class FlaskTestClientProxy(object):
'''Spoof user agent (Chrome)'''
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['REMOTE_ADDR'] = environ.get('REMOTE_ADDR', fake.ipv4())
environ['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X\
10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0\
Safari/537.36'
return self.app(environ, start_response)
class BadFlaskTestClientProxy(object):
'''Spoof user agent (iPad)'''
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['REMOTE_ADDR'] = environ.get('REMOTE_ADDR', fake.ipv4())
environ['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like \
Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) \
Version/4.0.4 Mobile/7B334b Safari/531.21.10'
return self.app(environ, start_response)
class PsiturkUnitTest(unittest.TestCase):
def setUp(self, case=None):
'''Build up fixtures'''
os.chdir('psiturk-example')
import psiturk.db
import psiturk.experiment
reload(psiturk.experiment)
fd, db_path = tempfile.mkstemp()
self.db_fd = fd
psiturk.experiment.app.config['DATABASE'] = db_path
psiturk.experiment.app.wsgi_app = FlaskTestClientProxy(
psiturk.experiment.app.wsgi_app)
self.app = psiturk.experiment.app.test_client()
self.config = psiturk.experiment.CONFIG
psiturk.db.init_db()
# Fake MTurk data
self.worker_id = fake.md5(raw_output=False)
self.hit_id = fake.md5(raw_output=False)
self.assignment_id = fake.md5(raw_output=False)
def tearDown(self):
'''Tear down fixtures'''
os.close(self.db_fd)
os.chdir('..')
os.unlink(psiturk.experiment.app.config['DATABASE'])
self.app = None
def set_config(self, section, field, value):
self.config.parent.set(self.config, section, field, str(value))
class PsiTurkStandardTests(PsiturkUnitTest):
# Test experiment.py
# ==================
def test_default_page(self):
'''Test that root page works.'''
rv = self.app.get('/')
assert 'Welcome to psiTurk!' in rv.data
def test_exp_debug_no_url_vars(self):
'''Test that exp page throws Error #1003 with no url vars.'''
rv = self.app.get('/exp')
assert '<b>Error</b>: 1003' in rv.data
def test_ad_no_url_vars(self):
'''Test that ad page throws Error #1001 with no url vars.'''
rv = self.app.get('/ad')
assert '<b>Error</b>: 1001' in rv.data
def test_ad_with_all_urls(self):
'''Test that ad page throws Error #1003 with no url vars.'''
args = '&'.join([
'assignmentId=debug%s' % self.assignment_id,
'workerId=debug%s' % self.worker_id,
'hitId=debug%s' % self.hit_id,
'mode=sandbox'])
rv = self.app.get('/ad?%s' % args)
assert 'Thank you for accepting this HIT!' in rv.data
def test_exp_with_all_url_vars_not_registered_on_ad_server(self):
'''Test that exp page throws Error #1018 with all url vars but not registered.'''
args = '&'.join([
'assignmentId=debug%s' % self.assignment_id,
'workerId=debug%s' % self.worker_id,
'hitId=debug%s' % self.hit_id,
'mode=sandbox'])
rv = self.app.get('/exp?%s' % args)
assert '<b>Error</b>: 1018' in rv.data
def test_sync_put(self):
request = "&".join([
"assignmentId=debug%s" % self.assignment_id,
"workerId=debug%s" % self.worker_id,
"hitId=debug%s" % self.hit_id,
"mode=debug"])
# put the user in the database
rv = self.app.get("/exp?%s" % request)
# try putting the sync
uniqueid = "debug%s:debug%s" % (self.worker_id, self.assignment_id)
rv = self.app.put('/sync/%s' % uniqueid)
status = json.loads(rv.data).get("status", "")
assert status == "user data saved"
def test_sync_get(self):
request = "&".join([
"assignmentId=debug%s" % self.assignment_id,
"workerId=debug%s" % self.worker_id,
"hitId=debug%s" % self.hit_id,
"mode=debug"])
# put the user in the database
rv = self.app.get("/exp?%s" % request)
# save data with sync PUT
uniqueid = "debug%s:debug%s" % (self.worker_id, self.assignment_id)
rv = self.app.put('/sync/%s' % uniqueid)
# get data with sync GET
uniqueid = "debug%s:debug%s" % (self.worker_id, self.assignment_id)
rv = self.app.get('/sync/%s' % uniqueid)
response = json.loads(rv.data)
assert response.get("assignmentId", "") == "debug%s" % self.assignment_id
assert response.get("workerId", "") == "debug%s" % self.worker_id
assert response.get("hitId", "") == "debug%s" % self.hit_id
assert response.get("condition", None) == 0
assert response.get("counterbalance", None) == 0
assert response.get("bonus", None) == 0.0
def test_favicon(self):
'''Test that favicon loads.'''
rv = self.app.get('/favicon.ico')
assert rv.status_code == 200
def test_complete_experiment(self):
'''Test that a participant can start and finish the experiment.'''
request = "&".join([
"assignmentId=debug%s" % self.assignment_id,
"workerId=debug%s" % self.worker_id,
"hitId=debug%s" % self.hit_id,
"mode=debug"])
# put the user in the database
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
# save data with sync PUT
uniqueid = "debug%s:debug%s" % (self.worker_id, self.assignment_id)
rv = self.app.put('/sync/%s' % uniqueid)
assert rv.status_code == 200
# complete experiment
mode = 'debug'
rv = self.app.get('/complete?uniqueId=%s&mode=%s' % (uniqueid, mode))
assert rv.status_code == 200
def test_repeat_experiment_fail(self):
'''Test that a participant cannot repeat the experiment.'''
request = "&".join([
"assignmentId=%s" % self.assignment_id,
"workerId=%s" % self.worker_id,
"hitId=%s" % self.hit_id,
"mode=debug"])
# put the user in the database
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
# save data with sync PUT
uniqueid = "%s:%s" % (self.worker_id, self.assignment_id)
rv = self.app.put('/sync/%s' % uniqueid)
assert rv.status_code == 200
# complete experiment
mode = 'debug'
rv = self.app.get('/complete?uniqueId=%s&mode=%s' % (uniqueid, mode))
assert rv.status_code == 200
# choose new assignment and hit ids
self.assignment_id = fake.md5(raw_output=False)
self.hit_id = fake.md5(raw_output=False)
request = "&".join([
"assignmentId=%s" % self.assignment_id,
"workerId=%s" % self.worker_id,
"hitId=%s" % self.hit_id,
"mode=debug"])
# make sure they are blocked on the ad page
rv = self.app.get('/ad?%s' % request)
assert ': 1010' in rv.data
# make sure they are blocked on the experiment page
rv = self.app.get("/exp?%s" % request)
assert ': 1010' in rv.data
def test_repeat_experiment_success(self):
'''Test that a participant can repeat the experiment.'''
self.set_config('HIT Configuration', 'allow_repeats', 'true')
request = "&".join([
"assignmentId=%s" % self.assignment_id,
"workerId=%s" % self.worker_id,
"hitId=%s" % self.hit_id,
"mode=debug"])
# put the user in the database
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
# save data with sync PUT
uniqueid = "%s:%s" % (self.worker_id, self.assignment_id)
rv = self.app.put('/sync/%s' % uniqueid)
assert rv.status_code == 200
# complete experiment
mode = 'debug'
rv = self.app.get('/complete?uniqueId=%s&mode=%s' % (uniqueid, mode))
assert rv.status_code == 200
# choose new assignment and hit ids
self.assignment_id = fake.md5(raw_output=False)
self.hit_id = fake.md5(raw_output=False)
request = "&".join([
"assignmentId=%s" % self.assignment_id,
"workerId=%s" % self.worker_id,
"hitId=%s" % self.hit_id,
"mode=debug"])
# make sure they are not blocked on the ad page
rv = self.app.get('/ad?%s' % request)
assert rv.status_code == 200
assert ': 1010' not in rv.data
# make sure they are not blocked on the experiment page
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
assert ': 1010' not in rv.data
# save data with sync PUT
uniqueid = "%s:%s" % (self.worker_id, self.assignment_id)
rv = self.app.put('/sync/%s' % uniqueid)
assert rv.status_code == 200
# complete experiment
mode = 'debug'
rv = self.app.get('/complete?uniqueId=%s&mode=%s' % (uniqueid, mode))
assert rv.status_code == 200
def test_repeat_experiment_quit(self):
'''Test that a participant cannot restart the experiment.'''
request = "&".join([
"assignmentId=%s" % self.assignment_id,
"workerId=%s" % self.worker_id,
"hitId=%s" % self.hit_id,
"mode=debug"])
# put the user in the database
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
# put the in the experiment
uniqueid = "%s:%s" % (self.worker_id, self.assignment_id)
rv = self.app.post("/inexp", data=dict(uniqueId=uniqueid))
assert rv.status_code == 200
# make sure they are blocked on the ad page
rv = self.app.get('/ad?%s' % request)
assert rv.status_code == 200
assert ': 1009' in rv.data
# make sure they are blocked on the experiment page
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
assert ': 1008' in rv.data
# have them quit the experiment
rv = self.app.post("/quitter", data=dict(uniqueId=uniqueid))
assert rv.status_code == 200
# make sure they are blocked on the ad page
rv = self.app.get('/ad?%s' % request)
assert rv.status_code == 200
assert ': 1009' in rv.data
# make sure they are blocked on the experiment page
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
assert ': 1008' in rv.data
def test_repeat_experiment_quit_allow_repeats(self):
'''Test that a participant cannot restart the experiment, even when repeats are allowed.'''
self.set_config('HIT Configuration', 'allow_repeats', 'true')
request = "&".join([
"assignmentId=%s" % self.assignment_id,
"workerId=%s" % self.worker_id,
"hitId=%s" % self.hit_id,
"mode=debug"])
# put the user in the database
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
# put the in the experiment
uniqueid = "%s:%s" % (self.worker_id, self.assignment_id)
rv = self.app.post("/inexp", data=dict(uniqueId=uniqueid))
assert rv.status_code == 200
# make sure they are blocked on the ad page
rv = self.app.get('/ad?%s' % request)
assert rv.status_code == 200
assert ': 1009' in rv.data
# make sure they are blocked on the experiment page
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
assert ': 1008' in rv.data
# have them quit the experiment
rv = self.app.post("/quitter", data=dict(uniqueId=uniqueid))
assert rv.status_code == 200
# make sure they are blocked on the ad page
rv = self.app.get('/ad?%s' % request)
assert rv.status_code == 200
assert ': 1009' in rv.data
# make sure they are blocked on the experiment page
rv = self.app.get("/exp?%s" % request)
assert rv.status_code == 200
assert ': 1008' in rv.data
class BadUserAgent(PsiturkUnitTest):
'''Setup test blocked user agent (iPad/tablets)'''
def setUp(self):
'''Build up fixtures'''
os.chdir('psiturk-example')
import psiturk.db
import psiturk.experiment
reload(psiturk.experiment)
fd, db_path = tempfile.mkstemp()
self.db_fd = fd
psiturk.experiment.app.config['DATABASE'] = db_path
psiturk.experiment.app.wsgi_app = BadFlaskTestClientProxy(
psiturk.experiment.app.wsgi_app)
self.app = psiturk.experiment.app.test_client()
psiturk.db.init_db()
# Fake MTurk data
self.worker_id = fake.md5(raw_output=False)
self.hit_id = fake.md5(raw_output=False)
self.assignment_id = fake.md5(raw_output=False)
def test_ad_with_bad_user_agent(self):
'''Test that ad page throws Error when user agent is blocked.'''
rv = self.app.get(
'/ad' + '?assignmentId=debug' + self.assignment_id + '&workerId=debug' +
self.worker_id + '&hitId=debug' + self.hit_id + '&mode=sandbox'
)
assert '<b>Error</b>: 1014' in rv.data
class PsiTurkTestPsiturkJS(PsiturkUnitTest):
''' Setup test for missing psiturk.js file. '''
def setUp(self):
'''Build up fixtures'''
self.PSITURK_JS_FILE = '../psiturk/psiturk_js/psiturk.js'
os.chdir('psiturk-example')
os.rename(self.PSITURK_JS_FILE, self.PSITURK_JS_FILE + '.bup')
import psiturk.db
import psiturk.experiment
reload(psiturk.experiment)
fd, db_path = tempfile.mkstemp()
self.db_fd = fd
psiturk.experiment.app.config['DATABASE'] = db_path
psiturk.experiment.app.wsgi_app = FlaskTestClientProxy(
psiturk.experiment.app.wsgi_app)
self.app = psiturk.experiment.app.test_client()
psiturk.db.init_db()
def test_psiturk_js_is_missing(self):
''' Test for missing psiturk.js '''
rv = self.app.get('static/js/psiturk.js')
assert 'file not found' in rv.data
def tearDown(self):
'''Tear down fixtures'''
super(PsiTurkTestPsiturkJS, self).tearDown()
os.chdir('psiturk-example')
os.rename(self.PSITURK_JS_FILE + '.bup', self.PSITURK_JS_FILE)
os.chdir('..')
class ExperimentErrorsTest(PsiturkUnitTest):
def test_experiment_errors(self):
"""Make sure every error has a description"""
error_cls = psiturk.experiment_errors.ExperimentError
for error_name in error_cls.experiment_errors:
assert error_name in error_cls.error_descriptions
for error_name in error_cls.error_descriptions:
assert error_name in error_cls.experiment_errors
if __name__ == '__main__':
unittest.main()