diff options
author | Blake Williams <github@qzxj.net> | 2016-06-02 19:04:42 +1000 |
---|---|---|
committer | Felix Hammerl <felix.hammerl@gmail.com> | 2016-06-02 11:04:42 +0200 |
commit | 460074b11ac9171cb7b260e1ffc01e040105b3e5 (patch) | |
tree | de20fc8a69d96a903d05d5ffca2b3fff97fd6a7e | |
parent | 45adea860ceaf23cfabdaf6a22d13c4650550bb3 (diff) |
RootResource should return 503 during startup (#699)
Return 503 code while RootResource is in MODE_STARTUP
-rw-r--r-- | service/pixelated/resources/__init__.py | 11 | ||||
-rw-r--r-- | service/pixelated/resources/root_resource.py | 7 | ||||
-rw-r--r-- | service/test/unit/resources/test_root_resource.py | 16 |
3 files changed, 31 insertions, 3 deletions
diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py index cd27fab0..8fc8080a 100644 --- a/service/pixelated/resources/__init__.py +++ b/service/pixelated/resources/__init__.py @@ -23,7 +23,7 @@ from twisted.web.resource import Resource # from pixelated.resources.login_resource import LoginResource from pixelated.resources.session import IPixelatedSession -from twisted.web.http import INTERNAL_SERVER_ERROR +from twisted.web.http import INTERNAL_SERVER_ERROR, SERVICE_UNAVAILABLE log = logging.getLogger(__name__) @@ -112,3 +112,12 @@ class UnAuthorizedResource(Resource): def render_POST(self, request): request.setResponseCode(UNAUTHORIZED) return "Unauthorized!" + + +class UnavailableResource(Resource): + def __init__(self): + Resource.__init__(self) + + def render(self, request): + request.setResponseCode(SERVICE_UNAVAILABLE) + return "Service Unavailable" diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py index 109dc08e..aacf2b61 100644 --- a/service/pixelated/resources/root_resource.py +++ b/service/pixelated/resources/root_resource.py @@ -18,7 +18,7 @@ import json import os from string import Template -from pixelated.resources import BaseResource, UnAuthorizedResource +from pixelated.resources import BaseResource, UnAuthorizedResource, UnavailableResource from pixelated.resources.attachments_resource import AttachmentsResource from pixelated.resources.sandbox_resource import SandboxResource from pixelated.resources.contacts_resource import ContactsResource @@ -57,7 +57,10 @@ class RootResource(BaseResource): if path == '': return self if self._is_xsrf_valid(request): - return self._child_resources.get(path) + if self._mode == MODE_RUNNING: + return self._child_resources.get(path) + else: + return UnavailableResource() return UnAuthorizedResource() def _is_xsrf_valid(self, request): diff --git a/service/test/unit/resources/test_root_resource.py b/service/test/unit/resources/test_root_resource.py index f2d7a951..b90e89eb 100644 --- a/service/test/unit/resources/test_root_resource.py +++ b/service/test/unit/resources/test_root_resource.py @@ -68,6 +68,21 @@ class TestRootResource(unittest.TestCase): self.root_resource._mode = MODE_RUNNING self._test_should_renew_xsrf_cookie() + def test_should_unavailable_child_resource_during_startup(self): + self.root_resource._mode = MODE_STARTUP + + request = DummyRequest(['/child']) + request.getCookie = MagicMock(return_value='irrelevant -- stubbed') + + d = self.web.get(request) + + def assert_unavailable(_): + self.assertEqual(503, request.responseCode) + self.assertEqual("Service Unavailable", request.written[0]) + + d.addCallback(assert_unavailable) + return d + def _mock_ajax_csrf(self, request, csrf_token): request.requestHeaders.setRawHeaders('x-requested-with', ['XMLHttpRequest']) request.requestHeaders.setRawHeaders('x-xsrf-token', [csrf_token]) @@ -92,6 +107,7 @@ class TestRootResource(unittest.TestCase): request.getCookie = MagicMock(return_value='irrelevant -- stubbed') self.root_resource._child_resources.add('features', FeaturesResource()) + self.root_resource._mode = MODE_RUNNING d = self.web.get(request) |