Skip to content
Snippets Groups Projects
Verified Commit 06ade52b authored by drebs's avatar drebs
Browse files

[refactor] make blobs backend delete_blob() agnostic of twisted.web requests

parent 8f94f9c2
No related branches found
No related tags found
1 merge request!169#9007 - improve and document IBlobsBackend
...@@ -137,11 +137,10 @@ class FilesystemBlobsBackend(object): ...@@ -137,11 +137,10 @@ class FilesystemBlobsBackend(object):
yield fbp.startProducing(blobfile) yield fbp.startProducing(blobfile)
yield self.semaphore.release() yield self.semaphore.release()
def delete_blob(self, user, blob_id, request, namespace=''): def delete_blob(self, user, blob_id, namespace=''):
blob_path = self._get_path(user, blob_id, namespace) blob_path = self._get_path(user, blob_id, namespace)
if not os.path.isfile(blob_path): if not os.path.isfile(blob_path):
request.setResponseCode(404) raise BlobNotFound
return "Blob doesn't exists: %s" % blob_id
self.__touch(blob_path + '.deleted') self.__touch(blob_path + '.deleted')
os.unlink(blob_path) os.unlink(blob_path)
try: try:
...@@ -304,8 +303,12 @@ class BlobsResource(resource.Resource): ...@@ -304,8 +303,12 @@ class BlobsResource(resource.Resource):
def render_DELETE(self, request): def render_DELETE(self, request):
logger.info("http put: %s" % request.path) logger.info("http put: %s" % request.path)
user, blob_id, namespace = self._validate(request) user, blob_id, namespace = self._validate(request)
self._handler.delete_blob(user, blob_id, request, namespace=namespace) try:
self._handler.delete_blob(user, blob_id, namespace=namespace)
return '' return ''
except BlobNotFound:
request.setResponseCode(404)
return "Blob doesn't exists: %s" % blob_id
def render_PUT(self, request): def render_PUT(self, request):
logger.info("http put: %s" % request.path) logger.info("http put: %s" % request.path)
......
...@@ -23,11 +23,6 @@ class IBlobsBackend(Interface): ...@@ -23,11 +23,6 @@ class IBlobsBackend(Interface):
""" """
An interface for a backend that can store blobs. An interface for a backend that can store blobs.
Classes that implement this interface are supposed to be used by
``BlobsResource``, which is a ``twisted.web.resource.Resource`` that serves
the Blobs API. Because of that, their methods receive instances of
``twisted.web.server.Request`` and should use them to serve the Blobs API.
""" """
def read_blob(user, blob_id, namespace=''): def read_blob(user, blob_id, namespace=''):
......
...@@ -172,14 +172,13 @@ class FilesystemBackendTestCase(unittest.TestCase): ...@@ -172,14 +172,13 @@ class FilesystemBackendTestCase(unittest.TestCase):
@mock.patch('leap.soledad.server._blobs.os.unlink') @mock.patch('leap.soledad.server._blobs.os.unlink')
def test_delete_blob(self, unlink_mock): def test_delete_blob(self, unlink_mock):
backend = _blobs.FilesystemBlobsBackend(blobs_path=self.tempdir) backend = _blobs.FilesystemBlobsBackend(blobs_path=self.tempdir)
request = DummyRequest([''])
# write a blob... # write a blob...
path = backend._get_path('user', 'blob_id', '') path = backend._get_path('user', 'blob_id', '')
mkdir_p(os.path.split(path)[0]) mkdir_p(os.path.split(path)[0])
with open(path, "w") as f: with open(path, "w") as f:
f.write("bl0b") f.write("bl0b")
# ...and delete it # ...and delete it
backend.delete_blob('user', 'blob_id', request) backend.delete_blob('user', 'blob_id')
unlink_mock.assert_any_call(backend._get_path('user', unlink_mock.assert_any_call(backend._get_path('user',
'blob_id')) 'blob_id'))
unlink_mock.assert_any_call(backend._get_path('user', unlink_mock.assert_any_call(backend._get_path('user',
...@@ -189,14 +188,13 @@ class FilesystemBackendTestCase(unittest.TestCase): ...@@ -189,14 +188,13 @@ class FilesystemBackendTestCase(unittest.TestCase):
@mock.patch('leap.soledad.server._blobs.os.unlink') @mock.patch('leap.soledad.server._blobs.os.unlink')
def test_delete_blob_custom_namespace(self, unlink_mock): def test_delete_blob_custom_namespace(self, unlink_mock):
backend = _blobs.FilesystemBlobsBackend(blobs_path=self.tempdir) backend = _blobs.FilesystemBlobsBackend(blobs_path=self.tempdir)
request = DummyRequest([''])
# write a blob... # write a blob...
path = backend._get_path('user', 'blob_id', 'trash') path = backend._get_path('user', 'blob_id', 'trash')
mkdir_p(os.path.split(path)[0]) mkdir_p(os.path.split(path)[0])
with open(path, "w") as f: with open(path, "w") as f:
f.write("bl0b") f.write("bl0b")
# ...and delete it # ...and delete it
backend.delete_blob('user', 'blob_id', request, namespace='trash') backend.delete_blob('user', 'blob_id', namespace='trash')
unlink_mock.assert_any_call(backend._get_path('user', unlink_mock.assert_any_call(backend._get_path('user',
'blob_id', 'blob_id',
'trash')) 'trash'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment