9982-getdata 1 import unittest
9982-getdata 2
9982-getdata 3 import time
9982-getdata 4
9982-getdata 5 import helper
9982-getdata 6 from mock import Mock
9982-getdata 7
9982-getdata 8 from lib.state import State
9982-getdata 9 from lib.message import Message
9982-getdata 10 from lib.direct import Direct
9982-getdata 11 from lib.long_buffer import LongBuffer
9982-getdata 12 from lib.order_buffer import OrderBuffer
9982-getdata 13
9982-getdata 14 class TestMessage(unittest.TestCase):
9982-getdata 15 def setUp(self):
9982-getdata 16 helper.setup()
9982-getdata 17 self.alice_socket = Mock()
9982-getdata 18 self.alice_state = State(self.alice_socket)
9982-getdata 19 self.bob_socket = Mock()
9982-getdata 20 self.bob_state = State(self.bob_socket)
9982-getdata 21 self.setupAlice()
9982-getdata 22 self.setupBob()
9982-getdata 23
9982-getdata 24 def setupAlice(self):
9982-getdata 25 self.bob_state.add_peer('alice')
9982-getdata 26 self.bob_state.add_key(
9982-getdata 27 'alice',
9982-getdata 28 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 29 )
9982-getdata 30 self.bob_state.update_at({
9982-getdata 31 'handle': 'alice',
9982-getdata 32 'address': '127.0.0.1',
9982-getdata 33 'port': 8888
9982-getdata 34 })
9982-getdata 35
9982-getdata 36 def setupBob(self):
9982-getdata 37 self.alice_state.add_peer('bob')
9982-getdata 38 self.alice_state.add_key(
9982-getdata 39 'bob',
9982-getdata 40 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
9982-getdata 41 )
9982-getdata 42 self.alice_state.update_at({
9982-getdata 43 'handle': 'bob',
9982-getdata 44 'address': '127.0.0.1',
9982-getdata 45 'port': 8889
9982-getdata 46 })
9982-getdata 47
9982-getdata 48 def tearDown(self):
9982-getdata 49 self.alice_state.remove_peer('bob')
9982-getdata 50 self.bob_state.remove_peer('alice')
9982-getdata 51
9982-getdata 52 def test_direct_message(self):
9982-getdata 53 message = Direct({
9982-getdata 54 'handle': 'bob',
9982-getdata 55 'speaker': 'alice',
9982-getdata 56 'body': 'm1',
9982-getdata 57 'long_buffer': LongBuffer(self.alice_state),
9982-getdata 58 'timestamp': int(time.time())
9982-getdata 59 }, self.alice_state)
9982-getdata 60 bob = self.alice_state.get_peer_by_handle('bob')
9982-getdata 61 message.message_bytes = message.get_message_bytes(bob)
9982-getdata 62 message.send()
9982-getdata 63 black_packet = Message.pack(bob, message.command, message.bounces, message.message_bytes)
9982-getdata 64 self.bob_socket.sendto.called_once_with(black_packet, (bob.address, bob.port))
9982-getdata 65
9982-getdata 66
9982-getdata 67 alice = self.bob_state.get_peer_by_handle('alice')
9982-getdata 68 received_message = Message.unpack(alice,
9982-getdata 69 black_packet,
9982-getdata 70 LongBuffer(self.bob_state),
9982-getdata 71 OrderBuffer(self.bob_state),
9982-getdata 72 {})
9982-getdata 73 self.assertEqual(message.body, received_message['body'])