-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_3_0_ldp.py
2293 lines (1834 loc) · 79.5 KB
/
test_3_0_ldp.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import hashlib
import pdb
import pytest
from base64 import b64encode
from datetime import timedelta
from hashlib import sha1, sha256, blake2b
from uuid import uuid4
from werkzeug.http import http_date
import arrow
import requests
from flask import g
from rdflib import Graph
from rdflib.compare import isomorphic
from rdflib.namespace import RDF
from rdflib.term import Literal, URIRef
from lakesuperior import env
from lakesuperior.dictionaries.namespaces import ns_collection as nsc
from lakesuperior.model.ldp.ldpr import Ldpr
from lakesuperior.util import toolbox
digest_algo = env.app_globals.config['application']['uuid']['algo']
@pytest.fixture(scope='module')
def random_uuid():
return str(uuid4())
@pytest.mark.usefixtures('client_class')
@pytest.mark.usefixtures('db')
class TestLdp:
"""
Test HTTP interaction with LDP endpoint.
"""
def test_get_root_node(self):
"""
Get the root node from two different endpoints.
The test triplestore must be initialized, hence the `db` fixture.
"""
ldp_resp = self.client.get('/ldp')
rest_resp = self.client.get('/rest')
assert ldp_resp.status_code == 200
assert rest_resp.status_code == 200
def test_put_empty_ldpr(self):
"""
Check response headers for a PUT operation with empty payload.
"""
url = f'/ldp/empty-{uuid4()}'
rsp = self.client.put(url)
assert rsp.status_code == 201
assert rsp.data == bytes(url.replace('/ldp', g.webroot), 'utf-8')
def test_put_empty_ldpnr(self):
"""
Check response headers for a PUT operation with empty payload.
Without specifying a MIME type, an LDP-NR should be created.
"""
url = f'/ldp/empty-{uuid4()}'
rsp = self.client.put(url)
get_rsp = self.client.get(url)
assert 'application/octet-stream' in get_rsp.content_type
assert (
'<http://www.w3.org/ns/ldp#NonRDFSource>;rel="type"'
in get_rsp.headers.get_all('link'))
def test_put_empty_ldprs(self):
"""
Check response headers for a PUT operation with empty payload.
Specify MIME type to force LDPC creation.
"""
url = f'/ldp/empty-{uuid4()}'
rsp = self.client.put(url, content_type='text/turtle')
get_rsp = self.client.get(url)
assert 'text/turtle' in get_rsp.content_type
assert (
'<http://www.w3.org/ns/ldp#RDFSource>;rel="type"'
in get_rsp.headers.get_all('link'))
def test_put_existing_ldpnr_empty(self):
"""
Trying to PUT an existing LDP-NR should return a 204 if the LDP
interaction type is the same.
"""
url = f'/ldp/overwrite-{uuid4()}'
self.client.put(url)
put2_resp = self.client.put(url)
assert put2_resp.status_code == 204
def test_put_existing_ldprs_empty(self):
"""
Trying to PUT an existing LDP-RS should return a 204 if the LDP
interaction type is the same.
"""
url = f'/ldp/overwrite-{uuid4()}'
self.client.put(url, content_type='text/turtle')
put2_resp = self.client.put(url, content_type='text/turtle')
with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
put2_resp = self.client.put(
url, data=f, content_type='text/turtle')
assert put2_resp.status_code == 204
def test_put_existing_ldpnr_conflict(self):
"""
Trying to PUT an existing LDP-RS should return a 415 if the LDP
interaction type is different.
"""
url = f'/ldp/overwrite-{uuid4()}'
put1_resp = self.client.put(url)
assert put1_resp.status_code == 201
with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
put2_resp = self.client.put(
url, data=f, content_type='text/turtle')
assert put2_resp.status_code == 415
def test_put_existing_ldpnr_conflict(self):
"""
Trying to PUT an existing LDP-RS should return a 415 if the LDP
interaction type is different.
"""
url = f'/ldp/overwrite-{uuid4()}'
put1_resp = self.client.put(url, content_type='text/turtle')
put2_resp = self.client.put(url, data=b'hello')
assert put2_resp.status_code == 415
def test_put_force_ldpnr(self):
"""
Test forcing LDP-NR creation even with an RDF content type.
"""
url = f'/ldp/force_ldpnr-{uuid4()}'
with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
rsp = self.client.put(
url, data=f, content_type='text/turtle',
headers={
'link': '<http://www.w3.org/ns/ldp#NonRDFSource>;rel="type"'
})
f.seek(0)
data = f.read()
get_rsp = self.client.get(url)
assert 'text/turtle' in get_rsp.content_type
assert (
'<http://www.w3.org/ns/ldp#NonRDFSource>;rel="type"'
in get_rsp.headers.get_all('link'))
assert get_rsp.data == data
def test_put_tree(self, client):
"""
PUT a resource with several path segments.
The test should create intermediate path segments that are LDPCs,
accessible to PUT or POST.
"""
path = '/ldp/test_tree/a/b/c/d/e/f/g'
self.client.put(path)
assert self.client.get(path).status_code == 200
assert self.client.get('/ldp/test_tree/a/b/c').status_code == 200
assert self.client.post(
'/ldp/test_tree/a/b',
content_type='text/turtle').status_code == 201
with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
put_int_resp = self.client.put(
'ldp/test_tree/a', data=f, content_type='text/turtle')
assert put_int_resp.status_code == 204
# @TODO More thorough testing of contents
def test_put_nested_tree(self, client):
"""
Verify that containment is set correctly in nested hierarchies.
First put a new hierarchy and verify that the root node is its
container; then put another hierarchy under it and verify that the
first hierarchy is the container of the second one.
"""
uuid1 = 'test_nested_tree/a/b/c/d'
uuid2 = uuid1 + '/e/f/g'
path1 = '/ldp/' + uuid1
path2 = '/ldp/' + uuid2
self.client.put(path1, content_type='text/turtle')
cont1_data = self.client.get('/ldp').data
gr1 = Graph().parse(data=cont1_data, format='turtle')
assert gr1[ URIRef(g.webroot + '/') : nsc['ldp'].contains : \
URIRef(g.webroot + '/test_nested_tree') ]
self.client.put(path2, content_type='text/turtle')
cont2_data = self.client.get(path1).data
gr2 = Graph().parse(data=cont2_data, format='turtle')
assert gr2[ URIRef(g.webroot + '/' + uuid1) : \
nsc['ldp'].contains : \
URIRef(g.webroot + '/' + uuid1 + '/e') ]
def test_put_ldp_rs(self, client):
"""
PUT a resource with RDF payload and verify.
"""
with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
self.client.put('/ldp/ldprs01', data=f, content_type='text/turtle')
resp = self.client.get('/ldp/ldprs01',
headers={'accept' : 'text/turtle'})
assert resp.status_code == 200
gr = Graph().parse(data=resp.data, format='text/turtle')
assert URIRef('http://vocab.getty.edu/ontology#Subject') in \
gr.objects(None, RDF.type)
def test_put_ldp_nr(self, rnd_img):
"""
PUT a resource with binary payload and verify checksums.
"""
rnd_img['content'].seek(0)
resp = self.client.put('/ldp/ldpnr01', data=rnd_img['content'],
headers={
'Content-Type': 'image/png',
'Content-Disposition' : 'attachment; filename={}'.format(
rnd_img['filename'])})
assert resp.status_code == 201
resp = self.client.get(
'/ldp/ldpnr01', headers={'accept' : 'image/png'})
assert resp.status_code == 200
assert sha1(resp.data).hexdigest() == rnd_img['hash']
def test_put_ldp_nr_multipart(self, rnd_img):
"""
PUT a resource with a multipart/form-data payload.
"""
rnd_img['content'].seek(0)
resp = self.client.put(
'/ldp/ldpnr02',
data={
'file': (
rnd_img['content'], rnd_img['filename'],
'image/png',
)
}
)
assert resp.status_code == 201
resp = self.client.get(
'/ldp/ldpnr02', headers={'accept' : 'image/png'})
assert resp.status_code == 200
assert sha1(resp.data).hexdigest() == rnd_img['hash']
def test_get_ldp_nr(self, rnd_img):
"""
PUT a resource with binary payload and test various retieval methods.
"""
uid = '/ldpnr03'
path = '/ldp' + uid
content = b'This is some exciting content.'
resp = self.client.put(path, data=content,
headers={
'Content-Type': 'text/plain',
'Content-Disposition' : 'attachment; filename=exciting.txt'})
assert resp.status_code == 201
uri = g.webroot + uid
# Content retrieval methods.
resp_bin1 = self.client.get(path)
assert resp_bin1.status_code == 200
assert resp_bin1.data == content
resp_bin2 = self.client.get(path, headers={'accept' : 'text/plain'})
assert resp_bin2.status_code == 200
assert resp_bin2.data == content
resp_bin3 = self.client.get(path + '/fcr:content')
assert resp_bin3.status_code == 200
assert resp_bin3.data == content
# Metadata retrieval methods.
resp_md1 = self.client.get(path, headers={'accept' : 'text/turtle'})
assert resp_md1.status_code == 200
gr1 = Graph().parse(data=resp_md1.data, format='text/turtle')
assert gr1[ URIRef(uri) : nsc['rdf'].type : nsc['ldp'].Resource]
resp_md2 = self.client.get(path + '/fcr:metadata')
assert resp_md2.status_code == 200
gr2 = Graph().parse(data=resp_md2.data, format='text/turtle')
assert isomorphic(gr1, gr2)
def test_put_ldprs_invalid_rdf(self):
"""
Verify that PUTting invalid RDF body returns HTTP 400.
However, when forcing LDP-RS, invalid RDF is always accepted.
"""
from lakesuperior.endpoints.ldp import rdf_serializable_mimetypes
rdfstr = b'This is valid RDF because it ends with a period.'
for mt in rdf_serializable_mimetypes:
rsp_notok = self.client.put(
f'/ldp/{uuid4()}', data=rdfstr, content_type=mt)
assert rsp_notok.status_code == 400
rsp_ok = self.client.put(
f'/ldp/{uuid4()}', data=rdfstr, content_type=mt,
headers={
'link': '<http://www.w3.org/ns/ldp#NonRDFSource>;rel="type"'
}
)
assert rsp_ok.status_code == 201
def test_post_ldprs_invalid_rdf(self):
"""
Verify that POSTing invalid RDF body returns HTTP 400.
However, when forcing LDP-RS, invalid RDF is always accepted.
"""
from lakesuperior.endpoints.ldp import rdf_serializable_mimetypes
rdfstr = b'This is valid RDF because it ends with a period.'
for mt in rdf_serializable_mimetypes:
rsp_notok = self.client.post(
f'/ldp', data=rdfstr, content_type=mt)
assert rsp_notok.status_code == 400
rsp_ok = self.client.post(
f'/ldp', data=rdfstr, content_type=mt,
headers={
'link': '<http://www.w3.org/ns/ldp#NonRDFSource>;rel="type"'
}
)
assert rsp_ok.status_code == 201
def test_metadata_describe_header(self):
"""
Verify that a "describe" Link header is presented for LDP-NR metadata.
"""
uid = f'/{uuid4()}'
self.client.put(f'/ldp{uid}', data=b'ciao')
md_rsp = self.client.get(f'/ldp{uid}/fcr:metadata')
assert (
f'<{g.tbox.uid_to_uri(uid)}>; rel=describes'
in md_rsp.headers.get_all('Link'))
def test_put_mismatched_ldp_rs(self, rnd_img):
"""
Verify MIME type / LDP mismatch.
PUT a LDP-RS, then PUT a LDP-NR on the same location and verify it
fails.
"""
path = '/ldp/' + str(uuid4())
rnd_img['content'].seek(0)
ldp_nr_resp = self.client.put(path, data=rnd_img['content'],
headers={
'Content-Disposition' : 'attachment; filename={}'.format(
rnd_img['filename'])})
assert ldp_nr_resp.status_code == 201
with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
ldp_rs_resp = self.client.put(path, data=f,
content_type='text/turtle')
assert ldp_rs_resp.status_code == 415
def test_put_mismatched_ldp_nr(self, rnd_img):
"""
Verify MIME type / LDP mismatch.
PUT a LDP-NR, then PUT a LDP-RS on the same location and verify it
fails.
"""
path = '/ldp/' + str(uuid4())
with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
ldp_rs_resp = self.client.put(path, data=f,
content_type='text/turtle')
assert ldp_rs_resp.status_code == 201
rnd_img['content'].seek(0)
ldp_nr_resp = self.client.put(path, data=rnd_img['content'],
headers={
'Content-Disposition' : 'attachment; filename={}'.format(
rnd_img['filename'])})
assert ldp_nr_resp.status_code == 415
def test_missing_reference(self, client):
"""
PUT a resource with RDF payload referencing a non-existing in-repo
resource.
"""
self.client.get('/ldp')
data = '''
PREFIX ns: <http://example.org#>
PREFIX res: <http://example-source.org/res/>
<> ns:p1 res:bogus ;
ns:p2 <{0}> ;
ns:p3 <{0}/> ;
ns:p4 <{0}/nonexistent> .
'''.format(g.webroot)
put_rsp = self.client.put('/ldp/test_missing_ref', data=data, headers={
'content-type': 'text/turtle'})
assert put_rsp.status_code == 201
resp = self.client.get('/ldp/test_missing_ref',
headers={'accept' : 'text/turtle'})
assert resp.status_code == 200
gr = Graph().parse(data=resp.data, format='text/turtle')
assert URIRef('http://example-source.org/res/bogus') in \
gr.objects(None, URIRef('http://example.org#p1'))
assert URIRef(g.webroot + '/') in (
gr.objects(None, URIRef('http://example.org#p2')))
assert URIRef(g.webroot + '/') in (
gr.objects(None, URIRef('http://example.org#p3')))
assert URIRef(g.webroot + '/nonexistent') not in (
gr.objects(None, URIRef('http://example.org#p4')))
def test_post_resource(self, client):
"""
Check response headers for a POST operation with empty payload.
"""
res = self.client.post('/ldp/')
assert res.status_code == 201
assert 'Location' in res.headers
def test_post_ldp_nr(self, rnd_img):
"""
POST a resource with binary payload and verify checksums.
"""
rnd_img['content'].seek(0)
resp = self.client.post('/ldp/', data=rnd_img['content'],
headers={
'slug': 'ldpnr04',
'Content-Type': 'image/png',
'Content-Disposition' : 'attachment; filename={}'.format(
rnd_img['filename'])})
assert resp.status_code == 201
resp = self.client.get(
'/ldp/ldpnr04', headers={'accept' : 'image/png'})
assert resp.status_code == 200
assert sha1(resp.data).hexdigest() == rnd_img['hash']
def test_post_slug(self):
"""
Verify that a POST with slug results in the expected URI only if the
resource does not exist already.
"""
slug01_resp = self.client.post('/ldp', headers={'slug' : 'slug01'})
assert slug01_resp.status_code == 201
assert slug01_resp.headers['location'] == \
g.webroot + '/slug01'
slug02_resp = self.client.post('/ldp', headers={'slug' : 'slug01'})
assert slug02_resp.status_code == 201
assert slug02_resp.headers['location'] != \
g.webroot + '/slug01'
def test_post_404(self):
"""
Verify that a POST to a non-existing parent results in a 404.
"""
assert self.client.post('/ldp/{}'.format(uuid4()))\
.status_code == 404
def test_post_409(self, rnd_img):
"""
Verify that you cannot POST to a binary resource.
"""
rnd_img['content'].seek(0)
self.client.put('/ldp/post_409', data=rnd_img['content'], headers={
'Content-Disposition' : 'attachment; filename={}'.format(
rnd_img['filename'])})
assert self.client.post('/ldp/post_409').status_code == 409
def test_patch_root(self):
"""
Test patching root node.
"""
path = '/ldp/'
self.client.get(path)
uri = g.webroot + '/'
with open('tests/data/sparql_update/simple_insert.sparql') as data:
resp = self.client.patch(path,
data=data,
headers={'content-type' : 'application/sparql-update'})
assert resp.status_code == 204
resp = self.client.get(path)
gr = Graph().parse(data=resp.data, format='text/turtle')
assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Hello') ]
def test_patch(self):
"""
Test patching a resource.
"""
path = '/ldp/test_patch01'
self.client.put(path, content_type='text/turtle')
uri = g.webroot + '/test_patch01'
with open('tests/data/sparql_update/simple_insert.sparql') as data:
resp = self.client.patch(path,
data=data,
headers={'content-type' : 'application/sparql-update'})
assert resp.status_code == 204
resp = self.client.get(path)
gr = Graph().parse(data=resp.data, format='text/turtle')
assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Hello') ]
self.client.patch(path,
data=open('tests/data/sparql_update/delete+insert+where.sparql'),
headers={'content-type' : 'application/sparql-update'})
resp = self.client.get(path)
gr = Graph().parse(data=resp.data, format='text/turtle')
assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Ciao') ]
def test_patch_no_single_subject(self):
"""
Test patching a resource violating the single-subject rule.
"""
path = '/ldp/test_patch_ssr'
self.client.put(path, content_type='text/turtle')
uri = g.webroot + '/test_patch_ssr'
nossr_qry = 'INSERT { <http://bogus.org> a <urn:ns:A> . } WHERE {}'
abs_qry = 'INSERT {{ <{}> a <urn:ns:A> . }} WHERE {{}}'.format(uri)
frag_qry = 'INSERT {{ <{}#frag> a <urn:ns:A> . }} WHERE {{}}'\
.format(uri)
# @TODO Leave commented until a decision is made about SSR.
assert self.client.patch(
path, data=nossr_qry,
headers={'content-type': 'application/sparql-update'}
).status_code == 204
assert self.client.patch(
path, data=abs_qry,
headers={'content-type': 'application/sparql-update'}
).status_code == 204
assert self.client.patch(
path, data=frag_qry,
headers={'content-type': 'application/sparql-update'}
).status_code == 204
def test_patch_ldp_nr_metadata(self):
"""
Test patching a LDP-NR metadata resource from the fcr:metadata URI.
"""
path = '/ldp/ldpnr01'
with open('tests/data/sparql_update/simple_insert.sparql') as data:
self.client.patch(path + '/fcr:metadata',
data=data,
headers={'content-type' : 'application/sparql-update'})
resp = self.client.get(path + '/fcr:metadata')
assert resp.status_code == 200
uri = g.webroot + '/ldpnr01'
gr = Graph().parse(data=resp.data, format='text/turtle')
assert gr[URIRef(uri) : nsc['dc'].title : Literal('Hello')]
with open(
'tests/data/sparql_update/delete+insert+where.sparql') as data:
patch_resp = self.client.patch(path + '/fcr:metadata',
data=data,
headers={'content-type' : 'application/sparql-update'})
assert patch_resp.status_code == 204
resp = self.client.get(path + '/fcr:metadata')
assert resp.status_code == 200
gr = Graph().parse(data=resp.data, format='text/turtle')
assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Ciao') ]
def test_patch_ldpnr(self):
"""
Verify that a direct PATCH to a LDP-NR results in a 415.
"""
with open(
'tests/data/sparql_update/delete+insert+where.sparql') as data:
patch_resp = self.client.patch('/ldp/ldpnr01',
data=data,
headers={'content-type': 'application/sparql-update'})
assert patch_resp.status_code == 415
def test_patch_invalid_mimetype(self, rnd_img):
"""
Verify that a PATCH using anything other than an
`application/sparql-update` MIME type results in an error.
"""
self.client.put(
'/ldp/test_patch_invalid_mimetype', content_type='text/turtle')
rnd_img['content'].seek(0)
ldpnr_resp = self.client.patch('/ldp/ldpnr01/fcr:metadata',
data=rnd_img,
headers={'content-type' : 'image/jpeg'})
ldprs_resp = self.client.patch('/ldp/test_patch_invalid_mimetype',
data=b'Hello, I\'m not a SPARQL update.',
headers={'content-type' : 'text/plain'})
assert ldprs_resp.status_code == ldpnr_resp.status_code == 415
def test_patch_srv_mgd_pred(self, rnd_img):
"""
Verify that adding or removing a server-managed predicate fails.
"""
uid = '/test_patch_sm_pred'
path = f'/ldp{uid}'
self.client.put(path, content_type='text/turtle')
self.client.put(path + '/child1', content_type='text/turtle')
uri = g.webroot + uid
ins_qry1 = f'INSERT {{ <> <{nsc["ldp"].contains}> <http://bogus.com/ext1> . }} WHERE {{}}'
ins_qry2 = (
f'INSERT {{ <> <{nsc["fcrepo"].created}>'
f'"2019-04-01T05:57:36.899033+00:00"^^<{nsc["xsd"].dateTime}> . }}'
'WHERE {}'
)
# The following won't change the graph so it does not raise an error.
ins_qry3 = f'INSERT {{ <> a <{nsc["ldp"].Container}> . }} WHERE {{}}'
del_qry1 = (
f'DELETE {{ <> <{nsc["ldp"].contains}> ?o . }} '
f'WHERE {{ <> <{nsc["ldp"].contains}> ?o . }}'
)
del_qry2 = f'DELETE {{ <> a <{nsc["ldp"].Container}> . }} WHERE {{}}'
# No-op as ins_qry3
del_qry3 = (
f'DELETE {{ <> a <{nsc["ldp"].DirectContainer}> .}} '
'WHERE {}'
)
assert self.client.patch(
path, data=ins_qry1,
headers={'content-type': 'application/sparql-update'}
).status_code == 412
assert self.client.patch(
path, data=ins_qry1,
headers={
'content-type': 'application/sparql-update',
'prefer': 'handling=lenient',
}
).status_code == 204
assert self.client.patch(
path, data=ins_qry2,
headers={'content-type': 'application/sparql-update'}
).status_code == 412
assert self.client.patch(
path, data=ins_qry2,
headers={
'content-type': 'application/sparql-update',
'prefer': 'handling=lenient',
}
).status_code == 204
assert self.client.patch(
path, data=ins_qry3,
headers={'content-type': 'application/sparql-update'}
).status_code == 204
assert self.client.patch(
path, data=del_qry1,
headers={'content-type': 'application/sparql-update'}
).status_code == 412
assert self.client.patch(
path, data=del_qry1,
headers={
'content-type': 'application/sparql-update',
'prefer': 'handling=lenient',
}
).status_code == 204
assert self.client.patch(
path, data=del_qry2,
headers={'content-type': 'application/sparql-update'}
).status_code == 412
assert self.client.patch(
path, data=ins_qry2,
headers={
'content-type': 'application/sparql-update',
'prefer': 'handling=lenient',
}
).status_code == 204
assert self.client.patch(
path, data=del_qry3,
headers={'content-type': 'application/sparql-update'}
).status_code == 204
def test_delete(self):
"""
Test delete response codes.
"""
self.client.put('/ldp/test_delete01')
delete_resp = self.client.delete('/ldp/test_delete01')
assert delete_resp.status_code == 204
bogus_delete_resp = self.client.delete('/ldp/test_delete101')
assert bogus_delete_resp.status_code == 404
def test_tombstone(self):
"""
Test tombstone behaviors.
For POST on a tombstone, check `test_resurrection`.
"""
tstone_resp = self.client.get('/ldp/test_delete01')
assert tstone_resp.status_code == 410
assert tstone_resp.headers['Link'] == \
'<{}/test_delete01/fcr:tombstone>; rel="hasTombstone"'\
.format(g.webroot)
tstone_path = '/ldp/test_delete01/fcr:tombstone'
assert self.client.get(tstone_path).status_code == 405
assert self.client.put(tstone_path).status_code == 405
assert self.client.delete(tstone_path).status_code == 204
assert self.client.get('/ldp/test_delete01').status_code == 404
def test_delete_recursive(self):
"""
Test response codes for resources deleted recursively and their
tombstones.
"""
child_suffixes = ('a', 'a/b', 'a/b/c', 'a1', 'a1/b1')
self.client.put('/ldp/test_delete_recursive01')
for cs in child_suffixes:
self.client.put('/ldp/test_delete_recursive01/{}'.format(cs))
assert self.client.delete(
'/ldp/test_delete_recursive01').status_code == 204
tstone_resp = self.client.get('/ldp/test_delete_recursive01')
assert tstone_resp.status_code == 410
assert tstone_resp.headers['Link'] == \
'<{}/test_delete_recursive01/fcr:tombstone>; rel="hasTombstone"'\
.format(g.webroot)
for cs in child_suffixes:
child_tstone_resp = self.client.get(
'/ldp/test_delete_recursive01/{}'.format(cs))
assert child_tstone_resp.status_code == tstone_resp.status_code
assert 'Link' not in child_tstone_resp.headers.keys()
def test_put_fragments(self):
"""
Test the correct handling of fragment URIs on PUT and GET.
"""
with open('tests/data/fragments.ttl', 'rb') as f:
self.client.put(
'/ldp/test_fragment01', content_type='text/turtle', data=f)
rsp = self.client.get('/ldp/test_fragment01')
gr = Graph().parse(data=rsp.data, format='text/turtle')
assert gr[
URIRef(g.webroot + '/test_fragment01#hash1')
: URIRef('http://ex.org/p2') : URIRef('http://ex.org/o2')]
def test_patch_fragments(self):
"""
Test the correct handling of fragment URIs on PATCH.
"""
self.client.put('/ldp/test_fragment_patch', content_type='text/turtle')
with open('tests/data/fragments_insert.sparql', 'rb') as f:
self.client.patch(
'/ldp/test_fragment_patch',
content_type='application/sparql-update', data=f)
ins_rsp = self.client.get('/ldp/test_fragment_patch')
ins_gr = Graph().parse(data=ins_rsp.data, format='text/turtle')
assert ins_gr[
URIRef(g.webroot + '/test_fragment_patch#hash1234')
: URIRef('http://ex.org/p3') : URIRef('http://ex.org/o3')]
with open('tests/data/fragments_delete.sparql', 'rb') as f:
self.client.patch(
'/ldp/test_fragment_patch',
headers={
'Content-Type' : 'application/sparql-update',
},
data=f
)
del_rsp = self.client.get('/ldp/test_fragment_patch')
del_gr = Graph().parse(data=del_rsp.data, format='text/turtle')
assert not del_gr[
URIRef(g.webroot + '/test_fragment_patch#hash1234')
: URIRef('http://ex.org/p3') : URIRef('http://ex.org/o3')]
@pytest.mark.usefixtures('client_class')
@pytest.mark.usefixtures('db')
class TestMimeType:
"""
Test ``Accept`` headers and input & output formats.
"""
def test_accept(self):
"""
Verify the default serialization method.
"""
accept_list = {
('', 'text/turtle'),
('text/turtle', 'text/turtle'),
('application/rdf+xml', 'application/rdf+xml'),
('application/n-triples', 'application/n-triples'),
('application/bogus', 'text/turtle'),
(
'application/rdf+xml;q=0.5,application/n-triples;q=0.7',
'application/n-triples'),
(
'application/rdf+xml;q=0.5,application/bogus;q=0.7',
'application/rdf+xml'),
('application/rdf+xml;q=0.5,text/n3;q=0.7', 'text/n3'),
(
'application/rdf+xml;q=0.5,application/ld+json;q=0.7',
'application/ld+json'),
}
for mimetype, fmt in accept_list:
rsp = self.client.get('/ldp', headers={'Accept': mimetype})
assert rsp.mimetype == fmt
gr = Graph(identifier=g.webroot + '/').parse(
data=rsp.data, format=fmt)
assert nsc['fcrepo'].RepositoryRoot in set(gr.objects())
def test_provided_rdf(self):
"""
Test several input RDF serialiation formats.
"""
self.client.get('/ldp')
gr = Graph()
gr.add((
URIRef(g.webroot + '/test_mimetype'),
nsc['dcterms'].title, Literal('Test MIME type.')))
test_list = {
'application/n-triples',
'application/rdf+xml',
'text/n3',
'text/turtle',
'application/ld+json',
}
for mimetype in test_list:
rdf_data = gr.serialize(format=mimetype)
self.client.put(
'/ldp/test_mimetype', data=rdf_data, content_type=mimetype)
rsp = self.client.get('/ldp/test_mimetype')
rsp_gr = Graph(identifier=g.webroot + '/test_mimetype').parse(
data=rsp.data, format='text/turtle')
assert (
URIRef(g.webroot + '/test_mimetype'),
nsc['dcterms'].title, Literal('Test MIME type.')) in rsp_gr
@pytest.mark.usefixtures('client_class')
class TestDigestHeaders:
"""
Test Digest and ETag headers.
"""
def test_etag_digest(self):
"""
Verify ETag and Digest headers on creation.
The headers must correspond to the checksum of the binary content.
"""
uid = '/test_etag1'
path = '/ldp' + uid
content = uuid4().bytes
content_cksum = hashlib.new(digest_algo, content)
put_rsp = self.client.put(
path, data=content, content_type='text/plain')
assert content_cksum.hexdigest() in \
put_rsp.headers.get('etag').split(',')
assert put_rsp.headers.get('digest') == \
f'{digest_algo.upper()}=' + b64encode(content_cksum.digest()).decode()
get_rsp = self.client.get(path)
assert content_cksum.hexdigest() in \
put_rsp.headers.get('etag').split(',')
assert get_rsp.headers.get('digest') == \
f'{digest_algo.upper()}=' + b64encode(content_cksum.digest()).decode()
def test_etag_ident(self):
"""
Verify that two resources with the same content yield identical ETags.
"""
path1 = f'/ldp/{uuid4()}'
path2 = f'/ldp/{uuid4()}'
content = uuid4().bytes
content_cksum = hashlib.new(digest_algo, content)
self.client.put(path1, data=content, content_type='text/plain')
self.client.put(path2, data=content, content_type='text/plain')
get_rsp1 = self.client.get(path1)
get_rsp2 = self.client.get(path2)
assert get_rsp1.headers.get('etag') == get_rsp2.headers.get('etag')
assert get_rsp1.headers.get('digest') == get_rsp2.headers.get('digest')
def test_etag_diff(self):
"""
Verify that two resources with different content yield different ETags.
"""
path1 = f'/ldp/{uuid4()}'
path2 = f'/ldp/{uuid4()}'
content1 = b'some interesting content.'
content_cksum1 = hashlib.new(digest_algo, content1)
content2 = b'Some great content.'
content_cksum2 = hashlib.new(digest_algo, content2)
self.client.put(path1, data=content1, content_type='text/plain')
self.client.put(path2, data=content2, content_type='text/plain')
get_rsp1 = self.client.get(path1)
get_rsp2 = self.client.get(path2)