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_connection_doesnt_keep_ignored_messages_sent_from_twitch(self): bot = "nick_BOTtom" user = "******" body = "Your shoes are made of peanut butter. Woot!" last_user_message = (user, body) 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}' 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 mock_socket = socket.socket(dont_print, message) s = Subject(connect_to, mock_socket, dont_print, dont_sleep) s.scan() self.assertEqual(mock_socket.message, message) self.assertEqual(s.last_response, last_user_message) mock_socket.message = bot_message s.scan() self.assertEqual(mock_socket.message, bot_message) self.assertEqual(s.last_response, last_user_message) self.assertNotEqual(s.last_response, (bot, body)) self.assertNotEqual(s.last_response, ('bot', 'No Messages Recieved'))
def test_commander_can_still_recieve_commands_while_a_long_running_process_runs( self): test_responses = self.chat_messages() expected_penultimate_message = test_responses[0] expected_last_command_message = test_responses[-1] long_command = expected_penultimate_message[1] short_command = expected_last_command_message[1] no_admins = [] dont_sleep = Time(dont_print).sleep mock_connection = Connection() spy = Spy_Command() no_validations = [] commands = [ (long_command, spy.long_run, no_validations), (short_command, spy.ping, no_validations), ] s = Subject(commands, no_admins, mock_connection, dont_print, dont_sleep) with ThreadPoolExecutor(max_workers=2) as e: e.submit(s.listen_for_commands) e.submit(self.chat_room, mock_connection) self.assertEqual(spy._history[-3], expected_penultimate_message) self.assertEqual(spy._history[-2], expected_last_command_message) self.assertEqual(spy._history[-1], (expected_penultimate_message[1], "completed")) self.assertEqual(len(spy._history), 3)
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_ignores_case_of_message_senders(self): command_string = "!green_group_stay_close_to_homing_sector_MG-7" admins = ["admIral_akbar"] test_response = ("admiral_Akbar", command_string) dont_sleep = Time(dont_print).sleep mock_connection = Connection() spy = Spy_Command() commands = [(command_string, spy.ping, ["admin_only"])] s = Subject(commands, admins, mock_connection, dont_print, 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(spy._history[-1], test_response)
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_connection_doesnt_report_pings(self): message = 'PING :tmi.twitch.tv\r\n' 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 mock_socket = socket.socket(dont_print, message) s = Subject(connect_to, mock_socket, dont_print, dont_sleep) s.scan() self.assertEqual(s.last_response, ('bot', 'No Messages Recieved'))
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_commander_does_not_call_command_when_a_non_admin_attempts_an_admin_only_command( self): command_string = "!arbitrary_admin_command" test_response = ("not_an_admin", command_string) no_admins = [] dont_sleep = Time(dont_print).sleep mock_connection = Connection() spy = Spy_Command() commands = [(command_string, spy.ping, ["admin_only"])] s = Subject(commands, no_admins, mock_connection, dont_print, dont_sleep) mock_connection.last_response = test_response s.check_connection_last_message() self.assertEqual(len(spy._history), 0)
def test_commander_cares_about_exact_command_strings(self): command_string = "An Exact String To Match" test_response = ("not_an_admin", "!anExact_string-toMATCH") no_admins = [] dont_sleep = Time(dont_print).sleep mock_connection = Connection() spy = Spy_Command() no_validations = [] commands = [(command_string, spy.ping, no_validations)] s = Subject(commands, no_admins, mock_connection, dont_print, 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_calls_command_when_a_non_admin_attempts_an_open_command( self): command_string = "!arbitrary_open_command" test_response = ("not_an_admin", command_string) no_admins = [] dont_sleep = Time(dont_print).sleep mock_connection = Connection() spy = Spy_Command() no_validations = [] commands = [(command_string, spy.ping, no_validations)] s = Subject(commands, no_admins, mock_connection, dont_print, 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(spy._history[-1], test_response)
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_ignores_messages_from_itself_sent_from_twitch(self): user = "******" body = "Your shoes are made of peanut butter. Woot!" message = f':{user}!{user}@{user}.tmi.twitch.tv PRIVMSG #t_tv :{body}' connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': user, 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "t_tv" } dont_sleep = Time(dont_print).sleep mock_socket = socket.socket(dont_print, message) s = Subject(connect_to, mock_socket, dont_print, dont_sleep) s.scan() self.assertEqual(s.last_response, ('bot', 'No Messages Recieved'))
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_connection_returns_poorly_formatted_messages_from_twitch(self): first_word = "Badabing" whole_bad_message = f"{first_word} BAD FORMAT: U shoes r peanut butter." connect_to = { 'irc_url':'some_twitch_url', 'irc_port': 1701, 'bot_name': 'nick_BOTtom', 'oauth_token': 'oauth:1337_P@SSw0rd123', 'channel': "home_shopping_network" } dont_sleep = Time(dont_print).sleep mock_socket = socket.socket(dont_print, whole_bad_message) s = Subject(connect_to, mock_socket, dont_print, dont_sleep) s.scan() self.assertEqual(s.last_response[0], first_word) self.assertEqual(s.last_response[1], whole_bad_message)
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_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_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)
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_commander_remembers_the_last_new_message_from_Connection(self): no_commands = [] no_admins = [] test_response1 = ("gandhi", "Be the change you want to see in the world.") test_response2 = ("the_beatles", "Love is all you need.") mock_connection = Connection() dont_sleep = Time(dont_print).sleep s = Subject(no_commands, no_admins, mock_connection, dont_print, dont_sleep) mock_connection.last_response = test_response1 s.check_connection_last_message() self.assertEqual(s.last_response, test_response1) mock_connection.last_response = test_response2 s.check_connection_last_message() self.assertEqual(s.last_response, test_response2)
def test_commander_can_recieve_commands_from_connection_asynchronously( self): test_responses = self.chat_messages() command_string = test_responses[3][1] no_admins = [] dont_sleep = Time(dont_print).sleep mock_connection = Connection() spy = Spy_Command() no_validations = [] commands = [(command_string, spy.ping, no_validations)] s = Subject(commands, no_admins, mock_connection, dont_print, dont_sleep) with ThreadPoolExecutor(max_workers=2) as e: e.submit(s.listen_for_commands) e.submit(self.chat_room, mock_connection) self.assertEqual(spy._history[-1], test_responses[3]) self.assertEqual(s.last_response, test_responses[-1])