|
| 1 | +from twisted.trial import unittest |
| 2 | +from twisted.python import log |
| 3 | +from mock import MagicMock |
| 4 | + |
| 5 | +from market.listeners import MessageListenerImpl |
| 6 | +from protos.objects import PlaintextMessage |
| 7 | + |
| 8 | +class MarketListenersTest(unittest.TestCase): |
| 9 | + |
| 10 | + def setUp(self): |
| 11 | + self.catcher = [] |
| 12 | + observer = self.catcher.append |
| 13 | + log.addObserver(observer) |
| 14 | + self.addCleanup(log.removeObserver, observer) |
| 15 | + self.db = MagicMock() |
| 16 | + self.ws = MagicMock() |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def _create_valid_plaintext_message(handle): |
| 20 | + p = PlaintextMessage() |
| 21 | + p.sender_guid = 'test_guid' |
| 22 | + p.handle = handle |
| 23 | + p.pubkey = 'test_pubkey' |
| 24 | + p.subject = 'test_subject' |
| 25 | + p.type = 1 |
| 26 | + p.message = 'test_message' |
| 27 | + p.timestamp = 10 |
| 28 | + p.avatar_hash = 'test_avatar_hash' |
| 29 | + return p |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def _create_valid_message_json(handle): |
| 33 | + new_line = '\n' |
| 34 | + tab = ' ' |
| 35 | + nlt = new_line + tab |
| 36 | + nldt = nlt + tab |
| 37 | + if handle != '': |
| 38 | + handle = '"handle": "'+handle+'", ' + nldt |
| 39 | + message = '{' + nlt + '"message": {' + nldt + \ |
| 40 | + '"public_key": "746573745f7075626b6579", ' + nldt + handle + \ |
| 41 | + '"sender": "746573745f67756964", ' + nldt + \ |
| 42 | + '"timestamp": 10, ' + nldt + \ |
| 43 | + '"avatar_hash": "746573745f6176617461725f68617368", ' + nldt + \ |
| 44 | + '"message": "test_message", ' + nldt + \ |
| 45 | + '"message_type": "ORDER", ' + nldt + \ |
| 46 | + '"subject": "test_subject"' + nlt + '}\n}' |
| 47 | + return message |
| 48 | + |
| 49 | + def test_MarketListeners_notify_without_handle_success(self): |
| 50 | + p = self._create_valid_plaintext_message('') |
| 51 | + signature = 'test_signature' |
| 52 | + l = MessageListenerImpl(self.ws, self.db) |
| 53 | + l.notify(p, signature) |
| 54 | + self.db.messages.save_message.assert_called_with('746573745f67756964', |
| 55 | + u'', 'test_pubkey', |
| 56 | + u'test_subject', |
| 57 | + 'ORDER', |
| 58 | + u'test_message', 10, |
| 59 | + 'test_avatar_hash', |
| 60 | + signature, False) |
| 61 | + self.ws.push.assert_called_with(self._create_valid_message_json('')) |
| 62 | + |
| 63 | + def test_MarketListeners_notify_with_handle_success(self): |
| 64 | + p = self._create_valid_plaintext_message('test_handle') |
| 65 | + signature = 'test_signature' |
| 66 | + l = MessageListenerImpl(self.ws, self.db) |
| 67 | + l.notify(p, signature) |
| 68 | + self.db.messages.save_message.assert_called_with('746573745f67756964', |
| 69 | + u'test_handle', |
| 70 | + 'test_pubkey', |
| 71 | + u'test_subject', |
| 72 | + 'ORDER', |
| 73 | + u'test_message', 10, |
| 74 | + 'test_avatar_hash', |
| 75 | + signature, False) |
| 76 | + self.ws.push.assert_called_with(self._create_valid_message_json('test_handle')) |
0 commit comments