# https://stackoverflow.com/questions/1896918/running-unittest-with-typical-test-directory-structure import unittest import logging from mock import Mock from mock import patch from lib.station import Station class TestStation(unittest.TestCase): def setUp(self): logging.basicConfig(level=logging.DEBUG) options = { "clients": {"clientsocket": Mock()}, "db_path": "tests/test.db", "socket": Mock() } self.station = Station(options) self.station.deliver = Mock() self.station.rebroadcast = Mock() self.station.rebroadcast.return_value = "foobar" def tearDown(self): pass def test_embargo_bounce_ordering(self): peer1 = Mock() peer1.handles = ["a", "b"] peer2 = Mock() peer2.handles = ["c", "d"] low_bounce_message = Mock() low_bounce_message.peer = peer1 low_bounce_message.bounces = 1 low_bounce_message.message_hash = "messagehash" high_bounce_message = Mock() high_bounce_message.peer = peer2 high_bounce_message.bounces = 2 high_bounce_message.message_hash = "messagehash" self.station.embargo_queue = { "messagehash": [ low_bounce_message, high_bounce_message ], } self.station.flush_hearsay_messages() self.station.deliver.assert_called_once_with(low_bounce_message) self.station.rebroadcast.assert_called_once_with(low_bounce_message) def test_immediate_message_delivered(self): peer = Mock() peer.handles = ["a", "b"] message = Mock() message.speaker = "a" message.peer = peer self.station.embargo_queue = { "messagehash": [ message ], } self.station.check_for_immediate_messages() self.station.deliver.assert_called_once_with(message) self.station.rebroadcast.assert_called_once_with(message) def test_hearsay_message_not_delivered(self): peer = Mock() peer.handles = ["a", "b"] message = Mock() message.speaker = "c" message.peer = peer self.station.embargo_queue = { "messagehash": [ message ], } self.station.check_for_immediate_messages() self.station.deliver.assert_not_called() def test_embargo_queue_cleared(self): peer = Mock() peer.handles = ["a", "b"] message = Mock() message.speaker = "c" message.peer = peer self.station.embargo_queue = { "messagehash": [ message ], } self.assertEqual(len(self.station.embargo_queue), 1) self.station.flush_hearsay_messages() self.assertEqual(len(self.station.embargo_queue), 0) def test_immediate_prefix(self): peer = Mock() peer.handles = ["a", "b"] message = Mock() message.speaker = "a" message.prefix = None message.peer = peer self.station.embargo_queue = { "messagehash": [ message ], } self.station.check_for_immediate_messages() self.assertEqual(message.prefix, None) def test_simple_hearsay_prefix(self): peer = Mock() peer.handles = ["a", "b"] message = Mock() message.speaker = "c" message.prefix = None message.peer = peer self.station.embargo_queue = { "messagehash": [ message ], } self.station.flush_hearsay_messages() self.assertEqual(message.prefix, "c[a]") def test_in_wot_hearsay_prefix_under_four(self): peer1 = Mock() peer1.handles = ["a", "b"] peer2 = Mock() peer2.handles = ["d", "e"] peer3 = Mock() peer3.handles = ["f", "g"] message_via_peer1 = Mock() message_via_peer1.speaker = "c" message_via_peer1.prefix = None message_via_peer1.peer = peer1 message_via_peer1.bounces = 1 message_via_peer2 = Mock() message_via_peer2.speaker = "c" message_via_peer2.prefix = None message_via_peer2.peer = peer2 message_via_peer2.bounces = 2 message_via_peer3 = Mock() message_via_peer3.speaker = "c" message_via_peer3.prefix = None message_via_peer3.peer = peer3 message_via_peer3.bounces = 1 self.station.embargo_queue = { "messagehash": [ message_via_peer1, message_via_peer2, message_via_peer3 ], } self.station.flush_hearsay_messages() self.station.deliver.assert_called_once_with(message_via_peer1) self.assertEqual(message_via_peer1.prefix, "c[a|d|f]") def test_in_wot_hearsay_prefix_more_than_three(self): peer1 = Mock() peer1.handles = ["a", "b"] peer2 = Mock() peer2.handles = ["d", "e"] peer3 = Mock() peer3.handles = ["f", "g"] peer4 = Mock() peer4.handles = ["f", "g"] message_via_peer1 = Mock() message_via_peer1.speaker = "c" message_via_peer1.prefix = None message_via_peer1.peer = peer1 message_via_peer1.bounces = 1 message_via_peer2 = Mock() message_via_peer2.speaker = "c" message_via_peer2.prefix = None message_via_peer2.peer = peer2 message_via_peer2.bounces = 2 message_via_peer3 = Mock() message_via_peer3.speaker = "c" message_via_peer3.prefix = None message_via_peer3.peer = peer3 message_via_peer3.bounces = 1 message_via_peer4 = Mock() message_via_peer4.speaker = "c" message_via_peer4.prefix = None message_via_peer4.peer = peer4 message_via_peer4.bounces = 1 self.station.embargo_queue = { "messagehash": [ message_via_peer1, message_via_peer2, message_via_peer3, message_via_peer4 ], } self.station.flush_hearsay_messages() self.station.deliver.assert_called_once_with(message_via_peer1) self.assertEqual(message_via_peer1.prefix, "c[4]")