-
+ B5E72914E0E830B2EBAB8B1052667ED3A36F72ACD69312F87516894510ACA9EBF0A629F502B02B960E7AA4BC383AF801575B9EA0FD7860C26B9029B9AF2D0540blatta/tests/test_direct.py(0 . 0)(1 . 73)
3162 import unittest
3163
3164 import time
3165
3166 import helper
3167 from mock import Mock
3168
3169 from lib.state import State
3170 from lib.message import Message
3171 from lib.direct import Direct
3172 from lib.long_buffer import LongBuffer
3173 from lib.order_buffer import OrderBuffer
3174
3175 class TestMessage(unittest.TestCase):
3176 def setUp(self):
3177 helper.setup()
3178 self.alice_socket = Mock()
3179 self.alice_state = State(self.alice_socket)
3180 self.bob_socket = Mock()
3181 self.bob_state = State(self.bob_socket)
3182 self.setupAlice()
3183 self.setupBob()
3184
3185 def setupAlice(self):
3186 self.bob_state.add_peer('alice')
3187 self.bob_state.add_key(
3188 'alice',
3189 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
3190 )
3191 self.bob_state.update_at({
3192 'handle': 'alice',
3193 'address': '127.0.0.1',
3194 'port': 8888
3195 })
3196
3197 def setupBob(self):
3198 self.alice_state.add_peer('bob')
3199 self.alice_state.add_key(
3200 'bob',
3201 '9h6wYndVjt8QpnIZOYb7KD2tYKCKw4rjlYg4LM1ODx1Qkr3qA0IuKNukkwKhQ4UP9ypMlhyPHa7AGD7NO7Ws5w=='
3202 )
3203 self.alice_state.update_at({
3204 'handle': 'bob',
3205 'address': '127.0.0.1',
3206 'port': 8889
3207 })
3208
3209 def tearDown(self):
3210 self.alice_state.remove_peer('bob')
3211 self.bob_state.remove_peer('alice')
3212
3213 def test_direct_message(self):
3214 message = Direct({
3215 'handle': 'bob',
3216 'speaker': 'alice',
3217 'body': 'm1',
3218 'long_buffer': LongBuffer(self.alice_state),
3219 'timestamp': int(time.time())
3220 }, self.alice_state)
3221 bob = self.alice_state.get_peer_by_handle('bob')
3222 message.message_bytes = message.get_message_bytes(bob)
3223 message.send()
3224 black_packet = Message.pack(bob, message.command, message.bounces, message.message_bytes)
3225 self.bob_socket.sendto.called_once_with(black_packet, (bob.address, bob.port))
3226
3227 # now bob must unpack the black packet
3228 alice = self.bob_state.get_peer_by_handle('alice')
3229 received_message = Message.unpack(alice,
3230 black_packet,
3231 LongBuffer(self.bob_state),
3232 OrderBuffer(self.bob_state),
3233 {})
3234 self.assertEqual(message.body, received_message['body'])