diff options
author | drebs <drebs@leap.se> | 2017-12-08 21:54:13 -0200 |
---|---|---|
committer | drebs <drebs@leap.se> | 2017-12-13 13:43:48 -0200 |
commit | ad1e3a4a4863006b3415e619138c3bae88da3941 (patch) | |
tree | 9ae4b6612f8a15874b47b085c73d542d9c237db0 | |
parent | 06ade52b27800ca94c1abf73964975a5bf717f04 (diff) |
[refactor] make delete_blob() return a deferred
-rw-r--r-- | src/leap/soledad/server/_blobs.py | 13 | ||||
-rw-r--r-- | src/leap/soledad/server/interfaces.py | 3 | ||||
-rw-r--r-- | tests/blobs/test_fs_backend.py | 6 |
3 files changed, 16 insertions, 6 deletions
diff --git a/src/leap/soledad/server/_blobs.py b/src/leap/soledad/server/_blobs.py index c0b3b237..f9166ce0 100644 --- a/src/leap/soledad/server/_blobs.py +++ b/src/leap/soledad/server/_blobs.py @@ -147,6 +147,7 @@ class FilesystemBlobsBackend(object): os.unlink(blob_path + '.flags') except Exception: pass + return defer.succeed(None) def get_blob_size(self, user, blob_id, namespace=''): blob_path = self._get_path(user, blob_id, namespace) @@ -303,13 +304,17 @@ class BlobsResource(resource.Resource): def render_DELETE(self, request): logger.info("http put: %s" % request.path) user, blob_id, namespace = self._validate(request) - try: - self._handler.delete_blob(user, blob_id, namespace=namespace) - return '' - except BlobNotFound: + + def catchBlobNotFound(failure): + failure.trap(BlobNotFound) request.setResponseCode(404) return "Blob doesn't exists: %s" % blob_id + d = self._handler.delete_blob(user, blob_id, namespace=namespace) + d.addCallback(lambda _: request.finish()) + d.addErrback(catchBlobNotFound) + return NOT_DONE_YET + def render_PUT(self, request): logger.info("http put: %s" % request.path) user, blob_id, namespace = self._validate(request) diff --git a/src/leap/soledad/server/interfaces.py b/src/leap/soledad/server/interfaces.py index 3bf6eaa9..f9257c9b 100644 --- a/src/leap/soledad/server/interfaces.py +++ b/src/leap/soledad/server/interfaces.py @@ -69,6 +69,9 @@ class IBlobsBackend(Interface): :type blob_id: str :param namespace: An optional namespace for the blob. :type namespace: str + + :return: A deferred that fires when the blob has been deleted. + :rtype: twisted.internet.defer.Deferred """ def get_blob_size(user, blob_id, namespace=''): diff --git a/tests/blobs/test_fs_backend.py b/tests/blobs/test_fs_backend.py index 4dbee595..59b7976f 100644 --- a/tests/blobs/test_fs_backend.py +++ b/tests/blobs/test_fs_backend.py @@ -170,6 +170,7 @@ class FilesystemBackendTestCase(unittest.TestCase): @pytest.mark.usefixtures("method_tmpdir") @mock.patch('leap.soledad.server._blobs.os.unlink') + @defer.inlineCallbacks def test_delete_blob(self, unlink_mock): backend = _blobs.FilesystemBlobsBackend(blobs_path=self.tempdir) # write a blob... @@ -178,7 +179,7 @@ class FilesystemBackendTestCase(unittest.TestCase): with open(path, "w") as f: f.write("bl0b") # ...and delete it - backend.delete_blob('user', 'blob_id') + yield backend.delete_blob('user', 'blob_id') unlink_mock.assert_any_call(backend._get_path('user', 'blob_id')) unlink_mock.assert_any_call(backend._get_path('user', @@ -186,6 +187,7 @@ class FilesystemBackendTestCase(unittest.TestCase): @pytest.mark.usefixtures("method_tmpdir") @mock.patch('leap.soledad.server._blobs.os.unlink') + @defer.inlineCallbacks def test_delete_blob_custom_namespace(self, unlink_mock): backend = _blobs.FilesystemBlobsBackend(blobs_path=self.tempdir) # write a blob... @@ -194,7 +196,7 @@ class FilesystemBackendTestCase(unittest.TestCase): with open(path, "w") as f: f.write("bl0b") # ...and delete it - backend.delete_blob('user', 'blob_id', namespace='trash') + yield backend.delete_blob('user', 'blob_id', namespace='trash') unlink_mock.assert_any_call(backend._get_path('user', 'blob_id', 'trash')) |