test_ldp.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 == 204
  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_root(self):
  237. '''
  238. Test patching root node.
  239. '''
  240. path = '/ldp/'
  241. self.client.get(path)
  242. uri = g.webroot + '/'
  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. def test_patch(self):
  252. '''
  253. Test patching a resource.
  254. '''
  255. path = '/ldp/test_patch01'
  256. self.client.put(path)
  257. uri = g.webroot + '/test_patch01'
  258. with open('tests/data/sparql_update/simple_insert.sparql') as data:
  259. resp = self.client.patch(path,
  260. data=data,
  261. headers={'content-type' : 'application/sparql-update'})
  262. assert resp.status_code == 204
  263. resp = self.client.get(path)
  264. gr = Graph().parse(data=resp.data, format='text/turtle')
  265. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Hello') ]
  266. self.client.patch(path,
  267. data=open('tests/data/sparql_update/delete+insert+where.sparql'),
  268. headers={'content-type' : 'application/sparql-update'})
  269. resp = self.client.get(path)
  270. gr = Graph().parse(data=resp.data, format='text/turtle')
  271. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Ciao') ]
  272. def test_patch_ssr(self):
  273. '''
  274. Test patching a resource violating the single-subject rule.
  275. '''
  276. path = '/ldp/test_patch_ssr'
  277. self.client.put(path)
  278. uri = g.webroot + '/test_patch_ssr'
  279. nossr_qry = 'INSERT { <http://bogus.org> a <urn:ns:A> . } WHERE {}'
  280. abs_qry = 'INSERT {{ <{}> a <urn:ns:A> . }} WHERE {{}}'.format(uri)
  281. frag_qry = 'INSERT {{ <{}#frag> a <urn:ns:A> . }} WHERE {{}}'\
  282. .format(uri)
  283. # @TODO Leave commented until a decision is made about SSR.
  284. assert self.client.patch(
  285. path, data=nossr_qry,
  286. headers={'content-type': 'application/sparql-update'}
  287. ).status_code == 204
  288. assert self.client.patch(
  289. path, data=abs_qry,
  290. headers={'content-type': 'application/sparql-update'}
  291. ).status_code == 204
  292. assert self.client.patch(
  293. path, data=frag_qry,
  294. headers={'content-type': 'application/sparql-update'}
  295. ).status_code == 204
  296. def test_patch_ldp_nr_metadata(self):
  297. '''
  298. Test patching a LDP-NR metadata resource from the fcr:metadata URI.
  299. '''
  300. path = '/ldp/ldpnr01'
  301. with open('tests/data/sparql_update/simple_insert.sparql') as data:
  302. self.client.patch(path + '/fcr:metadata',
  303. data=data,
  304. headers={'content-type' : 'application/sparql-update'})
  305. resp = self.client.get(path + '/fcr:metadata')
  306. assert resp.status_code == 200
  307. uri = g.webroot + '/ldpnr01'
  308. gr = Graph().parse(data=resp.data, format='text/turtle')
  309. assert gr[URIRef(uri) : nsc['dc'].title : Literal('Hello')]
  310. with open(
  311. 'tests/data/sparql_update/delete+insert+where.sparql') as data:
  312. patch_resp = self.client.patch(path + '/fcr:metadata',
  313. data=data,
  314. headers={'content-type' : 'application/sparql-update'})
  315. assert patch_resp.status_code == 204
  316. resp = self.client.get(path + '/fcr:metadata')
  317. assert resp.status_code == 200
  318. gr = Graph().parse(data=resp.data, format='text/turtle')
  319. assert gr[ URIRef(uri) : nsc['dc'].title : Literal('Ciao') ]
  320. def test_patch_ldpnr(self):
  321. '''
  322. Verify that a direct PATCH to a LDP-NR results in a 415.
  323. '''
  324. with open(
  325. 'tests/data/sparql_update/delete+insert+where.sparql') as data:
  326. patch_resp = self.client.patch('/ldp/ldpnr01',
  327. data=data,
  328. headers={'content-type': 'application/sparql-update'})
  329. assert patch_resp.status_code == 415
  330. def test_patch_invalid_mimetype(self, rnd_img):
  331. '''
  332. Verify that a PATCH using anything other than an
  333. `application/sparql-update` MIME type results in an error.
  334. '''
  335. self.client.put('/ldp/test_patch_invalid_mimetype')
  336. rnd_img['content'].seek(0)
  337. ldpnr_resp = self.client.patch('/ldp/ldpnr01/fcr:metadata',
  338. data=rnd_img,
  339. headers={'content-type' : 'image/jpeg'})
  340. ldprs_resp = self.client.patch('/ldp/test_patch_invalid_mimetype',
  341. data=b'Hello, I\'m not a SPARQL update.',
  342. headers={'content-type' : 'text/plain'})
  343. assert ldprs_resp.status_code == ldpnr_resp.status_code == 415
  344. def test_delete(self):
  345. '''
  346. Test delete response codes.
  347. '''
  348. self.client.put('/ldp/test_delete01')
  349. delete_resp = self.client.delete('/ldp/test_delete01')
  350. assert delete_resp.status_code == 204
  351. bogus_delete_resp = self.client.delete('/ldp/test_delete101')
  352. assert bogus_delete_resp.status_code == 404
  353. def test_tombstone(self):
  354. '''
  355. Test tombstone behaviors.
  356. For POST on a tombstone, check `test_resurrection`.
  357. '''
  358. tstone_resp = self.client.get('/ldp/test_delete01')
  359. assert tstone_resp.status_code == 410
  360. assert tstone_resp.headers['Link'] == \
  361. '<{}/test_delete01/fcr:tombstone>; rel="hasTombstone"'\
  362. .format(g.webroot)
  363. tstone_path = '/ldp/test_delete01/fcr:tombstone'
  364. assert self.client.get(tstone_path).status_code == 405
  365. assert self.client.put(tstone_path).status_code == 405
  366. assert self.client.delete(tstone_path).status_code == 204
  367. assert self.client.get('/ldp/test_delete01').status_code == 404
  368. def test_delete_recursive(self):
  369. '''
  370. Test response codes for resources deleted recursively and their
  371. tombstones.
  372. '''
  373. child_suffixes = ('a', 'a/b', 'a/b/c', 'a1', 'a1/b1')
  374. self.client.put('/ldp/test_delete_recursive01')
  375. for cs in child_suffixes:
  376. self.client.put('/ldp/test_delete_recursive01/{}'.format(cs))
  377. assert self.client.delete(
  378. '/ldp/test_delete_recursive01').status_code == 204
  379. tstone_resp = self.client.get('/ldp/test_delete_recursive01')
  380. assert tstone_resp.status_code == 410
  381. assert tstone_resp.headers['Link'] == \
  382. '<{}/test_delete_recursive01/fcr:tombstone>; rel="hasTombstone"'\
  383. .format(g.webroot)
  384. for cs in child_suffixes:
  385. child_tstone_resp = self.client.get(
  386. '/ldp/test_delete_recursive01/{}'.format(cs))
  387. assert child_tstone_resp.status_code == tstone_resp.status_code
  388. assert 'Link' not in child_tstone_resp.headers.keys()
  389. def test_put_fragments(self):
  390. '''
  391. Test the correct handling of fragment URIs on PUT and GET.
  392. '''
  393. with open('tests/data/fragments.ttl', 'rb') as f:
  394. self.client.put(
  395. '/ldp/test_fragment01',
  396. headers={
  397. 'Content-Type' : 'text/turtle',
  398. },
  399. data=f
  400. )
  401. rsp = self.client.get('/ldp/test_fragment01')
  402. gr = Graph().parse(data=rsp.data, format='text/turtle')
  403. assert gr[
  404. URIRef(g.webroot + '/test_fragment01#hash1')
  405. : URIRef('http://ex.org/p2') : URIRef('http://ex.org/o2')]
  406. def test_patch_fragments(self):
  407. '''
  408. Test the correct handling of fragment URIs on PATCH.
  409. '''
  410. self.client.put('/ldp/test_fragment_patch')
  411. with open('tests/data/fragments_insert.sparql', 'rb') as f:
  412. self.client.patch(
  413. '/ldp/test_fragment_patch',
  414. headers={
  415. 'Content-Type' : 'application/sparql-update',
  416. },
  417. data=f
  418. )
  419. ins_rsp = self.client.get('/ldp/test_fragment_patch')
  420. ins_gr = Graph().parse(data=ins_rsp.data, format='text/turtle')
  421. assert ins_gr[
  422. URIRef(g.webroot + '/test_fragment_patch#hash1234')
  423. : URIRef('http://ex.org/p3') : URIRef('http://ex.org/o3')]
  424. with open('tests/data/fragments_delete.sparql', 'rb') as f:
  425. self.client.patch(
  426. '/ldp/test_fragment_patch',
  427. headers={
  428. 'Content-Type' : 'application/sparql-update',
  429. },
  430. data=f
  431. )
  432. del_rsp = self.client.get('/ldp/test_fragment_patch')
  433. del_gr = Graph().parse(data=del_rsp.data, format='text/turtle')
  434. assert not del_gr[
  435. URIRef(g.webroot + '/test_fragment_patch#hash1234')
  436. : URIRef('http://ex.org/p3') : URIRef('http://ex.org/o3')]
  437. @pytest.mark.usefixtures('client_class')
  438. @pytest.mark.usefixtures('db')
  439. class TestPrefHeader:
  440. '''
  441. Test various combinations of `Prefer` header.
  442. '''
  443. @pytest.fixture(scope='class')
  444. def cont_structure(self):
  445. '''
  446. Create a container structure to be used for subsequent requests.
  447. '''
  448. parent_path = '/ldp/test_parent'
  449. self.client.put(parent_path)
  450. self.client.put(parent_path + '/child1')
  451. self.client.put(parent_path + '/child2')
  452. self.client.put(parent_path + '/child3')
  453. return {
  454. 'path' : parent_path,
  455. 'response' : self.client.get(parent_path),
  456. 'subject' : URIRef(g.webroot + '/test_parent')
  457. }
  458. def test_put_prefer_handling(self, random_uuid):
  459. '''
  460. Trying to PUT an existing resource should:
  461. - Return a 204 if the payload is empty
  462. - Return a 204 if the payload is RDF, server-managed triples are
  463. included and the 'Prefer' header is set to 'handling=lenient'
  464. - Return a 412 (ServerManagedTermError) if the payload is RDF,
  465. server-managed triples are included and handling is set to 'strict',
  466. or not set.
  467. '''
  468. path = '/ldp/put_pref_header01'
  469. assert self.client.put(path).status_code == 201
  470. assert self.client.get(path).status_code == 200
  471. assert self.client.put(path).status_code == 204
  472. # Default handling is strict.
  473. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  474. rsp_default = self.client.put(
  475. path,
  476. headers={
  477. 'Content-Type' : 'text/turtle',
  478. },
  479. data=f
  480. )
  481. assert rsp_default.status_code == 412
  482. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  483. rsp_len = self.client.put(
  484. path,
  485. headers={
  486. 'Prefer' : 'handling=lenient',
  487. 'Content-Type' : 'text/turtle',
  488. },
  489. data=f
  490. )
  491. assert rsp_len.status_code == 204
  492. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  493. rsp_strict = self.client.put(
  494. path,
  495. headers={
  496. 'Prefer' : 'handling=strict',
  497. 'Content-Type' : 'text/turtle',
  498. },
  499. data=f
  500. )
  501. assert rsp_strict.status_code == 412
  502. # @HOLD Embed children is debated.
  503. def _disabled_test_embed_children(self, cont_structure):
  504. '''
  505. verify the "embed children" prefer header.
  506. '''
  507. parent_path = cont_structure['path']
  508. cont_resp = cont_structure['response']
  509. cont_subject = cont_structure['subject']
  510. #minimal_resp = self.client.get(parent_path, headers={
  511. # 'Prefer' : 'return=minimal',
  512. #})
  513. incl_embed_children_resp = self.client.get(parent_path, headers={
  514. 'Prefer' : 'return=representation; include={}'\
  515. .format(Ldpr.EMBED_CHILD_RES_URI),
  516. })
  517. omit_embed_children_resp = self.client.get(parent_path, headers={
  518. 'Prefer' : 'return=representation; omit={}'\
  519. .format(Ldpr.EMBED_CHILD_RES_URI),
  520. })
  521. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  522. incl_gr = Graph().parse(
  523. data=incl_embed_children_resp.data, format='turtle')
  524. omit_gr = Graph().parse(
  525. data=omit_embed_children_resp.data, format='turtle')
  526. assert isomorphic(omit_gr, default_gr)
  527. children = set(incl_gr[cont_subject : nsc['ldp'].contains])
  528. assert len(children) == 3
  529. children = set(incl_gr[cont_subject : nsc['ldp'].contains])
  530. for child_uri in children:
  531. assert set(incl_gr[ child_uri : : ])
  532. assert not set(omit_gr[ child_uri : : ])
  533. def test_return_children(self, cont_structure):
  534. '''
  535. verify the "return children" prefer header.
  536. '''
  537. parent_path = cont_structure['path']
  538. cont_resp = cont_structure['response']
  539. cont_subject = cont_structure['subject']
  540. incl_children_resp = self.client.get(parent_path, headers={
  541. 'Prefer' : 'return=representation; include={}'\
  542. .format(Ldpr.RETURN_CHILD_RES_URI),
  543. })
  544. omit_children_resp = self.client.get(parent_path, headers={
  545. 'Prefer' : 'return=representation; omit={}'\
  546. .format(Ldpr.RETURN_CHILD_RES_URI),
  547. })
  548. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  549. incl_gr = Graph().parse(data=incl_children_resp.data, format='turtle')
  550. omit_gr = Graph().parse(data=omit_children_resp.data, format='turtle')
  551. assert isomorphic(incl_gr, default_gr)
  552. children = incl_gr[cont_subject : nsc['ldp'].contains]
  553. for child_uri in children:
  554. assert not omit_gr[cont_subject : nsc['ldp'].contains : child_uri]
  555. def test_inbound_rel(self, cont_structure):
  556. '''
  557. verify the "inbound relationships" prefer header.
  558. '''
  559. self.client.put('/ldp/test_target')
  560. data = '<> <http://ex.org/ns#shoots> <{}> .'.format(
  561. g.webroot + '/test_target')
  562. self.client.put('/ldp/test_shooter', data=data,
  563. headers={'Content-Type': 'text/turtle'})
  564. cont_resp = self.client.get('/ldp/test_target')
  565. incl_inbound_resp = self.client.get('/ldp/test_target', headers={
  566. 'Prefer' : 'return=representation; include="{}"'\
  567. .format(Ldpr.RETURN_INBOUND_REF_URI),
  568. })
  569. omit_inbound_resp = self.client.get('/ldp/test_target', headers={
  570. 'Prefer' : 'return=representation; omit="{}"'\
  571. .format(Ldpr.RETURN_INBOUND_REF_URI),
  572. })
  573. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  574. incl_gr = Graph().parse(data=incl_inbound_resp.data, format='turtle')
  575. omit_gr = Graph().parse(data=omit_inbound_resp.data, format='turtle')
  576. subject = URIRef(g.webroot + '/test_target')
  577. inbd_subject = URIRef(g.webroot + '/test_shooter')
  578. assert isomorphic(omit_gr, default_gr)
  579. assert len(set(incl_gr[inbd_subject : : ])) == 1
  580. assert incl_gr[
  581. inbd_subject : URIRef('http://ex.org/ns#shoots') : subject]
  582. assert not len(set(omit_gr[inbd_subject : :]))
  583. def test_srv_mgd_triples(self, cont_structure):
  584. '''
  585. verify the "server managed triples" prefer header.
  586. '''
  587. parent_path = cont_structure['path']
  588. cont_resp = cont_structure['response']
  589. cont_subject = cont_structure['subject']
  590. incl_srv_mgd_resp = self.client.get(parent_path, headers={
  591. 'Prefer' : 'return=representation; include={}'\
  592. .format(Ldpr.RETURN_SRV_MGD_RES_URI),
  593. })
  594. omit_srv_mgd_resp = self.client.get(parent_path, headers={
  595. 'Prefer' : 'return=representation; omit={}'\
  596. .format(Ldpr.RETURN_SRV_MGD_RES_URI),
  597. })
  598. default_gr = Graph().parse(data=cont_resp.data, format='turtle')
  599. incl_gr = Graph().parse(data=incl_srv_mgd_resp.data, format='turtle')
  600. omit_gr = Graph().parse(data=omit_srv_mgd_resp.data, format='turtle')
  601. assert isomorphic(incl_gr, default_gr)
  602. for pred in {
  603. nsc['fcrepo'].created,
  604. nsc['fcrepo'].createdBy,
  605. nsc['fcrepo'].lastModified,
  606. nsc['fcrepo'].lastModifiedBy,
  607. nsc['ldp'].contains,
  608. }:
  609. assert set(incl_gr[ cont_subject : pred : ])
  610. assert not set(omit_gr[ cont_subject : pred : ])
  611. for type in {
  612. nsc['fcrepo'].Resource,
  613. nsc['ldp'].Container,
  614. nsc['ldp'].Resource,
  615. }:
  616. assert incl_gr[ cont_subject : RDF.type : type ]
  617. assert not omit_gr[ cont_subject : RDF.type : type ]
  618. def test_delete_no_tstone(self):
  619. '''
  620. Test the `no-tombstone` Prefer option.
  621. '''
  622. self.client.put('/ldp/test_delete_no_tstone01')
  623. self.client.put('/ldp/test_delete_no_tstone01/a')
  624. self.client.delete('/ldp/test_delete_no_tstone01', headers={
  625. 'prefer' : 'no-tombstone'})
  626. resp = self.client.get('/ldp/test_delete_no_tstone01')
  627. assert resp.status_code == 404
  628. child_resp = self.client.get('/ldp/test_delete_no_tstone01/a')
  629. assert child_resp.status_code == 404
  630. @pytest.mark.usefixtures('client_class')
  631. @pytest.mark.usefixtures('db')
  632. class TestVersion:
  633. '''
  634. Test version creation, retrieval and deletion.
  635. '''
  636. def test_create_versions(self):
  637. '''
  638. Test that POSTing multiple times to fcr:versions creates the
  639. 'hasVersions' triple and yields multiple version snapshots.
  640. '''
  641. self.client.put('/ldp/test_version')
  642. create_rsp = self.client.post('/ldp/test_version/fcr:versions')
  643. assert create_rsp.status_code == 201
  644. rsrc_rsp = self.client.get('/ldp/test_version')
  645. rsrc_gr = Graph().parse(data=rsrc_rsp.data, format='turtle')
  646. assert len(set(rsrc_gr[: nsc['fcrepo'].hasVersions :])) == 1
  647. info_rsp = self.client.get('/ldp/test_version/fcr:versions')
  648. assert info_rsp.status_code == 200
  649. info_gr = Graph().parse(data=info_rsp.data, format='turtle')
  650. assert len(set(info_gr[: nsc['fcrepo'].hasVersion :])) == 1
  651. self.client.post('/ldp/test_version/fcr:versions')
  652. info2_rsp = self.client.get('/ldp/test_version/fcr:versions')
  653. info2_gr = Graph().parse(data=info2_rsp.data, format='turtle')
  654. assert len(set(info2_gr[: nsc['fcrepo'].hasVersion :])) == 2
  655. def test_version_with_slug(self):
  656. '''
  657. Test a version with a slug.
  658. '''
  659. self.client.put('/ldp/test_version_slug')
  660. create_rsp = self.client.post('/ldp/test_version_slug/fcr:versions',
  661. headers={'slug' : 'v1'})
  662. new_ver_uri = create_rsp.headers['Location']
  663. assert new_ver_uri == g.webroot + '/test_version_slug/fcr:versions/v1'
  664. info_rsp = self.client.get('/ldp/test_version_slug/fcr:versions')
  665. info_gr = Graph().parse(data=info_rsp.data, format='turtle')
  666. assert info_gr[
  667. URIRef(new_ver_uri) :
  668. nsc['fcrepo'].hasVersionLabel :
  669. Literal('v1')]
  670. def test_dupl_version(self):
  671. '''
  672. Make sure that two POSTs with the same slug result in two different
  673. versions.
  674. '''
  675. path = '/ldp/test_duplicate_slug'
  676. self.client.put(path)
  677. v1_rsp = self.client.post(path + '/fcr:versions',
  678. headers={'slug' : 'v1'})
  679. v1_uri = v1_rsp.headers['Location']
  680. dup_rsp = self.client.post(path + '/fcr:versions',
  681. headers={'slug' : 'v1'})
  682. dup_uri = dup_rsp.headers['Location']
  683. assert v1_uri != dup_uri
  684. # @TODO Reverting from version and resurrecting is not fully functional.
  685. def _disabled_test_revert_version(self):
  686. '''
  687. Take a version snapshot, update a resource, and then revert to the
  688. previous vresion.
  689. '''
  690. rsrc_path = '/ldp/test_revert_version'
  691. payload1 = '<> <urn:demo:p1> <urn:demo:o1> .'
  692. payload2 = '<> <urn:demo:p1> <urn:demo:o2> .'
  693. self.client.put(rsrc_path, headers={
  694. 'content-type': 'text/turtle'}, data=payload1)
  695. self.client.post(
  696. rsrc_path + '/fcr:versions', headers={'slug': 'v1'})
  697. v1_rsp = self.client.get(rsrc_path)
  698. v1_gr = Graph().parse(data=v1_rsp.data, format='turtle')
  699. assert v1_gr[
  700. URIRef(g.webroot + '/test_revert_version')
  701. : URIRef('urn:demo:p1')
  702. : URIRef('urn:demo:o1')
  703. ]
  704. self.client.put(rsrc_path, headers={
  705. 'content-type': 'text/turtle'}, data=payload2)
  706. v2_rsp = self.client.get(rsrc_path)
  707. v2_gr = Graph().parse(data=v2_rsp.data, format='turtle')
  708. assert v2_gr[
  709. URIRef(g.webroot + '/test_revert_version')
  710. : URIRef('urn:demo:p1')
  711. : URIRef('urn:demo:o2')
  712. ]
  713. self.client.patch(rsrc_path + '/fcr:versions/v1')
  714. revert_rsp = self.client.get(rsrc_path)
  715. revert_gr = Graph().parse(data=revert_rsp.data, format='turtle')
  716. assert revert_gr[
  717. URIRef(g.webroot + '/test_revert_version')
  718. : URIRef('urn:demo:p1')
  719. : URIRef('urn:demo:o1')
  720. ]
  721. #def test_resurrection(self):
  722. # '''
  723. # Delete and then resurrect a resource.
  724. # Make sure that the resource is resurrected to the latest version.
  725. # '''
  726. # path = '/ldp/test_lazarus'
  727. # self.client.put(path)
  728. # self.client.post(path + '/fcr:versions', headers={'slug': 'v1'})
  729. # self.client.put(
  730. # path, headers={'content-type': 'text/turtle'},
  731. # data=b'<> <urn:demo:p1> <urn:demo:o1> .')
  732. # self.client.post(path + '/fcr:versions', headers={'slug': 'v2'})
  733. # self.client.put(
  734. # path, headers={'content-type': 'text/turtle'},
  735. # data=b'<> <urn:demo:p1> <urn:demo:o2> .')
  736. # self.client.delete(path)
  737. # assert self.client.get(path).status_code == 410
  738. # self.client.post(path + '/fcr:tombstone')
  739. # laz_data = self.client.get(path).data
  740. # laz_gr = Graph().parse(data=laz_data, format='turtle')
  741. # assert laz_gr[
  742. # URIRef(g.webroot + '/test_lazarus')
  743. # : URIRef('urn:demo:p1')
  744. # : URIRef('urn:demo:o2')
  745. # ]