test_3_0_ldp.py 63 KB

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