|
| 1 | +from twisted.trial import unittest |
| 2 | +from twisted.python import log |
| 3 | +from mock import patch, MagicMock |
| 4 | +import mock |
| 5 | +from smtplib import SMTPAuthenticationError |
| 6 | + |
| 7 | +from market.smtpnotification import SMTPNotification |
| 8 | + |
| 9 | +class MarketSMTPTest(unittest.TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + self.catcher = [] |
| 13 | + observer = self.catcher.append |
| 14 | + log.addObserver(observer) |
| 15 | + self.addCleanup(log.removeObserver, observer) |
| 16 | + self.db = MagicMock() |
| 17 | + self.db.settings.get.return_value = ['0', '1', '2', '3', '4', '5', '6', |
| 18 | + '7', '8', '9', '10', '11', '12', |
| 19 | + '13', 1, 'test_server', |
| 20 | + 'test_sender', 'test_recipient', |
| 21 | + 'test_username', 'test_password'] |
| 22 | + |
| 23 | + def test_MarketSmtp_settings_success(self): |
| 24 | + '''SMTP Notification settings correctly set.''' |
| 25 | + s = SMTPNotification(self.db) |
| 26 | + self.assertEqual('test_server', s.server) |
| 27 | + self.assertEqual('test_sender', s.sender) |
| 28 | + self.assertEqual('test_recipient', s.recipient) |
| 29 | + self.assertEqual('test_username', s.username) |
| 30 | + self.assertEqual('test_password', s.password) |
| 31 | + |
| 32 | + @patch("smtplib.SMTP") |
| 33 | + def test_MarketSmtp_send_enabled_success(self, mock_smtp): |
| 34 | + '''Email sent when enabled''' |
| 35 | + instance = mock_smtp.return_value |
| 36 | + s = SMTPNotification(self.db) |
| 37 | + s.send('test_subject', 'test_body') |
| 38 | + mock_smtp.assert_called_once_with('test_server') |
| 39 | + instance.login.assert_called_once_with('test_username', 'test_password') |
| 40 | + instance.sendmail.assert_called_once_with('test_sender', 'test_recipient', mock.ANY) |
| 41 | + |
| 42 | + @patch("smtplib.SMTP") |
| 43 | + def test_MarketSmtp_send_disabled_not_sent(self, mock_smtp): |
| 44 | + '''Email not sent when disabled''' |
| 45 | + instance = mock_smtp.return_value |
| 46 | + self.db.settings.get.return_value = ['0', '1', '2', '3', '4', '5', '6', |
| 47 | + '7', '8', '9', '10', '11', '12', |
| 48 | + '13', 0, 'test_server', |
| 49 | + 'test_sender', 'test_recipient', |
| 50 | + 'test_username', 'test_password'] |
| 51 | + s = SMTPNotification(self.db) |
| 52 | + s.send('test_subject', 'test_body') |
| 53 | + assert mock_smtp.call_count == 0 |
| 54 | + assert instance.login.call_count == 0 |
| 55 | + assert instance.sendmail.call_count == 0 |
| 56 | + |
| 57 | + @patch("smtplib.SMTP") |
| 58 | + def test_MarketSmtp_send_throw_smtpexception(self, mock_smtp): |
| 59 | + '''Email sent when enabled''' |
| 60 | + catcher = self.catcher |
| 61 | + mock_smtp.side_effect = SMTPAuthenticationError(50, 'Test error thrown') |
| 62 | + s = SMTPNotification(self.db) |
| 63 | + s.send('test_subject', 'test_body') |
| 64 | + mock_smtp.assert_called_once_with('test_server') |
| 65 | + catch_exception = catcher.pop() |
| 66 | + self.assertEquals(catch_exception["message"][0], "[ERROR] Authentication Error: (50, 'Test error thrown')") |
| 67 | + |
| 68 | + @patch("smtplib.SMTP") |
| 69 | + def test_MarketSmtp_send_throw_exception(self, mock_smtp): |
| 70 | + '''Email sent when enabled''' |
| 71 | + catcher = self.catcher |
| 72 | + mock_smtp.side_effect = Exception('Test exception thrown') |
| 73 | + s = SMTPNotification(self.db) |
| 74 | + s.send('test_subject', 'test_body') |
| 75 | + mock_smtp.assert_called_once_with('test_server') |
| 76 | + catch_exception = catcher.pop() |
| 77 | + self.assertEquals(catch_exception["message"][0], "[ERROR] Test exception thrown") |
0 commit comments