11from twisted .trial import unittest
22from twisted .python import log
3- from mock import MagicMock
3+ from mock import patch , MagicMock
4+ import mock
45
56from market .smtpnotification import SMTPNotification
67
@@ -12,17 +13,42 @@ def setUp(self):
1213 log .addObserver (observer )
1314 self .addCleanup (log .removeObserver , observer )
1415 self .db = MagicMock ()
15-
16- def test_MarketSmtp_settings_success (self ):
17- '''SMTP Notification settings correctly set.'''
1816 self .db .settings .get .return_value = ['0' , '1' , '2' , '3' , '4' , '5' , '6' ,
1917 '7' , '8' , '9' , '10' , '11' , '12' ,
20- '13' , '14' , 'test_server' ,
18+ '13' , 1 , 'test_server' ,
2119 'test_sender' , 'test_recipient' ,
2220 'test_username' , 'test_password' ]
21+
22+ def test_MarketSmtp_settings_success (self ):
23+ '''SMTP Notification settings correctly set.'''
2324 s = SMTPNotification (self .db )
2425 self .assertEqual ('test_server' , s .server )
2526 self .assertEqual ('test_sender' , s .sender )
2627 self .assertEqual ('test_recipient' , s .recipient )
2728 self .assertEqual ('test_username' , s .username )
2829 self .assertEqual ('test_password' , s .password )
30+
31+ @patch ("smtplib.SMTP" )
32+ def test_MarketSmtp_send_enabled_success (self , mock_smtp ):
33+ '''Email sent when enabled'''
34+ instance = mock_smtp .return_value
35+ s = SMTPNotification (self .db )
36+ s .send ('test_subject' , 'test_body' )
37+ mock_smtp .assert_called_once_with ('test_server' )
38+ instance .login .assert_called_once_with ('test_username' , 'test_password' )
39+ instance .sendmail .assert_called_once_with ('test_sender' , 'test_recipient' , mock .ANY )
40+
41+ @patch ("smtplib.SMTP" )
42+ def test_MarketSmtp_send_disabled_not_sent (self , mock_smtp ):
43+ '''Email not sent when disabled'''
44+ instance = mock_smtp .return_value
45+ self .db .settings .get .return_value = ['0' , '1' , '2' , '3' , '4' , '5' , '6' ,
46+ '7' , '8' , '9' , '10' , '11' , '12' ,
47+ '13' , 0 , 'test_server' ,
48+ 'test_sender' , 'test_recipient' ,
49+ 'test_username' , 'test_password' ]
50+ s = SMTPNotification (self .db )
51+ s .send ('test_subject' , 'test_body' )
52+ assert mock_smtp .call_count == 0
53+ assert instance .login .call_count == 0
54+ assert instance .sendmail .call_count == 0
0 commit comments