diff options
author | mnandri <mnandri@eunglick.corporate.thoughtworks.com> | 2015-12-17 18:41:58 +0100 |
---|---|---|
committer | mnandri <mnandri@eunglick.corporate.thoughtworks.com> | 2015-12-18 11:22:34 +0100 |
commit | 06ac408dbd7629d387dd7b311a26c144ee56631e (patch) | |
tree | c263a0586628b627076b12132166322714394256 /service/test/unit/resources | |
parent | 39fa6e68fc2afaafc0e8440d212b464d2e20c326 (diff) |
extracted a leap attachment store, handling all attachment responsibilities, including saving attachments.
Issue #548
Diffstat (limited to 'service/test/unit/resources')
-rw-r--r-- | service/test/unit/resources/test_attachments_resource.py | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/service/test/unit/resources/test_attachments_resource.py b/service/test/unit/resources/test_attachments_resource.py index 837f5324..2afa208c 100644 --- a/service/test/unit/resources/test_attachments_resource.py +++ b/service/test/unit/resources/test_attachments_resource.py @@ -2,6 +2,7 @@ import json import unittest from mock import patch, MagicMock +from mockito import mock, when, verify from twisted.internet import defer from twisted.web.test.requesthelper import DummyRequest @@ -12,19 +13,22 @@ from test.unit.resources import DummySite class AttachmentsResourceTest(unittest.TestCase): def setUp(self): - self.mail_service = MagicMock() + self.mail_service = mock() self.mails_resource = AttachmentsResource(self.mail_service) self.mails_resource.isLeaf = True self.web = DummySite(self.mails_resource) - @patch('twisted.internet.defer.maybeDeferred') @patch('cgi.FieldStorage') - def test_post_new_attachment(self, mock_fields, mock_maybe_deferred): + def test_post_new_attachment(self, mock_fields): request = DummyRequest(['/attachment']) request.method = 'POST' request.content = 'mocked' attachment_id = 'B5B4ED80AC3B894523D72E375DACAA2FC6606C18EDF680FE95903086C8B5E14A' - mock_maybe_deferred.return_value = defer.succeed(attachment_id) + _file = MagicMock() + _file.value = 'some mocked value' + _file.type = 'some mocked type' + mock_fields.return_value = {'attachment': _file} + when(self.mail_service).save_attachment('some mocked value', 'some mocked type').thenReturn(defer.succeed(attachment_id)) d = self.web.get(request) @@ -32,24 +36,30 @@ class AttachmentsResourceTest(unittest.TestCase): 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])) + verify(self.mail_service).save_attachment('some mocked value', 'some mocked type') d.addCallback(assert_response) return d - @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) + def test_post_attachment_fails(self, mock_fields): request = DummyRequest(['/attachment']) request.method = 'POST' request.content = 'mocked' + _file = MagicMock() + _file.value = 'some mocked value' + _file.type = 'some mocked type' + mock_fields.return_value = {'attachment': _file} + when(self.mail_service).save_attachment('some mocked value', 'some mocked type').thenReturn(defer.fail(Exception)) + d = self.web.get(request) def assert_response(_): self.assertEqual(500, request.code) self.assertFalse('Location' in request.headers) self.assertEqual({"message": "Something went wrong. Attachement not saved."}, json.loads(request.written[0])) + verify(self.mail_service).save_attachment('some mocked value', 'some mocked type') d.addCallback(assert_response) return d |