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

Commit 0a006b2

Browse files
committed
Merge branch 'master' of https://github.com/OpenBazaar/OpenBazaar-Server into brian-stats-collection
2 parents 22933a2 + ddf06d3 commit 0a006b2

File tree

4 files changed

+128
-16
lines changed

4 files changed

+128
-16
lines changed

market/listeners.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
import json
44
import time
55
import random
6+
from log import Logger
67
from api.utils import sanitize_html
78
from interfaces import MessageListener, BroadcastListener, NotificationListener
89
from zope.interface import implements
910
from protos.objects import PlaintextMessage, Following
1011
from dht.utils import digest
1112

12-
1313
class MessageListenerImpl(object):
1414
implements(MessageListener)
1515

1616
def __init__(self, web_socket_factory, database):
1717
self.ws = web_socket_factory
1818
self.db = database
19+
self.log = Logger(system=self)
1920

2021
def notify(self, plaintext, signature):
2122
try:
@@ -45,9 +46,8 @@ def notify(self, plaintext, signature):
4546
if plaintext.handle:
4647
message_json["message"]["handle"] = plaintext.handle
4748
self.ws.push(json.dumps(sanitize_html(message_json), indent=4))
48-
except Exception:
49-
pass
50-
49+
except Exception as e:
50+
self.log.error('Market.Listener.notify Exception: %s' % e)
5151

5252
class BroadcastListenerImpl(object):
5353
implements(BroadcastListener)
@@ -60,6 +60,8 @@ def notify(self, guid, message):
6060
# pull the metadata for this node from the db
6161
f = Following()
6262
ser = self.db.follow.get_following()
63+
handle = ""
64+
avatar_hash = ""
6365
if ser is not None:
6466
f.ParseFromString(ser)
6567
for user in f.users:
@@ -107,7 +109,7 @@ def notify(self, guid, handle, notif_type, order_id, title, image_hash):
107109
"image_hash": image_hash.encode("hex")
108110
}
109111
}
110-
self.ws.push(json.dumps(sanitize_html(notification_json), indent=4))
112+
self.push_ws(notification_json)
111113

112114
def push_ws(self, json_obj):
113115
self.ws.push(json.dumps(sanitize_html(json_obj), indent=4))

market/smtpnotification.py

Lines changed: 3 additions & 10 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):
@@ -56,8 +49,8 @@ def send(self, subject, body):
5649
server.login(self.username, self.password)
5750

5851
server.sendmail(self.sender, self.recipient, msg.as_string())
52+
server.quit()
5953
except SMTPAuthenticationError as e:
54+
self.log.error('Authentication Error: %s' % e)
55+
except Exception as e:
6056
self.log.error(e)
61-
print e
62-
63-
server.quit()

market/tests/test_listeners.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from twisted.trial import unittest
22
from twisted.python import log
33
from mock import MagicMock
4+
import mock
45

5-
from market.listeners import MessageListenerImpl
6+
from market.listeners import MessageListenerImpl, BroadcastListenerImpl, NotificationListenerImpl
67
from protos.objects import PlaintextMessage
78

89
class MarketListenersTest(unittest.TestCase):
@@ -47,6 +48,7 @@ def _create_valid_message_json(handle):
4748
return message
4849

4950
def test_MarketListeners_notify_without_handle_success(self):
51+
'''MessageListenerImpl correctly notify without handle.'''
5052
p = self._create_valid_plaintext_message('')
5153
signature = 'test_signature'
5254
l = MessageListenerImpl(self.ws, self.db)
@@ -61,6 +63,7 @@ def test_MarketListeners_notify_without_handle_success(self):
6163
self.ws.push.assert_called_with(self._create_valid_message_json(''))
6264

6365
def test_MarketListeners_notify_with_handle_success(self):
66+
'''MessageListenerImpl correctly notify with handle.'''
6467
p = self._create_valid_plaintext_message('test_handle')
6568
signature = 'test_signature'
6669
l = MessageListenerImpl(self.ws, self.db)
@@ -74,3 +77,40 @@ def test_MarketListeners_notify_with_handle_success(self):
7477
'test_avatar_hash',
7578
signature, False)
7679
self.ws.push.assert_called_with(self._create_valid_message_json('test_handle'))
80+
81+
def test_MarketListeners_save_message_exception(self):
82+
p = self._create_valid_plaintext_message('test_handle')
83+
signature = 'test_signature'
84+
l = MessageListenerImpl(self.ws, self.db)
85+
self.db.messages.save_message.side_effect = Exception("test_exception")
86+
l.notify(p, signature)
87+
self.assertEqual('[ERROR] Market.Listener.notify Exception: test_exception', self.catcher[0]['message'][0])
88+
89+
def test_MarketListeners_broadcast_notify_success(self):
90+
'''BroadcastListenerImpl correctly notifies.'''
91+
b = BroadcastListenerImpl(self.ws, self.db)
92+
b.notify('123', 'test_message')
93+
self.db.broadcasts.save_broadcast.assert_called_once_with(mock.ANY,
94+
'313233', '',
95+
'test_message',
96+
mock.ANY, '')
97+
self.ws.push.assert_called_once_with(mock.ANY)
98+
99+
def test_MarketListeners_notifiation_notify_success(self):
100+
n = NotificationListenerImpl(self.ws, self.db)
101+
n.notify('1231', 'test_handle', 'test_notify_type', 'test_order_id',
102+
'test_title', 'test_image_hash')
103+
self.db.notifications.save_notification.assert_called_once_with(mock.ANY, '31323331', 'test_handle',
104+
'test_notify_type', 'test_order_id',
105+
'test_title', mock.ANY, 'test_image_hash')
106+
self.ws.push.assert_called_once_with(mock.ANY)
107+
108+
def test_MarketListeners_notifiation_push_success(self):
109+
notification_json = {
110+
"notification": {
111+
"guid": "guid"
112+
}
113+
}
114+
n = NotificationListenerImpl(self.ws, self.db)
115+
n.push_ws(notification_json)
116+
self.ws.push.assert_called_once_with('{\n "notification": {\n "guid": "guid"\n }\n}')
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)