test_ldp.py 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. import pdb
  2. import pytest
  3. from base64 import b64encode
  4. from datetime import timedelta
  5. from hashlib import sha1, sha256, blake2b
  6. from uuid import uuid4
  7. from werkzeug.http import http_date
  8. import arrow
  9. from flask import g
  10. from rdflib import Graph
  11. from rdflib.compare import isomorphic
  12. from rdflib.namespace import RDF
  13. from rdflib.term import Literal, URIRef
  14. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  15. from lakesuperior.model.ldpr import Ldpr
  16. @pytest.fixture(scope='module')
  17. def random_uuid():
  18. return str(uuid4())
  19. @pytest.mark.usefixtures('client_class')
  20. @pytest.mark.usefixtures('db')
  21. class TestLdp:
  22. """
  23. Test HTTP interaction with LDP endpoint.
  24. """
  25. def test_get_root_node(self):
  26. """
  27. Get the root node from two different endpoints.
  28. The test triplestore must be initialized, hence the `db` fixture.
  29. """
  30. ldp_resp = self.client.get('/ldp')
  31. rest_resp = self.client.get('/rest')
  32. assert ldp_resp.status_code == 200
  33. assert rest_resp.status_code == 200
  34. def test_put_empty_resource(self, random_uuid):
  35. """
  36. Check response headers for a PUT operation with empty payload.
  37. """
  38. resp = self.client.put('/ldp/new_resource')
  39. assert resp.status_code == 201
  40. assert resp.data == bytes(
  41. '{}/new_resource'.format(g.webroot), 'utf-8')
  42. def test_put_existing_resource(self, random_uuid):
  43. """
  44. Trying to PUT an existing resource should return a 204 if the payload
  45. is empty.
  46. """
  47. path = '/ldp/nonidempotent01'
  48. put1_resp = self.client.put(path)
  49. assert put1_resp.status_code == 201
  50. assert self.client.get(path).status_code == 200
  51. put2_resp = self.client.put(path)
  52. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  53. put2_resp = self.client.put(
  54. path, data=f, content_type='text/turtle')
  55. assert put2_resp.status_code == 204
  56. put2_resp = self.client.put(path)
  57. assert put2_resp.status_code == 204
  58. def test_put_tree(self, client):
  59. """
  60. PUT a resource with several path segments.
  61. The test should create intermediate path segments that are LDPCs,
  62. accessible to PUT or POST.
  63. """
  64. path = '/ldp/test_tree/a/b/c/d/e/f/g'
  65. self.client.put(path)
  66. assert self.client.get(path).status_code == 200
  67. assert self.client.get('/ldp/test_tree/a/b/c').status_code == 200
  68. assert self.client.post('/ldp/test_tree/a/b').status_code == 201
  69. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  70. put_int_resp = self.client.put(
  71. 'ldp/test_tree/a', data=f, content_type='text/turtle')
  72. assert put_int_resp.status_code == 204
  73. # @TODO More thorough testing of contents
  74. def test_put_nested_tree(self, client):
  75. """
  76. Verify that containment is set correctly in nested hierarchies.
  77. First put a new hierarchy and verify that the root node is its
  78. container; then put another hierarchy under it and verify that the
  79. first hierarchy is the container of the second one.
  80. """
  81. uuid1 = 'test_nested_tree/a/b/c/d'
  82. uuid2 = uuid1 + '/e/f/g'
  83. path1 = '/ldp/' + uuid1
  84. path2 = '/ldp/' + uuid2
  85. self.client.put(path1)
  86. cont1_data = self.client.get('/ldp').data
  87. gr1 = Graph().parse(data=cont1_data, format='turtle')
  88. assert gr1[ URIRef(g.webroot + '/') : nsc['ldp'].contains : \
  89. URIRef(g.webroot + '/test_nested_tree') ]
  90. self.client.put(path2)
  91. cont2_data = self.client.get(path1).data
  92. gr2 = Graph().parse(data=cont2_data, format='turtle')
  93. assert gr2[ URIRef(g.webroot + '/' + uuid1) : \
  94. nsc['ldp'].contains : \
  95. URIRef(g.webroot + '/' + uuid1 + '/e') ]
  96. def test_put_ldp_rs(self, client):
  97. """
  98. PUT a resource with RDF payload and verify.
  99. """
  100. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  101. self.client.put('/ldp/ldprs01', data=f, content_type='text/turtle')
  102. resp = self.client.get('/ldp/ldprs01',
  103. headers={'accept' : 'text/turtle'})
  104. assert resp.status_code == 200
  105. gr = Graph().parse(data=resp.data, format='text/turtle')
  106. assert URIRef('http://vocab.getty.edu/ontology#Subject') in \
  107. gr.objects(None, RDF.type)
  108. def test_put_ldp_nr(self, rnd_img):
  109. """
  110. PUT a resource with binary payload and verify checksums.
  111. """
  112. rnd_img['content'].seek(0)
  113. resp = self.client.put('/ldp/ldpnr01', data=rnd_img['content'],
  114. headers={
  115. 'Content-Type': 'image/png',
  116. 'Content-Disposition' : 'attachment; filename={}'.format(
  117. rnd_img['filename'])})
  118. assert resp.status_code == 201
  119. resp = self.client.get(
  120. '/ldp/ldpnr01', headers={'accept' : 'image/png'})
  121. assert resp.status_code == 200
  122. assert sha1(resp.data).hexdigest() == rnd_img['hash']
  123. def test_put_ldp_nr_multipart(self, rnd_img):
  124. """
  125. PUT a resource with a multipart/form-data payload.
  126. """
  127. rnd_img['content'].seek(0)
  128. resp = self.client.put(
  129. '/ldp/ldpnr02',
  130. data={
  131. 'file': (
  132. rnd_img['content'], rnd_img['filename'],
  133. 'image/png',
  134. )
  135. }
  136. )
  137. assert resp.status_code == 201
  138. resp = self.client.get(
  139. '/ldp/ldpnr02', headers={'accept' : 'image/png'})
  140. assert resp.status_code == 200
  141. assert sha1(resp.data).hexdigest() == rnd_img['hash']
  142. def test_get_ldp_nr(self, rnd_img):
  143. """
  144. PUT a resource with binary payload and test various retieval methods.
  145. """
  146. uid = '/ldpnr03'
  147. path = '/ldp' + uid
  148. content = b'This is some exciting content.'
  149. resp = self.client.put(path, data=content,
  150. headers={
  151. 'Content-Type': 'text/plain',
  152. 'Content-Disposition' : 'attachment; filename=exciting.txt'})
  153. assert resp.status_code == 201
  154. uri = g.webroot + uid
  155. # Content retrieval methods.
  156. resp_bin1 = self.client.get(path)
  157. assert resp_bin1.status_code == 200
  158. assert resp_bin1.data == content
  159. resp_bin2 = self.client.get(path, headers={'accept' : 'text/plain'})
  160. assert resp_bin2.status_code == 200
  161. assert resp_bin2.data == content
  162. resp_bin3 = self.client.get(path + '/fcr:content')
  163. assert resp_bin3.status_code == 200
  164. assert resp_bin3.data == content
  165. # Metadata retrieval methods.
  166. resp_md1 = self.client.get(path, headers={'accept' : 'text/turtle'})
  167. assert resp_md1.status_code == 200
  168. gr1 = Graph().parse(data=resp_md1.data, format='text/turtle')
  169. assert gr1[ URIRef(uri) : nsc['rdf'].type : nsc['ldp'].Resource]
  170. resp_md2 = self.client.get(path + '/fcr:metadata')
  171. assert resp_md2.status_code == 200
  172. gr2 = Graph().parse(data=resp_md2.data, format='text/turtle')
  173. assert isomorphic(gr1, gr2)
  174. def test_put_mismatched_ldp_rs(self, rnd_img):
  175. """
  176. Verify MIME type / LDP mismatch.
  177. PUT a LDP-RS, then PUT a LDP-NR on the same location and verify it
  178. fails.
  179. """
  180. path = '/ldp/' + str(uuid4())
  181. rnd_img['content'].seek(0)
  182. ldp_nr_resp = self.client.put(path, data=rnd_img['content'],
  183. headers={
  184. 'Content-Disposition' : 'attachment; filename={}'.format(
  185. rnd_img['filename'])})
  186. assert ldp_nr_resp.status_code == 201
  187. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  188. ldp_rs_resp = self.client.put(path, data=f,
  189. content_type='text/turtle')
  190. assert ldp_rs_resp.status_code == 415
  191. def test_put_mismatched_ldp_nr(self, rnd_img):
  192. """
  193. Verify MIME type / LDP mismatch.
  194. PUT a LDP-NR, then PUT a LDP-RS on the same location and verify it
  195. fails.
  196. """
  197. path = '/ldp/' + str(uuid4())
  198. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  199. ldp_rs_resp = self.client.put(path, data=f,
  200. content_type='text/turtle')
  201. assert ldp_rs_resp.status_code == 201
  202. rnd_img['content'].seek(0)
  203. ldp_nr_resp = self.client.put(path, data=rnd_img['content'],
  204. headers={
  205. 'Content-Disposition' : 'attachment; filename={}'.format(
  206. rnd_img['filename'])})
  207. assert ldp_nr_resp.status_code == 415
  208. def test_missing_reference(self, client):
  209. """
  210. PUT a resource with RDF payload referencing a non-existing in-repo
  211. resource.
  212. """
  213. self.client.get('/ldp')
  214. data = '''
  215. PREFIX ns: <http://example.org#>
  216. PREFIX res: <http://example-source.org/res/>
  217. <> ns:p1 res:bogus ;
  218. ns:p2 <{0}> ;
  219. ns:p3 <{0}/> ;
  220. ns:p4 <{0}/nonexistent> .
  221. '''.format(g.webroot)
  222. put_rsp = self.client.put('/ldp/test_missing_ref', data=data, headers={
  223. 'content-type': 'text/turtle'})
  224. assert put_rsp.status_code == 201
  225. resp = self.client.get('/ldp/test_missing_ref',
  226. headers={'accept' : 'text/turtle'})
  227. assert resp.status_code == 200
  228. gr = Graph().parse(data=resp.data, format='text/turtle')
  229. assert URIRef('http://example-source.org/res/bogus') in \
  230. gr.objects(None, URIRef('http://example.org#p1'))
  231. assert URIRef(g.webroot + '/') in (
  232. gr.objects(None, URIRef('http://example.org#p2')))
  233. assert URIRef(g.webroot + '/') in (
  234. gr.objects(None, URIRef('http://example.org#p3')))
  235. assert URIRef(g.webroot + '/nonexistent') not in (
  236. gr.objects(None, URIRef('http://example.org#p4')))
  237. def test_post_resource(self, client):
  238. """
  239. Check response headers for a POST operation with empty payload.
  240. """
  241. res = self.client.post('/ldp/')
  242. assert res.status_code == 201
  243. assert 'Location' in res.headers
  244. def test_post_ldp_nr(self, rnd_img):
  245. """
  246. POST a resource with binary payload and verify checksums.
  247. """
  248. rnd_img['content'].seek(0)
  249. resp = self.client.post('/ldp/', data=rnd_img['content'],
  250. headers={
  251. 'slug': 'ldpnr04',
  252. 'Content-Type': 'image/png',
  253. 'Content-Disposition' : 'attachment; filename={}'.format(
  254. rnd_img['filename'])})
  255. assert resp.status_code == 201
  256. resp = self.client.get(
  257. '/ldp/ldpnr04', headers={'accept' : 'image/png'})
  258. assert resp.status_code == 200
  259. assert sha1(resp.data).hexdigest() == rnd_img['hash']
  260. def test_post_slug(self):
  261. """
  262. Verify that a POST with slug results in the expected URI only if the
  263. resource does not exist already.
  264. """
  265. slug01_resp = self.client.post('/ldp', headers={'slug' : 'slug01'})
  266. assert slug01_resp.status_code == 201
  267. assert slug01_resp.headers['location'] == \
  268. g.webroot + '/slug01'
  269. slug02_resp = self.client.post('/ldp', headers={'slug' : 'slug01'})
  270. assert slug02_resp.status_code == 201
  271. assert slug02_resp.headers['location'] != \
  272. g.webroot + '/slug01'
  273. def test_post_404(self):
  274. """
  275. Verify that a POST to a non-existing parent results in a 404.
  276. """
  277. assert self.client.post('/ldp/{}'.format(uuid4()))\
  278. .status_code == 404
  279. def test_post_409(self, rnd_img):
  280. """
  281. Verify that you cannot POST to a binary resource.
  282. """
  283. rnd_img['content'].seek(0)
  284. self.client.put('/ldp/post_409', data=rnd_img['content'], headers={
  285. 'Content-Disposition' : 'attachment; filename={}'.format(
  286. rnd_img['filename'])})
  287. assert self.client.post('/ldp/post_409').status_code == 409
  288. def test_patch_root(self):
  289. """
  290. Test patching root node.
  291. """
  292. path = '/ldp/'
  293. self.client.get(path)
  294. uri = g.webroot + '/'
  295. with open('tests/data/sparql_update/simple_insert.sparql') as data:
  296. resp = self.client.patch(path,
  297. data=data,
  298. headers={'content-type' : 'application/sparql-update'})
  299. assert resp.status_code == 204
  300. resp = self.client.get(path)
  301. gr = Graph().parse(data=resp.data, format='text/turtle')
  302. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Hello') ]
  303. def test_patch(self):
  304. """
  305. Test patching a resource.
  306. """
  307. path = '/ldp/test_patch01'
  308. self.client.put(path)
  309. uri = g.webroot + '/test_patch01'
  310. with open('tests/data/sparql_update/simple_insert.sparql') as data:
  311. resp = self.client.patch(path,
  312. data=data,
  313. headers={'content-type' : 'application/sparql-update'})
  314. assert resp.status_code == 204
  315. resp = self.client.get(path)
  316. gr = Graph().parse(data=resp.data, format='text/turtle')
  317. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Hello') ]
  318. self.client.patch(path,
  319. data=open('tests/data/sparql_update/delete+insert+where.sparql'),
  320. headers={'content-type' : 'application/sparql-update'})
  321. resp = self.client.get(path)
  322. gr = Graph().parse(data=resp.data, format='text/turtle')
  323. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Ciao') ]
  324. def test_patch_ssr(self):
  325. """
  326. Test patching a resource violating the single-subject rule.
  327. """
  328. path = '/ldp/test_patch_ssr'
  329. self.client.put(path)
  330. uri = g.webroot + '/test_patch_ssr'
  331. nossr_qry = 'INSERT { <http://bogus.org> a <urn:ns:A> . } WHERE {}'
  332. abs_qry = 'INSERT {{ <{}> a <urn:ns:A> . }} WHERE {{}}'.format(uri)
  333. frag_qry = 'INSERT {{ <{}#frag> a <urn:ns:A> . }} WHERE {{}}'\
  334. .format(uri)
  335. # @TODO Leave commented until a decision is made about SSR.
  336. assert self.client.patch(
  337. path, data=nossr_qry,
  338. headers={'content-type': 'application/sparql-update'}
  339. ).status_code == 204
  340. assert self.client.patch(
  341. path, data=abs_qry,
  342. headers={'content-type': 'application/sparql-update'}
  343. ).status_code == 204
  344. assert self.client.patch(
  345. path, data=frag_qry,
  346. headers={'content-type': 'application/sparql-update'}
  347. ).status_code == 204
  348. def test_patch_ldp_nr_metadata(self):
  349. """
  350. Test patching a LDP-NR metadata resource from the fcr:metadata URI.
  351. """
  352. path = '/ldp/ldpnr01'
  353. with open('tests/data/sparql_update/simple_insert.sparql') as data:
  354. self.client.patch(path + '/fcr:metadata',
  355. data=data,
  356. headers={'content-type' : 'application/sparql-update'})
  357. resp = self.client.get(path + '/fcr:metadata')
  358. assert resp.status_code == 200
  359. uri = g.webroot + '/ldpnr01'
  360. gr = Graph().parse(data=resp.data, format='text/turtle')
  361. assert gr[URIRef(uri) : nsc['dc'].title : Literal('Hello')]
  362. with open(
  363. 'tests/data/sparql_update/delete+insert+where.sparql') as data:
  364. patch_resp = self.client.patch(path + '/fcr:metadata',
  365. data=data,
  366. headers={'content-type' : 'application/sparql-update'})
  367. assert patch_resp.status_code == 204
  368. resp = self.client.get(path + '/fcr:metadata')
  369. assert resp.status_code == 200
  370. gr = Graph().parse(data=resp.data, format='text/turtle')
  371. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Ciao') ]
  372. def test_patch_ldpnr(self):
  373. """
  374. Verify that a direct PATCH to a LDP-NR results in a 415.
  375. """
  376. with open(
  377. 'tests/data/sparql_update/delete+insert+where.sparql') as data:
  378. patch_resp = self.client.patch('/ldp/ldpnr01',
  379. data=data,
  380. headers={'content-type': 'application/sparql-update'})
  381. assert patch_resp.status_code == 415
  382. def test_patch_invalid_mimetype(self, rnd_img):
  383. """
  384. Verify that a PATCH using anything other than an
  385. `application/sparql-update` MIME type results in an error.
  386. """
  387. self.client.put('/ldp/test_patch_invalid_mimetype')
  388. rnd_img['content'].seek(0)
  389. ldpnr_resp = self.client.patch('/ldp/ldpnr01/fcr:metadata',
  390. data=rnd_img,
  391. headers={'content-type' : 'image/jpeg'})
  392. ldprs_resp = self.client.patch('/ldp/test_patch_invalid_mimetype',
  393. data=b'Hello, I\'m not a SPARQL update.',
  394. headers={'content-type' : 'text/plain'})
  395. assert ldprs_resp.status_code == ldpnr_resp.status_code == 415
  396. def test_delete(self):
  397. """
  398. Test delete response codes.
  399. """
  400. self.client.put('/ldp/test_delete01')
  401. delete_resp = self.client.delete('/ldp/test_delete01')
  402. assert delete_resp.status_code == 204
  403. bogus_delete_resp = self.client.delete('/ldp/test_delete101')
  404. assert bogus_delete_resp.status_code == 404
  405. def test_tombstone(self):
  406. """
  407. Test tombstone behaviors.
  408. For POST on a tombstone, check `test_resurrection`.
  409. """
  410. tstone_resp = self.client.get('/ldp/test_delete01')
  411. assert tstone_resp.status_code == 410
  412. assert tstone_resp.headers['Link'] == \
  413. '<{}/test_delete01/fcr:tombstone>; rel="hasTombstone"'\
  414. .format(g.webroot)
  415. tstone_path = '/ldp/test_delete01/fcr:tombstone'
  416. assert self.client.get(tstone_path).status_code == 405
  417. assert self.client.put(tstone_path).status_code == 405
  418. assert self.client.delete(tstone_path).status_code == 204
  419. assert self.client.get('/ldp/test_delete01').status_code == 404
  420. def test_delete_recursive(self):
  421. """
  422. Test response codes for resources deleted recursively and their
  423. tombstones.
  424. """
  425. child_suffixes = ('a', 'a/b', 'a/b/c', 'a1', 'a1/b1')
  426. self.client.put('/ldp/test_delete_recursive01')
  427. for cs in child_suffixes:
  428. self.client.put('/ldp/test_delete_recursive01/{}'.format(cs))
  429. assert self.client.delete(
  430. '/ldp/test_delete_recursive01').status_code == 204
  431. tstone_resp = self.client.get('/ldp/test_delete_recursive01')
  432. assert tstone_resp.status_code == 410
  433. assert tstone_resp.headers['Link'] == \
  434. '<{}/test_delete_recursive01/fcr:tombstone>; rel="hasTombstone"'\
  435. .format(g.webroot)
  436. for cs in child_suffixes:
  437. child_tstone_resp = self.client.get(
  438. '/ldp/test_delete_recursive01/{}'.format(cs))
  439. assert child_tstone_resp.status_code == tstone_resp.status_code
  440. assert 'Link' not in child_tstone_resp.headers.keys()
  441. def test_put_fragments(self):
  442. """
  443. Test the correct handling of fragment URIs on PUT and GET.
  444. """
  445. with open('tests/data/fragments.ttl', 'rb') as f:
  446. self.client.put(
  447. '/ldp/test_fragment01',
  448. headers={
  449. 'Content-Type' : 'text/turtle',
  450. },
  451. data=f
  452. )
  453. rsp = self.client.get('/ldp/test_fragment01')
  454. gr = Graph().parse(data=rsp.data, format='text/turtle')
  455. assert gr[
  456. URIRef(g.webroot + '/test_fragment01#hash1')
  457. : URIRef('http://ex.org/p2') : URIRef('http://ex.org/o2')]
  458. def test_patch_fragments(self):
  459. """
  460. Test the correct handling of fragment URIs on PATCH.
  461. """
  462. self.client.put('/ldp/test_fragment_patch')
  463. with open('tests/data/fragments_insert.sparql', 'rb') as f:
  464. self.client.patch(
  465. '/ldp/test_fragment_patch',
  466. headers={
  467. 'Content-Type' : 'application/sparql-update',
  468. },
  469. data=f
  470. )
  471. ins_rsp = self.client.get('/ldp/test_fragment_patch')
  472. ins_gr = Graph().parse(data=ins_rsp.data, format='text/turtle')
  473. assert ins_gr[
  474. URIRef(g.webroot + '/test_fragment_patch#hash1234')
  475. : URIRef('http://ex.org/p3') : URIRef('http://ex.org/o3')]
  476. with open('tests/data/fragments_delete.sparql', 'rb') as f:
  477. self.client.patch(
  478. '/ldp/test_fragment_patch',
  479. headers={
  480. 'Content-Type' : 'application/sparql-update',
  481. },
  482. data=f
  483. )
  484. del_rsp = self.client.get('/ldp/test_fragment_patch')
  485. del_gr = Graph().parse(data=del_rsp.data, format='text/turtle')
  486. assert not del_gr[
  487. URIRef(g.webroot + '/test_fragment_patch#hash1234')
  488. : URIRef('http://ex.org/p3') : URIRef('http://ex.org/o3')]
  489. @pytest.mark.usefixtures('client_class')
  490. @pytest.mark.usefixtures('db')
  491. class TestMimeType:
  492. """
  493. Test ``Accept`` headers and input & output formats.
  494. """
  495. def test_accept(self):
  496. """
  497. Verify the default serialization method.
  498. """
  499. accept_list = {
  500. ('', 'text/turtle'),
  501. ('text/turtle', 'text/turtle'),
  502. ('application/rdf+xml', 'application/rdf+xml'),
  503. ('application/n-triples', 'application/n-triples'),
  504. ('application/bogus', 'text/turtle'),
  505. (
  506. 'application/rdf+xml;q=0.5,application/n-triples;q=0.7',
  507. 'application/n-triples'),
  508. (
  509. 'application/rdf+xml;q=0.5,application/bogus;q=0.7',
  510. 'application/rdf+xml'),
  511. ('application/rdf+xml;q=0.5,text/n3;q=0.7', 'text/n3'),
  512. (
  513. 'application/rdf+xml;q=0.5,application/ld+json;q=0.7',
  514. 'application/ld+json'),
  515. }
  516. for mimetype, fmt in accept_list:
  517. rsp = self.client.get('/ldp', headers={'Accept': mimetype})
  518. assert rsp.mimetype == fmt
  519. gr = Graph(identifier=g.webroot + '/').parse(
  520. data=rsp.data, format=fmt)
  521. assert nsc['fcrepo'].RepositoryRoot in set(gr.objects())
  522. def test_provided_rdf(self):
  523. """
  524. Test several input RDF serialiation formats.
  525. """
  526. self.client.get('/ldp')
  527. gr = Graph()
  528. gr.add((
  529. URIRef(g.webroot + '/test_mimetype'),
  530. nsc['dcterms'].title, Literal('Test MIME type.')))
  531. test_list = {
  532. 'application/n-triples',
  533. 'application/rdf+xml',
  534. 'text/n3',
  535. 'text/turtle',
  536. 'application/ld+json',
  537. }
  538. for mimetype in test_list:
  539. rdf_data = gr.serialize(format=mimetype)
  540. self.client.put('/ldp/test_mimetype', data=rdf_data, headers={
  541. 'content-type': mimetype})
  542. rsp = self.client.get('/ldp/test_mimetype')
  543. rsp_gr = Graph(identifier=g.webroot + '/test_mimetype').parse(
  544. data=rsp.data, format='text/turtle')
  545. assert (
  546. URIRef(g.webroot + '/test_mimetype'),
  547. nsc['dcterms'].title, Literal('Test MIME type.')) in rsp_gr
  548. @pytest.mark.usefixtures('client_class')
  549. class TestDigestHeaders:
  550. """
  551. Test Digest and ETag headers.
  552. """
  553. def test_etag_digest(self):
  554. """
  555. Verify ETag and Digest headers on creation.
  556. The headers must correspond to the SHA1 checksum of the binary content.
  557. """
  558. uid = '/test_etag1'
  559. path = '/ldp' + uid
  560. content = uuid4().bytes
  561. content_cksum = sha1(content)
  562. put_rsp = self.client.put(
  563. path, data=content, headers={'content-type': 'text/plain'})
  564. assert content_cksum.hexdigest() in \
  565. put_rsp.headers.get('etag').split(',')
  566. assert put_rsp.headers.get('digest') == \
  567. 'SHA1=' + b64encode(content_cksum.digest()).decode()
  568. get_rsp = self.client.get(path)
  569. assert content_cksum.hexdigest() in \
  570. put_rsp.headers.get('etag').split(',')
  571. assert get_rsp.headers.get('digest') == \
  572. 'SHA1=' + b64encode(content_cksum.digest()).decode()
  573. def test_etag_ident(self):
  574. """
  575. Verify that two resources with the same content yield identical ETags.
  576. """
  577. path1 = f'/ldp/{uuid4()}'
  578. path2 = f'/ldp/{uuid4()}'
  579. content = uuid4().bytes
  580. content_cksum = sha1(content)
  581. self.client.put(
  582. path1, data=content, headers={'content-type': 'text/plain'})
  583. self.client.put(
  584. path2, data=content, headers={'content-type': 'text/plain'})
  585. get_rsp1 = self.client.get(path1)
  586. get_rsp2 = self.client.get(path2)
  587. assert get_rsp1.headers.get('etag') == get_rsp2.headers.get('etag')
  588. assert get_rsp1.headers.get('digest') == get_rsp2.headers.get('digest')
  589. def test_etag_diff(self):
  590. """
  591. Verify that two resources with different content yield different ETags.
  592. """
  593. path1 = f'/ldp/{uuid4()}'
  594. path2 = f'/ldp/{uuid4()}'
  595. content1 = b'some interesting content.'
  596. content_cksum1 = sha1(content1)
  597. content2 = b'Some great content.'
  598. content_cksum2 = sha1(content2)
  599. self.client.put(
  600. path1, data=content1, headers={'content-type': 'text/plain'})
  601. self.client.put(
  602. path2, data=content2, headers={'content-type': 'text/plain'})
  603. get_rsp1 = self.client.get(path1)
  604. get_rsp2 = self.client.get(path2)
  605. assert get_rsp1.headers.get('etag') != get_rsp2.headers.get('etag')
  606. assert get_rsp1.headers.get('digest') != get_rsp2.headers.get('digest')
  607. def test_etag_update(self):
  608. """
  609. Verify that ETag and digest change when the resource is updated.
  610. The headers should NOT change if the same binary content is
  611. re-submitted.
  612. """
  613. path = f'/ldp/{uuid4()}'
  614. content1 = uuid4().bytes
  615. content_cksum1 = sha1(content1)
  616. content2 = uuid4().bytes
  617. content_cksum2 = sha1(content2)
  618. self.client.put(
  619. path, data=content1, headers={'content-type': 'text/plain'})
  620. get_rsp = self.client.get(path)
  621. assert content_cksum1.hexdigest() == \
  622. get_rsp.headers.get('etag').strip('"')
  623. assert get_rsp.headers.get('digest') == \
  624. 'SHA1=' + b64encode(content_cksum1.digest()).decode()
  625. put_rsp = self.client.put(
  626. path, data=content2, headers={'content-type': 'text/plain'})
  627. assert content_cksum2.hexdigest() == \
  628. put_rsp.headers.get('etag').strip('"')
  629. assert put_rsp.headers.get('digest') == \
  630. 'SHA1=' + b64encode(content_cksum2.digest()).decode()
  631. get_rsp = self.client.get(path)
  632. assert content_cksum2.hexdigest() == \
  633. get_rsp.headers.get('etag').strip('"')
  634. assert get_rsp.headers.get('digest') == \
  635. 'SHA1=' + b64encode(content_cksum2.digest()).decode()
  636. def test_etag_rdf(self):
  637. """
  638. Verify that LDP-RS resources don't get an ETag.
  639. TODO This is by design for now; when a reliable hashing method
  640. for a graph is devised, this test should change.
  641. """
  642. path = f'/ldp/{uuid4()}'
  643. put_rsp = self.client.put(path)
  644. assert not put_rsp.headers.get('etag')
  645. assert not put_rsp.headers.get('digest')
  646. get_rsp = self.client.get(path)
  647. assert not get_rsp.headers.get('etag')
  648. assert not get_rsp.headers.get('digest')
  649. def test_digest_put(self):
  650. """
  651. Test the ``Digest`` header with PUT to verify content integrity.
  652. """
  653. path1 = f'/ldp/{uuid4()}'
  654. path2 = f'/ldp/{uuid4()}'
  655. path3 = f'/ldp/{uuid4()}'
  656. content = uuid4().bytes
  657. content_sha1 = sha1(content).hexdigest()
  658. content_sha256 = sha256(content).hexdigest()
  659. content_blake2b = blake2b(content).hexdigest()
  660. assert self.client.put(path1, data=content, headers={
  661. 'digest': 'sha1=abcd'}).status_code == 409
  662. assert self.client.put(path1, data=content, headers={
  663. 'digest': f'sha1={content_sha1}'}).status_code == 201
  664. assert self.client.put(path2, data=content, headers={
  665. 'digest': f'SHA1={content_sha1}'}).status_code == 201
  666. assert self.client.put(path3, data=content, headers={
  667. 'digest': f'SHA256={content_sha256}'}).status_code == 201
  668. assert self.client.put(path3, data=content, headers={
  669. 'digest': f'blake2b={content_blake2b}'}).status_code == 204
  670. def test_digest_post(self):
  671. """
  672. Test the ``Digest`` header with POST to verify content integrity.
  673. """
  674. path = '/ldp'
  675. content = uuid4().bytes
  676. content_sha1 = sha1(content).hexdigest()
  677. content_sha256 = sha256(content).hexdigest()
  678. content_blake2b = blake2b(content).hexdigest()
  679. assert self.client.post(path, data=content, headers={
  680. 'digest': 'sha1=abcd'}).status_code == 409
  681. assert self.client.post(path, data=content, headers={
  682. 'digest': f'sha1={content_sha1}'}).status_code == 201
  683. assert self.client.post(path, data=content, headers={
  684. 'digest': f'SHA1={content_sha1}'}).status_code == 201
  685. assert self.client.post(path, data=content, headers={
  686. 'digest': f'SHA256={content_sha256}'}).status_code == 201
  687. assert self.client.post(path, data=content, headers={
  688. 'digest': f'blake2b={content_blake2b}'}).status_code == 201
  689. @pytest.mark.usefixtures('client_class')
  690. class TestETagCondHeaders:
  691. """
  692. Test Digest and ETag headers.
  693. """
  694. def test_if_match_get(self):
  695. """
  696. Test the If-Match header on GET requests.
  697. Test providing single and multiple ETags.
  698. """
  699. path = '/ldp/test_if_match1'
  700. content = uuid4().bytes
  701. content_cksum = sha1(content).hexdigest()
  702. bogus_cksum = uuid4().hex
  703. self.client.put(
  704. path, data=content, headers={'content-type': 'text/plain'})
  705. get_rsp = self.client.get(path, headers={
  706. 'if-match': f'"{content_cksum}"'})
  707. assert get_rsp.status_code == 200
  708. get_rsp = self.client.get(path, headers={
  709. 'if-match': f'"{bogus_cksum}"'})
  710. assert get_rsp.status_code == 412
  711. get_rsp = self.client.get(path, headers={
  712. 'if-match': f'"{content_cksum}", "{bogus_cksum}"'})
  713. assert get_rsp.status_code == 200
  714. def test_if_match_put(self):
  715. """
  716. Test the If-Match header on PUT requests.
  717. Test providing single and multiple ETags.
  718. """
  719. path = '/ldp/test_if_match1'
  720. content = uuid4().bytes
  721. content_cksum = sha1(content).hexdigest()
  722. bogus_cksum = uuid4().hex
  723. get_rsp = self.client.get(path)
  724. old_cksum = get_rsp.headers.get('etag')
  725. put_rsp = self.client.put(path, data=content, headers={
  726. 'if-match': f'"{content_cksum}"'})
  727. assert put_rsp.status_code == 412
  728. put_rsp = self.client.put(path, data=content, headers={
  729. 'if-match': f'"{content_cksum}", "{bogus_cksum}"'})
  730. assert put_rsp.status_code == 412
  731. put_rsp = self.client.put(path, data=content, headers={
  732. 'if-match': f'"{old_cksum}", "{bogus_cksum}"'})
  733. assert put_rsp.status_code == 204
  734. # Now contents have changed.
  735. put_rsp = self.client.put(path, data=content, headers={
  736. 'if-match': f'"{old_cksum}"'})
  737. assert put_rsp.status_code == 412
  738. put_rsp = self.client.put(path, data=content, headers={
  739. 'if-match': f'"{content_cksum}"'})
  740. assert put_rsp.status_code == 204
  741. # Exactly the same content was uploaded, so the ETag should not have
  742. # changed.
  743. put_rsp = self.client.put(path, data=content, headers={
  744. 'if-match': f'"{content_cksum}"'})
  745. assert put_rsp.status_code == 204
  746. # Catch-all: Proceed if resource exists at the given location.
  747. put_rsp = self.client.put(path, data=content, headers={
  748. 'if-match': '*'})
  749. assert put_rsp.status_code == 204
  750. # This is wrong syntax. It will not update because the literal asterisk
  751. # won't match.
  752. put_rsp = self.client.put(path, data=content, headers={
  753. 'if-match': '"*"'})
  754. assert put_rsp.status_code == 412
  755. # Test delete.
  756. del_rsp = self.client.delete(path, headers={
  757. 'if-match': f'"{old_cksum}"', 'Prefer': 'no-tombstone'})
  758. assert del_rsp.status_code == 412
  759. del_rsp = self.client.delete(path, headers={
  760. 'if-match': f'"{content_cksum}"', 'Prefer': 'no-tombstone'})
  761. assert del_rsp.status_code == 204
  762. put_rsp = self.client.put(path, data=content, headers={
  763. 'if-match': '*'})
  764. assert put_rsp.status_code == 412
  765. def test_if_none_match_get(self):
  766. """
  767. Test the If-None-Match header on GET requests.
  768. Test providing single and multiple ETags.
  769. """
  770. path = '/ldp/test_if_none_match1'
  771. content = uuid4().bytes
  772. content_cksum = sha1(content).hexdigest()
  773. bogus_cksum = uuid4().hex
  774. self.client.put(
  775. path, data=content, headers={'content-type': 'text/plain'})
  776. get_rsp1 = self.client.get(path, headers={
  777. 'if-none-match': f'"{content_cksum}"'})
  778. assert get_rsp1.status_code == 304
  779. get_rsp2 = self.client.get(path, headers={
  780. 'if-none-match': f'"{bogus_cksum}"'})
  781. assert get_rsp2.status_code == 200
  782. get_rsp3 = self.client.get(path, headers={
  783. 'if-none-match': f'"{content_cksum}", "{bogus_cksum}"'})
  784. assert get_rsp3.status_code == 304
  785. # 404 has precedence on ETag handling.
  786. get_rsp = self.client.get('/ldp/bogus', headers={
  787. 'if-none-match': f'"{bogus_cksum}"'})
  788. assert get_rsp.status_code == 404
  789. get_rsp = self.client.get('/ldp/bogus', headers={
  790. 'if-none-match': f'"{content_cksum}"'})
  791. assert get_rsp.status_code == 404
  792. def test_if_none_match_put(self):
  793. """
  794. Test the If-None-Match header on PUT requests.
  795. Test providing single and multiple ETags.
  796. Uses a previously created resource.
  797. """
  798. path = '/ldp/test_if_none_match1'
  799. content = uuid4().bytes
  800. content_cksum = sha1(content).hexdigest()
  801. bogus_cksum = uuid4().hex
  802. get_rsp = self.client.get(path)
  803. old_cksum = get_rsp.headers.get('etag')
  804. put_rsp = self.client.put(path, data=content, headers={
  805. 'if-none-match': f'"{old_cksum}"'})
  806. assert put_rsp.status_code == 412
  807. put_rsp = self.client.put(path, data=content, headers={
  808. 'if-none-match': f'"{old_cksum}", "{bogus_cksum}"'})
  809. assert put_rsp.status_code == 412
  810. put_rsp = self.client.put(path, data=content, headers={
  811. 'if-none-match': f'"{bogus_cksum}"'})
  812. assert put_rsp.status_code == 204
  813. # Now contents have changed.
  814. put_rsp = self.client.put(path, data=content, headers={
  815. 'if-none-match': f'"{content_cksum}"'})
  816. assert put_rsp.status_code == 412
  817. put_rsp = self.client.put(path, data=content, headers={
  818. 'if-none-match': f'"{old_cksum}"'})
  819. assert put_rsp.status_code == 204
  820. # Catch-all: fail if any resource exists at the given location.
  821. put_rsp = self.client.put(path, data=content, headers={
  822. 'if-none-match': '*'})
  823. assert put_rsp.status_code == 412
  824. # Test delete.
  825. del_rsp = self.client.delete(path, headers={
  826. 'if-none-match': f'"{content_cksum}"', 'Prefer': 'no-tombstone'})
  827. assert del_rsp.status_code == 412
  828. del_rsp = self.client.delete(path, headers={
  829. 'if-none-match': f'"{bogus_cksum}"', 'Prefer': 'no-tombstone'})
  830. assert del_rsp.status_code == 204
  831. put_rsp = self.client.put(path, data=content, headers={
  832. 'if-none-match': '*'})
  833. assert put_rsp.status_code == 201
  834. # This is wrong syntax. It will update because the literal asterisk
  835. # won't match.
  836. put_rsp = self.client.put(path, data=content, headers={
  837. 'if-none-match': '"*"'})
  838. assert put_rsp.status_code == 204
  839. def test_etag_notfound(self):
  840. """
  841. Verify that 404 and 410 have precedence on ETag handling.
  842. """
  843. path = f'/ldp/{uuid4()}'
  844. bogus_cksum = uuid4().hex
  845. get_rsp = self.client.get(path, headers={
  846. 'if-match': f'"{bogus_cksum}"'})
  847. assert get_rsp.status_code == 404
  848. get_rsp = self.client.get(path, headers={
  849. 'if-match': '*'})
  850. assert get_rsp.status_code == 404
  851. get_rsp = self.client.get(path, headers={
  852. 'if-none-match': f'"{bogus_cksum}"'})
  853. assert get_rsp.status_code == 404
  854. self.client.put(path)
  855. self.client.delete(path)
  856. get_rsp = self.client.get(path, headers={
  857. 'if-match': f'"{bogus_cksum}"'})
  858. assert get_rsp.status_code == 410
  859. get_rsp = self.client.get(path, headers={
  860. 'if-none-match': f'"{bogus_cksum}"'})
  861. assert get_rsp.status_code == 410
  862. get_rsp = self.client.get(path, headers={
  863. 'if-match': '*'})
  864. assert get_rsp.status_code == 410
  865. @pytest.mark.usefixtures('client_class')
  866. class TestModifyTimeCondHeaders:
  867. """
  868. Test time-related conditional headers.
  869. """
  870. @pytest.fixture(scope='class')
  871. def timeframe(self):
  872. """
  873. Times used in these tests: UTC midnight of today, yesterday, tomorrow.
  874. """
  875. today = arrow.utcnow().floor('day')
  876. yesterday = today.shift(days=-1)
  877. tomorrow = today.shift(days=1)
  878. path = f'/ldp/{uuid4()}'
  879. self.client.put(path)
  880. return path, today, yesterday, tomorrow
  881. def test_nothing(self):
  882. """
  883. For some reason, without this the fixture won't initialize properly.
  884. """
  885. self.client.get('/')
  886. def test_if_modified_since(self, timeframe):
  887. """
  888. Test various uses of the If-Modified-Since header.
  889. """
  890. path, today, yesterday, tomorrow = timeframe
  891. assert self.client.head(
  892. path, headers={'if-modified-since': http_date(today.timestamp)}
  893. ).status_code == 200
  894. assert self.client.get(
  895. path, headers={'if-modified-since': http_date(today.timestamp)}
  896. ).status_code == 200
  897. assert self.client.head(
  898. path, headers={'if-modified-since': http_date(yesterday.timestamp)}
  899. ).status_code == 200
  900. assert self.client.get(
  901. path, headers={'if-modified-since': http_date(yesterday.timestamp)}
  902. ).status_code == 200
  903. assert self.client.head(
  904. path, headers={'if-modified-since': http_date(tomorrow.timestamp)}
  905. ).status_code == 304
  906. assert self.client.get(
  907. path, headers={'if-modified-since': http_date(tomorrow.timestamp)}
  908. ).status_code == 304
  909. def test_if_unmodified_since(self, timeframe):
  910. """
  911. Test various uses of the If-Unmodified-Since header.
  912. """
  913. path, today, yesterday, tomorrow = timeframe
  914. assert self.client.head(
  915. path, headers={'if-unmodified-since': http_date(today.timestamp)}
  916. ).status_code == 304
  917. assert self.client.get(
  918. path, headers={'if-unmodified-since': http_date(today.timestamp)}
  919. ).status_code == 304
  920. assert self.client.head(
  921. path, headers={'if-unmodified-since': http_date(yesterday.timestamp)}
  922. ).status_code == 304
  923. assert self.client.get(
  924. path, headers={'if-unmodified-since': http_date(yesterday.timestamp)}
  925. ).status_code == 304
  926. assert self.client.head(
  927. path, headers={'if-unmodified-since': http_date(tomorrow.timestamp)}
  928. ).status_code == 200
  929. assert self.client.get(
  930. path, headers={'if-unmodified-since': http_date(tomorrow.timestamp)}
  931. ).status_code == 200
  932. def test_time_range(self, timeframe):
  933. """
  934. Test conditions inside and outside of a time range.
  935. """
  936. path, today, yesterday, tomorrow = timeframe
  937. # Send me the resource if it has been modified between yesterday
  938. # and tomorrow.
  939. assert self.client.get(path, headers={
  940. 'if-modified-since': http_date(yesterday.timestamp),
  941. 'if-unmodified-since': http_date(tomorrow.timestamp),
  942. }).status_code == 200
  943. # Send me the resource if it has been modified between today
  944. # and tomorrow.
  945. assert self.client.get(path, headers={
  946. 'if-modified-since': http_date(today.timestamp),
  947. 'if-unmodified-since': http_date(tomorrow.timestamp),
  948. }).status_code == 200
  949. # Send me the resource if it has been modified between yesterday
  950. # and today.
  951. assert self.client.get(path, headers={
  952. 'if-modified-since': http_date(yesterday.timestamp),
  953. 'if-unmodified-since': http_date(today.timestamp),
  954. }).status_code == 304
  955. # Send me the resource if it has been modified between two days ago
  956. # and yesterday.
  957. assert self.client.get(path, headers={
  958. 'if-modified-since': http_date(yesterday.shift(days=-1).timestamp),
  959. 'if-unmodified-since': http_date(yesterday.timestamp),
  960. }).status_code == 304
  961. # Send me the resource if it has been modified between tomorrow
  962. # and two days from today.
  963. assert self.client.get(path, headers={
  964. 'if-modified-since': http_date(tomorrow.timestamp),
  965. 'if-unmodified-since': http_date(tomorrow.shift(days=1).timestamp),
  966. }).status_code == 304
  967. def test_time_etag_combo(self, timeframe):
  968. """
  969. Test evaluation priorities among ETag and time headers.
  970. """
  971. _, today, yesterday, tomorrow = timeframe
  972. path = f'/ldp/{uuid4()}'
  973. content = uuid4().bytes
  974. content_cksum = sha1(content).hexdigest()
  975. bogus_cksum = uuid4().hex
  976. self.client.put(
  977. path, data=content, headers={'content-type': 'text/plain'})
  978. # Negative ETag match wins.
  979. assert self.client.get(path, headers={
  980. 'if-match': f'"{bogus_cksum}"',
  981. 'if-modified-since': http_date(yesterday.timestamp),
  982. }).status_code == 412
  983. assert self.client.get(path, headers={
  984. 'if-match': f'"{bogus_cksum}"',
  985. 'if-unmodified-since': http_date(tomorrow.timestamp),
  986. }).status_code == 412
  987. assert self.client.get(path, headers={
  988. 'if-none-match': f'"{content_cksum}"',
  989. 'if-modified-since': http_date(yesterday.timestamp),
  990. }).status_code == 304
  991. assert self.client.get(path, headers={
  992. 'if-none-match': f'"{content_cksum}"',
  993. 'if-unmodified-since': http_date(tomorrow.timestamp),
  994. }).status_code == 304
  995. # Positive ETag match wins.
  996. assert self.client.get(path, headers={
  997. 'if-match': f'"{content_cksum}"',
  998. 'if-unmodified-since': http_date(yesterday.timestamp),
  999. }).status_code == 200
  1000. assert self.client.get(path, headers={
  1001. 'if-match': f'"{content_cksum}"',
  1002. 'if-modified-since': http_date(tomorrow.timestamp),
  1003. }).status_code == 200
  1004. assert self.client.get(path, headers={
  1005. 'if-none-match': f'"{bogus_cksum}"',
  1006. 'if-unmodified-since': http_date(yesterday.timestamp),
  1007. }).status_code == 200
  1008. assert self.client.get(path, headers={
  1009. 'if-none-match': f'"{bogus_cksum}"',
  1010. 'if-modified-since': http_date(tomorrow.timestamp),
  1011. }).status_code == 200
  1012. @pytest.mark.usefixtures('client_class')
  1013. class TestRange:
  1014. """
  1015. Test byte range retrieval.
  1016. This should not need too deep testing since it's functionality implemented
  1017. in Werkzeug/Flask.
  1018. """
  1019. @pytest.fixture(scope='class')
  1020. def bytestream(self):
  1021. """
  1022. Create a sample bytestream with predictable (8x8 bytes) content.
  1023. """
  1024. return b''.join([bytes([n] * 8) for n in range(8)])
  1025. def test_get_range(self, bytestream):
  1026. """
  1027. Get different ranges of the bitstream.
  1028. """
  1029. path = '/ldp/test_range'
  1030. self.client.put(path, data=bytestream)
  1031. # First 8 bytes.
  1032. assert self.client.get(
  1033. path, headers={'range': 'bytes=0-7'}).data == b'\x00' * 8
  1034. # Last 4 bytes of first block, first 4 of second block.
  1035. assert self.client.get(
  1036. path, headers={'range': 'bytes=4-11'}
  1037. ).data == b'\x00' * 4 + b'\x01' * 4
  1038. # Last 8 bytes.
  1039. assert self.client.get(
  1040. path, headers={'range': 'bytes=56-'}).data == b'\x07' * 8
  1041. def test_fail_ranges(self, bytestream):
  1042. """
  1043. Test malformed or unsupported ranges.
  1044. """
  1045. path = '/ldp/test_range'
  1046. # TODO This shall be a 206 when multiple ranges are supported.
  1047. fail_rsp = self.client.get(path, headers={'range': 'bytes=0-1, 7-8'})
  1048. assert fail_rsp.status_code == 501
  1049. # Bad ranges will be ignored.
  1050. for rng in ((10, 4), ('', 3), (3600, 6400)):
  1051. bad_rsp = self.client.get(
  1052. path, headers={'range': 'bytes={rng[0]}-{rng[1]}'})
  1053. assert bad_rsp.status_code == 200
  1054. assert bad_rsp.data == bytestream
  1055. assert int(bad_rsp.headers['content-length']) == len(bytestream)
  1056. def test_range_rsp_headers(self, bytestream):
  1057. """
  1058. Test various headers for a ranged response.
  1059. """
  1060. path = '/ldp/test_range'
  1061. start_b = 0
  1062. end_b = 7
  1063. full_rsp = self.client.get(path)
  1064. part_rsp = self.client.get(path, headers={
  1065. 'range': f'bytes={start_b}-{end_b}'})
  1066. for hdr_name in ['etag', 'digest', 'content-type']:
  1067. assert part_rsp.headers[hdr_name] == full_rsp.headers[hdr_name]
  1068. for hdr in part_rsp.headers['link']:
  1069. assert hdr in full_rsp.headers['link']
  1070. assert int(part_rsp.headers['content-length']) == end_b - start_b + 1
  1071. assert part_rsp.headers['content-range'] == \
  1072. f'bytes {start_b}-{end_b} / {len(bytestream)}'
  1073. @pytest.mark.usefixtures('client_class')
  1074. class TestPrefHeader:
  1075. """
  1076. Test various combinations of `Prefer` header.
  1077. """
  1078. @pytest.fixture(scope='class')
  1079. def cont_structure(self):
  1080. """
  1081. Create a container structure to be used for subsequent requests.
  1082. """
  1083. parent_path = '/ldp/test_parent'
  1084. self.client.put(parent_path)
  1085. self.client.put(parent_path + '/child1')
  1086. self.client.put(parent_path + '/child2')
  1087. self.client.put(parent_path + '/child3')
  1088. return {
  1089. 'path' : parent_path,
  1090. 'response' : self.client.get(parent_path),
  1091. }
  1092. def test_put_prefer_handling(self, random_uuid):
  1093. """
  1094. Trying to PUT an existing resource should:
  1095. - Return a 204 if the payload is empty
  1096. - Return a 204 if the payload is RDF, server-managed triples are
  1097. included and the 'Prefer' header is set to 'handling=lenient'
  1098. - Return a 412 (ServerManagedTermError) if the payload is RDF,
  1099. server-managed triples are included and handling is set to 'strict',
  1100. or not set.
  1101. """
  1102. path = '/ldp/put_pref_header01'
  1103. assert self.client.put(path).status_code == 201
  1104. assert self.client.get(path).status_code == 200
  1105. assert self.client.put(path).status_code == 204
  1106. # Default handling is strict.
  1107. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  1108. rsp_default = self.client.put(
  1109. path,
  1110. headers={
  1111. 'Content-Type' : 'text/turtle',
  1112. },
  1113. data=f
  1114. )
  1115. assert rsp_default.status_code == 412
  1116. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  1117. rsp_len = self.client.put(
  1118. path,
  1119. headers={
  1120. 'Prefer' : 'handling=lenient',
  1121. 'Content-Type' : 'text/turtle',
  1122. },
  1123. data=f
  1124. )
  1125. assert rsp_len.status_code == 204
  1126. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  1127. rsp_strict = self.client.put(
  1128. path,
  1129. headers={
  1130. 'Prefer' : 'handling=strict',
  1131. 'Content-Type' : 'text/turtle',
  1132. },
  1133. data=f
  1134. )
  1135. assert rsp_strict.status_code == 412
  1136. # @HOLD Embed children is debated.
  1137. def _disabled_test_embed_children(self, cont_structure):
  1138. """
  1139. verify the "embed children" prefer header.
  1140. """
  1141. self.client.get('/ldp')
  1142. parent_path = cont_structure['path']
  1143. cont_resp = cont_structure['response']
  1144. cont_subject = URIRef(g.webroot + '/test_parent')
  1145. #minimal_resp = self.client.get(parent_path, headers={
  1146. # 'Prefer' : 'return=minimal',
  1147. #})
  1148. incl_embed_children_resp = self.client.get(parent_path, headers={
  1149. 'Prefer' : 'return=representation; include={}'\
  1150. .format(Ldpr.EMBED_CHILD_RES_URI),
  1151. })
  1152. omit_embed_children_resp = self.client.get(parent_path, headers={
  1153. 'Prefer' : 'return=representation; omit={}'\
  1154. .format(Ldpr.EMBED_CHILD_RES_URI),
  1155. })
  1156. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  1157. incl_gr = Graph().parse(
  1158. data=incl_embed_children_resp.data, format='turtle')
  1159. omit_gr = Graph().parse(
  1160. data=omit_embed_children_resp.data, format='turtle')
  1161. assert isomorphic(omit_gr, default_gr)
  1162. children = set(incl_gr[cont_subject : nsc['ldp'].contains])
  1163. assert len(children) == 3
  1164. children = set(incl_gr[cont_subject : nsc['ldp'].contains])
  1165. for child_uri in children:
  1166. assert set(incl_gr[ child_uri : : ])
  1167. assert not set(omit_gr[ child_uri : : ])
  1168. def test_return_children(self, cont_structure):
  1169. """
  1170. verify the "return children" prefer header.
  1171. """
  1172. self.client.get('/ldp')
  1173. parent_path = cont_structure['path']
  1174. cont_resp = cont_structure['response']
  1175. cont_subject = URIRef(g.webroot + '/test_parent')
  1176. incl_children_resp = self.client.get(parent_path, headers={
  1177. 'Prefer' : 'return=representation; include={}'\
  1178. .format(Ldpr.RETURN_CHILD_RES_URI),
  1179. })
  1180. omit_children_resp = self.client.get(parent_path, headers={
  1181. 'Prefer' : 'return=representation; omit={}'\
  1182. .format(Ldpr.RETURN_CHILD_RES_URI),
  1183. })
  1184. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  1185. incl_gr = Graph().parse(data=incl_children_resp.data, format='turtle')
  1186. omit_gr = Graph().parse(data=omit_children_resp.data, format='turtle')
  1187. assert isomorphic(incl_gr, default_gr)
  1188. children = incl_gr[cont_subject : nsc['ldp'].contains]
  1189. for child_uri in children:
  1190. assert not omit_gr[cont_subject : nsc['ldp'].contains : child_uri]
  1191. def test_inbound_rel(self, cont_structure):
  1192. """
  1193. verify the "inbound relationships" prefer header.
  1194. """
  1195. self.client.put('/ldp/test_target')
  1196. data = '<> <http://ex.org/ns#shoots> <{}> .'.format(
  1197. g.webroot + '/test_target')
  1198. self.client.put('/ldp/test_shooter', data=data,
  1199. headers={'Content-Type': 'text/turtle'})
  1200. cont_resp = self.client.get('/ldp/test_target')
  1201. incl_inbound_resp = self.client.get('/ldp/test_target', headers={
  1202. 'Prefer' : 'return=representation; include="{}"'\
  1203. .format(Ldpr.RETURN_INBOUND_REF_URI),
  1204. })
  1205. omit_inbound_resp = self.client.get('/ldp/test_target', headers={
  1206. 'Prefer' : 'return=representation; omit="{}"'\
  1207. .format(Ldpr.RETURN_INBOUND_REF_URI),
  1208. })
  1209. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  1210. incl_gr = Graph().parse(data=incl_inbound_resp.data, format='turtle')
  1211. omit_gr = Graph().parse(data=omit_inbound_resp.data, format='turtle')
  1212. subject = URIRef(g.webroot + '/test_target')
  1213. inbd_subject = URIRef(g.webroot + '/test_shooter')
  1214. assert isomorphic(omit_gr, default_gr)
  1215. assert len(set(incl_gr[inbd_subject : : ])) == 1
  1216. assert incl_gr[
  1217. inbd_subject : URIRef('http://ex.org/ns#shoots') : subject]
  1218. assert not len(set(omit_gr[inbd_subject : :]))
  1219. def test_srv_mgd_triples(self, cont_structure):
  1220. """
  1221. verify the "server managed triples" prefer header.
  1222. """
  1223. self.client.get('/ldp')
  1224. parent_path = cont_structure['path']
  1225. cont_resp = cont_structure['response']
  1226. cont_subject = URIRef(g.webroot + '/test_parent')
  1227. incl_srv_mgd_resp = self.client.get(parent_path, headers={
  1228. 'Prefer' : 'return=representation; include={}'\
  1229. .format(Ldpr.RETURN_SRV_MGD_RES_URI),
  1230. })
  1231. omit_srv_mgd_resp = self.client.get(parent_path, headers={
  1232. 'Prefer' : 'return=representation; omit={}'\
  1233. .format(Ldpr.RETURN_SRV_MGD_RES_URI),
  1234. })
  1235. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  1236. incl_gr = Graph().parse(data=incl_srv_mgd_resp.data, format='turtle')
  1237. omit_gr = Graph().parse(data=omit_srv_mgd_resp.data, format='turtle')
  1238. assert isomorphic(incl_gr, default_gr)
  1239. for pred in {
  1240. nsc['fcrepo'].created,
  1241. nsc['fcrepo'].createdBy,
  1242. nsc['fcrepo'].lastModified,
  1243. nsc['fcrepo'].lastModifiedBy,
  1244. nsc['ldp'].contains,
  1245. }:
  1246. assert set(incl_gr[ cont_subject : pred : ])
  1247. assert not set(omit_gr[ cont_subject : pred : ])
  1248. for type in {
  1249. nsc['fcrepo'].Resource,
  1250. nsc['ldp'].Container,
  1251. nsc['ldp'].Resource,
  1252. }:
  1253. assert incl_gr[ cont_subject : RDF.type : type ]
  1254. assert not omit_gr[ cont_subject : RDF.type : type ]
  1255. def test_delete_no_tstone(self):
  1256. """
  1257. Test the `no-tombstone` Prefer option.
  1258. """
  1259. self.client.put('/ldp/test_delete_no_tstone01')
  1260. self.client.put('/ldp/test_delete_no_tstone01/a')
  1261. self.client.delete('/ldp/test_delete_no_tstone01', headers={
  1262. 'prefer' : 'no-tombstone'})
  1263. resp = self.client.get('/ldp/test_delete_no_tstone01')
  1264. assert resp.status_code == 404
  1265. child_resp = self.client.get('/ldp/test_delete_no_tstone01/a')
  1266. assert child_resp.status_code == 404
  1267. #@pytest.mark.usefixtures('client_class')
  1268. #@pytest.mark.usefixtures('db')
  1269. #class TestDigest:
  1270. # """
  1271. # Test digest and ETag handling.
  1272. # """
  1273. # @pytest.mark.skip(reason='TODO Need to implement async digest queue')
  1274. # def test_digest_post(self):
  1275. # """
  1276. # Test ``Digest`` and ``ETag`` headers on resource POST.
  1277. # """
  1278. # resp = self.client.post('/ldp/')
  1279. # assert 'Digest' in resp.headers
  1280. # assert 'ETag' in resp.headers
  1281. # assert (
  1282. # b64encode(bytes.fromhex(
  1283. # resp.headers['ETag'].replace('W/', '')
  1284. # )).decode('ascii') ==
  1285. # resp.headers['Digest'].replace('SHA256=', ''))
  1286. #
  1287. #
  1288. # @pytest.mark.skip(reason='TODO Need to implement async digest queue')
  1289. # def test_digest_put(self):
  1290. # """
  1291. # Test ``Digest`` and ``ETag`` headers on resource PUT.
  1292. # """
  1293. # resp_put = self.client.put('/ldp/test_digest_put')
  1294. # assert 'Digest' in resp_put.headers
  1295. # assert 'ETag' in resp_put.headers
  1296. # assert (
  1297. # b64encode(bytes.fromhex(
  1298. # resp_put.headers['ETag'].replace('W/', '')
  1299. # )).decode('ascii') ==
  1300. # resp_put.headers['Digest'].replace('SHA256=', ''))
  1301. #
  1302. # resp_get = self.client.get('/ldp/test_digest_put')
  1303. # assert 'Digest' in resp_get.headers
  1304. # assert 'ETag' in resp_get.headers
  1305. # assert (
  1306. # b64encode(bytes.fromhex(
  1307. # resp_get.headers['ETag'].replace('W/', '')
  1308. # )).decode('ascii') ==
  1309. # resp_get.headers['Digest'].replace('SHA256=', ''))
  1310. #
  1311. #
  1312. # @pytest.mark.skip(reason='TODO Need to implement async digest queue')
  1313. # def test_digest_patch(self):
  1314. # """
  1315. # Verify that the digest and ETag change on resource change.
  1316. # """
  1317. # path = '/ldp/test_digest_patch'
  1318. # self.client.put(path)
  1319. # rsp1 = self.client.get(path)
  1320. #
  1321. # self.client.patch(
  1322. # path, data=b'DELETE {} INSERT {<> a <http://ex.org/Test> .} '
  1323. # b'WHERE {}',
  1324. # headers={'Content-Type': 'application/sparql-update'})
  1325. # rsp2 = self.client.get(path)
  1326. #
  1327. # assert rsp1.headers['ETag'] != rsp2.headers['ETag']
  1328. # assert rsp1.headers['Digest'] != rsp2.headers['Digest']
  1329. @pytest.mark.usefixtures('client_class')
  1330. @pytest.mark.usefixtures('db')
  1331. class TestVersion:
  1332. """
  1333. Test version creation, retrieval and deletion.
  1334. """
  1335. def test_create_versions(self):
  1336. """
  1337. Test that POSTing multiple times to fcr:versions creates the
  1338. 'hasVersions' triple and yields multiple version snapshots.
  1339. """
  1340. self.client.put('/ldp/test_version')
  1341. create_rsp = self.client.post('/ldp/test_version/fcr:versions')
  1342. assert create_rsp.status_code == 201
  1343. rsrc_rsp = self.client.get('/ldp/test_version')
  1344. rsrc_gr = Graph().parse(data=rsrc_rsp.data, format='turtle')
  1345. assert len(set(rsrc_gr[: nsc['fcrepo'].hasVersions :])) == 1
  1346. info_rsp = self.client.get('/ldp/test_version/fcr:versions')
  1347. assert info_rsp.status_code == 200
  1348. info_gr = Graph().parse(data=info_rsp.data, format='turtle')
  1349. assert len(set(info_gr[: nsc['fcrepo'].hasVersion :])) == 1
  1350. self.client.post('/ldp/test_version/fcr:versions')
  1351. info2_rsp = self.client.get('/ldp/test_version/fcr:versions')
  1352. info2_gr = Graph().parse(data=info2_rsp.data, format='turtle')
  1353. assert len(set(info2_gr[: nsc['fcrepo'].hasVersion :])) == 2
  1354. def test_version_with_slug(self):
  1355. """
  1356. Test a version with a slug.
  1357. """
  1358. self.client.put('/ldp/test_version_slug')
  1359. create_rsp = self.client.post('/ldp/test_version_slug/fcr:versions',
  1360. headers={'slug' : 'v1'})
  1361. new_ver_uri = create_rsp.headers['Location']
  1362. assert new_ver_uri == g.webroot + '/test_version_slug/fcr:versions/v1'
  1363. info_rsp = self.client.get('/ldp/test_version_slug/fcr:versions')
  1364. info_gr = Graph().parse(data=info_rsp.data, format='turtle')
  1365. assert info_gr[
  1366. URIRef(new_ver_uri) :
  1367. nsc['fcrepo'].hasVersionLabel :
  1368. Literal('v1')]
  1369. def test_dupl_version(self):
  1370. """
  1371. Make sure that two POSTs with the same slug result in two different
  1372. versions.
  1373. """
  1374. path = '/ldp/test_duplicate_slug'
  1375. self.client.put(path)
  1376. v1_rsp = self.client.post(path + '/fcr:versions',
  1377. headers={'slug' : 'v1'})
  1378. v1_uri = v1_rsp.headers['Location']
  1379. dup_rsp = self.client.post(path + '/fcr:versions',
  1380. headers={'slug' : 'v1'})
  1381. dup_uri = dup_rsp.headers['Location']
  1382. assert v1_uri != dup_uri
  1383. def test_revert_version(self):
  1384. """
  1385. Take a version snapshot, update a resource, and then revert to the
  1386. previous vresion.
  1387. """
  1388. rsrc_path = '/ldp/test_revert_version'
  1389. payload1 = '<> <urn:demo:p1> <urn:demo:o1> .'
  1390. payload2 = '<> <urn:demo:p1> <urn:demo:o2> .'
  1391. self.client.put(rsrc_path, headers={
  1392. 'content-type': 'text/turtle'}, data=payload1)
  1393. self.client.post(
  1394. rsrc_path + '/fcr:versions', headers={'slug': 'v1'})
  1395. v1_rsp = self.client.get(rsrc_path)
  1396. v1_gr = Graph().parse(data=v1_rsp.data, format='turtle')
  1397. assert v1_gr[
  1398. URIRef(g.webroot + '/test_revert_version')
  1399. : URIRef('urn:demo:p1')
  1400. : URIRef('urn:demo:o1')
  1401. ]
  1402. self.client.put(rsrc_path, headers={
  1403. 'content-type': 'text/turtle'}, data=payload2)
  1404. v2_rsp = self.client.get(rsrc_path)
  1405. v2_gr = Graph().parse(data=v2_rsp.data, format='turtle')
  1406. assert v2_gr[
  1407. URIRef(g.webroot + '/test_revert_version')
  1408. : URIRef('urn:demo:p1')
  1409. : URIRef('urn:demo:o2')
  1410. ]
  1411. self.client.patch(rsrc_path + '/fcr:versions/v1')
  1412. revert_rsp = self.client.get(rsrc_path)
  1413. revert_gr = Graph().parse(data=revert_rsp.data, format='turtle')
  1414. assert revert_gr[
  1415. URIRef(g.webroot + '/test_revert_version')
  1416. : URIRef('urn:demo:p1')
  1417. : URIRef('urn:demo:o1')
  1418. ]
  1419. def test_resurrection(self):
  1420. """
  1421. Delete and then resurrect a resource.
  1422. Make sure that the resource is resurrected to the latest version.
  1423. """
  1424. path = '/ldp/test_lazarus'
  1425. self.client.put(path)
  1426. self.client.post(path + '/fcr:versions', headers={'slug': 'v1'})
  1427. self.client.put(
  1428. path, headers={'content-type': 'text/turtle'},
  1429. data=b'<> <urn:demo:p1> <urn:demo:o1> .')
  1430. self.client.post(path + '/fcr:versions', headers={'slug': 'v2'})
  1431. self.client.put(
  1432. path, headers={'content-type': 'text/turtle'},
  1433. data=b'<> <urn:demo:p1> <urn:demo:o2> .')
  1434. self.client.delete(path)
  1435. assert self.client.get(path).status_code == 410
  1436. self.client.post(path + '/fcr:tombstone')
  1437. laz_data = self.client.get(path).data
  1438. laz_gr = Graph().parse(data=laz_data, format='turtle')
  1439. assert laz_gr[
  1440. URIRef(g.webroot + '/test_lazarus')
  1441. : URIRef('urn:demo:p1')
  1442. : URIRef('urn:demo:o2')
  1443. ]