summaryrefslogtreecommitdiff
path: root/service/test
diff options
context:
space:
mode:
authorDuda Dornelles <ddornell@thoughtworks.com>2015-02-18 13:40:41 -0200
committerDuda Dornelles <ddornell@thoughtworks.com>2015-02-18 13:40:49 -0200
commitdd63db87fdbb9667c1027edd700b4047b6983d1f (patch)
tree85c7a2875f535412760dc7d87c7550777f265814 /service/test
parent4a8217744e1d1ca60d158c3497a09b92f857a7fd (diff)
#294 - not failing service startup if smtp fails to start
Diffstat (limited to 'service/test')
-rw-r--r--service/test/unit/adapter/services/test_mail_sender.py25
-rw-r--r--service/test/unit/bitmask_libraries/test_session.py3
-rw-r--r--service/test/unit/bitmask_libraries/test_smtp.py4
3 files changed, 25 insertions, 7 deletions
diff --git a/service/test/unit/adapter/services/test_mail_sender.py b/service/test/unit/adapter/services/test_mail_sender.py
index 8536e5e3..0e7ec015 100644
--- a/service/test/unit/adapter/services/test_mail_sender.py
+++ b/service/test/unit/adapter/services/test_mail_sender.py
@@ -16,7 +16,7 @@
from twisted.trial import unittest
from mockito import mock, when, verify, any, unstub
-from pixelated.adapter.services.mail_sender import MailSender
+from pixelated.adapter.services.mail_sender import MailSender, SMTPDownException
from pixelated.adapter.model.mail import PixelatedMail, InputMail
from test.support.test_helper import mail_dict
from twisted.internet import reactor
@@ -24,10 +24,14 @@ from twisted.internet.defer import Deferred
class MailSenderTest(unittest.TestCase):
+ def setUp(self):
+ self.ensure_smtp_is_running_cb = lambda: True
+ self.ensure_smtp_is_not_running_cb = lambda: False
+
def test_sendmail(self):
when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None)
input_mail = InputMail.from_dict(mail_dict())
- mail_sender = MailSender('someone@somedomain.tld')
+ mail_sender = MailSender('someone@somedomain.tld', self.ensure_smtp_is_running_cb)
return self._succeed(mail_sender.sendmail(input_mail))
@@ -38,7 +42,8 @@ class MailSenderTest(unittest.TestCase):
when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None)
input_mail = InputMail.from_dict(mail_dict())
- mail_sender = MailSender('someone@somedomain.tld')
+
+ mail_sender = MailSender('someone@somedomain.tld', self.ensure_smtp_is_running_cb)
sent_deferred = mail_sender.sendmail(input_mail)
@@ -49,7 +54,7 @@ class MailSenderTest(unittest.TestCase):
def test_senmail_returns_deffered(self):
when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None)
input_mail = InputMail.from_dict(mail_dict())
- mail_sender = MailSender('someone@somedomain.tld')
+ mail_sender = MailSender('someone@somedomain.tld', self.ensure_smtp_is_running_cb)
deferred = mail_sender.sendmail(input_mail)
@@ -58,6 +63,18 @@ class MailSenderTest(unittest.TestCase):
return self._succeed(deferred)
+ def test_doesnt_send_mail_if_smtp_is_not_running(self):
+ mail_sender = MailSender('someone@somedomain.tld', self.ensure_smtp_is_not_running_cb)
+
+ deferred = mail_sender.sendmail({})
+
+ def _assert(_):
+ self.assertTrue(isinstance(deferred.result.value, SMTPDownException))
+
+ deferred.addErrback(_assert)
+
+ return deferred
+
def _succeed(self, deferred):
deferred.callback(None)
return deferred
diff --git a/service/test/unit/bitmask_libraries/test_session.py b/service/test/unit/bitmask_libraries/test_session.py
index 67722557..0bfd59f2 100644
--- a/service/test/unit/bitmask_libraries/test_session.py
+++ b/service/test/unit/bitmask_libraries/test_session.py
@@ -24,6 +24,7 @@ class SessionTest(AbstractLeapTest):
def setUp(self):
self.mail_fetcher_mock = MagicMock()
+ self.smtp_mock = MagicMock()
def tearDown(self):
self.mail_fetcher_mock = MagicMock()
@@ -65,7 +66,7 @@ class SessionTest(AbstractLeapTest):
def _create_session(self):
return LeapSession(self.provider, self.srp_session, self.soledad_session, self.nicknym, self.soledad_account,
- self.mail_fetcher_mock)
+ self.mail_fetcher_mock, self.smtp_mock)
def _execute_func(func):
diff --git a/service/test/unit/bitmask_libraries/test_smtp.py b/service/test/unit/bitmask_libraries/test_smtp.py
index 4087cbf5..c0e30573 100644
--- a/service/test/unit/bitmask_libraries/test_smtp.py
+++ b/service/test/unit/bitmask_libraries/test_smtp.py
@@ -71,7 +71,7 @@ class LeapSmtpTest(AbstractLeapTest):
smtp.TWISTED_PORT = port
gateway_mock.return_value = (None, None)
with HTTMock(ca_cert_mock, not_found_mock):
- smtp.start()
+ smtp.ensure_running()
cert_path = self._client_cert_path()
gateway_mock.assert_called_with(keymanager=self.keymanager, smtp_cert=cert_path, smtp_key=cert_path, userid='test_user@some-server.test', smtp_port='1234', encrypted_only=False, smtp_host='smtp.some-sever.test', port=port)
@@ -91,7 +91,7 @@ class LeapSmtpTest(AbstractLeapTest):
gateway_mock.return_value = (smtp_service, smtp_port)
with HTTMock(ca_cert_mock, not_found_mock):
- smtp.start()
+ smtp.ensure_running()
smtp.stop()
smtp_port.stopListening.assert_called_with()