diff options
Diffstat (limited to 'service/test/unit')
-rw-r--r-- | service/test/unit/adapter/mail_sender_test.py | 46 | ||||
-rw-r--r-- | service/test/unit/controllers/mails_controller_test.py | 46 | ||||
-rw-r--r-- | service/test/unit/controllers/sync_info_controller_test.py | 4 | ||||
-rw-r--r-- | service/test/unit/runserver_test.py | 23 |
4 files changed, 42 insertions, 77 deletions
diff --git a/service/test/unit/adapter/mail_sender_test.py b/service/test/unit/adapter/mail_sender_test.py deleted file mode 100644 index 721d61b7..00000000 --- a/service/test/unit/adapter/mail_sender_test.py +++ /dev/null @@ -1,46 +0,0 @@ -# -# Copyright (c) 2014 ThoughtWorks, Inc. -# -# Pixelated is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pixelated is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# 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 unittest - -from pixelated.adapter.mail import PixelatedMail -from pixelated.adapter.mail_sender import MailSender -from mockito import * -from test.support import test_helper - - -class MailSenderTest(unittest.TestCase): - def setUp(self): - self.mail_address = "pixelated@pixelated.org" - self.smtp_client = mock() - self.mail_sender = MailSender(self.mail_address, self.smtp_client) - - def test_send_mail_sends_to_To_Cc_and_Bcc(self): - headers = { - 'To': ['to@pixelated.org', 'anotherto@pixelated.org'], - 'Cc': ['cc@pixelated.org', 'anothercc@pixelated.org'], - 'Bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'] - } - - mail = PixelatedMail.from_soledad(*test_helper.leap_mail(extra_headers=headers)) - mail.to_smtp_format = lambda: "mail as smtp string" - - self.mail_sender.sendmail(mail) - - expected_recipients = ['to@pixelated.org', 'anotherto@pixelated.org', 'cc@pixelated.org', - 'anothercc@pixelated.org', - 'bcc@pixelated.org', 'anotherbcc@pixelated.org'] - - verify(self.smtp_client).sendmail(self.mail_address, expected_recipients, "mail as smtp string") diff --git a/service/test/unit/controllers/mails_controller_test.py b/service/test/unit/controllers/mails_controller_test.py index a64207e9..93748de9 100644 --- a/service/test/unit/controllers/mails_controller_test.py +++ b/service/test/unit/controllers/mails_controller_test.py @@ -13,16 +13,21 @@ # # 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 json import unittest +from klein.test_resource import requestMock +from mock import MagicMock from mockito import * from pixelated.controllers.mails_controller import MailsController class TestMailsController(unittest.TestCase): + def setUp(self): self.mail_service = mock() self.search_engine = mock() + self.dummy_request = MagicMock(spec=['code', 'responseHeaders']) draft_service = mock() self.mails_controller = MailsController(mail_service=self.mail_service, @@ -42,20 +47,22 @@ class TestMailsController(unittest.TestCase): def test_sending_mail_return_sent_mail_data_when_send_succeeds(self): self.mail_service.send = self._successfuly_send_mail + request = requestMock('', body=json.dumps(self.input_mail.json)) - result = self.mails_controller.send_mail(self.input_mail) + result = self.mails_controller.send_mail(request) - self.assertEqual(result.status_code, 200) - self.assertEqual(result.data, + self.assertEqual(request.code, 200) + self.assertEqual(result, '{"status": [], "body": "email body", "ident": 1, "tags": [], "header": {"to": "b@b.b", "from": "a@a.a"}, "security_casing": {}}') def test_sending_mail_return_error_message_when_send_fails(self): self.mail_service.send = self._send_that_throws_exception - result = self.mails_controller.send_mail(self.input_mail) + request = requestMock('', body=json.dumps(self.input_mail.json)) + result = self.mails_controller.send_mail(request) - self.assertEqual(result.status_code, 422) - self.assertEqual(result.data, + self.assertEqual(request.code, 422) + self.assertEqual(result, '{"message": "email sending failed\\nmore information of error\\n123\\nthere was a code before this"}') def test_fetching_mail_gets_mail_from_mail_service(self): @@ -63,17 +70,17 @@ class TestMailsController(unittest.TestCase): mail.as_dict = lambda: {'ident': 1, 'body': 'le mail body'} when(self.mail_service).mail(1).thenReturn(mail) - response = self.mails_controller.mail(1) + response = self.mails_controller.mail(self.dummy_request, 1) verify(self.mail_service).mail(1) - self.assertEqual(response.data, '{"body": "le mail body", "ident": 1}') + self.assertEqual(response, '{"body": "le mail body", "ident": 1}') def test_marking_mail_as_read_set_mail_as_read_on_the_service(self): mail = mock() when(self.mail_service).mark_as_read(1).thenReturn(mail) when(self.search_engine).index_mail(mail).thenReturn(None) - self.mails_controller.mark_mail_as_read(1) + self.mails_controller.mark_mail_as_read(None, 1) verify(self.mail_service).mark_as_read(1) verify(self.search_engine).index_mail(mail) @@ -83,27 +90,28 @@ class TestMailsController(unittest.TestCase): when(self.mail_service).mark_as_unread(1).thenReturn(mail) when(self.search_engine).index_mail(mail).thenReturn(None) - self.mails_controller.mark_mail_as_unread(1) + self.mails_controller.mark_mail_as_unread(None, 1) verify(self.mail_service).mark_as_unread(1) verify(self.search_engine).index_mail(mail) - def test_delete_permanently_when_mail_in_trash(self): + def test_move_message_to_trash(self): mail = mock() - mail.mailbox_name = 'TRASH' + mail.mailbox_name = 'INBOX' when(self.mail_service).mail(1).thenReturn(mail) - self.mails_controller.delete_mail(1) + when(self.mail_service).delete_mail(1).thenReturn(mail) - verify(self.mail_service).delete_permanent(1) + self.mails_controller.delete_mail(self.dummy_request, 1) - def test_move_message_to_trash(self): + verify(self.search_engine).index_mail(mail) + + def test_delete_permanently_when_mail_in_trash(self): mail = mock() - mail.mailbox_name = 'INBOX' + mail.mailbox_name = 'TRASH' when(self.mail_service).mail(1).thenReturn(mail) - when(self.mails_controller).delete_mail(1).thenReturn(mail) - when(self.search_engine).index_mail(mail) + self.mails_controller.delete_mail(self.dummy_request, 1) - verify(self.search_engine).index_mail(mail) + verify(self.mail_service).delete_permanent(1) def _successfuly_send_mail(self, ident, mail): sent_mail = mock() diff --git a/service/test/unit/controllers/sync_info_controller_test.py b/service/test/unit/controllers/sync_info_controller_test.py index d3bf1190..ce9a0dff 100644 --- a/service/test/unit/controllers/sync_info_controller_test.py +++ b/service/test/unit/controllers/sync_info_controller_test.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 unittest +from mock import MagicMock from pixelated.controllers import SyncInfoController from mockito import * import json @@ -22,6 +23,7 @@ import json class SyncInfoControllerTest(unittest.TestCase): def setUp(self): + self.dummy_request = MagicMock() self.controller = SyncInfoController() def _set_count(self, current, total): @@ -30,7 +32,7 @@ class SyncInfoControllerTest(unittest.TestCase): self.controller.set_sync_info(soledad_sync_data) def get_sync_info(self): - return json.loads(self.controller.sync_info().data) + return json.loads(self.controller.sync_info(self.dummy_request)) def test_is_not_syncing_if_total_is_equal_to_current(self): self._set_count(total=0, current=0) diff --git a/service/test/unit/runserver_test.py b/service/test/unit/runserver_test.py index 57e211c9..4a9bca6f 100644 --- a/service/test/unit/runserver_test.py +++ b/service/test/unit/runserver_test.py @@ -21,28 +21,29 @@ import thread import pixelated.runserver from mockito import * -import pixelated.config.reactor_manager as reactor_manager import pixelated.config.app_factory as app_factory +from leap.common.events import server as events_server class RunserverTest(unittest.TestCase): def setUp(self): - when(reactor_manager).start_reactor().thenReturn(None) + events_server.ensure_server = lambda port=None: None when(app_factory).create_app().thenReturn(None) def test_that_config_file_can_be_specified_on_command_line(self): - orig_config = pixelated.runserver.app.config - try: - pixelated.runserver.app.config = mock(dict) - pixelated.runserver.app.config.__setitem__ = mock() + self.config_file_loaded = None - sys.argv = ['/tmp/does_not_exist', '--config', '/tmp/some/config/file'] - pixelated.runserver.setup() + def _mock_parse_config_from_file(config_file): + self.config_file_loaded = config_file + return 1, 2, 3 - verify(pixelated.runserver.app.config).from_pyfile('/tmp/some/config/file') - finally: - pixelated.runserver.app.config = orig_config + pixelated.runserver.parse_config_from_file = _mock_parse_config_from_file + sys.argv = ['pixelated-user-agent', '--config', 'pixelated.cfg'] + + pixelated.runserver.setup() + + self.assertEquals('pixelated.cfg', self.config_file_loaded) def test_that_organization_switch_reads_the_credentials_from_pipe(self): fifo_path = '/tmp/credentials-pipe' |