test_ldp.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import pytest
  2. import uuid
  3. from hashlib import sha1
  4. from flask import url_for
  5. from rdflib import Graph
  6. from rdflib.namespace import RDF
  7. from rdflib.term import Literal, URIRef
  8. @pytest.fixture(scope='module')
  9. def random_uuid():
  10. return str(uuid.uuid4())
  11. def test_get_root_node(client, db):
  12. #assert client.get(url_for('ldp.get_resource')).status_code == 200
  13. assert client.get('/ldp').status_code == 200
  14. def test_post_resource(client):
  15. '''
  16. Check response headers for a POST operation with empty payload.
  17. '''
  18. res = client.post('/ldp/')
  19. assert res.status_code == 201
  20. assert 'Location' in res.headers
  21. def test_put_empty_resource(client, random_uuid):
  22. '''
  23. Check response headers for a PUT operation with empty payload.
  24. '''
  25. res = client.put('/ldp/{}'.format(random_uuid))
  26. assert res.status_code == 201
  27. def test_put_ldp_rs(client):
  28. '''
  29. PUT a resource with RDF payload and verify.
  30. '''
  31. with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
  32. client.put('/ldp/ldprs01', data=f, content_type='text/turtle')
  33. resp = client.get('/ldp/ldprs01', headers={'accept' : 'text/turtle'})
  34. assert resp.status_code == 200
  35. g = Graph().parse(data=resp.data, format='text/turtle')
  36. assert URIRef('http://vocab.getty.edu/ontology#Subject') in \
  37. g.objects(None, RDF.type)
  38. def test_put_ldp_nr(client, rnd_image, rnd_utf8_string):
  39. '''
  40. PUT a resource with binary payload and verify checksums.
  41. '''
  42. rnd_image['content'].seek(0)
  43. print('Filename: {}'.format(rnd_image['filename']))
  44. print('Data: {}'.format(rnd_image['content']))
  45. client.put('/ldp/ldpnr01', data=rnd_image['content'], headers={
  46. 'Content-Disposition' : 'attachment; filename={}'.format(
  47. rnd_image['filename'])})
  48. resp = client.get('/ldp/ldpnr01', headers={'accept' : 'image/png'})
  49. assert resp.status_code == 200
  50. rnd_image['content'].seek(0)
  51. #print('Original sample: {}'.format(rnd_image['content'].read(64)))
  52. #print('Response sample: {}'.format(resp.data))
  53. assert sha1(resp.data).hexdigest() == rnd_image['hash']
  54. def test_put_existing_resource(client, db, random_uuid):
  55. '''
  56. Trying to PUT an existing resource should:
  57. - Return a 204 if the payload is empty
  58. - Return a 204 if the payload is RDF, server-managed triples are included
  59. and the 'Prefer' header is set to 'handling=lenient'
  60. - Return a 412 (ServerManagedTermError) if the payload is RDF,
  61. server-managed triples are included and handling is set to 'strict'
  62. '''
  63. assert client.get('/ldp/{}'.format(random_uuid)).status_code == 200
  64. assert client.put('/ldp/{}'.format(random_uuid)).status_code == 204
  65. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  66. rsp_len = client.put(
  67. '/ldp/{}'.format(random_uuid),
  68. headers={
  69. 'Prefer' : 'handling=lenient',
  70. 'Content-Type' : 'text/turtle',
  71. },
  72. data=f
  73. )
  74. assert rsp_len.status_code == 204
  75. with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
  76. rsp_strict = client.put(
  77. '/ldp/{}'.format(random_uuid),
  78. headers={
  79. 'Prefer' : 'handling=strict',
  80. 'Content-Type' : 'text/turtle',
  81. },
  82. data=f
  83. )
  84. assert rsp_strict.status_code == 412