forked from IllDepence/JSONkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
513 lines (439 loc) · 22.5 KB
/
test.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import json
import os
import unittest
import uuid
from jsonkeeper import create_app
from jsonkeeper.models import JSON_document, db
class JkTestCase(unittest.TestCase):
""" Test JSONkeeper
If just called as $ python3 test.py, a default config is used where
JSON-LD @id rewriting and Activity Stream serving are active. To test
more thoroughly the environment variables JK_ID_REWRITE and JK_AS_SERVE
can be set to 0 or 1. Example:
$ JK_ID_REWRITE=1 JK_AS_SERVE=0 python3 test.py
would run the test with a config where JSON-LD @ids are rewritten but
a Activity Stream is not being served.
Note: the combination of JK_ID_REWRITE=0 and JK_AS_SERVE=1 makes no
sense (the AS needs to point to dereferencable @ids) and should not
be used.
Implementation note: tried to implement running multiple variations of
JkTestCase by using subclasses instead of environment variables.
Even though JSONkeeper uses the application factory pattern, the AS
collection route would be set to None despite the config value
being set to a 'as/collection.json'.
"""
def setUp(self):
""" Set up sqlite DB in memory and JSON storage in a tmp directory.
Read environment variables if set.
"""
self.id_rewrite = True
self.as_serve = True
if os.environ.get('JK_ID_REWRITE'):
self.id_rewrite = bool(int(os.environ.get('JK_ID_REWRITE')))
if os.environ.get('JK_AS_SERVE'):
self.as_serve = bool(int(os.environ.get('JK_AS_SERVE')))
app = create_app(id_rewrite=self.id_rewrite, as_serve=self.as_serve)
self.app = app
# ↓ Temporary "fix" for https://github.com/pallets/flask/issues/2549
self.app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
self.tc = app.test_client()
def tearDown(self):
""" Remove tmp directory set up for JSON storage.
"""
pass
def test_info_page_JSON(self):
""" Test info page when client accepts application/json.
"""
with self.app.app_context():
resp = self.tc.get('/', headers={'Accept': 'application/json'})
self.assertEqual(resp.status, '200 OK')
self.assertEqual(resp.headers.get('Content-Type'),
'application/json')
json_obj = json.loads(resp.data.decode('utf-8'))
self.assertIn('message', json_obj)
self.assertIn('JSON documents.', json_obj['message'])
self.assertNotIn(b'Activity Stream', resp.data)
def test_info_page_PLAIN(self):
""" Test info page when client does not accept application/json.
"""
with self.app.app_context():
resp = self.tc.get('/')
self.assertEqual(resp.status, '200 OK')
self.assertIn('text/plain', resp.headers.get('Content-Type'))
self.assertIn(b'JSON documents.', resp.data)
self.assertNotIn(b'Activity Stream', resp.data)
self.assertNotIn(b'{', resp.data)
def test_redirects(self):
""" Test redirection to info page.
"""
with self.app.app_context():
resp = self.tc.get('/{}'.format(self.app.cfg.api_path()))
self.assertEqual(resp.status, '302 FOUND')
resp = self.tc.get(('/{}/daa1f3e9-6928-453b-81aa-4'
'5ae7f99bbe9').format(self.app.cfg.api_path()))
self.assertEqual(resp.status, '302 FOUND')
def test_nonexistent_JSON(self):
""" Test 404s for when JSON document with the given ID doesn't exist.
"""
with self.app.app_context():
resp = self.tc.get('/{}/foo'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json'})
self.assertEqual(resp.status, '404 NOT FOUND')
resp = self.tc.put('/{}/foo'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/json'})
self.assertEqual(resp.status, '404 NOT FOUND')
resp = self.tc.delete('/{}/foo'.format(self.app.cfg.api_path()))
self.assertEqual(resp.status, '404 NOT FOUND')
def test_nonexistent_AS(self):
""" Test 404 for when Activity Stream doesn't exist.
"""
with self.app.app_context():
resp = self.tc.get('/{}'.format(self.app.cfg.as_coll_url()))
self.assertEqual(resp.status, '404 NOT FOUND')
def test_unprotected_JSON(self):
""" Test create, retrieve, update, delete lifecycle of a JSON document
when no access token is provided.
"""
with self.app.app_context():
# Create
# # HTTP response
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/json'},
data='{"foo":"bar"}')
self.assertEqual(resp.status, '201 CREATED')
json_obj = json.loads(resp.data.decode('utf-8'))
self.assertIn('foo', json_obj)
self.assertEqual(json_obj['foo'], 'bar')
location = resp.headers.get('Location')
json_id = location.split('/')[-1]
# # DB
json_doc = JSON_document.query.filter_by(
id=json_id).first()
self.assertEqual(json_doc.id, json_id)
self.assertEqual(json_doc.access_token, '')
json_obj = json.loads(json_doc.json_string)
self.assertIn('foo', json_obj)
self.assertEqual(json_obj['foo'], 'bar')
# Access
# # HTTP response
resp = self.tc.get(location,
headers={'Accept': 'application/json'})
self.assertEqual(resp.status, '200 OK')
json_obj = json.loads(resp.data.decode('utf-8'))
self.assertIn('foo', json_obj)
self.assertEqual(json_obj['foo'], 'bar')
# Update
# # HTTP response
resp = self.tc.put(location,
headers={'Accept': 'application/json',
'Content-Type': 'application/json'},
data='["ほげ"]')
self.assertEqual(resp.status, '200 OK')
json_obj = json.loads(resp.data.decode('utf-8'))
self.assertIn('ほげ', json_obj)
# # DB
json_doc = JSON_document.query.filter_by(
id=json_id).first()
json_obj = json.loads(json_doc.json_string)
self.assertIn('ほげ', json_obj)
# Delete
# # HTTP response
resp = self.tc.delete(location)
self.assertEqual(resp.status, '200 OK')
# # DB
json_docs = JSON_document.query.all()
json_ids = [j.id for j in json_docs]
self.assertNotIn(json_id, json_ids)
def test_legacy_id(self):
""" Test if legacy IDs are working.
"""
with self.app.app_context():
legacy_id = ('6833bc52d8c7bf9fc6b744350fd513b4ac4a682f2cdf203de2d6'
'ea1195422e4a')
json_string = '{"old": "doc"}'
json_doc = JSON_document(id=legacy_id,
access_token='',
unlisted=False,
is_json_ld=False,
json_string=json_string)
db.session.add(json_doc)
db.session.commit()
resp = self.tc.get('/{}/{}'.format(self.app.cfg.api_path(),
legacy_id),
headers={'Accept': 'application/json'})
self.assertEqual(resp.status, '200 OK')
self.assertEqual(resp.data.decode('utf-8'), json_string)
def _get_curation_json(self, init_id):
can_id = ('http://iiif.bodleian.ox.ac.uk/iiif/canvas/03818fac-9ba6-438'
'2-b339-e27a0a075f31.json#xywh=986,4209,538,880')
man_id = ('http://iiif.bodleian.ox.ac.uk/iiif/manifest/60834383-7146-4'
'1ab-bfe1-48ee97bc04be.json')
curation_json = '''
{{
"@context":[
"http://iiif.io/api/presentation/2/context.json",
"http://codh.rois.ac.jp/iiif/curation/1/context.json"
],
"@id":"{}",
"@type":"cr:Curation",
"selections":[
{{
"@id":"{}",
"@type":"sc:Range",
"label":"",
"canvases": [
{{
"@id":"{}",
"label":"Marine exploration"
}}
],
"within": [
{{
"@id": "{}",
"@type": "sc:Manifest",
"label": "MS. Bodl. 264"
}}
]
}}
]
}}'''.format(init_id, init_id, can_id, man_id)
return curation_json
def _upload_JSON_LD(self):
init_id = str(uuid.uuid4())
curation_json = self._get_curation_json(init_id)
# # JSON
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Access-Token': 'foo',
},
data=curation_json)
self.assertEqual(resp.status, '201 CREATED')
json_obj = json.loads(resp.data.decode('utf-8'))
self.assertEqual(json_obj['@type'], 'cr:Curation')
self.assertEqual(json_obj['@id'], init_id)
# # JSON-LD
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/ld+json',
'X-Access-Token': 'foo',
},
data=curation_json)
self.assertEqual(resp.status, '201 CREATED')
json_obj = json.loads(resp.data.decode('utf-8'))
self.assertEqual(json_obj['@type'], 'cr:Curation')
self.assertNotEqual(json_obj['@id'], init_id)
# location = resp.headers.get('Location')
# self.assertEqual(json_obj['@id'], location)
# for some reason location doesn't include a port for the unit test
# BUT it works when JSONkeeper is normally run
location = resp.headers.get('Location')
return location
def test_restrictive_accpet_header(self):
""" Test uploading a JSON-LD document with the 'Accept' header set to
only 'application/ld+json'.
"""
with self.app.app_context():
init_id = 'foo'
curation_json = self._get_curation_json(init_id)
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/ld+json',
'Content-Type': 'application/ld+json'
},
data=curation_json)
self.assertEqual(resp.status, '201 CREATED')
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/foo+json',
'Content-Type': 'application/ld+json'
},
data=curation_json)
self.assertNotEqual(resp.status, '201 CREATED')
def test_JSON_LD(self):
""" JSON-LD @id rewriting.
"""
if not self.id_rewrite:
raise unittest.SkipTest('Test not applicable for current config.')
with self.app.app_context():
self._upload_JSON_LD()
def _get_activities_of_last_as_page(self):
""" Access the AS and return the orderedItems of the last (i.e. most
recently added) page.
"""
resp = self.tc.get('/{}'.format(self.app.cfg.as_coll_url()))
coll = json.loads(resp.data.decode('utf-8'))
last_page_url = coll['last']['id']
resp = self.tc.get('{}'.format(last_page_url),
headers={'Accept': 'application/json'})
last_page = json.loads(resp.data.decode('utf-8'))
return last_page['orderedItems']
def test_AS(self):
""" Activity Stream hosting (and JSON-LD @id rewriting).
"""
if not self.as_serve:
raise unittest.SkipTest('Test not applicable for current config.')
with self.app.app_context():
location = self._upload_JSON_LD()
resp = self.tc.get('/{}'.format(self.app.cfg.as_coll_url()))
self.assertEqual(resp.status, '200 OK')
curation_json = self._get_curation_json('foo')
curation_json_changed = curation_json.replace('exploration',
'adventure')
resp = self.tc.put('{}'.format(location),
headers={'Accept': 'application/json',
'Content-Type': 'application/ld+json',
'X-Access-Token': 'foo'
},
data=curation_json_changed)
most_recent_actions = self._get_activities_of_last_as_page()
self.assertEqual(most_recent_actions[0]['type'], 'Update')
resp = self.tc.delete(location,
headers={'X-Access-Token': 'foo'})
most_recent_actions = self._get_activities_of_last_as_page()
self.assertEqual(most_recent_actions[0]['type'], 'Delete')
def test_protected_JSON(self):
with self.app.app_context():
""" Test update and delete restrictions of a JSON document when
access token is provided.
"""
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Access-Token': 'secret'},
data='{"foo":"bar"}')
location = resp.headers.get('Location')
resp = self.tc.put(location,
headers={'Accept': 'application/json',
'Content-Type': 'application/json'},
data='["ほげ"]')
self.assertEqual(resp.status, '403 FORBIDDEN')
resp = self.tc.put(location,
headers={'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Access-Token': 'secret'},
data='["ほげ"]')
self.assertEqual(resp.status, '200 OK')
resp = self.tc.delete(location)
self.assertEqual(resp.status, '403 FORBIDDEN')
resp = self.tc.delete(location,
headers={'X-Access-Token': 'secret'})
self.assertEqual(resp.status, '200 OK')
def test_userdocs(self):
""" Test the /<api_path>/userdocs endpoint.
"""
with self.app.app_context():
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/json'},
data='{"foo":"bar"}')
# ↑ no access restriction, so shouldn't end up in /userdocs
resp = self.tc.get('/{}/userdocs'.format(
self.app.cfg.api_path()))
self.assertEqual(resp.status, '200 OK')
self.assertEqual(len(json.loads(resp.data.decode('utf-8'))), 0)
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Access-Token': 'secret'},
data='{"foo":"bar"}')
resp = self.tc.get('/{}/userdocs'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'X-Access-Token': 'secret'})
self.assertEqual(len(json.loads(resp.data.decode('utf-8'))), 1)
resp = self.tc.get('/{}/userdocs'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'X-Access-Token': 'foo'})
self.assertEqual(len(json.loads(resp.data.decode('utf-8'))), 0)
resp = self.tc.get('/{}/userdocs'.format(self.app.cfg.api_path()))
self.assertEqual(len(json.loads(resp.data.decode('utf-8'))), 0)
def test_Curation_Range_access(self):
""" Test special behavior for cr:Curations where contained sc:Range
objects can be retrieved separately.
"""
if not self.id_rewrite:
raise unittest.SkipTest('Test not applicable for current config.')
with self.app.app_context():
location = self._upload_JSON_LD()
resp = self.tc.get('{}/range1'.format(location),
headers={'Accept': 'application/json'})
self.assertEqual(resp.status, '200 OK')
json_obj = json.loads(resp.data.decode('utf-8'))
self.assertEqual(json_obj['@type'], 'sc:Range')
def test_status_endpoint(self):
""" Test status endpoint.
"""
with self.app.app_context():
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Access-Token': 'foo',
'X-Unlisted': 'true'},
data='{"baz":"bam"}')
location = resp.headers.get('Location')
resp = self.tc.get('{}/status'.format(location),
headers={'Accept': 'application/json',
'Content-Type': 'application/json'})
self.assertEqual(resp.status, '403 FORBIDDEN')
resp = self.tc.get('{}/status'.format(location),
headers={'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Access-Token': 'foo'})
json_obj = json.loads(resp.data.decode('utf-8'))
self.assertEqual(json_obj['access_token'], 'foo')
self.assertEqual(json_obj['unlisted'], True)
def test_anon_AS(self):
""" Test posting JSON-LD anonymously not ending up in AS.
"""
if not self.as_serve:
raise unittest.SkipTest('Test not applicable for current config.')
with self.app.app_context():
init_id = 'foo'
curation_json = self._get_curation_json(init_id)
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/ld+json',
'X-Unlisted': 'false'},
data=curation_json)
resp = self.tc.get('/{}'.format(self.app.cfg.as_coll_url()))
self.assertEqual(resp.status, '404 NOT FOUND')
def test_unlisted_AS(self):
""" Test X-Unlisted header.
"""
if not self.as_serve:
raise unittest.SkipTest('Test not applicable for current config.')
with self.app.app_context():
init_id = 'foo'
curation_json = self._get_curation_json(init_id)
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/ld+json',
'X-Unlisted': 'true'},
data=curation_json)
resp = self.tc.get('/{}'.format(self.app.cfg.as_coll_url()))
self.assertEqual(resp.status, '404 NOT FOUND')
curation_json = self._get_curation_json(init_id)
resp = self.tc.post('/{}'.format(self.app.cfg.api_path()),
headers={'Accept': 'application/json',
'Content-Type': 'application/ld+json',
'X-Access-Token': 'foo',
'X-Unlisted': 'false'},
data=curation_json)
location = resp.headers.get('Location')
resp = self.tc.get('/{}'.format(self.app.cfg.as_coll_url()))
self.assertEqual(resp.status, '200 OK')
json_obj = json.loads(resp.data.decode('utf-8'))
# a Create Action should have been added
self.assertEqual(json_obj.get('totalItems'), 1)
resp = self.tc.patch('{}/status'.format(location),
headers={'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Access-Token': 'foo'
},
data='{"unlisted": true}')
resp = self.tc.get('/{}'.format(self.app.cfg.as_coll_url()))
json_obj = json.loads(resp.data.decode('utf-8'))
# a Delete Action should have been added
self.assertEqual(json_obj.get('totalItems'), 2)
if __name__ == '__main__':
unittest.main()