summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNavaL <mnandri@thoughtworks.com>2016-01-18 18:19:30 +0100
committerNavaL <mnandri@thoughtworks.com>2016-01-18 18:19:30 +0100
commitaa5eeee6bab6721f89860690ae76ba70768104e4 (patch)
tree4c55d6e5ad564c9b997bf38cace460117b84c6ed
parenta37ddad1b004c8b0f27b006284abd00483b17a95 (diff)
migrating mails resources to use services factory
Issue #576
-rw-r--r--service/pixelated/resources/__init__.py3
-rw-r--r--service/pixelated/resources/mails_resource.py50
-rw-r--r--service/pixelated/resources/root_resource.py2
-rw-r--r--service/test/support/integration/app_test_client.py1
-rw-r--r--service/test/unit/resources/test_mails_resource.py14
5 files changed, 45 insertions, 25 deletions
diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py
index 023bcd7e..c5ec02f0 100644
--- a/service/pixelated/resources/__init__.py
+++ b/service/pixelated/resources/__init__.py
@@ -66,3 +66,6 @@ class BaseResource(Resource):
def search_engine(self, request):
return self._service(request, 'search_engine')
+
+ def draft_service(self, request):
+ return self._service(request, 'draft_service')
diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py
index a2f07093..c81cbd3b 100644
--- a/service/pixelated/resources/mails_resource.py
+++ b/service/pixelated/resources/mails_resource.py
@@ -2,7 +2,7 @@ import json
from pixelated.adapter.services.mail_sender import SMTPDownException
from pixelated.adapter.model.mail import InputMail
from twisted.web.server import NOT_DONE_YET
-from pixelated.resources import respond_json_deferred
+from pixelated.resources import respond_json_deferred, BaseResource
from twisted.web.resource import Resource
from twisted.web import server
from twisted.internet import defer
@@ -111,7 +111,7 @@ class MailsArchiveResource(Resource):
return NOT_DONE_YET
-class MailsResource(Resource):
+class MailsResource(BaseResource):
def _register_smtp_error_handler(self):
@@ -121,22 +121,29 @@ class MailsResource(Resource):
events.register(events.catalog.SMTP_SEND_MESSAGE_ERROR, callback=on_error)
- def __init__(self, mail_service, draft_service):
- Resource.__init__(self)
- self.putChild('delete', MailsDeleteResource(mail_service))
- self.putChild('recover', MailsRecoverResource(mail_service))
- self.putChild('archive', MailsArchiveResource(mail_service))
- self.putChild('read', MailsReadResource(mail_service))
- self.putChild('unread', MailsUnreadResource(mail_service))
-
- self._draft_service = draft_service
- self._mail_service = mail_service
+ def __init__(self, services_factory):
+ BaseResource.__init__(self, services_factory)
self._register_smtp_error_handler()
+ def getChild(self, action, request):
+ _mail_service = self.mail_service(request)
+
+ if action == 'delete':
+ return MailsDeleteResource(_mail_service)
+ if action == 'recover':
+ return MailsRecoverResource(_mail_service)
+ if action == 'archive':
+ return MailsArchiveResource(_mail_service)
+ if action == 'read':
+ return MailsReadResource(_mail_service)
+ if action == 'unread':
+ return MailsUnreadResource(_mail_service)
+
def render_GET(self, request):
+ _mail_service = self.mail_service(request)
query, window_size, page = request.args.get('q')[0], request.args.get('w')[0], request.args.get('p')[0]
unicode_query = to_unicode(query)
- d = self._mail_service.mails(unicode_query, window_size, page)
+ d = _mail_service.mails(unicode_query, window_size, page)
d.addCallback(lambda (mails, total): {
"stats": {
@@ -177,30 +184,33 @@ class MailsResource(Resource):
return server.NOT_DONE_YET
@defer.inlineCallbacks
- def _fetch_attachment_contents(self, content_dict):
+ def _fetch_attachment_contents(self, content_dict, _mail_service):
attachments = content_dict.get('attachments', []) if content_dict else []
for attachment in attachments:
- retrieved_attachment = yield self._mail_service.attachment(attachment['ident'])
+ retrieved_attachment = yield _mail_service.attachment(attachment['ident'])
attachment['raw'] = retrieved_attachment['content']
content_dict['attachments'] = attachments
defer.returnValue(content_dict)
@defer.inlineCallbacks
def _handle_post(self, request):
+ _mail_service = self.mail_service(request)
content_dict = json.loads(request.content.read())
- with_attachment_content = yield self._fetch_attachment_contents(content_dict)
+ with_attachment_content = yield self._fetch_attachment_contents(content_dict, _mail_service)
- sent_mail = yield self._mail_service.send_mail(with_attachment_content)
+ sent_mail = yield _mail_service.send_mail(with_attachment_content)
respond_json_deferred(sent_mail.as_dict(), request)
@defer.inlineCallbacks
def _handle_put(self, request):
+ _draft_service = self.draft_service(request)
+ _mail_service = self.mail_service(request)
content_dict = json.loads(request.content.read())
- with_attachment_content = yield self._fetch_attachment_contents(content_dict)
+ with_attachment_content = yield self._fetch_attachment_contents(content_dict, _mail_service)
- _mail = InputMail.from_dict(with_attachment_content, from_address=self._mail_service.account_email)
+ _mail = InputMail.from_dict(with_attachment_content, from_address=_mail_service.account_email)
draft_id = content_dict.get('ident')
- pixelated_mail = yield self._draft_service.process_draft(draft_id, _mail)
+ pixelated_mail = yield _draft_service.process_draft(draft_id, _mail)
if not pixelated_mail:
respond_json_deferred("", request, status_code=422)
diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py
index c5985257..eefa9835 100644
--- a/service/pixelated/resources/root_resource.py
+++ b/service/pixelated/resources/root_resource.py
@@ -45,7 +45,7 @@ class RootResource(Resource):
self.putChild('contacts', ContactsResource(services_factory))
self.putChild('features', FeaturesResource())
self.putChild('tags', TagsResource(services_factory))
- self.putChild('mails', MailsResource(mail_service, draft_service))
+ self.putChild('mails', MailsResource(services_factory))
self.putChild('mail', MailResource(mail_service))
self.putChild('feedback', FeedbackResource(feedback_service))
self.putChild('user-settings', UserSettingsResource(self.account_email))
diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py
index 2fa3a2dd..3d224941 100644
--- a/service/test/support/integration/app_test_client.py
+++ b/service/test/support/integration/app_test_client.py
@@ -91,6 +91,7 @@ class AppTestClient(object):
services = mock()
services.keymanager = self.keymanager
services.mail_service = self.mail_service
+ services.draft_service = self.draft_service
services.search_engine = self.search_engine
self.service_factory.add_session('someuserid', services)
diff --git a/service/test/unit/resources/test_mails_resource.py b/service/test/unit/resources/test_mails_resource.py
index efa37b27..e36e4f5f 100644
--- a/service/test/unit/resources/test_mails_resource.py
+++ b/service/test/unit/resources/test_mails_resource.py
@@ -17,7 +17,7 @@
import unittest
from mock import patch
-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
@@ -28,6 +28,12 @@ from test.unit.resources import DummySite
class TestMailsResource(unittest.TestCase):
def setUp(self):
self.mail_service = mock()
+ self.servicesFactory = mock()
+ self.services = mock()
+ self.services.mail_service = self.mail_service
+ self.services.draft_service = mock()
+ self.servicesFactory._services_by_user = {'someuserid': self.mail_service}
+ when(self.servicesFactory).services(ANY()).thenReturn(self.services)
@patch('leap.common.events.register')
def test_render_GET_should_unicode_mails_search_query(self, mock_register):
@@ -40,7 +46,7 @@ class TestMailsResource(unittest.TestCase):
unicodified_search_term = u'coração'
when(self.mail_service).mails(unicodified_search_term, 25, 1).thenReturn(defer.Deferred())
- mails_resource = MailsResource(self.mail_service, mock())
+ mails_resource = MailsResource(self.servicesFactory)
mails_resource.isLeaf = True
web = DummySite(mails_resource)
d = web.get(request)
@@ -60,7 +66,7 @@ class TestMailsResource(unittest.TestCase):
when(self.mail_service).attachment('some fake attachment id').thenReturn(defer.Deferred())
request.content = content
- mails_resource = MailsResource(self.mail_service, mock())
+ mails_resource = MailsResource(self.servicesFactory)
mails_resource.isLeaf = True
web = DummySite(mails_resource)
d = web.get(request)
@@ -84,7 +90,7 @@ class TestMailsResource(unittest.TestCase):
.thenReturn(defer.succeed(as_dictable))
request.content = content
- mails_resource = MailsResource(self.mail_service, mock())
+ mails_resource = MailsResource(self.servicesFactory)
mails_resource.isLeaf = True
web = DummySite(mails_resource)
d = web.get(request)