summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrebs <drebs@leap.se>2017-03-19 10:48:39 +0100
committerdrebs <drebs@leap.se>2017-04-04 18:27:36 +0200
commit123880b6e524019d25404a85f04a369705a318b8 (patch)
tree3c1d0beebdbf7ef6a3b02877ae55058bcd41d66b
parent36183b7c9daa68ca120ef80767f311faba588cc6 (diff)
[refactor] pass configured blobs resource to server auth class
-rw-r--r--server/src/leap/soledad/server/_blobs.py11
-rw-r--r--server/src/leap/soledad/server/_resource.py11
-rw-r--r--server/src/leap/soledad/server/auth.py4
-rw-r--r--testing/tests/server/test__resource.py20
4 files changed, 22 insertions, 24 deletions
diff --git a/server/src/leap/soledad/server/_blobs.py b/server/src/leap/soledad/server/_blobs.py
index d5476aae..f50e3da8 100644
--- a/server/src/leap/soledad/server/_blobs.py
+++ b/server/src/leap/soledad/server/_blobs.py
@@ -35,10 +35,9 @@ from twisted.web.client import FileBodyProducer
from twisted.web.server import NOT_DONE_YET
from zope.interface import Interface, implementer
-from leap.soledad.server._config import get_config
-__all__ = ['BlobsResource', 'blobs_resource']
+__all__ = ['BlobsResource']
logger = Logger()
@@ -199,12 +198,6 @@ class BlobsResource(resource.Resource):
return self._handler.write_blob(user, blob_id, request)
-# provide a configured instance of the resource
-_config = get_config()
-_path = _config['blobs_path']
-blobs_resource = BlobsResource(_path)
-
-
if __name__ == '__main__':
# A dummy blob server
# curl -X PUT --data-binary @/tmp/book.pdf localhost:9000/user/someid
@@ -220,7 +213,7 @@ if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
- parser.add_argument('--port', default=9000)
+ parser.add_argument('--port', default=9000, type=int)
parser.add_argument('--path', default='/tmp/blobs/user')
args = parser.parse_args()
diff --git a/server/src/leap/soledad/server/_resource.py b/server/src/leap/soledad/server/_resource.py
index 7a00ad9a..49c4b742 100644
--- a/server/src/leap/soledad/server/_resource.py
+++ b/server/src/leap/soledad/server/_resource.py
@@ -19,7 +19,6 @@ A twisted resource that serves the Soledad Server.
"""
from twisted.web.resource import Resource
-from ._blobs import blobs_resource
from ._server_info import ServerInfo
from ._wsgi import get_sync_resource
@@ -56,12 +55,12 @@ class SoledadResource(Resource):
for the Soledad Server.
"""
- def __init__(self, enable_blobs=False, sync_pool=None):
+ def __init__(self, blobs_resource=None, sync_pool=None):
"""
Initialize the Soledad resource.
- :param enable_blobs: Whether the blobs feature should be enabled.
- :type enable_blobs: bool
+ :param blobs_resource: a resource to serve blobs, if enabled.
+ :type blobs_resource: _blobs.BlobsResource
:param sync_pool: A pool to pass to the WSGI sync resource.
:type sync_pool: twisted.python.threadpool.ThreadPool
@@ -69,11 +68,11 @@ class SoledadResource(Resource):
Resource.__init__(self)
# requests to / return server information
- server_info = ServerInfo(enable_blobs)
+ server_info = ServerInfo(bool(blobs_resource))
self.putChild('', server_info)
# requests to /blobs will serve blobs if enabled
- if enable_blobs:
+ if blobs_resource:
self.putChild('blobs', blobs_resource)
# other requests are routed to legacy sync resource
diff --git a/server/src/leap/soledad/server/auth.py b/server/src/leap/soledad/server/auth.py
index b5744fe9..7f99e23d 100644
--- a/server/src/leap/soledad/server/auth.py
+++ b/server/src/leap/soledad/server/auth.py
@@ -39,6 +39,7 @@ from twisted.web.resource import IResource
from leap.soledad.common.couch import couch_server
from ._resource import SoledadResource, SoledadAnonResource
+from ._blobs import BlobsResource
from ._config import get_config
@@ -53,10 +54,11 @@ class SoledadRealm(object):
if conf is None:
conf = get_config()
blobs = conf['blobs']
+ blobs_resource = BlobsResource(conf['blobs_path']) if blobs else None
self.anon_resource = SoledadAnonResource(
enable_blobs=blobs)
self.auth_resource = SoledadResource(
- enable_blobs=blobs,
+ blobs_resource=blobs_resource,
sync_pool=sync_pool)
def requestAvatar(self, avatarId, mind, *interfaces):
diff --git a/testing/tests/server/test__resource.py b/testing/tests/server/test__resource.py
index c066435e..be02b5a1 100644
--- a/testing/tests/server/test__resource.py
+++ b/testing/tests/server/test__resource.py
@@ -35,22 +35,25 @@ _pool = reactor.getThreadPool()
class SoledadResourceTestCase(unittest.TestCase):
def test_get_root(self):
- enable_blobs = None # doesn't matter
- resource = SoledadResource(enable_blobs=enable_blobs, sync_pool=_pool)
+ blobs_resource = None # doesn't matter
+ resource = SoledadResource(
+ blobs_resource=blobs_resource, sync_pool=_pool)
request = DummyRequest([''])
child = getChildForRequest(resource, request)
self.assertIsInstance(child, ServerInfo)
def test_get_blobs_enabled(self):
- enable_blobs = True
- resource = SoledadResource(enable_blobs=enable_blobs, sync_pool=_pool)
+ blobs_resource = BlobsResource('/tmp')
+ resource = SoledadResource(
+ blobs_resource=blobs_resource, sync_pool=_pool)
request = DummyRequest(['blobs'])
child = getChildForRequest(resource, request)
self.assertIsInstance(child, BlobsResource)
def test_get_blobs_disabled(self):
- enable_blobs = False
- resource = SoledadResource(enable_blobs=enable_blobs, sync_pool=_pool)
+ blobs_resource = None
+ resource = SoledadResource(
+ blobs_resource=blobs_resource, sync_pool=_pool)
request = DummyRequest(['blobs'])
child = getChildForRequest(resource, request)
# if blobs is disabled, the request should be routed to sync
@@ -58,8 +61,9 @@ class SoledadResourceTestCase(unittest.TestCase):
self.assertIsInstance(child._application, GzipMiddleware)
def test_get_sync(self):
- enable_blobs = None # doesn't matter
- resource = SoledadResource(enable_blobs=enable_blobs, sync_pool=_pool)
+ blobs_resource = None # doesn't matter
+ resource = SoledadResource(
+ blobs_resource=blobs_resource, sync_pool=_pool)
request = DummyRequest(['user-db', 'sync-from', 'source-id'])
child = getChildForRequest(resource, request)
self.assertIsInstance(child, WSGIResource)