diff options
author | NavaL <mnandri@thoughtworks.com> | 2016-01-18 17:27:19 +0100 |
---|---|---|
committer | NavaL <mnandri@thoughtworks.com> | 2016-01-18 17:27:19 +0100 |
commit | bb9a1c8827c02ac71c16b7d5d0b4c4c18b34f24c (patch) | |
tree | 7027cf3f00276753c12514864c9527459063d77a | |
parent | c5d61bc8001b16e3dcedda9fd1229c27cca4bd3b (diff) |
migrating AttachmentsResources to use the service factory
Issue #576
-rw-r--r-- | service/pixelated/resources/__init__.py | 19 | ||||
-rw-r--r-- | service/pixelated/resources/attachments_resource.py | 15 | ||||
-rw-r--r-- | service/pixelated/resources/root_resource.py | 2 | ||||
-rw-r--r-- | service/test/support/integration/app_test_client.py | 1 | ||||
-rw-r--r-- | service/test/unit/resources/test_attachments_resource.py | 10 |
5 files changed, 32 insertions, 15 deletions
diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py index 556469c1..cddfff97 100644 --- a/service/pixelated/resources/__init__.py +++ b/service/pixelated/resources/__init__.py @@ -47,10 +47,19 @@ class BaseResource(Resource): Resource.__init__(self) self._services_factory = services_factory - def keymanager(self, request): - user_id = self._get_user_id_from_request() - return self._services_factory.services(user_id).keymanager - - def _get_user_id_from_request(self): + def _get_user_id_from_request(self, request): # currently we are faking this return self._services_factory._services_by_user.keys()[0] + + def _services(self, request): + user_id = self._get_user_id_from_request(request) + return self._services_factory.services(user_id) + + def _service(self, request, attribute): + return getattr(self._services(request), attribute) + + def keymanager(self, request): + return self._service(request, 'keymanager') + + def mail_service(self, request): + return self._service(request, 'mail_service') diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py index 09715282..249d268a 100644 --- a/service/pixelated/resources/attachments_resource.py +++ b/service/pixelated/resources/attachments_resource.py @@ -25,7 +25,7 @@ from twisted.web import server from twisted.web.resource import Resource from twisted.web.server import NOT_DONE_YET -from pixelated.resources import respond_json_deferred +from pixelated.resources import respond_json_deferred, BaseResource logger = logging.getLogger(__name__) @@ -72,21 +72,22 @@ class AttachmentResource(Resource): return match.group(1) -class AttachmentsResource(Resource): +class AttachmentsResource(BaseResource): BASE_URL = 'attachment' - def __init__(self, mail_service): - Resource.__init__(self) - self.mail_service = mail_service + def __init__(self, services_factory): + BaseResource.__init__(self, services_factory) def getChild(self, attachment_id, request): - return AttachmentResource(self.mail_service, attachment_id) + _mail_service = self.mail_service(request) + return AttachmentResource(_mail_service, attachment_id) def render_POST(self, request): + _mail_service = self.mail_service(request) fields = cgi.FieldStorage(fp=request.content, headers=(request.getAllHeaders()), environ={'REQUEST_METHOD': 'POST'}) _file = fields['attachment'] - deferred = self.mail_service.save_attachment(_file.value, _file.type) + deferred = _mail_service.save_attachment(_file.value, _file.type) def send_location(attachment_id): request.headers['Location'] = '/%s/%s' % (self.BASE_URL, attachment_id) diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py index fac63bd7..af3c6c98 100644 --- a/service/pixelated/resources/root_resource.py +++ b/service/pixelated/resources/root_resource.py @@ -41,7 +41,7 @@ class RootResource(Resource): self.putChild('assets', File(self._static_folder)) self.putChild('keys', KeysResource(services_factory)) - self.putChild(AttachmentsResource.BASE_URL, AttachmentsResource(mail_service)) + self.putChild(AttachmentsResource.BASE_URL, AttachmentsResource(services_factory)) self.putChild('contacts', ContactsResource(search_engine)) self.putChild('features', FeaturesResource()) self.putChild('tags', TagsResource(search_engine)) diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 16344171..6a43ed1a 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -90,6 +90,7 @@ class AppTestClient(object): self.service_factory = ServicesFactory() services = mock() services.keymanager = self.keymanager + services.mail_service = self.mail_service self.service_factory.add_session('someuserid', services) self.resource.initialize( diff --git a/service/test/unit/resources/test_attachments_resource.py b/service/test/unit/resources/test_attachments_resource.py index 8beb02fa..f3d8ce53 100644 --- a/service/test/unit/resources/test_attachments_resource.py +++ b/service/test/unit/resources/test_attachments_resource.py @@ -3,7 +3,7 @@ import unittest import logging from mock import patch, MagicMock -from mockito import mock, when, verify +from mockito import mock, when, verify, any as ANY from twisted.internet import defer from twisted.web.test.requesthelper import DummyRequest @@ -17,7 +17,13 @@ class AttachmentsResourceTest(unittest.TestCase): def setUp(self): self.mail_service = mock() - self.mails_resource = AttachmentsResource(self.mail_service) + self.servicesFactory = mock() + self.services = mock() + self.services.mail_service = self.mail_service + self.servicesFactory._services_by_user = {'someuserid': self.mail_service} + when(self.servicesFactory).services(ANY()).thenReturn(self.services) + + self.mails_resource = AttachmentsResource(self.servicesFactory) self.mails_resource.isLeaf = True self.web = DummySite(self.mails_resource) |