Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 09e3dde

Browse files
authored
Merge pull request #438 from tomgalloway/SMTP_Tests
Smtp tests
2 parents 9e34976 + 4f6933d commit 09e3dde

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

market/smtpnotification.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@ class SMTPNotification(object):
1414

1515
def __init__(self, db):
1616
self.db = db
17-
self.server = 'localhost:25'
18-
self.sender = 'OpenBazaar'
19-
self.recipient = ''
20-
self.username = None
21-
self.password = None
22-
2317
self.log = Logger(system=self)
24-
2518
self.get_smtp_settings()
2619

2720
def get_smtp_settings(self):
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)