diff options
4 files changed, 19 insertions, 17 deletions
diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 2f63a2bc..4c1c1c0d 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -25,6 +25,7 @@ from pixelated.adapter.model.status import Status from pixelated.adapter.services.tag_service import extract_reserved_tags from leap.mail.adaptors.soledad import SoledadMailAdaptor + class MailService(object): def __init__(self, mail_sender, mail_store, search_engine, account_email): diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py index 2b5a6912..911fc43d 100644 --- a/service/pixelated/resources/attachments_resource.py +++ b/service/pixelated/resources/attachments_resource.py @@ -87,12 +87,13 @@ class AttachmentsResource(Resource): '</body></html>' def render_POST(self, request): - fields = cgi.FieldStorage(fp=request.content, headers=request.headers, environ={'REQUEST_METHOD':'POST'}) + fields = cgi.FieldStorage(fp=request.content, headers=(request.getAllHeaders()), + environ={'REQUEST_METHOD': 'POST'}) _file = fields['attachment'] deferred = defer.maybeDeferred(self.mail_service.attachment_id, _file.value, _file.type) def send_location(attachment_id): - request.headers['Location'] = '/%s/%s'% (self.BASE_URL, attachment_id) + request.headers['Location'] = '/%s/%s' % (self.BASE_URL, attachment_id) respond_json_deferred({"attachment_id": attachment_id}, request, status_code=201) def error_handler(error): diff --git a/service/test/integration/test_retrieve_attachment.py b/service/test/integration/test_retrieve_attachment.py index ca11a14d..b6010666 100644 --- a/service/test/integration/test_retrieve_attachment.py +++ b/service/test/integration/test_retrieve_attachment.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. import base64 +import json from email import encoders from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart @@ -66,3 +67,4 @@ class RetrieveAttachmentTest(SoledadTestBase): self.assertEqual(201, req.code) self.assertEqual('/attachment/B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A', req.headers['Location']) + self.assertEqual({'attachment_id': 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A'}, json.loads(req.written[0])) diff --git a/service/test/unit/resources/test_attachments_resource.py b/service/test/unit/resources/test_attachments_resource.py index c377324b..837f5324 100644 --- a/service/test/unit/resources/test_attachments_resource.py +++ b/service/test/unit/resources/test_attachments_resource.py @@ -1,7 +1,7 @@ import json import unittest -from mockito import mock, when, verify +from mock import patch, MagicMock from twisted.internet import defer from twisted.web.test.requesthelper import DummyRequest @@ -12,24 +12,23 @@ from test.unit.resources import DummySite class AttachmentsResourceTest(unittest.TestCase): def setUp(self): - self.mail_service = mock() + self.mail_service = MagicMock() self.mails_resource = AttachmentsResource(self.mail_service) self.mails_resource.isLeaf = True self.web = DummySite(self.mails_resource) - def test_post_new_attachment(self): + @patch('twisted.internet.defer.maybeDeferred') + @patch('cgi.FieldStorage') + def test_post_new_attachment(self, mock_fields, mock_maybe_deferred): request = DummyRequest(['/attachment']) request.method = 'POST' - attachment = 'some fake file' - request.args = {'attachment': [attachment,]} + request.content = 'mocked' attachment_id = 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A' - - when(self.mail_service).attachment_id(attachment).thenReturn(defer.succeed(attachment_id)) + mock_maybe_deferred.return_value = defer.succeed(attachment_id) d = self.web.get(request) def assert_response(_): - verify(self.mail_service).attachment_id(attachment) self.assertEqual(201, request.code) self.assertEqual('/attachment/%s' % attachment_id, request.headers['Location']) self.assertEqual({'attachment_id': attachment_id}, json.loads(request.written[0])) @@ -37,21 +36,20 @@ class AttachmentsResourceTest(unittest.TestCase): d.addCallback(assert_response) return d - def test_post_attachment_fails(self): + @patch('twisted.internet.defer.maybeDeferred') + @patch('cgi.FieldStorage') + def test_post_attachment_fails(self, mock_fields, mock_maybe_deferred): + mock_maybe_deferred.return_value = defer.fail(Exception) request = DummyRequest(['/attachment']) request.method = 'POST' - attachment = 'some fake file' - request.args = {'attachment': [attachment,]} - - when(self.mail_service).attachment_id(attachment).thenReturn(defer.fail(Exception)) + request.content = 'mocked' d = self.web.get(request) def assert_response(_): self.assertEqual(500, request.code) self.assertFalse('Location' in request.headers) - verify(self.mail_service).attachment_id(attachment) self.assertEqual({"message": "Something went wrong. Attachement not saved."}, json.loads(request.written[0])) d.addCallback(assert_response) - return d
\ No newline at end of file + return d |