class Test_events(unittest.TestCase): def setUp(self): with logging_disabled(): from b3.fake import FakeConsole, FakeClient self.console = FakeConsole('@b3/conf/b3.distribution.xml') self.conf = CfgConfigParser() self.p = GaPlugin(self.console, self.conf) self.conf.loadFromString( dedent(""" [google analytics] tracking ID: UA-000000-0 """)) self.p.onLoadConfig() self.p.onStartup() self.assertIsNotNone(self.p._ga_tracking_id) self.joe = FakeClient(self.console, name="Joe", guid="GUID_joe", ip="12.12.12.12") self.jack = FakeClient(self.console, name="Jack", guid="GUID_jack", ip="45.45.45.45") def test_auth(self, fire_mock): self.joe.connects("1") # THEN self.assertEqual(1, len(fire_mock.mock_calls)) def test_disconnect(self, fire_mock): self.joe.connects("1") self.joe.disconnects() # THEN self.assertEqual(2, len(fire_mock.mock_calls)) def test_new_round(self, fire_mock): # GIVEN self.joe.connects("1") self.jack.connects("2") # WHEN self.console.game.gameType = "The game type" self.console.game.mapName = "The map name" self.console.queueEvent( self.console.getEvent('EVT_GAME_ROUND_START', data=self.console.game)) # THEN self.assertEqual(4, len(fire_mock.mock_calls))
class Test_events(unittest.TestCase): def setUp(self): with logging_disabled(): from b3.fake import FakeConsole, FakeClient self.console = FakeConsole('@b3/conf/b3.distribution.xml') self.conf = CfgConfigParser() self.p = GaPlugin(self.console, self.conf) self.conf.loadFromString(dedent(""" [google analytics] tracking ID: UA-000000-0 """)) self.p.onLoadConfig() self.p.onStartup() self.assertIsNotNone(self.p._ga_tracking_id) self.joe = FakeClient(self.console, name="Joe", guid="GUID_joe", ip="12.12.12.12") self.jack = FakeClient(self.console, name="Jack", guid="GUID_jack", ip="45.45.45.45") def test_auth(self, fire_mock): self.joe.connects("1") # THEN self.assertEqual(1, len(fire_mock.mock_calls)) def test_disconnect(self, fire_mock): self.joe.connects("1") self.joe.disconnects() # THEN self.assertEqual(2, len(fire_mock.mock_calls)) def test_new_round(self, fire_mock): # GIVEN self.joe.connects("1") self.jack.connects("2") # WHEN self.console.game.gameType = "The game type" self.console.game.mapName = "The map name" self.console.queueEvent(self.console.getEvent('EVT_GAME_ROUND_START', data=self.console.game)) # THEN self.assertEqual(4, len(fire_mock.mock_calls))
class LocationTestCase(unittest2.TestCase): def setUp(self): # create a FakeConsole parser parser_ini_conf = CfgConfigParser() parser_ini_conf.loadFromString(r'''''') self.parser_main_conf = MainConfig(parser_ini_conf) with logging_disabled(): from b3.fake import FakeConsole self.console = FakeConsole(self.parser_main_conf) with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') self.adminPlugin._commands = {} self.adminPlugin.onStartup() # make sure the admin plugin obtained by other plugins is our admin plugin when(self.console).getPlugin('admin').thenReturn(self.adminPlugin) # simulate geolocation plugin registering events self.console.createEvent('EVT_CLIENT_GEOLOCATION_SUCCESS', 'Event client geolocation success') self.console.screen = Mock() self.console.time = time.time self.console.upTime = Mock(return_value=1000) self.console.cron.stop() self.conf = CfgConfigParser() self.conf.loadFromString( dedent(r""" [settings] announce: yes [messages] client_connect: ^7$name ^3from ^7$city ^3(^7$country^3) connected cmd_locate: ^7$name ^3is connected from ^7$city ^3(^7$country^3) cmd_locate_failed: ^7Could not locate ^1$name cmd_distance: ^7$name ^3is ^7$distance ^3km away from you cmd_distance_self: ^7Sorry, I'm not that smart...meh! cmd_distance_failed: ^7Could not compute distance with ^1$name cmd_isp: ^7$name ^3is using ^7$isp ^3as isp cmd_isp_failed: ^7Could not determine ^1$name ^7isp [commands] locate: user distance: user isp: mod """)) self.p = LocationPlugin(self.console, self.conf) self.p.onLoadConfig() self.p.onStartup() with logging_disabled(): from b3.fake import FakeClient self.mike = FakeClient(console=self.console, name="Mike", guid="MIKEGUID", groupBits=1) self.bill = FakeClient(console=self.console, name="Bill", guid="BILLGUID", groupBits=16) self.mike.location = LOCATION_MIKE self.bill.location = LOCATION_BILL def tearDown(self): unstub() #################################################################################################################### # # # TEST EVENTS # # # #################################################################################################################### def test_event_client_geolocation_success(self): # GIVEN self.mike.connects('1') self.console.say = Mock() # WHEN self.console.queueEvent( self.console.getEvent('EVT_CLIENT_GEOLOCATION_SUCCESS', client=self.mike)) # THEN self.console.say.assert_called_with( '^7Mike ^3from ^7Rome ^3(^7Italy^3) connected') #################################################################################################################### # # # TEST COMMANDS # # # #################################################################################################################### def test_cmd_locate_no_arguments(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!locate") # THEN self.assertListEqual(['missing data, try !help locate'], self.mike.message_history) def test_cmd_locate_failed(self): # GIVEN self.mike.connects('1') self.bill.connects('2') self.bill.location = None # WHEN self.mike.clearMessageHistory() self.mike.says("!locate bill") # THEN self.assertListEqual(['Could not locate Bill'], self.mike.message_history) def test_cmd_locate(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!locate bill") # THEN self.assertListEqual( ['Bill is connected from Mountain View (United States)'], self.mike.message_history) def test_cmd_distance_no_arguments(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!distance") # THEN self.assertListEqual(['missing data, try !help distance'], self.mike.message_history) def test_cmd_distance_self(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!distance mike") # THEN self.assertListEqual(['Sorry, I\'m not that smart...meh!'], self.mike.message_history) def test_cmd_distance_failed(self): # GIVEN self.mike.connects('1') self.bill.connects('2') self.bill.location = None # WHEN self.mike.clearMessageHistory() self.mike.says("!distance bill") # THEN self.assertListEqual(['Could not compute distance with Bill'], self.mike.message_history) def test_cmd_distance(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!distance bill") # THEN self.assertListEqual(['Bill is 10068.18 km away from you'], self.mike.message_history) def test_cmd_isp_no_arguments(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.bill.clearMessageHistory() self.bill.says("!isp") # THEN self.assertListEqual(['missing data, try !help isp'], self.bill.message_history) def test_cmd_isp_failed(self): # GIVEN self.mike.connects('1') self.bill.connects('2') self.mike.location = None # WHEN self.bill.clearMessageHistory() self.bill.says("!isp mike") # THEN self.assertListEqual(['Could not determine Mike isp'], self.bill.message_history) def test_cmd_isp(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.bill.clearMessageHistory() self.bill.says("!isp mike") # THEN self.assertListEqual(['Mike is using Fastweb as isp'], self.bill.message_history)
class LocationTestCase(unittest2.TestCase): def setUp(self): # create a FakeConsole parser parser_ini_conf = CfgConfigParser() parser_ini_conf.loadFromString(r'''''') self.parser_main_conf = MainConfig(parser_ini_conf) with logging_disabled(): from b3.fake import FakeConsole self.console = FakeConsole(self.parser_main_conf) with logging_disabled(): self.adminPlugin = AdminPlugin(self.console, '@b3/conf/plugin_admin.ini') self.adminPlugin._commands = {} self.adminPlugin.onStartup() # make sure the admin plugin obtained by other plugins is our admin plugin when(self.console).getPlugin('admin').thenReturn(self.adminPlugin) # simulate geolocation plugin registering events self.console.createEvent('EVT_CLIENT_GEOLOCATION_SUCCESS', 'Event client geolocation success') self.console.screen = Mock() self.console.time = time.time self.console.upTime = Mock(return_value=1000) self.console.cron.stop() self.conf = CfgConfigParser() self.conf.loadFromString(dedent(r""" [settings] announce: yes [messages] client_connect: ^7$name ^3from ^7$city ^3(^7$country^3) connected cmd_locate: ^7$name ^3is connected from ^7$city ^3(^7$country^3) cmd_locate_failed: ^7Could not locate ^1$name cmd_distance: ^7$name ^3is ^7$distance ^3km away from you cmd_distance_self: ^7Sorry, I'm not that smart...meh! cmd_distance_failed: ^7Could not compute distance with ^1$name cmd_isp: ^7$name ^3is using ^7$isp ^3as isp cmd_isp_failed: ^7Could not determine ^1$name ^7isp [commands] locate: user distance: user isp: mod """)) self.p = LocationPlugin(self.console, self.conf) self.p.onLoadConfig() self.p.onStartup() with logging_disabled(): from b3.fake import FakeClient self.mike = FakeClient(console=self.console, name="Mike", guid="MIKEGUID", groupBits=1) self.bill = FakeClient(console=self.console, name="Bill", guid="BILLGUID", groupBits=16) self.mike.location = LOCATION_MIKE self.bill.location = LOCATION_BILL def tearDown(self): unstub() #################################################################################################################### # # # TEST EVENTS # # # #################################################################################################################### def test_event_client_geolocation_success(self): # GIVEN self.mike.connects('1') self.console.say = Mock() # WHEN self.console.queueEvent(self.console.getEvent('EVT_CLIENT_GEOLOCATION_SUCCESS', client=self.mike)) # THEN self.console.say.assert_called_with('^7Mike ^3from ^7Rome ^3(^7Italy^3) connected') #################################################################################################################### # # # TEST COMMANDS # # # #################################################################################################################### def test_cmd_locate_no_arguments(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!locate") # THEN self.assertListEqual(['missing data, try !help locate'], self.mike.message_history) def test_cmd_locate_failed(self): # GIVEN self.mike.connects('1') self.bill.connects('2') self.bill.location = None # WHEN self.mike.clearMessageHistory() self.mike.says("!locate bill") # THEN self.assertListEqual(['Could not locate Bill'], self.mike.message_history) def test_cmd_locate(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!locate bill") # THEN self.assertListEqual(['Bill is connected from Mountain View (United States)'], self.mike.message_history) def test_cmd_distance_no_arguments(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!distance") # THEN self.assertListEqual(['missing data, try !help distance'], self.mike.message_history) def test_cmd_distance_self(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!distance mike") # THEN self.assertListEqual(['Sorry, I\'m not that smart...meh!'], self.mike.message_history) def test_cmd_distance_failed(self): # GIVEN self.mike.connects('1') self.bill.connects('2') self.bill.location = None # WHEN self.mike.clearMessageHistory() self.mike.says("!distance bill") # THEN self.assertListEqual(['Could not compute distance with Bill'], self.mike.message_history) def test_cmd_distance(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.mike.clearMessageHistory() self.mike.says("!distance bill") # THEN self.assertListEqual(['Bill is 10068.18 km away from you'], self.mike.message_history) def test_cmd_isp_no_arguments(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.bill.clearMessageHistory() self.bill.says("!isp") # THEN self.assertListEqual(['missing data, try !help isp'], self.bill.message_history) def test_cmd_isp_failed(self): # GIVEN self.mike.connects('1') self.bill.connects('2') self.mike.location = None # WHEN self.bill.clearMessageHistory() self.bill.says("!isp mike") # THEN self.assertListEqual(['Could not determine Mike isp'], self.bill.message_history) def test_cmd_isp(self): # GIVEN self.mike.connects('1') self.bill.connects('2') # WHEN self.bill.clearMessageHistory() self.bill.says("!isp mike") # THEN self.assertListEqual(['Mike is using Fastweb as isp'], self.bill.message_history)