Skip to content
Snippets Groups Projects
Commit 6f823928 authored by drebs's avatar drebs
Browse files

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

parent a78ed867
No related branches found
No related tags found
No related merge requests found
......@@ -137,11 +137,10 @@ class FilesystemBlobsBackend(object):
yield fbp.startProducing(blobfile)
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)
if not os.path.isfile(blob_path):
request.setResponseCode(404)
return "Blob doesn't exists: %s" % blob_id
raise BlobNotFound
self.__touch(blob_path + '.deleted')
os.unlink(blob_path)
try:
......@@ -304,8 +303,12 @@ class BlobsResource(resource.Resource):
def render_DELETE(self, request):
logger.info("http put: %s" % request.path)
user, blob_id, namespace = self._validate(request)
self._handler.delete_blob(user, blob_id, request, namespace=namespace)
return ''
try:
self._handler.delete_blob(user, blob_id, namespace=namespace)
return ''
except BlobNotFound:
request.setResponseCode(404)
return "Blob doesn't exists: %s" % blob_id
def render_PUT(self, request):
logger.info("http put: %s" % request.path)
......
......@@ -23,11 +23,6 @@ class IBlobsBackend(Interface):
"""
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=''):
......
......@@ -172,14 +172,13 @@ class FilesystemBackendTestCase(unittest.TestCase):
@mock.patch('leap.soledad.server._blobs.os.unlink')
def test_delete_blob(self, unlink_mock):
backend = _blobs.FilesystemBlobsBackend(blobs_path=self.tempdir)
request = DummyRequest([''])
# write a blob...
path = backend._get_path('user', 'blob_id', '')
mkdir_p(os.path.split(path)[0])
with open(path, "w") as f:
f.write("bl0b")
# ...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',
'blob_id'))
unlink_mock.assert_any_call(backend._get_path('user',
......@@ -189,14 +188,13 @@ class FilesystemBackendTestCase(unittest.TestCase):
@mock.patch('leap.soledad.server._blobs.os.unlink')
def test_delete_blob_custom_namespace(self, unlink_mock):
backend = _blobs.FilesystemBlobsBackend(blobs_path=self.tempdir)
request = DummyRequest([''])
# write a blob...
path = backend._get_path('user', 'blob_id', 'trash')
mkdir_p(os.path.split(path)[0])
with open(path, "w") as f:
f.write("bl0b")
# ...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',
'blob_id',
'trash'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment