def test_connection_doesnt_log_ignored_messages_sent_from_twitch(self): user = "******" bot = "nick_BOTtom" body = "Woot!" message = f':{user}!{user}@{user}.tmi.twitch.tv PRIVMSG #t_tv :{body}' bot_message = f':{bot}!{bot}@{bot}.tmi.twitch.tv PRIVMSG #t_tv :{body}' expected_print = "Chat Message From: happy_lass : Woot!" connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': bot, 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "t_tv" } dont_sleep = Time(dont_print).sleep spy_log = Spy_Log() mock_socket = socket.socket(dont_print, message) s = Subject(connect_to, mock_socket, spy_log.log, dont_sleep) s.scan() self.assertEqual(spy_log._history[-1], expected_print) mock_socket.message = bot_message s.scan() self.assertEqual(spy_log._history[-1], expected_print) self.assertTrue(bot not in spy_log._history[-1])
def test_go_command_logs_if_theres_an_error(self): spy = Spy_Log() s = Go("mocks/config.txt", "mocks/bad_triviaset.csv", Questions_Asked(), Player_Scores(), spy.log) mock_connection = Connection() _message = "irrelevant in this instance" s.run_the_next_trivia_round(mock_connection, _message) self.assertTrue("Ask" in spy._history[0] and "4" in spy._history[0])
def test_commander_logs_when_a_non_admin_attempts_a_stop_command(self): command_string = "!stop" test_response = ("not_an_admin", command_string) no_admins = [] dont_sleep = Time(dont_print).sleep mock_connection = Connection() spy = Spy_Log() commands = [(command_string, Spy_Command().ping, ["admin_only"])] s = Subject(commands, no_admins, mock_connection, spy.log, dont_sleep) mock_connection.last_response = test_response s.check_connection_last_message() expected_log = Log.bad_admin(test_response[0], test_response[1]) self.assertEqual(spy._history[-1], expected_log)
def test_commander_does_not_log_non_existent_commands(self): no_commands = [] admins = ["admiral_akbar"] test_response = (admins[0], "!not_a_command") expected_log = Log.bad_admin(test_response[0], test_response[1]) mock_connection = Connection() spy = Spy_Log() dont_sleep = Time(dont_print).sleep s = Subject(no_commands, admins, mock_connection, spy.log, dont_sleep) with ThreadPoolExecutor(max_workers=2) as e: e.submit(s.listen_for_commands) e.submit(self.chat_and_close, mock_connection, test_response) self.assertEqual(len(spy._history), 0)
def test_commander_logs_when_an_admin_makes_an_admin_only_command(self): command_string = "!green_group_stay_close_to_homing_sector_MG-7" admins = ["admiral_akbar"] test_response = (admins[0], command_string) dont_sleep = Time(dont_print).sleep mock_connection = Connection() spy = Spy_Log() commands = [(command_string, Spy_Command().ping, ["admin_only"])] s = Subject(commands, admins, mock_connection, spy.log, dont_sleep) with ThreadPoolExecutor(max_workers=2) as e: e.submit(s.listen_for_commands) e.submit(self.chat_and_close, mock_connection, test_response) expected_log = Log.good_admin(admins[0], command_string) self.assertEqual(spy._history[-1], expected_log)
def test_go_command_skips_running_if_another_game_is_in_progress(self): spy = Spy_Log() mock_player_scores = Player_Scores() s = Go("mocks/config.txt", "mocks/triviaset.csv", Questions_Asked(), mock_player_scores, spy.log) mock_connection = Connection() _message = "irrelevant in this instance" with ThreadPoolExecutor(max_workers=2) as e: e.submit(s.run_the_next_trivia_round, mock_connection, _message) e.submit(s.run_the_next_trivia_round, mock_connection, _message) round_start_count = 0 for message in mock_connection._message_list: if mock_player_scores._round_winners[0][0] in message: round_start_count += 1 self.assertEqual(round_start_count, 1)
def test_commander_logs_when_any_chat_participant_makes_an_open_command( self): command_string = "!everythings_fine_we're_all_fine_here_situation_normal" no_admins = [] test_response = ("han_solo", command_string) dont_sleep = Time(dont_print).sleep mock_connection = Connection() spy = Spy_Log() no_validations = [] commands = [(command_string, Spy_Command().ping, no_validations)] s = Subject(commands, no_admins, mock_connection, spy.log, dont_sleep) with ThreadPoolExecutor(max_workers=2) as e: e.submit(s.listen_for_commands) e.submit(self.chat_and_close, mock_connection, test_response) expected_log = Log.good_command(test_response[0], command_string) self.assertEqual(spy._history[-1], expected_log)
def test_connection_doesnt_recieve_messages_too_fast(self): connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': 'nick_BOTtom', 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "home_shopping_network" } spy_log = Spy_Log() spy_on_sleep = Time(spy_log.log).sleep mock_socket = socket.socket(dont_print) s = Subject(connect_to, mock_socket, dont_print, spy_on_sleep) scan_rate = s.seconds_per_message s.scan() self.assertEqual(scan_rate, 1 / 120 ) self.assertEqual(spy_log._history[-1], f'slept for {scan_rate} second(s)')
def test_connection_logs_its_pongs(self): ping = 'PING :tmi.twitch.tv\r\n' pong_log = Log.connect_pong connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': 'nick_BOTtom', 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "t_tv" } dont_sleep = Time(dont_print).sleep spy_log = Spy_Log() mock_socket = socket.socket(dont_print, ping) s = Subject(connect_to, mock_socket, spy_log.log, dont_sleep) s.scan() self.assertEqual(spy_log._history[-1], pong_log)
def test_go_command_logs_if_another_game_is_in_progress(self): spy = Spy_Log() mock_player_scores = Player_Scores() s = Go("mocks/config.txt", "mocks/triviaset.csv", Questions_Asked(), mock_player_scores, spy.log) mock_connection = Connection() user = "******" command = "!go" message = (user, command) with ThreadPoolExecutor(max_workers=2) as e: e.submit(s.run_the_next_trivia_round, mock_connection, message) e.submit(s.run_the_next_trivia_round, mock_connection, message) error_message_count = 0 for message in spy._history: if message == Log.in_progress(user, command): error_message_count += 1 self.assertEqual(error_message_count, 1)
def test_connection_sends_twitch_a_greeting_on_connection(self): my_id = b':nick_BOTtom!nick_BOTtom@nick_BOTtom.tmi.twitch.tv ' address = b'PRIVMSG #home_shopping_network :' message = Chat.good_morning.encode('utf-8') crlf = b'\r\n' expected_message = my_id + address + message + crlf connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': 'nick_BOTtom', 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "home_shopping_network" } spy_log = Spy_Log() dont_sleep = Time(dont_print).sleep Subject(connect_to, socket.socket(spy_log.log), dont_print, dont_sleep) self.assertEqual(spy_log._history[-2], expected_message)
def test_connection_successfully_connects_to_twitch(self): what_blocking_should_be_set_to = 0 connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': 'crash_test_dummy_bot', 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "home_shopping_network" } spy_log = Spy_Log() dont_sleep = Time(dont_print).sleep Subject(connect_to, socket.socket(spy_log.log), dont_print, dont_sleep) self.assertEqual(spy_log._history[0], ('some_twitch_url', 1701)) self.assertEqual(spy_log._history[1], b'PASS oauth:1337_P@SSw0rd123\r\n') self.assertEqual(spy_log._history[2], b'NICK crash_test_dummy_bot\r\n') self.assertEqual(spy_log._history[3], b'JOIN #home_shopping_network\r\n') self.assertEqual(spy_log._history[-1], what_blocking_should_be_set_to)
def test_connection_sends_twitch_arbitrary_messages(self): my_id = b':nick_BOTtom!nick_BOTtom@nick_BOTtom.tmi.twitch.tv ' address = b'PRIVMSG #home_shopping_network :' body = "My cat's breath smells like cat food." crlf = b'\r\n' expected_message = my_id + address + body.encode('utf-8') + crlf connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': 'nick_BOTtom', 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "home_shopping_network" } spy_log = Spy_Log() dont_sleep = Time(dont_print).sleep s = Subject(connect_to, socket.socket(spy_log.log), dont_print, dont_sleep) s.send(body) self.assertEqual(spy_log._history[-1], expected_message)
def test_logs_a_series_of_feedback_reports_as_it_connects(self): connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': 'nick_BOTtom', 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "home_shopping_network" } spy_log = Spy_Log() dont_sleep = Time(dont_print).sleep Subject(connect_to, socket.socket(dont_print), spy_log.log, dont_sleep) self.assertEqual(spy_log._history[0], Log.connect_loading) self.assertEqual(spy_log._history[1], Log.connect_pass) self.assertEqual(spy_log._history[2], Log.connect_nick) self.assertEqual(spy_log._history[3], Log.connect_join) self.assertEqual(spy_log._history[4], Log.connect_hi) self.assertEqual(spy_log._history[5], Log.connect_complete) self.assertEqual(len(spy_log._history), 6)
def test_connection_logs_messages_recieved(self): user = "******" body = "Woot!" message = f':{user}!{user}@{user}.tmi.twitch.tv PRIVMSG #t_tv :{body}' expected_print = Log.connect_response(user, body) connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': 'nick_BOTtom', 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "t_tv" } dont_sleep = Time(dont_print).sleep spy_log = Spy_Log() mock_socket = socket.socket(dont_print, message) s = Subject(connect_to, mock_socket, spy_log.log, dont_sleep) s.scan() self.assertEqual(spy_log._history[-1], expected_print)