2014-09-13 19:24:08 +02:00
# Yay tests!
import csv
import unittest
2014-09-13 21:18:42 +02:00
import bot as botcode
2014-09-13 19:24:08 +02:00
import time
import pdb
2015-03-15 15:20:17 +01:00
# To configure bot, please make changes in bot_settings.py
import bot_settings as settings
2014-09-13 19:24:08 +02:00
#########################
2015-03-15 15:20:17 +01:00
### FAKE IRCSOCK ###
2014-09-13 19:24:08 +02:00
#########################
class fake_ircsock ( object ) :
def __init__ ( self ) :
self . sent_messages = [ ]
2015-03-15 15:20:17 +01:00
2014-09-13 19:24:08 +02:00
def send ( self , msg ) :
self . sent_messages . append ( msg )
def sent_message ( self ) :
return self . sent_messages [ - 1 ]
2015-03-15 15:20:17 +01:00
2014-09-13 19:24:08 +02:00
def has_sent_message ( self ) :
if self . sent_messages :
return True
else :
return False
def fake_irc_start ( ) :
ircsock = fake_ircsock ( )
2015-03-15 15:20:17 +01:00
return ircsock
2014-09-13 19:24:08 +02:00
class TestBotClass ( unittest . TestCase ) :
def setUp ( self ) :
2014-09-13 21:18:42 +02:00
self . bot = botcode . Bot ( )
2014-09-13 19:24:08 +02:00
def test_csv_source ( self ) :
self . assertEqual ( self . bot . nick_source , ' nicks.csv ' )
def test_known_nicks_setup ( self ) :
2015-03-15 15:20:17 +01:00
bot = botcode . Bot ( nick_source = ' test_nicks.csv ' )
2014-09-13 19:24:08 +02:00
self . assertEqual ( bot . known_nicks , [ [ ' Alice ' ] , [ ' Bob ' ] ] )
def test_wait_time ( self ) :
2015-03-15 15:20:17 +01:00
self . assertEqual ( self . bot . wait_time , settings . wait_time )
2014-09-13 19:24:08 +02:00
def test_custom_wait_time ( self ) :
2014-09-13 21:18:42 +02:00
bot = botcode . Bot ( wait_time = 30 )
2014-09-13 19:24:08 +02:00
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 ) :
2015-03-15 15:20:17 +01:00
bot = botcode . Bot ( nick_source = ' test_nicks.csv ' )
2014-09-13 19:24:08 +02:00
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 \n Bob \n ' )
class TestNewComerClass ( unittest . TestCase ) :
def setUp ( self ) :
2014-09-13 21:18:42 +02:00
self . bot = botcode . Bot ( ' test_nicks.csv ' )
self . NewComer = botcode . NewComer ( ' Nancy ' , self . bot )
2014-09-13 19:24:08 +02:00
def test_newcomer_init_nick ( self ) :
self . assertEqual ( self . NewComer . nick , ' Nancy ' )
def test_newcomer_init_born ( self ) :
2014-09-13 21:18:42 +02:00
newComer = botcode . NewComer ( ' Baby ' , botcode . Bot ( ) )
2014-09-13 19:24:08 +02:00
time . sleep ( 0.01 )
self . assertAlmostEqual ( newComer . born , time . time ( ) - .01 , places = 2 )
def test_newcomer_around_for ( self ) :
2014-09-13 21:18:42 +02:00
newComer = botcode . NewComer ( ' Shauna ' , botcode . Bot ( ) )
2014-09-13 19:24:08 +02:00
time . sleep ( 0.01 )
self . assertAlmostEqual ( newComer . around_for ( ) , .01 , places = 2 )
class TestJoinIRC ( unittest . TestCase ) :
def setUp ( self ) :
self . ircsock = fake_irc_start ( )
2014-11-22 00:03:27 +01:00
self . bot = botcode . Bot ( )
2015-03-15 15:20:17 +01:00
2014-09-13 19:24:08 +02:00
def test_sent_messages ( self ) :
2015-03-15 15:20:17 +01:00
botcode . join_irc ( self . ircsock , settings . botnick , settings . channel )
expected = [ " USER {} {} {} :This is http://openhatch.org/ ' s greeter bot. \n " . format ( self . bot . botnick , self . bot . botnick , self . bot . botnick ) , ' NICK {} \n ' . format ( self . bot . botnick ) , ' JOIN {} \n ' . format ( settings . channel ) ]
2014-09-13 19:24:08 +02:00
self . assertEqual ( self . ircsock . sent_messages , expected )
class TestProcessNewcomers ( unittest . TestCase ) :
def setUp ( self ) :
2015-03-15 15:20:17 +01:00
self . bot = botcode . Bot ( nick_source = ' test_nicks.csv ' , wait_time = .1 )
2014-09-13 21:18:42 +02:00
botcode . NewComer ( ' Harry ' , self . bot )
botcode . NewComer ( ' Hermione ' , self . bot )
2014-09-13 19:24:08 +02:00
time . sleep ( .15 )
2014-09-13 21:18:42 +02:00
botcode . NewComer ( ' Ron ' , self . bot )
2014-09-13 19:24:08 +02:00
self . ircsock = fake_irc_start ( )
def test_check_new_newcomers ( self ) :
2015-03-15 15:20:17 +01:00
botcode . process_newcomers ( self . bot , [ i for i in self . bot . newcomers if i . around_for ( ) > self . bot . wait_time ] , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters , welcome = 0 )
2014-09-13 19:24:08 +02:00
self . assertEqual ( len ( self . bot . newcomers ) , 1 )
def test_check_new_known_nicks ( self ) :
2015-03-15 15:20:17 +01:00
botcode . process_newcomers ( self . bot , [ i for i in self . bot . newcomers if i . around_for ( ) > self . bot . wait_time ] , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters , welcome = 0 )
2014-09-13 19:24:08 +02:00
self . assertEqual ( self . bot . known_nicks , [ [ ' Alice ' ] , [ ' Bob ' ] , [ ' Harry ' ] , [ ' Hermione ' ] ] )
2015-03-15 15:20:17 +01:00
2014-09-13 19:24:08 +02:00
def test_welcome_nick ( self ) :
2015-03-15 15:20:17 +01:00
botcode . process_newcomers ( bot = self . bot , newcomerlist = [ i for i in self . bot . newcomers if i . around_for ( ) > self . bot . wait_time ] , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters , welcome = 1 )
self . assertEqual ( self . ircsock . sent_message ( ) , " PRIVMSG {0} :Welcome Hermione! The channel is pretty quiet right now, so I thought I ' d say hello, and ping some people (like {1} ) 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 ( settings . channel , botcode . greeter_string ( settings . channel_greeters ) ) )
2014-09-13 19:24:08 +02:00
def tearDown ( self ) :
with open ( ' test_nicks.csv ' , ' w ' ) as csv_file :
csv_file . write ( ' Alice \n Bob \n ' )
class TestParseMessages ( unittest . TestCase ) :
def test_good_string ( self ) :
2014-09-13 21:18:42 +02:00
ircmsg , actor = botcode . parse_messages ( " :vader!darth@darkside.org PRIVMSG #deathstar : I find your lack of faith disturbing " )
2014-09-13 19:24:08 +02:00
self . assertEqual ( [ ircmsg , actor ] , [ ' :vader!darth@darkside.org PRIVMSG #deathstar : I find your lack of faith disturbing ' , ' vader ' ] )
2015-03-15 15:20:17 +01:00
2014-09-13 19:24:08 +02:00
def test_bad_string ( self ) :
2014-09-13 21:18:42 +02:00
ircmsg , actor = botcode . parse_messages ( " we should probably replace this with a bad string more likely to occur " )
2014-09-13 19:24:08 +02:00
self . assertEqual ( [ ircmsg , actor ] , [ None , None ] )
class TestMessageResponse ( unittest . TestCase ) :
def setUp ( self ) :
2014-09-13 21:18:42 +02:00
self . bot = botcode . Bot ( ' test_nicks.csv ' )
botcode . NewComer ( ' Chappe ' , self . bot )
2014-09-13 19:24:08 +02:00
self . ircsock = fake_irc_start ( )
def test_newcomer_speaking ( self ) :
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " ~q@r.m.us PRIVMSG {} :hah " . format ( settings . channel ) , " Chappe " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters ) # Standard message by newcomer
2014-09-13 19:24:08 +02:00
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 ) :
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " ~q@r.m.us PRIVMSG {} :hah " . format ( settings . channel ) , " Alice " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters ) # Standard message by oldtimer
2014-09-13 19:24:08 +02:00
nicklist = [ i . nick for i in self . bot . newcomers ] # Makes a list of newcomers nicks for easy asserting
self . assertEqual ( nicklist , [ ] )
2015-03-15 15:20:17 +01:00
2014-09-13 19:24:08 +02:00
def test_join ( self ) :
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " JOIN {} right now! " . format ( settings . channel ) , " Shauna " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters ) # Replace with actual ping message
2014-09-13 19:24:08 +02:00
self . assertEqual ( self . bot . newcomers [ 1 ] . nick , ' Shauna ' )
2015-03-15 15:20:17 +01:00
2014-09-13 19:24:08 +02:00
def test_part ( self ) :
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " JOIN {} right now! " . format ( settings . channel ) , " Shauna " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters ) # Replace with actual ping message
2014-09-13 19:24:08 +02:00
self . assertEqual ( len ( self . bot . newcomers ) , 2 )
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " PART {} " . format ( settings . channel ) , " Shauna " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters ) # Replace with actual ping message
self . assertEqual ( len ( self . bot . newcomers ) , 1 )
2014-09-13 19:24:08 +02:00
def test_hello ( self ) :
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " PRIVMSG sup {} " . format ( self . bot . botnick ) , " Shauna " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters )
2014-09-13 19:24:08 +02:00
self . assertTrue ( self . ircsock . has_sent_message ( ) )
2015-03-15 15:20:17 +01:00
self . assertIn ( self . ircsock . sent_message ( ) , [ " PRIVMSG {} :hello Shauna \n " . format ( settings . channel ) , " PRIVMSG {} :hi Shauna \n " . format ( settings . channel ) , " PRIVMSG {} :hey Shauna \n " . format ( settings . channel ) , " PRIVMSG {} :yo Shauna \n " . format ( settings . channel ) , " PRIVMSG {} :sup Shauna \n " . format ( settings . channel ) ] )
2014-09-13 19:24:08 +02:00
def test_help ( self ) :
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " PRIVMSG info {} " . format ( self . bot . botnick ) , " Shauna " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters )
2014-09-13 19:24:08 +02:00
self . assertTrue ( self . ircsock . has_sent_message ( ) )
2015-03-15 15:20:17 +01:00
self . assertEqual ( self . ircsock . sent_message ( ) , " PRIVMSG {} :I ' m a bot! I ' m from here <https://github.com/shaunagm/oh-irc-bot>. You can change my behavior by submitting a pull request or by talking to shauna. \n " . format ( settings . channel ) )
2014-09-13 19:24:08 +02:00
def test_wait_time_from_admin ( self ) :
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " {} --wait-time 40 " . format ( self . bot . botnick ) , " shauna " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters ) # Channel-greeters may also be changed. :(
self . assertEqual ( self . ircsock . sent_message ( ) , " PRIVMSG {} :shauna the wait time is changing to 40 seconds. \n " . format ( settings . channel ) )
def test_wait_time_from_non_admin ( self ) :
botcode . message_response ( self . bot , " {} --wait-time 40 " . format ( self . bot . botnick ) , " Impostor " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters ) # Channel-greeters may also be changed. :(
self . assertEqual ( self . ircsock . sent_message ( ) , " PRIVMSG {0} :Impostor you are not authorized to make that change. Please contact one of the channel greeters, like {1} , for assistance. \n " . format ( settings . channel , botcode . greeter_string ( settings . channel_greeters ) ) )
2014-09-13 19:24:08 +02:00
def test_pong ( self ) :
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " PING : " , " Shauna " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters ) # Replace this with actual ping message
2014-09-13 19:24:08 +02:00
self . assertEqual ( self . ircsock . sent_message ( ) , " PONG :pingis \n " )
2015-03-15 15:20:17 +01:00
2014-09-13 19:24:08 +02:00
def test_bad_pong ( self ) :
2015-03-15 15:20:17 +01:00
botcode . message_response ( self . bot , " PING!!! : " , " Shauna " , ircsock = self . ircsock , channel = settings . channel , greeters = settings . channel_greeters ) # Replace this with actual ping message
self . assertFalse ( self . ircsock . has_sent_message ( ) )
2014-09-13 19:24:08 +02:00
def tearDown ( self ) :
with open ( ' test_nicks.csv ' , ' w ' ) as csv_file :
csv_file . write ( ' Alice \n Bob \n ' )
2015-03-15 15:20:17 +01:00
class TestGreeterString ( unittest . TestCase ) :
2014-09-13 19:24:08 +02:00
def setUp ( self ) :
2014-09-13 21:18:42 +02:00
self . bot = botcode . Bot ( ' test_nicks.csv ' )
2015-03-15 15:20:17 +01:00
def test_one_greeter ( self ) :
2014-11-22 00:03:27 +01:00
greeterstring = botcode . greeter_string ( [ ' shauna ' ] )
2014-09-13 19:24:08 +02:00
self . assertEqual ( greeterstring , " shauna " )
2015-03-15 15:20:17 +01:00
def test_two_greeters ( self ) :
2014-11-22 00:03:27 +01:00
greeters = botcode . greeter_string ( [ ' shauna ' , ' sauna ' ] )
2014-09-13 19:24:08 +02:00
self . assertEqual ( greeters , " shauna and sauna " )
2015-03-15 15:20:17 +01:00
def test_three_greeters ( self ) :
2014-11-22 00:03:27 +01:00
greeters = botcode . greeter_string ( [ ' shauna ' , ' sauna ' , ' megafauna ' ] )
2014-09-13 19:24:08 +02:00
self . assertEqual ( greeters , " shauna, sauna, and megafauna " )
# Runs all the unit-tests
if __name__ == ' __main__ ' :
unittest . main ( )