From 53b5a6788ad8416f78b24cc9880d02da73c52d70 Mon Sep 17 00:00:00 2001 From: drebs Date: Fri, 27 Jan 2017 20:30:02 -0200 Subject: [refacor] make proper use of twisted web dyamic resources in server --- server/src/leap/soledad/server/_resource.py | 42 +++++++++++------------------ testing/tests/server/test__resource.py | 27 +++++++++---------- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/server/src/leap/soledad/server/_resource.py b/server/src/leap/soledad/server/_resource.py index 046e20d1..fec290a2 100644 --- a/server/src/leap/soledad/server/_resource.py +++ b/server/src/leap/soledad/server/_resource.py @@ -17,11 +17,9 @@ """ A twisted resource that serves the Soledad Server. """ -from twisted.web.error import Error from twisted.web.resource import Resource from ._blobs import blobs_resource -from ._config import get_config from ._server_info import ServerInfo from ._wsgi import get_sync_resource @@ -35,33 +33,25 @@ class SoledadResource(Resource): for the Soledad Server. """ - def __init__(self, sync_pool=None): - conf = get_config() - self._blobs_enabled = conf['soledad-server']['blobs'] - server_info = ServerInfo(self._blobs_enabled) - sync_resource = get_sync_resource(sync_pool) - self.children = { - '': server_info, - 'sync': sync_resource, - 'blobs': blobs_resource, - 'sync': sync_resource, - } + def __init__(self, conf, sync_pool=None): + Resource.__init__(self) + + blobs_enabled = conf['soledad-server']['blobs'] - def getChild(self, path, request): - """ - Decide which child resource to serve based on the given path. - """ # requests to / return server information - if path == '': - return self.children[''] + server_info = ServerInfo(blobs_enabled) + self.putChild('', server_info) # requests to /blobs will serve blobs if enabled - if path == 'blobs': - if not self._blobs_enabled: - msg = 'Blobs feature is disabled in this server.' - raise Error(403, message=msg) - return self.children['blobs'] + if blobs_enabled: + self.putChild('blobs', blobs_resource) - # other requesta are routed to legacy sync resource + # other requests are routed to legacy sync resource + self._sync_resource = get_sync_resource(sync_pool) + + def getChild(self, path, request): + """ + Route requests to legacy WSGI sync resource dynamically. + """ request.postpath.insert(0, request.prepath.pop()) - return self.children['sync'] + return self._sync_resource diff --git a/testing/tests/server/test__resource.py b/testing/tests/server/test__resource.py index 2a387416..30ef782d 100644 --- a/testing/tests/server/test__resource.py +++ b/testing/tests/server/test__resource.py @@ -19,8 +19,8 @@ Tests for Soledad server main resource. """ from twisted.trial import unittest from twisted.web.test.test_web import DummyRequest -from twisted.web.error import Error from twisted.web.wsgi import WSGIResource +from twisted.web.resource import getChildForRequest from twisted.internet import reactor from leap.soledad.server._resource import SoledadResource @@ -37,33 +37,30 @@ class SoledadResourceTestCase(unittest.TestCase): def test_get_root(self): conf = {'soledad-server': {'blobs': None}} # doesn't matter resource = SoledadResource(conf, sync_pool=_pool) - path = '' - request = DummyRequest([]) - child = resource.getChild(path, request) + request = DummyRequest(['']) + child = getChildForRequest(resource, request) self.assertIsInstance(child, ServerInfo) def test_get_blobs_enabled(self): conf = {'soledad-server': {'blobs': True}} resource = SoledadResource(conf, sync_pool=_pool) - path = 'blobs' - request = DummyRequest([]) - child = resource.getChild(path, request) + request = DummyRequest(['blobs']) + child = getChildForRequest(resource, request) self.assertIsInstance(child, BlobsResource) def test_get_blobs_disabled(self): conf = {'soledad-server': {'blobs': False}} resource = SoledadResource(conf, sync_pool=_pool) - path = 'blobs' - request = DummyRequest([]) - with self.assertRaises(Error): - resource.getChild(path, request) + request = DummyRequest(['blobs']) + child = getChildForRequest(resource, request) + # if blobs is disabled, the request should be routed to sync + self.assertIsInstance(child, WSGIResource) + self.assertIsInstance(child._application, GzipMiddleware) def test_get_sync(self): conf = {'soledad-server': {'blobs': None}} # doesn't matter resource = SoledadResource(conf, sync_pool=_pool) - path = 'sync' # if not 'blobs' or '', should be routed to sync - request = DummyRequest([]) - request.prepath = ['user-db'] - child = resource.getChild(path, request) + request = DummyRequest(['user-db', 'sync-from', 'source-id']) + child = getChildForRequest(resource, request) self.assertIsInstance(child, WSGIResource) self.assertIsInstance(child._application, GzipMiddleware) -- cgit v1.2.3