test_ldp.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. import pdb
  2. import pytest
  3. import uuid
  4. from hashlib import sha1
  5. from flask import g
  6. from rdflib import Graph
  7. from rdflib.compare import isomorphic
  8. from rdflib.namespace import RDF
  9. from rdflib.term import Literal, URIRef
  10. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  11. from lakesuperior.model.ldpr import Ldpr
  12. @pytest.fixture(scope='module')
  13. def random_uuid():
  14. return str(uuid.uuid4())
  15. @pytest.mark.usefixtures('client_class')
  16. @pytest.mark.usefixtures('db')
  17. class TestLdp:
  18. '''
  19. Test HTTP interaction with LDP endpoint.
  20. '''
  21. def test_get_root_node(self):
  22. '''
  23. Get the root node from two different endpoints.
  24. The test triplestore must be initialized, hence the `db` fixture.
  25. '''
  26. ldp_resp = self.client.get('/ldp')
  27. rest_resp = self.client.get('/rest')
  28. assert ldp_resp.status_code == 200
  29. assert rest_resp.status_code == 200
  30. def test_put_empty_resource(self, random_uuid):
  31. '''
  32. Check response headers for a PUT operation with empty payload.
  33. '''
  34. resp = self.client.put('/ldp/new_resource')
  35. assert resp.status_code == 201
  36. assert resp.data == bytes(
  37. '{}/new_resource'.format(g.webroot), 'utf-8')
  38. def test_put_existing_resource(self, random_uuid):
  39. '''
  40. Trying to PUT an existing resource should return a 204 if the payload
  41. is empty.
  42. '''
  43. path = '/ldp/nonidempotent01'
  44. put1_resp = self.client.put(path)
  45. assert put1_resp.status_code == 201
  46. assert self.client.get(path).status_code == 200
  47. put2_resp = self.client.put(path)
  48. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  49. put2_resp = self.client.put(
  50. path, data=f, content_type='text/turtle')
  51. assert put2_resp.status_code == 204
  52. put2_resp = self.client.put(path)
  53. assert put2_resp.status_code == 409
  54. def test_put_tree(self, client):
  55. '''
  56. PUT a resource with several path segments.
  57. The test should create intermediate path segments that are LDPCs,
  58. accessible to PUT or POST.
  59. '''
  60. path = '/ldp/test_tree/a/b/c/d/e/f/g'
  61. self.client.put(path)
  62. assert self.client.get(path).status_code == 200
  63. assert self.client.get('/ldp/test_tree/a/b/c').status_code == 200
  64. assert self.client.post('/ldp/test_tree/a/b').status_code == 201
  65. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  66. put_int_resp = self.client.put(
  67. 'ldp/test_tree/a', data=f, content_type='text/turtle')
  68. assert put_int_resp.status_code == 204
  69. # @TODO More thorough testing of contents
  70. def test_put_nested_tree(self, client):
  71. '''
  72. Verify that containment is set correctly in nested hierarchies.
  73. First put a new hierarchy and verify that the root node is its
  74. container; then put another hierarchy under it and verify that the
  75. first hierarchy is the container of the second one.
  76. '''
  77. uuid1 = 'test_nested_tree/a/b/c/d'
  78. uuid2 = uuid1 + '/e/f/g'
  79. path1 = '/ldp/' + uuid1
  80. path2 = '/ldp/' + uuid2
  81. self.client.put(path1)
  82. cont1_data = self.client.get('/ldp').data
  83. gr1 = Graph().parse(data=cont1_data, format='turtle')
  84. assert gr1[ URIRef(g.webroot + '/') : nsc['ldp'].contains : \
  85. URIRef(g.webroot + '/test_nested_tree') ]
  86. self.client.put(path2)
  87. cont2_data = self.client.get(path1).data
  88. gr2 = Graph().parse(data=cont2_data, format='turtle')
  89. assert gr2[ URIRef(g.webroot + '/' + uuid1) : \
  90. nsc['ldp'].contains : \
  91. URIRef(g.webroot + '/' + uuid1 + '/e') ]
  92. def test_put_ldp_rs(self, client):
  93. '''
  94. PUT a resource with RDF payload and verify.
  95. '''
  96. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  97. self.client.put('/ldp/ldprs01', data=f, content_type='text/turtle')
  98. resp = self.client.get('/ldp/ldprs01',
  99. headers={'accept' : 'text/turtle'})
  100. assert resp.status_code == 200
  101. gr = Graph().parse(data=resp.data, format='text/turtle')
  102. assert URIRef('http://vocab.getty.edu/ontology#Subject') in \
  103. gr.objects(None, RDF.type)
  104. def test_put_ldp_nr(self, rnd_img):
  105. '''
  106. PUT a resource with binary payload and verify checksums.
  107. '''
  108. rnd_img['content'].seek(0)
  109. resp = self.client.put('/ldp/ldpnr01', data=rnd_img['content'],
  110. headers={
  111. 'Content-Type': 'image/png',
  112. 'Content-Disposition' : 'attachment; filename={}'.format(
  113. rnd_img['filename'])})
  114. assert resp.status_code == 201
  115. resp = self.client.get(
  116. '/ldp/ldpnr01', headers={'accept' : 'image/png'})
  117. assert resp.status_code == 200
  118. assert sha1(resp.data).hexdigest() == rnd_img['hash']
  119. def test_put_ldp_nr_multipart(self, rnd_img):
  120. '''
  121. PUT a resource with a multipart/form-data payload.
  122. '''
  123. rnd_img['content'].seek(0)
  124. resp = self.client.put(
  125. '/ldp/ldpnr02',
  126. data={
  127. 'file': (
  128. rnd_img['content'], rnd_img['filename'],
  129. 'image/png',
  130. )
  131. }
  132. )
  133. assert resp.status_code == 201
  134. resp = self.client.get(
  135. '/ldp/ldpnr02', headers={'accept' : 'image/png'})
  136. assert resp.status_code == 200
  137. assert sha1(resp.data).hexdigest() == rnd_img['hash']
  138. def test_put_mismatched_ldp_rs(self, rnd_img):
  139. '''
  140. Verify MIME type / LDP mismatch.
  141. PUT a LDP-RS, then PUT a LDP-NR on the same location and verify it
  142. fails.
  143. '''
  144. path = '/ldp/' + str(uuid.uuid4())
  145. rnd_img['content'].seek(0)
  146. ldp_nr_resp = self.client.put(path, data=rnd_img['content'],
  147. headers={
  148. 'Content-Disposition' : 'attachment; filename={}'.format(
  149. rnd_img['filename'])})
  150. assert ldp_nr_resp.status_code == 201
  151. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  152. ldp_rs_resp = self.client.put(path, data=f,
  153. content_type='text/turtle')
  154. assert ldp_rs_resp.status_code == 415
  155. def test_put_mismatched_ldp_nr(self, rnd_img):
  156. '''
  157. Verify MIME type / LDP mismatch.
  158. PUT a LDP-NR, then PUT a LDP-RS on the same location and verify it
  159. fails.
  160. '''
  161. path = '/ldp/' + str(uuid.uuid4())
  162. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  163. ldp_rs_resp = self.client.put(path, data=f,
  164. content_type='text/turtle')
  165. assert ldp_rs_resp.status_code == 201
  166. rnd_img['content'].seek(0)
  167. ldp_nr_resp = self.client.put(path, data=rnd_img['content'],
  168. headers={
  169. 'Content-Disposition' : 'attachment; filename={}'.format(
  170. rnd_img['filename'])})
  171. assert ldp_nr_resp.status_code == 415
  172. def test_missing_reference(self, client):
  173. '''
  174. PUT a resource with RDF payload referencing a non-existing in-repo
  175. resource.
  176. '''
  177. self.client.get('/ldp')
  178. data = '''
  179. PREFIX ns: <http://example.org#>
  180. PREFIX res: <http://example-source.org/res/>
  181. <> ns:p1 res:bogus ;
  182. ns:p2 <{0}> ;
  183. ns:p3 <{0}/> ;
  184. ns:p4 <{0}/nonexistent> .
  185. '''.format(g.webroot)
  186. put_rsp = self.client.put('/ldp/test_missing_ref', data=data, headers={
  187. 'content-type': 'text/turtle'})
  188. assert put_rsp.status_code == 201
  189. resp = self.client.get('/ldp/test_missing_ref',
  190. headers={'accept' : 'text/turtle'})
  191. assert resp.status_code == 200
  192. gr = Graph().parse(data=resp.data, format='text/turtle')
  193. assert URIRef('http://example-source.org/res/bogus') in \
  194. gr.objects(None, URIRef('http://example.org#p1'))
  195. assert URIRef(g.webroot + '/') in (
  196. gr.objects(None, URIRef('http://example.org#p2')))
  197. assert URIRef(g.webroot + '/') in (
  198. gr.objects(None, URIRef('http://example.org#p3')))
  199. assert URIRef(g.webroot + '/nonexistent') not in (
  200. gr.objects(None, URIRef('http://example.org#p4')))
  201. def test_post_resource(self, client):
  202. '''
  203. Check response headers for a POST operation with empty payload.
  204. '''
  205. res = self.client.post('/ldp/')
  206. assert res.status_code == 201
  207. assert 'Location' in res.headers
  208. def test_post_slug(self):
  209. '''
  210. Verify that a POST with slug results in the expected URI only if the
  211. resource does not exist already.
  212. '''
  213. slug01_resp = self.client.post('/ldp', headers={'slug' : 'slug01'})
  214. assert slug01_resp.status_code == 201
  215. assert slug01_resp.headers['location'] == \
  216. g.webroot + '/slug01'
  217. slug02_resp = self.client.post('/ldp', headers={'slug' : 'slug01'})
  218. assert slug02_resp.status_code == 201
  219. assert slug02_resp.headers['location'] != \
  220. g.webroot + '/slug01'
  221. def test_post_404(self):
  222. '''
  223. Verify that a POST to a non-existing parent results in a 404.
  224. '''
  225. assert self.client.post('/ldp/{}'.format(uuid.uuid4()))\
  226. .status_code == 404
  227. def test_post_409(self, rnd_img):
  228. '''
  229. Verify that you cannot POST to a binary resource.
  230. '''
  231. rnd_img['content'].seek(0)
  232. self.client.put('/ldp/post_409', data=rnd_img['content'], headers={
  233. 'Content-Disposition' : 'attachment; filename={}'.format(
  234. rnd_img['filename'])})
  235. assert self.client.post('/ldp/post_409').status_code == 409
  236. def test_patch(self):
  237. '''
  238. Test patching a resource.
  239. '''
  240. path = '/ldp/test_patch01'
  241. self.client.put(path)
  242. uri = g.webroot + '/test_patch01'
  243. with open('tests/data/sparql_update/simple_insert.sparql') as data:
  244. resp = self.client.patch(path,
  245. data=data,
  246. headers={'content-type' : 'application/sparql-update'})
  247. assert resp.status_code == 204
  248. resp = self.client.get(path)
  249. gr = Graph().parse(data=resp.data, format='text/turtle')
  250. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Hello') ]
  251. self.client.patch(path,
  252. data=open('tests/data/sparql_update/delete+insert+where.sparql'),
  253. headers={'content-type' : 'application/sparql-update'})
  254. resp = self.client.get(path)
  255. gr = Graph().parse(data=resp.data, format='text/turtle')
  256. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Ciao') ]
  257. def test_patch_ssr(self):
  258. '''
  259. Test patching a resource violating the single-subject rule.
  260. '''
  261. path = '/ldp/test_patch_ssr'
  262. self.client.put(path)
  263. uri = g.webroot + '/test_patch_ssr'
  264. nossr_qry = 'INSERT { <http://bogus.org> a <urn:ns:A> . } WHERE {}'
  265. abs_qry = 'INSERT {{ <{}> a <urn:ns:A> . }} WHERE {{}}'.format(uri)
  266. frag_qry = 'INSERT {{ <{}#frag> a <urn:ns:A> . }} WHERE {{}}'\
  267. .format(uri)
  268. # @TODO Leave commented until a decision is made about SSR.
  269. assert self.client.patch(
  270. path, data=nossr_qry,
  271. headers={'content-type': 'application/sparql-update'}
  272. ).status_code == 204
  273. assert self.client.patch(
  274. path, data=abs_qry,
  275. headers={'content-type': 'application/sparql-update'}
  276. ).status_code == 204
  277. assert self.client.patch(
  278. path, data=frag_qry,
  279. headers={'content-type': 'application/sparql-update'}
  280. ).status_code == 204
  281. def test_patch_ldp_nr_metadata(self):
  282. '''
  283. Test patching a LDP-NR metadata resource from the fcr:metadata URI.
  284. '''
  285. path = '/ldp/ldpnr01'
  286. with open('tests/data/sparql_update/simple_insert.sparql') as data:
  287. self.client.patch(path + '/fcr:metadata',
  288. data=data,
  289. headers={'content-type' : 'application/sparql-update'})
  290. resp = self.client.get(path + '/fcr:metadata')
  291. assert resp.status_code == 200
  292. uri = g.webroot + '/ldpnr01'
  293. gr = Graph().parse(data=resp.data, format='text/turtle')
  294. assert gr[URIRef(uri) : nsc['dc'].title : Literal('Hello')]
  295. with open(
  296. 'tests/data/sparql_update/delete+insert+where.sparql') as data:
  297. patch_resp = self.client.patch(path + '/fcr:metadata',
  298. data=data,
  299. headers={'content-type' : 'application/sparql-update'})
  300. assert patch_resp.status_code == 204
  301. resp = self.client.get(path + '/fcr:metadata')
  302. assert resp.status_code == 200
  303. gr = Graph().parse(data=resp.data, format='text/turtle')
  304. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Ciao') ]
  305. def test_patch_ldpnr(self):
  306. '''
  307. Verify that a direct PATCH to a LDP-NR results in a 415.
  308. '''
  309. with open(
  310. 'tests/data/sparql_update/delete+insert+where.sparql') as data:
  311. patch_resp = self.client.patch('/ldp/ldpnr01',
  312. data=data,
  313. headers={'content-type': 'application/sparql-update'})
  314. assert patch_resp.status_code == 415
  315. def test_patch_invalid_mimetype(self, rnd_img):
  316. '''
  317. Verify that a PATCH using anything other than an
  318. `application/sparql-update` MIME type results in an error.
  319. '''
  320. self.client.put('/ldp/test_patch_invalid_mimetype')
  321. rnd_img['content'].seek(0)
  322. ldpnr_resp = self.client.patch('/ldp/ldpnr01/fcr:metadata',
  323. data=rnd_img,
  324. headers={'content-type' : 'image/jpeg'})
  325. ldprs_resp = self.client.patch('/ldp/test_patch_invalid_mimetype',
  326. data=b'Hello, I\'m not a SPARQL update.',
  327. headers={'content-type' : 'text/plain'})
  328. assert ldprs_resp.status_code == ldpnr_resp.status_code == 415
  329. def test_delete(self):
  330. '''
  331. Test delete response codes.
  332. '''
  333. self.client.put('/ldp/test_delete01')
  334. delete_resp = self.client.delete('/ldp/test_delete01')
  335. assert delete_resp.status_code == 204
  336. bogus_delete_resp = self.client.delete('/ldp/test_delete101')
  337. assert bogus_delete_resp.status_code == 404
  338. def test_tombstone(self):
  339. '''
  340. Test tombstone behaviors.
  341. For POST on a tombstone, check `test_resurrection`.
  342. '''
  343. tstone_resp = self.client.get('/ldp/test_delete01')
  344. assert tstone_resp.status_code == 410
  345. assert tstone_resp.headers['Link'] == \
  346. '<{}/test_delete01/fcr:tombstone>; rel="hasTombstone"'\
  347. .format(g.webroot)
  348. tstone_path = '/ldp/test_delete01/fcr:tombstone'
  349. assert self.client.get(tstone_path).status_code == 405
  350. assert self.client.put(tstone_path).status_code == 405
  351. assert self.client.delete(tstone_path).status_code == 204
  352. assert self.client.get('/ldp/test_delete01').status_code == 404
  353. def test_delete_recursive(self):
  354. '''
  355. Test response codes for resources deleted recursively and their
  356. tombstones.
  357. '''
  358. child_suffixes = ('a', 'a/b', 'a/b/c', 'a1', 'a1/b1')
  359. self.client.put('/ldp/test_delete_recursive01')
  360. for cs in child_suffixes:
  361. self.client.put('/ldp/test_delete_recursive01/{}'.format(cs))
  362. assert self.client.delete(
  363. '/ldp/test_delete_recursive01').status_code == 204
  364. tstone_resp = self.client.get('/ldp/test_delete_recursive01')
  365. assert tstone_resp.status_code == 410
  366. assert tstone_resp.headers['Link'] == \
  367. '<{}/test_delete_recursive01/fcr:tombstone>; rel="hasTombstone"'\
  368. .format(g.webroot)
  369. for cs in child_suffixes:
  370. child_tstone_resp = self.client.get(
  371. '/ldp/test_delete_recursive01/{}'.format(cs))
  372. assert child_tstone_resp.status_code == tstone_resp.status_code
  373. assert 'Link' not in child_tstone_resp.headers.keys()
  374. def test_put_fragments(self):
  375. '''
  376. Test the correct handling of fragment URIs on PUT and GET.
  377. '''
  378. with open('tests/data/fragments.ttl', 'rb') as f:
  379. self.client.put(
  380. '/ldp/test_fragment01',
  381. headers={
  382. 'Content-Type' : 'text/turtle',
  383. },
  384. data=f
  385. )
  386. rsp = self.client.get('/ldp/test_fragment01')
  387. gr = Graph().parse(data=rsp.data, format='text/turtle')
  388. assert gr[
  389. URIRef(g.webroot + '/test_fragment01#hash1')
  390. : URIRef('http://ex.org/p2') : URIRef('http://ex.org/o2')]
  391. def test_patch_fragments(self):
  392. '''
  393. Test the correct handling of fragment URIs on PATCH.
  394. '''
  395. self.client.put('/ldp/test_fragment_patch')
  396. with open('tests/data/fragments_insert.sparql', 'rb') as f:
  397. self.client.patch(
  398. '/ldp/test_fragment_patch',
  399. headers={
  400. 'Content-Type' : 'application/sparql-update',
  401. },
  402. data=f
  403. )
  404. ins_rsp = self.client.get('/ldp/test_fragment_patch')
  405. ins_gr = Graph().parse(data=ins_rsp.data, format='text/turtle')
  406. assert ins_gr[
  407. URIRef(g.webroot + '/test_fragment_patch#hash1234')
  408. : URIRef('http://ex.org/p3') : URIRef('http://ex.org/o3')]
  409. with open('tests/data/fragments_delete.sparql', 'rb') as f:
  410. self.client.patch(
  411. '/ldp/test_fragment_patch',
  412. headers={
  413. 'Content-Type' : 'application/sparql-update',
  414. },
  415. data=f
  416. )
  417. del_rsp = self.client.get('/ldp/test_fragment_patch')
  418. del_gr = Graph().parse(data=del_rsp.data, format='text/turtle')
  419. assert not del_gr[
  420. URIRef(g.webroot + '/test_fragment_patch#hash1234')
  421. : URIRef('http://ex.org/p3') : URIRef('http://ex.org/o3')]
  422. @pytest.mark.usefixtures('client_class')
  423. @pytest.mark.usefixtures('db')
  424. class TestPrefHeader:
  425. '''
  426. Test various combinations of `Prefer` header.
  427. '''
  428. @pytest.fixture(scope='class')
  429. def cont_structure(self):
  430. '''
  431. Create a container structure to be used for subsequent requests.
  432. '''
  433. parent_path = '/ldp/test_parent'
  434. self.client.put(parent_path)
  435. self.client.put(parent_path + '/child1')
  436. self.client.put(parent_path + '/child2')
  437. self.client.put(parent_path + '/child3')
  438. return {
  439. 'path' : parent_path,
  440. 'response' : self.client.get(parent_path),
  441. 'subject' : URIRef(g.webroot + '/test_parent')
  442. }
  443. def test_put_prefer_handling(self, random_uuid):
  444. '''
  445. Trying to PUT an existing resource should:
  446. - Return a 409 if the payload is empty
  447. - Return a 204 if the payload is RDF, server-managed triples are
  448. included and the 'Prefer' header is set to 'handling=lenient'
  449. - Return a 412 (ServerManagedTermError) if the payload is RDF,
  450. server-managed triples are included and handling is set to 'strict',
  451. or not set.
  452. '''
  453. path = '/ldp/put_pref_header01'
  454. assert self.client.put(path).status_code == 201
  455. assert self.client.get(path).status_code == 200
  456. assert self.client.put(path).status_code == 409
  457. # Default handling is strict.
  458. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  459. rsp_default = self.client.put(
  460. path,
  461. headers={
  462. 'Content-Type' : 'text/turtle',
  463. },
  464. data=f
  465. )
  466. assert rsp_default.status_code == 412
  467. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  468. rsp_len = self.client.put(
  469. path,
  470. headers={
  471. 'Prefer' : 'handling=lenient',
  472. 'Content-Type' : 'text/turtle',
  473. },
  474. data=f
  475. )
  476. assert rsp_len.status_code == 204
  477. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  478. rsp_strict = self.client.put(
  479. path,
  480. headers={
  481. 'Prefer' : 'handling=strict',
  482. 'Content-Type' : 'text/turtle',
  483. },
  484. data=f
  485. )
  486. assert rsp_strict.status_code == 412
  487. # @HOLD Embed children is debated.
  488. def _disabled_test_embed_children(self, cont_structure):
  489. '''
  490. verify the "embed children" prefer header.
  491. '''
  492. parent_path = cont_structure['path']
  493. cont_resp = cont_structure['response']
  494. cont_subject = cont_structure['subject']
  495. #minimal_resp = self.client.get(parent_path, headers={
  496. # 'Prefer' : 'return=minimal',
  497. #})
  498. incl_embed_children_resp = self.client.get(parent_path, headers={
  499. 'Prefer' : 'return=representation; include={}'\
  500. .format(Ldpr.EMBED_CHILD_RES_URI),
  501. })
  502. omit_embed_children_resp = self.client.get(parent_path, headers={
  503. 'Prefer' : 'return=representation; omit={}'\
  504. .format(Ldpr.EMBED_CHILD_RES_URI),
  505. })
  506. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  507. incl_gr = Graph().parse(
  508. data=incl_embed_children_resp.data, format='turtle')
  509. omit_gr = Graph().parse(
  510. data=omit_embed_children_resp.data, format='turtle')
  511. assert isomorphic(omit_gr, default_gr)
  512. children = set(incl_gr[cont_subject : nsc['ldp'].contains])
  513. assert len(children) == 3
  514. children = set(incl_gr[cont_subject : nsc['ldp'].contains])
  515. for child_uri in children:
  516. assert set(incl_gr[ child_uri : : ])
  517. assert not set(omit_gr[ child_uri : : ])
  518. def test_return_children(self, cont_structure):
  519. '''
  520. verify the "return children" prefer header.
  521. '''
  522. parent_path = cont_structure['path']
  523. cont_resp = cont_structure['response']
  524. cont_subject = cont_structure['subject']
  525. incl_children_resp = self.client.get(parent_path, headers={
  526. 'Prefer' : 'return=representation; include={}'\
  527. .format(Ldpr.RETURN_CHILD_RES_URI),
  528. })
  529. omit_children_resp = self.client.get(parent_path, headers={
  530. 'Prefer' : 'return=representation; omit={}'\
  531. .format(Ldpr.RETURN_CHILD_RES_URI),
  532. })
  533. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  534. incl_gr = Graph().parse(data=incl_children_resp.data, format='turtle')
  535. omit_gr = Graph().parse(data=omit_children_resp.data, format='turtle')
  536. assert isomorphic(incl_gr, default_gr)
  537. children = incl_gr[cont_subject : nsc['ldp'].contains]
  538. for child_uri in children:
  539. assert not omit_gr[cont_subject : nsc['ldp'].contains : child_uri]
  540. def test_inbound_rel(self, cont_structure):
  541. '''
  542. verify the "inbound relationships" prefer header.
  543. '''
  544. self.client.put('/ldp/test_target')
  545. data = '<> <http://ex.org/ns#shoots> <{}> .'.format(
  546. g.webroot + '/test_target')
  547. self.client.put('/ldp/test_shooter', data=data,
  548. headers={'Content-Type': 'text/turtle'})
  549. cont_resp = self.client.get('/ldp/test_target')
  550. incl_inbound_resp = self.client.get('/ldp/test_target', headers={
  551. 'Prefer' : 'return=representation; include="{}"'\
  552. .format(Ldpr.RETURN_INBOUND_REF_URI),
  553. })
  554. omit_inbound_resp = self.client.get('/ldp/test_target', headers={
  555. 'Prefer' : 'return=representation; omit="{}"'\
  556. .format(Ldpr.RETURN_INBOUND_REF_URI),
  557. })
  558. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  559. incl_gr = Graph().parse(data=incl_inbound_resp.data, format='turtle')
  560. omit_gr = Graph().parse(data=omit_inbound_resp.data, format='turtle')
  561. subject = URIRef(g.webroot + '/test_target')
  562. inbd_subject = URIRef(g.webroot + '/test_shooter')
  563. assert isomorphic(omit_gr, default_gr)
  564. assert len(set(incl_gr[inbd_subject : : ])) == 1
  565. assert incl_gr[
  566. inbd_subject : URIRef('http://ex.org/ns#shoots') : subject]
  567. assert not len(set(omit_gr[inbd_subject : :]))
  568. def test_srv_mgd_triples(self, cont_structure):
  569. '''
  570. verify the "server managed triples" prefer header.
  571. '''
  572. parent_path = cont_structure['path']
  573. cont_resp = cont_structure['response']
  574. cont_subject = cont_structure['subject']
  575. incl_srv_mgd_resp = self.client.get(parent_path, headers={
  576. 'Prefer' : 'return=representation; include={}'\
  577. .format(Ldpr.RETURN_SRV_MGD_RES_URI),
  578. })
  579. omit_srv_mgd_resp = self.client.get(parent_path, headers={
  580. 'Prefer' : 'return=representation; omit={}'\
  581. .format(Ldpr.RETURN_SRV_MGD_RES_URI),
  582. })
  583. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  584. incl_gr = Graph().parse(data=incl_srv_mgd_resp.data, format='turtle')
  585. omit_gr = Graph().parse(data=omit_srv_mgd_resp.data, format='turtle')
  586. assert isomorphic(incl_gr, default_gr)
  587. for pred in {
  588. nsc['fcrepo'].created,
  589. nsc['fcrepo'].createdBy,
  590. nsc['fcrepo'].lastModified,
  591. nsc['fcrepo'].lastModifiedBy,
  592. nsc['ldp'].contains,
  593. }:
  594. assert set(incl_gr[ cont_subject : pred : ])
  595. assert not set(omit_gr[ cont_subject : pred : ])
  596. for type in {
  597. nsc['fcrepo'].Resource,
  598. nsc['ldp'].Container,
  599. nsc['ldp'].Resource,
  600. }:
  601. assert incl_gr[ cont_subject : RDF.type : type ]
  602. assert not omit_gr[ cont_subject : RDF.type : type ]
  603. def test_delete_no_tstone(self):
  604. '''
  605. Test the `no-tombstone` Prefer option.
  606. '''
  607. self.client.put('/ldp/test_delete_no_tstone01')
  608. self.client.put('/ldp/test_delete_no_tstone01/a')
  609. self.client.delete('/ldp/test_delete_no_tstone01', headers={
  610. 'prefer' : 'no-tombstone'})
  611. resp = self.client.get('/ldp/test_delete_no_tstone01')
  612. assert resp.status_code == 404
  613. child_resp = self.client.get('/ldp/test_delete_no_tstone01/a')
  614. assert child_resp.status_code == 404
  615. @pytest.mark.usefixtures('client_class')
  616. @pytest.mark.usefixtures('db')
  617. class TestVersion:
  618. '''
  619. Test version creation, retrieval and deletion.
  620. '''
  621. def test_create_versions(self):
  622. '''
  623. Test that POSTing multiple times to fcr:versions creates the
  624. 'hasVersions' triple and yields multiple version snapshots.
  625. '''
  626. self.client.put('/ldp/test_version')
  627. create_rsp = self.client.post('/ldp/test_version/fcr:versions')
  628. assert create_rsp.status_code == 201
  629. rsrc_rsp = self.client.get('/ldp/test_version')
  630. rsrc_gr = Graph().parse(data=rsrc_rsp.data, format='turtle')
  631. assert len(set(rsrc_gr[: nsc['fcrepo'].hasVersions :])) == 1
  632. info_rsp = self.client.get('/ldp/test_version/fcr:versions')
  633. assert info_rsp.status_code == 200
  634. info_gr = Graph().parse(data=info_rsp.data, format='turtle')
  635. assert len(set(info_gr[: nsc['fcrepo'].hasVersion :])) == 1
  636. self.client.post('/ldp/test_version/fcr:versions')
  637. info2_rsp = self.client.get('/ldp/test_version/fcr:versions')
  638. info2_gr = Graph().parse(data=info2_rsp.data, format='turtle')
  639. assert len(set(info2_gr[: nsc['fcrepo'].hasVersion :])) == 2
  640. def test_version_with_slug(self):
  641. '''
  642. Test a version with a slug.
  643. '''
  644. self.client.put('/ldp/test_version_slug')
  645. create_rsp = self.client.post('/ldp/test_version_slug/fcr:versions',
  646. headers={'slug' : 'v1'})
  647. new_ver_uri = create_rsp.headers['Location']
  648. assert new_ver_uri == g.webroot + '/test_version_slug/fcr:versions/v1'
  649. info_rsp = self.client.get('/ldp/test_version_slug/fcr:versions')
  650. info_gr = Graph().parse(data=info_rsp.data, format='turtle')
  651. assert info_gr[
  652. URIRef(new_ver_uri) :
  653. nsc['fcrepo'].hasVersionLabel :
  654. Literal('v1')]
  655. def test_dupl_version(self):
  656. '''
  657. Make sure that two POSTs with the same slug result in two different
  658. versions.
  659. '''
  660. path = '/ldp/test_duplicate_slug'
  661. self.client.put(path)
  662. v1_rsp = self.client.post(path + '/fcr:versions',
  663. headers={'slug' : 'v1'})
  664. v1_uri = v1_rsp.headers['Location']
  665. dup_rsp = self.client.post(path + '/fcr:versions',
  666. headers={'slug' : 'v1'})
  667. dup_uri = dup_rsp.headers['Location']
  668. assert v1_uri != dup_uri
  669. # @TODO Reverting from version and resurrecting is not fully functional.
  670. def _disabled_test_revert_version(self):
  671. '''
  672. Take a version snapshot, update a resource, and then revert to the
  673. previous vresion.
  674. '''
  675. rsrc_path = '/ldp/test_revert_version'
  676. payload1 = '<> <urn:demo:p1> <urn:demo:o1> .'
  677. payload2 = '<> <urn:demo:p1> <urn:demo:o2> .'
  678. self.client.put(rsrc_path, headers={
  679. 'content-type': 'text/turtle'}, data=payload1)
  680. self.client.post(
  681. rsrc_path + '/fcr:versions', headers={'slug': 'v1'})
  682. v1_rsp = self.client.get(rsrc_path)
  683. v1_gr = Graph().parse(data=v1_rsp.data, format='turtle')
  684. assert v1_gr[
  685. URIRef(g.webroot + '/test_revert_version')
  686. : URIRef('urn:demo:p1')
  687. : URIRef('urn:demo:o1')
  688. ]
  689. self.client.put(rsrc_path, headers={
  690. 'content-type': 'text/turtle'}, data=payload2)
  691. v2_rsp = self.client.get(rsrc_path)
  692. v2_gr = Graph().parse(data=v2_rsp.data, format='turtle')
  693. assert v2_gr[
  694. URIRef(g.webroot + '/test_revert_version')
  695. : URIRef('urn:demo:p1')
  696. : URIRef('urn:demo:o2')
  697. ]
  698. self.client.patch(rsrc_path + '/fcr:versions/v1')
  699. revert_rsp = self.client.get(rsrc_path)
  700. revert_gr = Graph().parse(data=revert_rsp.data, format='turtle')
  701. assert revert_gr[
  702. URIRef(g.webroot + '/test_revert_version')
  703. : URIRef('urn:demo:p1')
  704. : URIRef('urn:demo:o1')
  705. ]
  706. #def test_resurrection(self):
  707. # '''
  708. # Delete and then resurrect a resource.
  709. # Make sure that the resource is resurrected to the latest version.
  710. # '''
  711. # path = '/ldp/test_lazarus'
  712. # self.client.put(path)
  713. # self.client.post(path + '/fcr:versions', headers={'slug': 'v1'})
  714. # self.client.put(
  715. # path, headers={'content-type': 'text/turtle'},
  716. # data=b'<> <urn:demo:p1> <urn:demo:o1> .')
  717. # self.client.post(path + '/fcr:versions', headers={'slug': 'v2'})
  718. # self.client.put(
  719. # path, headers={'content-type': 'text/turtle'},
  720. # data=b'<> <urn:demo:p1> <urn:demo:o2> .')
  721. # self.client.delete(path)
  722. # assert self.client.get(path).status_code == 410
  723. # self.client.post(path + '/fcr:tombstone')
  724. # laz_data = self.client.get(path).data
  725. # laz_gr = Graph().parse(data=laz_data, format='turtle')
  726. # assert laz_gr[
  727. # URIRef(g.webroot + '/test_lazarus')
  728. # : URIRef('urn:demo:p1')
  729. # : URIRef('urn:demo:o2')
  730. # ]