Procházet zdrojové kódy

REST API endpoint for fixity check.

Stefano Cossu před 5 roky
rodič
revize
9c2f182b01
2 změnil soubory, kde provedl 94 přidání a 4 odebrání
  1. 25 4
      lakesuperior/endpoints/admin.py
  2. 69 0
      tests/2_endpoints/test_admin.py

+ 25 - 4
lakesuperior/endpoints/admin.py

@@ -3,6 +3,8 @@ import logging
 from flask import Blueprint, render_template
 
 from lakesuperior.api import admin as admin_api
+from lakesuperior.exceptions import (
+    ChecksumValidationError, ResourceNotExistsError, TombstoneError)
 
 
 # Admin interface and REST API.
@@ -24,15 +26,16 @@ def stats():
         https://stackoverflow.com/a/1094933/3758232
 
         :param int num: Size value in bytes.
-        :param string suffix: Suffix label (defaults to `B`).
+        :param str suffix: Suffix label (defaults to ``b``).
 
-        @return string Formatted size to largest fitting unit.
+        :rtype: str
+        :return: Formatted size to largest fitting unit.
         """
         for unit in ['','K','M','G','T','P','E','Z']:
             if abs(num) < 1024.0:
-                return "{:3.1f} {}{}".format(num, unit, suffix)
+                return f'{num:3.1f} {unit}{suffix}'
             num /= 1024.0
-        return "{:.1f} {}{}".format(num, 'Y', suffix)
+        return f'{num:.1f} Y{suffix}'
 
     repo_stats = admin_api.stats()
 
@@ -48,3 +51,21 @@ def admin_tools():
     @TODO stub.
     """
     return render_template('admin_tools.html')
+
+
+@admin.route('/<path:uid>/fixity', methods=['GET'])
+def fixity_check(uid):
+    """
+    Check the fixity of a resource.
+    """
+    uid = '/' + uid.strip('/')
+    try:
+        admin_api.fixity_check(uid)
+    except ResourceNotExistsError as e:
+        return str(e), 404
+    except TombstoneError as e:
+        return str(e), 410
+    except ChecksumValidationError as e:
+        return str(e), 412
+
+    return f'Checksum for {uid} passed.', 200

+ 69 - 0
tests/2_endpoints/test_admin.py

@@ -0,0 +1,69 @@
+import pytest
+
+from io import BytesIO
+from uuid import uuid4
+
+from lakesuperior.api import resource as rsrc_api
+
+
+@pytest.mark.usefixtures('client_class')
+@pytest.mark.usefixtures('db')
+class TestAdminApi:
+    """
+    Test admin endpoint.
+    """
+
+    def test_fixity_check_ok(self):
+        """
+        Verify that fixity check passes for a non-corrupted resource.
+        """
+        uid = uuid4()
+        content = uuid4().bytes
+        path = f'/ldp/{uid}'
+        fix_path = f'/admin/{uid}/fixity'
+
+        self.client.put(
+            path, data=content, headers={'content-type': 'text/plain'})
+
+        assert self.client.get(fix_path).status_code == 200
+
+
+    def test_fixity_check_corrupt(self):
+        """
+        Verify that fixity check fails for a corrupted resource.
+        """
+        uid = uuid4()
+        content = uuid4().bytes
+        path = f'/ldp/{uid}'
+        fix_path = f'/admin/{uid}/fixity'
+
+        self.client.put(
+            path, data=content, headers={'content-type': 'text/plain'})
+
+        rsrc = rsrc_api.get(f'/{uid}')
+
+        with open(rsrc.local_path, 'wb') as fh:
+            fh.write(uuid4().bytes)
+
+        assert self.client.get(fix_path).status_code == 412
+
+
+    def test_fixity_check_missing(self):
+        """
+        Verify that fixity check is not performed on a missing resource.
+        """
+        uid = uuid4()
+        content = uuid4().bytes
+        path = f'/ldp/{uid}'
+        fix_path = f'/admin/{uid}/fixity'
+
+        assert self.client.get(fix_path).status_code == 404
+
+        self.client.put(
+            path, data=content, headers={'content-type': 'text/plain'})
+
+        self.client.delete(path)
+
+        assert self.client.get(fix_path).status_code == 410
+
+