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

Commit 9e34976

Browse files
committed
Merge pull request #431 from tomgalloway/market_listeners_tests
Market listeners tests
2 parents da0c261 + 4bd9fc8 commit 9e34976

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

market/listeners.py

Lines changed: 6 additions & 4 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:

market/tests/test_listeners.py

Lines changed: 22 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
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,21 @@ 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)

0 commit comments

Comments
 (0)