From 30c47eea2b10f10204b5d61ecb550edd24e59067 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 26 Jan 2017 08:24:39 -0200 Subject: [test] add tests for server resource and server info --- testing/tests/server/test__resource.py | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 testing/tests/server/test__resource.py (limited to 'testing/tests/server/test__resource.py') diff --git a/testing/tests/server/test__resource.py b/testing/tests/server/test__resource.py new file mode 100644 index 00000000..0ea46d6a --- /dev/null +++ b/testing/tests/server/test__resource.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# test__resource.py +# Copyright (C) 2017 LEAP +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" +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 leap.soledad.server._resource import SoledadResource +from leap.soledad.server._server_info import ServerInfo +from leap.soledad.server._blobs import BlobsResource + + +conf_blobs_false = {'soledad-server': {'blobs': False}} + + +class SoledadResourceTestCase(unittest.TestCase): + + def test_get_root(self): + conf = {'soledad-server': {'blobs': None}} # doesn't matter + resource = SoledadResource(conf) + path = '' + request = DummyRequest([]) + child = resource.getChild(path, request) + self.assertIsInstance(child, ServerInfo) + + def test_get_blobs_enabled(self): + conf = {'soledad-server': {'blobs': True}} + resource = SoledadResource(conf) + path = 'blobs' + request = DummyRequest([]) + child = resource.getChild(path, request) + self.assertIsInstance(child, BlobsResource) + + def test_get_blobs_disabled(self): + conf = {'soledad-server': {'blobs': False}} + resource = SoledadResource(conf) + path = 'blobs' + request = DummyRequest([]) + with self.assertRaises(Error): + resource.getChild(path, request) + + def test_get_sync(self): + conf = {'soledad-server': {'blobs': None}} # doesn't matter + resource = SoledadResource(conf) + path = 'sync' # if not 'blobs' or '', should be routed to sync + request = DummyRequest([]) + request.prepath = ['user-db'] + child = resource.getChild(path, request) + self.assertIsInstance(child, WSGIResource) -- cgit v1.2.3 From 7432b72471fa3de03d13b9d35f6004d14deae63d Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 26 Jan 2017 18:37:45 -0200 Subject: [refactor] move wsgi sync setup to its own module --- testing/tests/server/test__resource.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'testing/tests/server/test__resource.py') diff --git a/testing/tests/server/test__resource.py b/testing/tests/server/test__resource.py index 0ea46d6a..b5285195 100644 --- a/testing/tests/server/test__resource.py +++ b/testing/tests/server/test__resource.py @@ -25,6 +25,7 @@ from twisted.web.wsgi import WSGIResource from leap.soledad.server._resource import SoledadResource from leap.soledad.server._server_info import ServerInfo from leap.soledad.server._blobs import BlobsResource +from leap.soledad.server.gzip_middleware import GzipMiddleware conf_blobs_false = {'soledad-server': {'blobs': False}} @@ -64,3 +65,4 @@ class SoledadResourceTestCase(unittest.TestCase): request.prepath = ['user-db'] child = resource.getChild(path, request) self.assertIsInstance(child, WSGIResource) + self.assertIsInstance(child._application, GzipMiddleware) -- cgit v1.2.3 From 1f1a9847117d23a767e99fe80484baf232375d36 Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 26 Jan 2017 19:18:15 -0200 Subject: [refactor] allow passing threadpool pool for server sync resource --- testing/tests/server/test__resource.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'testing/tests/server/test__resource.py') diff --git a/testing/tests/server/test__resource.py b/testing/tests/server/test__resource.py index b5285195..2a387416 100644 --- a/testing/tests/server/test__resource.py +++ b/testing/tests/server/test__resource.py @@ -21,6 +21,7 @@ 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.internet import reactor from leap.soledad.server._resource import SoledadResource from leap.soledad.server._server_info import ServerInfo @@ -28,14 +29,14 @@ from leap.soledad.server._blobs import BlobsResource from leap.soledad.server.gzip_middleware import GzipMiddleware -conf_blobs_false = {'soledad-server': {'blobs': False}} +_pool = reactor.getThreadPool() class SoledadResourceTestCase(unittest.TestCase): def test_get_root(self): conf = {'soledad-server': {'blobs': None}} # doesn't matter - resource = SoledadResource(conf) + resource = SoledadResource(conf, sync_pool=_pool) path = '' request = DummyRequest([]) child = resource.getChild(path, request) @@ -43,7 +44,7 @@ class SoledadResourceTestCase(unittest.TestCase): def test_get_blobs_enabled(self): conf = {'soledad-server': {'blobs': True}} - resource = SoledadResource(conf) + resource = SoledadResource(conf, sync_pool=_pool) path = 'blobs' request = DummyRequest([]) child = resource.getChild(path, request) @@ -51,7 +52,7 @@ class SoledadResourceTestCase(unittest.TestCase): def test_get_blobs_disabled(self): conf = {'soledad-server': {'blobs': False}} - resource = SoledadResource(conf) + resource = SoledadResource(conf, sync_pool=_pool) path = 'blobs' request = DummyRequest([]) with self.assertRaises(Error): @@ -59,7 +60,7 @@ class SoledadResourceTestCase(unittest.TestCase): def test_get_sync(self): conf = {'soledad-server': {'blobs': None}} # doesn't matter - resource = SoledadResource(conf) + resource = SoledadResource(conf, sync_pool=_pool) path = 'sync' # if not 'blobs' or '', should be routed to sync request = DummyRequest([]) request.prepath = ['user-db'] -- cgit v1.2.3 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 --- testing/tests/server/test__resource.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'testing/tests/server/test__resource.py') 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 From a2f041de7f1ea653f078ac9cd532e2d2b774248f Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 2 Feb 2017 11:45:57 -0200 Subject: [refactor] parametrize blobs toggling in soledad server resource --- testing/tests/server/test__resource.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'testing/tests/server/test__resource.py') diff --git a/testing/tests/server/test__resource.py b/testing/tests/server/test__resource.py index 30ef782d..c066435e 100644 --- a/testing/tests/server/test__resource.py +++ b/testing/tests/server/test__resource.py @@ -35,22 +35,22 @@ _pool = reactor.getThreadPool() class SoledadResourceTestCase(unittest.TestCase): def test_get_root(self): - conf = {'soledad-server': {'blobs': None}} # doesn't matter - resource = SoledadResource(conf, sync_pool=_pool) + enable_blobs = None # doesn't matter + resource = SoledadResource(enable_blobs=enable_blobs, sync_pool=_pool) 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) + enable_blobs = True + resource = SoledadResource(enable_blobs=enable_blobs, sync_pool=_pool) 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) + enable_blobs = False + resource = SoledadResource(enable_blobs=enable_blobs, sync_pool=_pool) request = DummyRequest(['blobs']) child = getChildForRequest(resource, request) # if blobs is disabled, the request should be routed to sync @@ -58,8 +58,8 @@ class SoledadResourceTestCase(unittest.TestCase): self.assertIsInstance(child._application, GzipMiddleware) def test_get_sync(self): - conf = {'soledad-server': {'blobs': None}} # doesn't matter - resource = SoledadResource(conf, sync_pool=_pool) + enable_blobs = None # doesn't matter + resource = SoledadResource(enable_blobs=enable_blobs, sync_pool=_pool) request = DummyRequest(['user-db', 'sync-from', 'source-id']) child = getChildForRequest(resource, request) self.assertIsInstance(child, WSGIResource) -- cgit v1.2.3