summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2016-03-04 10:22:14 +0100
committerFolker Bernitt <fbernitt@thoughtworks.com>2016-03-04 10:22:35 +0100
commit9284db24513ff33c2c9c0cdc34c12fca91e42f36 (patch)
treec07500c4ff1cb569f31af250acbf06b43e282a98 /service
parent59532a50c8b0233b2a596b996cbe799b745c4fac (diff)
Fix: Logout calls session expire
- Deferred was not handled as required - Test did not assert call to expire
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/resources/logout_resource.py19
-rw-r--r--service/test/unit/resources/test_logout_resources.py15
2 files changed, 25 insertions, 9 deletions
diff --git a/service/pixelated/resources/logout_resource.py b/service/pixelated/resources/logout_resource.py
index 344ad2e9..01092b05 100644
--- a/service/pixelated/resources/logout_resource.py
+++ b/service/pixelated/resources/logout_resource.py
@@ -1,5 +1,8 @@
+from twisted.web.server import NOT_DONE_YET
+
from pixelated.resources import BaseResource
from twisted.web import util
+from twisted.internet import defer
from pixelated.resources.login_resource import LoginResource
@@ -8,9 +11,19 @@ class LogoutResource(BaseResource):
BASE_URL = "logout"
isLeaf = True
- def render_POST(self, request):
+ @defer.inlineCallbacks
+ def _execute_logout(self, request):
session = self.get_session(request)
- self._services_factory.log_out_user(session.user_uuid)
+ yield self._services_factory.log_out_user(session.user_uuid)
session.expire()
- return util.redirectTo("/%s" % LoginResource.BASE_URL, request)
+ def render_POST(self, request):
+ def _redirect_to_login(_):
+ content = util.redirectTo("/%s" % LoginResource.BASE_URL, request)
+ request.write(content)
+ request.finish()
+
+ d = self._execute_logout(request)
+ d.addCallback(_redirect_to_login)
+
+ return NOT_DONE_YET
diff --git a/service/test/unit/resources/test_logout_resources.py b/service/test/unit/resources/test_logout_resources.py
index 6246eeb9..312d2ba4 100644
--- a/service/test/unit/resources/test_logout_resources.py
+++ b/service/test/unit/resources/test_logout_resources.py
@@ -1,6 +1,6 @@
-from mock import patch
-from mockito import mock, verify
+from mock import patch, MagicMock
from twisted.trial import unittest
+from twisted.internet import defer
from twisted.web.error import UnsupportedMethod
from twisted.web.test.requesthelper import DummyRequest
@@ -10,8 +10,9 @@ from test.unit.resources import DummySite
class TestLogoutResource(unittest.TestCase):
def setUp(self):
- self.services_factory = mock()
+ self.services_factory = MagicMock()
self.resource = LogoutResource(self.services_factory)
+ self.services_factory.log_out_user.return_value = defer.succeed(None)
self.web = DummySite(self.resource)
@patch('twisted.web.util.redirectTo')
@@ -19,14 +20,16 @@ class TestLogoutResource(unittest.TestCase):
request = DummyRequest(['/logout'])
request.method = 'POST'
- mock_redirect.return_value = 'haha'
+ session = self.resource.get_session(request)
+ session.expire = MagicMock()
+ mock_redirect.return_value = 'some redirect response'
d = self.web.get(request)
def expire_session_and_redirect(_):
session = self.resource.get_session(request)
- self.assertFalse(session.is_logged_in())
- verify(self.services_factory).log_out_user(session.user_uuid)
+ self.services_factory.log_out_user.assert_called_once_with(session.user_uuid)
+ session.expire.assert_called_once_with()
mock_redirect.assert_called_once_with('/login', request)
d.addCallback(expire_session_and_redirect)