Browse Source

Add HTTP tests providing random image.

Stefano Cossu 6 years ago
parent
commit
58cd3a6cb0
4 changed files with 129 additions and 371 deletions
  1. 62 1
      conftest.py
  2. 1 0
      doc/notes/TODO
  3. 1 366
      tests/data/marcel_duchamp_ULAN.ttl
  4. 65 4
      tests/endpoints/test_ldp.py

+ 62 - 1
conftest.py

@@ -1,10 +1,16 @@
+import io
 import sys
 sys.path.append('.')
-
+import numpy
+import random
 import uuid
 
+from hashlib import sha1
+
 import pytest
 
+from PIL import Image
+
 from lakesuperior.app import create_app
 from lakesuperior.config_parser import config
 from lakesuperior.store_layouts.rdf.graph_store_connector import \
@@ -39,3 +45,58 @@ def db(app):
         db.ds.remove_graph(g)
     db.store.commit()
 
+
+@pytest.fixture
+def rnd_image(rnd_utf8_string):
+    '''
+    Generate a square image with random color tiles.
+    '''
+    ts = 8 # Tile width and height. @TODO parametrize.
+    ims = 256 # Image width and height. @TODO parametrize.
+    imarray = numpy.random.rand(ts, ts, 3) * 255
+    im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
+    im = im.resize((ims, ims), Image.NEAREST)
+
+    imf = io.BytesIO()
+    im.save(imf, format='png')
+    imf.seek(0)
+    hash = sha1(imf.read()).hexdigest()
+
+    return {
+        'content' : imf,
+        'hash' : hash,
+        'filename' : rnd_utf8_string + '.png'
+    }
+
+
+@pytest.fixture
+def rnd_utf8_string():
+    '''
+    Generate a random UTF-8 string.
+    '''
+    # @TODO Update this to include code point ranges to be sampled
+    include_ranges = [
+        ( 0x0021, 0x0021 ),
+        ( 0x0023, 0x0026 ),
+        ( 0x0028, 0x007E ),
+        ( 0x00A1, 0x00AC ),
+        ( 0x00AE, 0x00FF ),
+        ( 0x0100, 0x017F ),
+        ( 0x0180, 0x024F ),
+        ( 0x2C60, 0x2C7F ),
+        ( 0x16A0, 0x16F0 ),
+        ( 0x0370, 0x0377 ),
+        ( 0x037A, 0x037E ),
+        ( 0x0384, 0x038A ),
+        ( 0x038C, 0x038C ),
+    ]
+    length = 64 # String length. @TODO parametrize.
+    alphabet = [
+        chr(code_point) for current_range in include_ranges
+            for code_point in range(current_range[0], current_range[1] + 1)
+    ]
+    return ''.join(random.choice(alphabet) for i in range(length))
+
+
+
+

+ 1 - 0
doc/notes/TODO

@@ -25,6 +25,7 @@
 - [ ] Optimize queries
 - [ ] Messaging SPI
 - [ ] Hook up Hyrax
+- [ ] Documentation
 
 # Alpha 2 TODO
 

File diff suppressed because it is too large
+ 1 - 366
tests/data/marcel_duchamp_ULAN.ttl


+ 65 - 4
tests/endpoints/test_ldp.py

@@ -1,7 +1,12 @@
 import pytest
 import uuid
 
+from hashlib import sha1
+
 from flask import url_for
+from rdflib import Graph
+from rdflib.namespace import RDF
+from rdflib.term import Literal, URIRef
 
 
 @pytest.fixture(scope='module')
@@ -10,10 +15,11 @@ def random_uuid():
 
 
 def test_get_root_node(client, db):
-    assert client.get(url_for('ldp.get_resource')).status_code == 200
+    #assert client.get(url_for('ldp.get_resource')).status_code == 200
+    assert client.get('/ldp').status_code == 200
 
 
-def test_post_resource(client, db):
+def test_post_resource(client):
     '''
     Check response headers for a POST operation with empty payload.
     '''
@@ -22,7 +28,7 @@ def test_post_resource(client, db):
     assert 'Location' in res.headers
 
 
-def test_put_empty_resource(client, db, random_uuid):
+def test_put_empty_resource(client, random_uuid):
     '''
     Check response headers for a PUT operation with empty payload.
     '''
@@ -30,6 +36,40 @@ def test_put_empty_resource(client, db, random_uuid):
     assert res.status_code == 201
 
 
+def test_put_ldp_rs(client):
+    '''
+    PUT a resource with RDF payload and verify.
+    '''
+    with open('tests/data/marcel_duchamp_single_subject.ttl', 'rb') as f:
+        client.put('/ldp/ldprs01', data=f, content_type='text/turtle')
+
+    resp = client.get('/ldp/ldprs01', headers={'accept' : 'text/turtle'})
+    assert resp.status_code == 200
+
+    g = Graph().parse(data=resp.data, format='text/turtle')
+    assert URIRef('http://vocab.getty.edu/ontology#Subject') in \
+            g.objects(None, RDF.type)
+
+
+def test_put_ldp_nr(client, rnd_image, rnd_utf8_string):
+    '''
+    PUT a resource with binary payload and verify checksums.
+    '''
+    rnd_image['content'].seek(0)
+    print('Filename: {}'.format(rnd_image['filename']))
+    print('Data: {}'.format(rnd_image['content']))
+    client.put('/ldp/ldpnr01', data=rnd_image['content'], headers={
+            'Content-Disposition' : 'attachment; filename={}'.format(
+            rnd_image['filename'])})
+
+    resp = client.get('/ldp/ldpnr01', headers={'accept' : 'image/png'})
+    assert resp.status_code == 200
+    rnd_image['content'].seek(0)
+    #print('Original sample: {}'.format(rnd_image['content'].read(64)))
+    #print('Response sample: {}'.format(resp.data))
+    assert sha1(resp.data).hexdigest() == rnd_image['hash']
+
+
 def test_put_existing_resource(client, db, random_uuid):
     '''
     Trying to PUT an existing resource should:
@@ -37,8 +77,29 @@ def test_put_existing_resource(client, db, random_uuid):
     - Return a 204 if the payload is empty
     - Return a 204 if the payload is RDF, server-managed triples are included
       and the 'Prefer' header is set to 'handling=lenient'
-    - Return a 409 (ServerManagedTermError) if the payload is RDF,
+    - Return a 412 (ServerManagedTermError) if the payload is RDF,
       server-managed triples are included and handling is set to 'strict'
     '''
     assert client.get('/ldp/{}'.format(random_uuid)).status_code == 200
     assert client.put('/ldp/{}'.format(random_uuid)).status_code == 204
+    with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
+        rsp_len = client.put(
+            '/ldp/{}'.format(random_uuid),
+            headers={
+                'Prefer' : 'handling=lenient',
+                'Content-Type' : 'text/turtle',
+            },
+            data=f
+        )
+    assert rsp_len.status_code == 204
+    with open('tests/data/rdf_payload_w_srv_mgd_trp.ttl', 'rb') as f:
+        rsp_strict = client.put(
+            '/ldp/{}'.format(random_uuid),
+            headers={
+                'Prefer' : 'handling=strict',
+                'Content-Type' : 'text/turtle',
+            },
+            data=f
+        )
+    assert rsp_strict.status_code == 412
+

Some files were not shown because too many files changed in this diff