class CountryFilterTestCase(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() # create our plugin instance self.p = CountryfilterPlugin(self.console, CfgConfigParser()) with logging_disabled(): from b3.fake import FakeClient self.mike = FakeClient(console=self.console, name="Mike", guid="MIKEGUID", ip='1.2.3.4', groupBits=0) self.bill = FakeClient(console=self.console, name="Bill", guid="BILLGUID", ip='2.3.4.5', groupBits=0) self.mike.location = LOCATION_MIKE self.bill.location = LOCATION_BILL def tearDown(self): unstub() def init(self, config_content): """ Initialize the plugin using the given configuration file content """ self.p.config.loadFromString(config_content) self.p.onLoadConfig() self.p.onStartup()
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()
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()
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)