From 3b545ee35f1928833b3ed82722668e64eac954f5 Mon Sep 17 00:00:00 2001 From: Shauna Date: Sat, 13 Sep 2014 13:24:08 -0400 Subject: [PATCH] Replace old code with new, add tests --- bot.py | 387 +++++++++++++++++++++++++--------------------------- test_bot.py | 226 ++++++++++++++++++++++++++++++ 2 files changed, 409 insertions(+), 204 deletions(-) create mode 100644 test_bot.py diff --git a/bot.py b/bot.py index 95b799a..ea347f5 100644 --- a/bot.py +++ b/bot.py @@ -1,116 +1,84 @@ -######################################################################### -################# Abundant notes are appreciated here! ################## -######################################################################### +# Welcome to WelcomeBot. Find source, documentation, etc here: https://github.com/shaunagm/WelcomeBot/ Licensed https://creativecommons.org/licenses/by-sa/2.0/ # Import some necessary libraries. -import socket -import time -import csv -import Queue -import random -import re -from sys import exc_info +import socket, sys, time, csv, Queue, random, re, pdb, select from threading import Thread - # Some basic variables used to configure the bot. server = "irc.freenode.net" -channel = "#openhatch" -botnick = 'WelcomeBot' -channel_greeters = ['shauna', 'paulproteus', 'marktraceur'] -wait_time = 60 # amount of time after joining before bot replies to someone -change_wait = botnick + " --wait-time " -hello_list = [r'hello', r'hi', r'hey', r'yo', r'sup'] +channel = "#openhatch-bots" +botnick = "WelcomeBot2" +channel_greeters = ['shauna'] #'paulproteus', 'marktraceur'] +hello_list = [r'hello', r'hi', r'hey', r'yo', r'sup'] help_list = [r'help', r'info', r'faq', r'explain yourself'] -#################### Classes #################### -# Newcomer class that is created when someone joins the room. +######################### +### Class Definitions ### +######################### + +# Defines a bot +class Bot(object): + + def __init__(self, nick_source='nicks.csv', wait_time=60): + self.nick_source = nick_source + self.wait_time = wait_time + self.known_nicks = [] + with open(self.nick_source, 'rb') as csv_file: + csv_file_data = csv.reader(csv_file, delimiter=',', quotechar='|') + for row in csv_file_data: + self.known_nicks.append(row) + self.newcomers = [] + self.hello_regex = re.compile(get_regex(hello_list), re.I) # Regexed version of hello list + self.help_regex = re.compile(get_regex(help_list), re.I) # Regexed version of help list + + # Adds the current newcomer's nick to nicks.csv and known_nicks. + def add_known_nick(self,new_known_nick): + new_known_nick = new_known_nick.replace("_", "") + self.known_nicks.append([new_known_nick]) + with open(self.nick_source, 'a') as csvfile: + nickwriter = csv.writer(csvfile, delimiter=',', quotechar='|', + quoting=csv.QUOTE_MINIMAL) + nickwriter.writerow([new_known_nick]) + +# Defines a newcomer object class NewComer(object): - def __init__(self, nick): - self.nick = nick - self.born = time.time() - self.status = 0 + def __init__(self, nick, bot): + self.nick = nick + self.born = time.time() + bot.newcomers.append(self) - def update_status(self): - self.status = 1 - - def around_for(self): - return int(time.time() - self.born) + def around_for(self): + return time.time() - self.born -#################### Functions! #################### -# Joins specified channel. -def join_channel(chan): - ircsock.send("JOIN {} \n".format(chan)) +######################### +### Startup Functions ### +######################### +# Creates a socket that will be used to send and receive messages, +# then connects the socket to an IRC server and joins the channel. +def irc_start(): # pragma: no cover (this excludes this function from testing) + ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + ircsock.connect((server, 6667)) # Here we connect to server using port 6667. + return ircsock + +def join_irc(ircsock): + ircsock.send("USER {0} {0} {0} :This is http://openhatch.org/'s greeter bot" + ".\n".format(botnick)) # bot authentication + ircsock.send("NICK {}\n".format(botnick)) # Assign the nick to the bot. + ircsock.send("JOIN {} \n".format(channel)) # Joins channel # Reads the messages from the server and adds them to the Queue and prints # them to the console. This function will be run in a thread, see below. -def msg_handler(): - while True: - new_msg = ircsock.recv(2048) # receive data from the server - new_msg = new_msg.strip('\n\r') # removing any unnecessary linebreaks - q.put(new_msg) # put in queue for main loop to read - print(new_msg) +def msg_handler(ircsock): # pragma: no cover (this excludes this function from testing) + new_msg = ircsock.recv(2048) # receive data from the server + new_msg = new_msg.strip('\n\r') # removing any unnecessary linebreaks + print(new_msg) #### Potentially make this a log instead? + return new_msg - -# Responds to server Pings. -def pong(): - ircsock.send("PONG :pingis\n") - - -# This function responds to a user that inputs "Hello Mybot". -def bot_hello(greeting): - ircsock.send("PRIVMSG {0} :{1} {2}\n".format(channel, greeting, actor)) - - -# This function explains what the bot is when queried. -def bot_help(): - ircsock.send("PRIVMSG {} :I'm a bot! I'm from here . You can change my behavior by " - "submitting a pull request or by talking to shauna" - ".\n".format(channel)) - - -# Returns a grammatically correct string of the channel_greeters. -def greeter_string(conjunction): - greeters = "" - if len(channel_greeters) > 2: - for name in channel_greeters[:-1]: - greeters += "{}, ".format(name) - greeters += "{0} {1}".format(conjunction, channel_greeters[-1]) - elif len(channel_greeters) == 2: - greeters = "{0} {1} {2}".format(channel_greeters[0], conjunction, - channel_greeters[1]) - else: - greeters = channel_greeters[0] - return greeters - - -# This welcomes the "person" passed to it. -def welcome(newcomer): - ircsock.send("PRIVMSG {0} :Welcome {1}! The channel is pretty quiet " - "right now, so I thought I'd say hello, and ping some people " - "(like {2}) that you're here. If no one responds for a " - "while, try emailing us at hello@openhatch.org or just try " - "coming back later. FYI, you're now on my list of known " - "nicknames, so I won't bother you again." - "\n".format(channel, newcomer, greeter_string("and"))) - - -# Adds the current NewComer's nick to nicks.csv and known_nicks. -def add_known_nick(new_known_nick): - new_known_nick = new_known_nick.replace("_", "") - known_nicks.append([new_known_nick]) - with open('nicks.csv', 'a') as csvfile: - nickwriter = csv.writer(csvfile, delimiter=',', quotechar='|', - quoting=csv.QUOTE_MINIMAL) - nickwriter.writerow([new_known_nick]) - - -# Builds a regex that matches one of the options + (space) botnick. +# Called by bot on startup. Builds a regex that matches one of the options + (space) botnick. def get_regex(options): pattern = "(" for s in options: @@ -121,123 +89,134 @@ def get_regex(options): return pattern -# This function is used to change the wait time from the channel. -# It confirms that the attempt is allowed and then returns the requested value. -# If the attempt is not allowed, a message is sent to help -def wait_time_change(): +######################### +### General Functions ### +######################### + +# Welcomes the "person" passed to it. +def welcome_nick(newcomer, ircsock): + ircsock.send("PRIVMSG {0} :Welcome {1}! The channel is pretty quiet " + "right now, so I thought I'd say hello, and ping some people " + "(like {2}) that you're here. If no one responds for a " + "while, try emailing us at hello@openhatch.org or just try " + "coming back later. FYI, you're now on my list of known " + "nicknames, so I won't bother you again." + "\n".format(channel, newcomer, greeter_string("and", channel_greeters))) + +# Checks and manages the status of newcomers. +def process_newcomers(bot, newcomerlist, ircsock, welcome=1): + for person in newcomerlist: + if welcome == 1: + welcome_nick(person.nick, ircsock) + bot.add_known_nick(person.nick) + bot.newcomers.remove(person) + +# Checks for messages. +def parse_messages(ircmsg): + try: + actor = ircmsg.split(":")[1].split("!")[0] # and get the nick of the msg sender + return ircmsg, actor + except: + return None, None + +# Parses messages and responds to them appropriately. +def message_response(bot, ircmsg, actor, ircsock): + + # if someone other than a newcomer speaks into the channel + if ircmsg.find("PRIVMSG " + channel) != -1 and actor not in [i.nick for i in bot.newcomers]: + process_newcomers(bot,bot.newcomers, ircsock, welcome=0) # Process/check newcomers without welcoming them + + # if someone (other than the bot) joins the channel + if ircmsg.find("JOIN " + channel) != -1 and actor != botnick: + if actor.replace("_", "") not in bot.known_nicks and (i.nick for i in bot.newcomers): # And they're new + NewComer(actor, bot) + + # If someone parts or quits the #channel... + if ircmsg.find("PART " + channel) != -1 or ircmsg.find("QUIT") != -1: + for i in bot.newcomers: # and that person is on the newlist + if actor == i.nick: + bot.newcomers.remove(i) # remove them from the list + + # If someone talks to (or refers to) the bot. + if botnick.lower() and "PRIVMSG".lower() in ircmsg.lower(): + if bot.hello_regex.search(ircmsg): + bot_hello(random.choice(hello_list), actor, ircsock) + if bot.help_regex.search(ircmsg): + bot_help(ircsock) + + # If someone tries to change the wait time... + if ircmsg.find(botnick + " --wait-time ") != -1: + bot.wait_time = wait_time_change(actor, ircmsg, ircsock) # call this to check and change it + + # If the server pings us then we've got to respond! + if ircmsg.find("PING :") != -1: + pong(ircsock) + + +############################################################# +### Bot Response Functions (called by message_response()) ### +############################################################# + +# Responds to a user that inputs "Hello Mybot". +def bot_hello(greeting, actor, ircsock): + ircsock.send("PRIVMSG {0} :{1} {2}\n".format(channel, greeting, actor)) + +# Explains what the bot is when queried. +def bot_help(ircsock): + ircsock.send("PRIVMSG {} :I'm a bot! I'm from here . You can change my behavior by " + "submitting a pull request or by talking to shauna" + ".\n".format(channel)) + +# Returns a grammatically correct string of the channel_greeters. +def greeter_string(conjunction, greeters): + greeterstring = "" + if len(greeters) > 2: + for name in greeters[:-1]: + greeterstring += "{}, ".format(name) + greeterstring += "{0} {1}".format(conjunction, greeters[-1]) + elif len(greeters) == 2: + greeterstring = "{0} {1} {2}".format(greeters[0], conjunction, + greeters[1]) + else: + greeterstring = greeters[0] + return greeterstring + +# Changes the wait time from the channel. +def wait_time_change(actor, ircmsg, ircsock): for admin in channel_greeters: if actor == admin: - finder = re.search(r'\d\d*', re.search(r'--wait-time \d\d*', - ircmsg).group()) + finder = re.search(r'\d\d*', re.search(r'--wait-time \d\d*', ircmsg) + .group()) ircsock.send("PRIVMSG {0} :{1} the wait time is changing to {2} " "seconds.\n".format(channel, actor, finder.group())) return int(finder.group()) - ircsock.send("PRIVMSG {0} :{1} you are not authorized to make that change" - ". Please contact one of the channel greeters, like {2}, for " - "assistance.\n".format(channel, actor, greeter_string("or"))) + ircsock.send("PRIVMSG {0} :{1} you are not authorized to make that " + "change. Please contact one of the channel greeters, like {2}, for " + "assistance.\n".format(channel, actor, greeter_string("or", channel_greeters))) + +# Responds to server Pings. +def pong(ircsock): + ircsock.send("PONG :pingis\n") -#################### Startup #################### -# Creates a socket that will be used to send and receive messages, -# then connects the socket to an IRC server and joins the channel. -ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -ircsock.connect((server, 6667)) # Here we connect to server using port 6667. -ircsock.send("USER {0} {0} {0} :This is http://openhatch.org/'s greeter bot" - ".\n".format(botnick)) # bot authentication -ircsock.send("NICK {}\n".format(botnick)) # Assign the nick to the bot. -join_channel(channel) +########################## +### The main function. ### +########################## -# Creates a list of known nicks from nicks.csv. -# The list is checked to ensure that people are only greeted once. -# NewComers will be added to the list and the csv. -known_nicks = [] -with open('nicks.csv', 'rb') as csv_file: - csv_file_data = csv.reader(csv_file, delimiter=',', quotechar='|') - for row in csv_file_data: - known_nicks.append(row) +def main(): + ircsock = irc_start() + join_irc(ircsock) + WelcomeBot = Bot() + while 1: # Loop forever + ready_to_read, b, c = select.select([ircsock],[],[], 1) # ignore b&c, doesn't allow keywords + if ready_to_read: + ircmsg = msg_handler(ircsock) + process_newcomers(WelcomeBot, [i for i in WelcomeBot.newcomers if i.around_for() > WelcomeBot.wait_time],ircsock) + ircmsg, actor = parse_messages(ircmsg) # parse the next msg in the queue + if ircmsg is not None: # If we were able to parse it + message_response(WelcomeBot, ircmsg, actor, ircsock) # Respond to the parsed message -# Creates a Queue that will hold the incoming messages. -q = Queue.Queue() -# Creates a separate thread that will listen on the server and add any -# messages to the Queue. The thread is created to run target which equals -# the msg_handler function from above. -t = Thread(target=msg_handler) -t.daemon = True -t.start() +if __name__ == "__main__": # This line tells the interpreter to only execute main() if the program is being run, not imported. + sys.exit(main()) -# This is the array of NewComer objects that people who join are added to. -newcomers = [] - -# Create a couple of regular expressions to use in the main loop -# Basically, it creates a RE that matches something from the list + botnick -hello_RE = re.compile(get_regex(hello_list), re.I) -help_RE = re.compile(get_regex(help_list), re.I) - - -#################### The Workhorse #################### -# This is the main loop that monitors the channel and sends and receives the -# messages, either in the below code or by calling a function. This is the -# brain of this code. -while 1: # loop forever - - for i in newcomers: - if i.status == 0 and i.around_for() > wait_time: - welcome(i.nick) - i.update_status() - add_known_nick(i.nick) - newcomers.remove(i) - - # If the queue is not empty... - if q.empty() == 0: - # get the next msg in the queue - ircmsg = q.get() - # and get the nick of the msg sender - try: - actor = ircmsg.split(":")[1].split("!")[0] - except IndexError: - err_log = open('./error.log', 'a') - err_log.write('\nError occurred: {0}.\nircmsg was: {1}.\nSystem ' - 'error was: {2}' - '\n'.format(time.strftime('%x %X %Z'), - ircmsg, exc_info()[0])) - err_log.close() - - ##### Welcome functions ##### - # If someone has spoken into the channel... - if ircmsg.find("PRIVMSG " + channel) != -1: - for i in newcomers: - if actor != i.nick: # and is not the new NewComer - i.update_status() # set the status to 1 - add_known_nick(i.nick) - newcomers.remove(i) - ## Else: Do we want to do something extra if the person who - # joined the chat says something with no response? - - # If someone joins #channel... - if ircmsg.find("JOIN " + channel) != -1: - if actor != botnick: # and it is not the bot - if [actor.replace("_", "")] not in known_nicks: - if actor not in (i.nick for i in newcomers): - newcomers.append(NewComer(actor)) - - # If someone parts or quits the #channel... - if ircmsg.find("PART " + channel) != -1 or ircmsg.find("QUIT") != -1: - for i in newcomers: # and that person is on the newlist - if actor == i.nick: - newcomers.remove(i) # remove them from the list - - ##### Unwelcome functions ##### - # If someone talks to (or refers to) the bot. - if botnick.lower() and "PRIVMSG".lower() in ircmsg.lower(): - if hello_RE.search(ircmsg): - bot_hello(random.choice(hello_list)) - if help_RE.search(ircmsg): - bot_help() - - # If someone tries to change the wait time... - if ircmsg.find(change_wait) != -1: - wait_time = wait_time_change() # call this to check and change it - - # If the server pings us then we've got to respond! - if ircmsg.find("PING :") != -1: - pong() diff --git a/test_bot.py b/test_bot.py new file mode 100644 index 0000000..32f2cb1 --- /dev/null +++ b/test_bot.py @@ -0,0 +1,226 @@ +# Yay tests! + +import csv +import unittest +import newbot +import time +import pdb + +######################### +### FAKE IRCSOCK ### +######################### + +class fake_ircsock(object): + + def __init__(self): + self.sent_messages = [] + + def send(self, msg): + self.sent_messages.append(msg) + + def sent_message(self): + return self.sent_messages[-1] + + def has_sent_message(self): + if self.sent_messages: + return True + else: + return False + +def fake_irc_start(): + ircsock = fake_ircsock() + return ircsock + + +class TestBotClass(unittest.TestCase): + + def setUp(self): + self.bot = newbot.Bot() + + def test_csv_source(self): + self.assertEqual(self.bot.nick_source, 'nicks.csv') + + def test_known_nicks_setup(self): + bot = newbot.Bot('test_nicks.csv') + self.assertEqual(bot.known_nicks, [['Alice'], ['Bob']]) + + def test_wait_time(self): + self.assertEqual(self.bot.wait_time, 60) + + def test_custom_wait_time(self): + bot = newbot.Bot(wait_time=30) + self.assertEqual(bot.wait_time, 30) + + def test_newcomers_setup(self): + self.assertEqual(self.bot.newcomers, []) + + def test_add_nick_to_list(self): + self.bot.known_nicks = [['Fluffy'], ['Spot']] + self.bot.add_known_nick('Roger') + self.assertEqual(self.bot.known_nicks,[['Fluffy'], ['Spot'], ['Roger']]) + + def test_add_nick_underscore_removal(self): + self.bot.known_nicks = [['Fluffy'], ['Spot']] + self.bot.add_known_nick('Roger__') + self.assertEqual(self.bot.known_nicks,[['Fluffy'], ['Spot'], ['Roger']]) + + def test_add_nick_to_csv(self): + bot = newbot.Bot('test_nicks.csv') + bot.add_known_nick('Roger__') + with open('test_nicks.csv', 'rb') as csv_file: + known_nicks = [] + csv_file_data = csv.reader(csv_file, delimiter=',', quotechar='|') + for row in csv_file_data: + known_nicks.append(row) + self.assertEqual(known_nicks, [['Alice'], ['Bob'], ['Roger']]) + + def tearDown(self): + with open('test_nicks.csv', 'w') as csv_file: + csv_file.write('Alice\nBob\n') + +class TestNewComerClass(unittest.TestCase): + + def setUp(self): + self.bot = newbot.Bot('test_nicks.csv') + self.NewComer = newbot.NewComer('Nancy', self.bot) + + def test_newcomer_init_nick(self): + self.assertEqual(self.NewComer.nick, 'Nancy') + + def test_newcomer_init_born(self): + newComer = newbot.NewComer('Baby', newbot.Bot()) + time.sleep(0.01) + self.assertAlmostEqual(newComer.born, time.time() - .01, places=2) + + def test_add_newcomer_to_bot(self): + pass + + def test_newcomer_around_for(self): + newComer = newbot.NewComer('Shauna', newbot.Bot()) + time.sleep(0.01) + self.assertAlmostEqual(newComer.around_for(), .01, places=2) + +class TestJoinIRC(unittest.TestCase): + + def setUp(self): + self.ircsock = fake_irc_start() + + def test_sent_messages(self): + newbot.join_irc(self.ircsock) + expected = ["USER WelcomeBot2 WelcomeBot2 WelcomeBot2 :This is http://openhatch.org/'s greeter bot.\n", 'NICK WelcomeBot2\n', 'JOIN #openhatch-bots \n'] + self.assertEqual(self.ircsock.sent_messages,expected) + +class TestProcessNewcomers(unittest.TestCase): + + def setUp(self): + self.bot = newbot.Bot('test_nicks.csv', wait_time=.1) + newbot.NewComer('Harry', self.bot) + newbot.NewComer('Hermione', self.bot) + time.sleep(.15) + newbot.NewComer('Ron', self.bot) + self.ircsock = fake_irc_start() + + def test_check_new_newcomers(self): + newbot.process_newcomers(self.bot, [i for i in self.bot.newcomers if i.around_for() > self.bot.wait_time], ircsock=self.ircsock, welcome=0) + self.assertEqual(len(self.bot.newcomers), 1) + + def test_check_new_known_nicks(self): + newbot.process_newcomers(self.bot, [i for i in self.bot.newcomers if i.around_for() > self.bot.wait_time], ircsock=self.ircsock, welcome=0) + self.assertEqual(self.bot.known_nicks,[['Alice'],['Bob'],['Harry'],['Hermione']]) + + def test_welcome_nick(self): + newbot.process_newcomers(bot=self.bot, newcomerlist=[i for i in self.bot.newcomers if i.around_for() > self.bot.wait_time], ircsock=self.ircsock, welcome=1) + self.assertEqual(self.ircsock.sent_message(), "PRIVMSG #openhatch-bots :Welcome Hermione! The channel is pretty quiet right now, so I though I'd say hello, and ping some people (like shauna) that you're here. If no one responds for a while, try emailing us at hello@openhatch.org or just try coming back later. FYI, you're now on my list of known nicknames, so I won't bother you again.\n") + + def tearDown(self): + with open('test_nicks.csv', 'w') as csv_file: + csv_file.write('Alice\nBob\n') + +class TestParseMessages(unittest.TestCase): + + def test_good_string(self): + ircmsg, actor = newbot.parse_messages(":vader!darth@darkside.org PRIVMSG #deathstar : I find your lack of faith disturbing") + self.assertEqual([ircmsg, actor], [':vader!darth@darkside.org PRIVMSG #deathstar : I find your lack of faith disturbing', 'vader']) + + def test_bad_string(self): + ircmsg, actor = newbot.parse_messages("we should probably replace this with a bad string more likely to occur") + self.assertEqual([ircmsg, actor], [None, None]) + +class TestMessageResponse(unittest.TestCase): + + def setUp(self): + self.bot = newbot.Bot('test_nicks.csv') + newbot.NewComer('Chappe', self.bot) + self.ircsock = fake_irc_start() + + def test_newcomer_speaking(self): + newbot.message_response(self.bot,"~q@r.m.us PRIVMSG #openhatch-bots :hah","Chappe", ircsock=self.ircsock) # Standard message by newcomer + nicklist = [i.nick for i in self.bot.newcomers] # Makes a list of newcomers nicks for easy asserting + self.assertEqual(nicklist, ['Chappe']) + + def test_oldtimer_speaking(self): + newbot.message_response(self.bot,"~q@r.m.us PRIVMSG #openhatch-bots :hah","Alice", ircsock=self.ircsock) # Standard message by oldtimer + nicklist = [i.nick for i in self.bot.newcomers] # Makes a list of newcomers nicks for easy asserting + self.assertEqual(nicklist, []) + + def test_join(self): + newbot.message_response(self.bot,"JOIN #openhatch-bots right now!","Shauna", ircsock=self.ircsock) # Replace with actual ping message ALSO argh the channel variable might mess things up if folks change it, which they very well might :/ (Also true for tests below.) I think maybe use the format.() style that is used for wait_change etc + self.assertEqual(self.bot.newcomers[1].nick,'Shauna') + + def test_part(self): + newbot.message_response(self.bot,"JOIN #openhatch-bots right now!","Shauna", ircsock=self.ircsock) # Replace with actual ping message ALSO argh the channel variable might mess things up if folks change it, which they very well might :/ (Also true for tests below.) + self.assertEqual(len(self.bot.newcomers), 2) + newbot.message_response(self.bot,"PART #openhatch-bots","Shauna", ircsock=self.ircsock) # Replace with actual ping message ALSO argh the channel variable might mess things up :/ + self.assertEqual(len(self.bot.newcomers), 1) + + def test_hello(self): + newbot.message_response(self.bot,"PRIVMSG sup WelcomeBot2","Shauna", ircsock=self.ircsock) # The botnick may also be changed. :( + self.assertTrue(self.ircsock.has_sent_message()) + self.assertIn(self.ircsock.sent_message(), ["PRIVMSG #openhatch-bots :hello Shauna\n", "PRIVMSG #openhatch-bots :hi Shauna\n", "PRIVMSG #openhatch-bots :hey Shauna\n", "PRIVMSG #openhatch-bots :yo Shauna\n", "PRIVMSG #openhatch-bots :sup Shauna\n"]) + + def test_help(self): + newbot.message_response(self.bot,"PRIVMSG info WelcomeBot2","Shauna", ircsock=self.ircsock) # The botnick may also be changed. :( + self.assertTrue(self.ircsock.has_sent_message()) + self.assertEqual(self.ircsock.sent_message(), "PRIVMSG #openhatch-bots :I'm a bot! I'm from here . You can change my behavior by submitting a pull request or by talking to shauna.\n") + + def test_wait_time_from_admin(self): + newbot.message_response(self.bot,"WelcomeBot2 --wait-time 40","shauna",ircsock=self.ircsock) # Channel-greeters may also be changed. :( + self.assertEqual(self.ircsock.sent_message(), "PRIVMSG #openhatch-bots :shauna the wait time is changing to 40 seconds.\n") + + def test_wait_time_from_non_admin(self): + newbot.message_response(self.bot,"WelcomeBot2 --wait-time 40","Impostor",ircsock=self.ircsock) # Channel-greeters may also be changed. :( + self.assertEqual(self.ircsock.sent_message(), "PRIVMSG #openhatch-bots :Impostor you are not authorized to make that change. Please contact one of the channel greeters, like shauna, for assistance.\n") + + def test_pong(self): + newbot.message_response(self.bot,"PING :","Shauna",ircsock=self.ircsock) # Replace this with actual ping message + self.assertEqual(self.ircsock.sent_message(),"PONG :pingis\n") + + def test_bad_pong(self): + newbot.message_response(self.bot,"PING!!! :","Shauna",ircsock=self.ircsock) # Replace this with actual ping message + self.assertFalse(self.ircsock.has_sent_message()) + + def tearDown(self): + with open('test_nicks.csv', 'w') as csv_file: + csv_file.write('Alice\nBob\n') + +class TestGreeterString(unittest.TestCase): + + def setUp(self): + self.bot = newbot.Bot('test_nicks.csv') + + def test_one_greeter(self): + greeterstring = newbot.greeter_string("and", ['shauna']) + self.assertEqual(greeterstring, "shauna") + + def test_two_greeters(self): + greeters = newbot.greeter_string("and", ['shauna','sauna']) + self.assertEqual(greeters, "shauna and sauna") + + def test_three_greeters(self): + greeters = newbot.greeter_string("and", ['shauna','sauna','megafauna']) + self.assertEqual(greeters, "shauna, sauna, and megafauna") + + +# Runs all the unit-tests +if __name__ == '__main__': + unittest.main()