def build_sweetiebot(config=None): if config is None: import config resource = config.nickname + randomstr() if config.fake_redis: log.debug('faking out redis') redis_conn = FakeRedis() else: redis_conn = redis.from_url(config.redis_url) jid = config.username + '/' + resource nick = config.nickname room = config.chatroom password = config.password if config.hostname is not None: address = (config.hostname, config.port) else: address = () bot = MUCJabberBot(jid, password, room, nick, address) crest = SweetieCrest(config.crest_base_url, config.crest_client_id, config.crest_client_secret, config.crest_refresh_token) lookup = SweetieLookup(bot, crest) admin = SweetieAdmin(bot, config.chatroom) mq = SweetieMQ(config) de = SweetieDe(bot, admin, mq, ResponsesFile('data/deowl_fails.txt')) actions = ResponsesFile('data/actions.txt') sass = ResponsesFile('data/sass.txt') cadmusic = ResponsesFile('data/cadmusic.txt') tell = SweetieTell(bot, redis_conn) dictionary = SweetieDictionary(bot) chat = SweetieChat(bot, actions, sass, config.chatroom, cadmusic, tell, dictionary) roulette = SweetieRoulette(bot, admin) pings = SweetiePings(bot, redis_conn) moon = SweetieMoon(bot) if config.twitter_key is not None: twitter = TwitterClient.get_client(config.twitter_key, config.twitter_secret) watchers = list(map(twitter.get_timeline_watcher, ['EVE_Status'])) else: watchers = [] watchers.append( AtomWatcher.get_watcher( 'http://eveion.blogspot.com/feeds/posts/default')) seen = SweetieSeen(bot, redis_conn) sweet = Sweetiebot(config.nickname, bot, lookup, mq, admin, chat, roulette, de, pings, watchers, moon) return sweet
def setUp(self): self.bot = MagicMock() self.bot.get_jid_from_nick.side_effect = self.get_jid self.store = FakeRedis() self.tell = SweetieTell(self.bot, self.store)
class TellTests(unittest.TestCase): def get_jid(self, nick): if nick == 'myself': return '*****@*****.**' if nick == 'ZhuLi': return '*****@*****.**' if nick == 'Sweetiebot': return '*****@*****.**' if nick == 'sender': return '*****@*****.**' def setUp(self): self.bot = MagicMock() self.bot.get_jid_from_nick.side_effect = self.get_jid self.store = FakeRedis() self.tell = SweetieTell(self.bot, self.store) def test_can_tell_someone_a_thing(self): response = self.tell.tell(create_message('!tell ZhuLi Do the thing!')) self.assertEqual('Message received for [email protected]', response) response = self.tell.get_messages_for( create_message_zhuli('hello there')) self.assertEqual('ZhuLi, sender left you a message: Do the thing!', response) response = self.tell.get_messages_for( create_message_myself('anybody left me a message?')) self.assertIsNone(response) response = self.tell.get_messages_for( create_message_zhuli('another thing')) # zhuli shouldn't get pinged a second time self.assertIsNone(response) def test_can_tell_someone_two_things(self): r1 = self.tell.tell(create_message('!tell ZhuLi Do the thing!')) self.assertEqual('Message received for [email protected]', r1) r2 = self.tell.tell(create_message('!tell ZhuLi Do another thing!')) self.assertEqual( 'Message received for [email protected] (appended to previous message)', r2) r3 = self.tell.get_messages_for(create_message_zhuli('hello~~')) self.assertEqual( 'ZhuLi, sender left you a message: Do the thing!\nDo another thing!', r3) def test_denies_tell_in_pm(self): response = self.tell.tell( create_message('!tell ZhuLi Do another thing!', is_pm=True)) self.assertEqual('Sorry, you can\'t use !tell in a PM', response) def test_denies_tell_self(self): response = self.tell.tell( create_message_myself('!tell myself to do a thing')) self.assertEqual( 'Talking to yourself is more efficient in real life than on jabber', response) def test_denies_tell_bot(self): response = self.tell.tell( create_message_myself('!tell Sweetiebot to do a thing')) self.assertEqual('I\'m right here, you know', response) def test_denies_tell_to_unknown_person(self): response = self.tell.tell(create_message('!tell nobody to do a thing')) self.assertEqual('Sorry, I don\'t know who \'nobody\' is', response) def test_denies_tell_with_empty_message(self): response = self.tell.tell(create_message('!tell ZhuLi')) self.assertEqual('A message is required', response) def test_can_remember_nicks_not_in_chat(self): response = self.tell.tell( create_message('!tell AfkPerson to do a thing')) self.assertEqual('Sorry, I don\'t know who \'AfkPerson\' is', response) self.tell.nicktojid.on_presence( Presence('[email protected]/AfkPerson', '*****@*****.**', None, None)) response = self.tell.tell( create_message('!tell AfkPerson to do a thing')) self.assertEqual('Message received for [email protected]', response) def test_message_length_cannot_exceed_limit(self): response = self.tell.tell( create_message('!tell ZhuLi ' + ('do a thing' * 1000))) self.assertEqual('Sorry, that message is too long (1000 char maximum)', response) def test_combined_message_length_cannot_exceed_limit(self): r1 = self.tell.tell( create_message('!tell ZhuLi ' + ('do a thing' * 90))) r2 = self.tell.tell( create_message('!tell ZhuLi ' + ('do a thing' * 11))) # the count here is technically inaccurate because the message text contains 'sender left you a message:' self.assertEqual( 'Sorry, that message is too long (1000 char maximum; you\'ve already used ~927)', r2)
class TellTests(unittest.TestCase): def get_jid(self, nick): if nick == 'myself': return '*****@*****.**' if nick == 'ZhuLi': return '*****@*****.**' if nick == 'Sweetiebot': return '*****@*****.**' if nick == 'sender': return '*****@*****.**' def setUp(self): self.bot = MagicMock() self.bot.get_jid_from_nick.side_effect = self.get_jid self.store = FakeRedis() self.tell = SweetieTell(self.bot, self.store) def test_can_tell_someone_a_thing(self): response = self.tell.tell(create_message('!tell ZhuLi Do the thing!')) self.assertEqual('Message received for [email protected]', response) response = self.tell.get_messages_for(create_message_zhuli('hello there')) self.assertEqual('ZhuLi, sender left you a message: Do the thing!', response) response = self.tell.get_messages_for(create_message_myself('anybody left me a message?')) self.assertIsNone(response) response = self.tell.get_messages_for(create_message_zhuli('another thing')) # zhuli shouldn't get pinged a second time self.assertIsNone(response) def test_can_tell_someone_two_things(self): r1 = self.tell.tell(create_message('!tell ZhuLi Do the thing!')) self.assertEqual('Message received for [email protected]', r1) r2 = self.tell.tell(create_message('!tell ZhuLi Do another thing!')) self.assertEqual('Message received for [email protected] (appended to previous message)', r2) r3 = self.tell.get_messages_for(create_message_zhuli('hello~~')) self.assertEqual('ZhuLi, sender left you a message: Do the thing!\nDo another thing!', r3) def test_denies_tell_in_pm(self): response = self.tell.tell(create_message('!tell ZhuLi Do another thing!', is_pm=True)) self.assertEqual('Sorry, you can\'t use !tell in a PM', response) def test_denies_tell_self(self): response = self.tell.tell(create_message_myself('!tell myself to do a thing')) self.assertEqual('Talking to yourself is more efficient in real life than on jabber', response) def test_denies_tell_bot(self): response = self.tell.tell(create_message_myself('!tell Sweetiebot to do a thing')) self.assertEqual('I\'m right here, you know', response) def test_denies_tell_to_unknown_person(self): response = self.tell.tell(create_message('!tell nobody to do a thing')) self.assertEqual('Sorry, I don\'t know who \'nobody\' is', response) def test_denies_tell_with_empty_message(self): response = self.tell.tell(create_message('!tell ZhuLi')) self.assertEqual('A message is required', response) def test_can_remember_nicks_not_in_chat(self): response = self.tell.tell(create_message('!tell AfkPerson to do a thing')) self.assertEqual('Sorry, I don\'t know who \'AfkPerson\' is', response) self.tell.nicktojid.on_presence(Presence('[email protected]/AfkPerson', '*****@*****.**', None, None)) response = self.tell.tell(create_message('!tell AfkPerson to do a thing')) self.assertEqual('Message received for [email protected]', response) def test_message_length_cannot_exceed_limit(self): response = self.tell.tell(create_message('!tell ZhuLi ' + ('do a thing' * 1000))) self.assertEqual('Sorry, that message is too long (1000 char maximum)', response) def test_combined_message_length_cannot_exceed_limit(self): r1 = self.tell.tell(create_message('!tell ZhuLi ' + ('do a thing' * 90))) r2 = self.tell.tell(create_message('!tell ZhuLi ' + ('do a thing' * 11))) # the count here is technically inaccurate because the message text contains 'sender left you a message:' self.assertEqual('Sorry, that message is too long (1000 char maximum; you\'ve already used ~927)', r2)
class TellTests(unittest.TestCase): def get_jid(self, nick): if nick == 'myself': return '*****@*****.**' if nick == 'ZhuLi': return '*****@*****.**' if nick == 'Sweetiebot': return '*****@*****.**' if nick == 'sender': return '*****@*****.**' def setUp(self): self.bot = MagicMock() self.bot.get_jid_from_nick.side_effect = self.get_jid self.store = FakeRedis() self.tell = SweetieTell(self.bot, self.store) def test_can_tell_someone_a_thing(self): response = self.tell.tell(create_message('!tell ZhuLi Do the thing!')) self.assertEqual('Message received for [email protected]', response) response = self.tell.get_messages_for(create_message_zhuli('hello there')) self.assertEqual('ZhuLi, sender left you a message: Do the thing!', response) response = self.tell.get_messages_for(create_message_myself('anybody left me a message?')) self.assertIsNone(response) response = self.tell.get_messages_for(create_message_zhuli('another thing')) # zhuli shouldn't get pinged a second time self.assertIsNone(response) def test_denies_telling_someone_two_things(self): r1 = self.tell.tell(create_message('!tell ZhuLi Do the thing!')) r2 = self.tell.tell(create_message('!tell ZhuLi Do the thing!')) self.assertEqual('Sorry, you\'ve already left a message for ZhuLi', r2) def test_denies_tell_in_pm(self): response = self.tell.tell(create_message('!tell ZhuLi Do another thing!', is_pm=True)) self.assertEqual('Sorry, you can\'t use !tell in a PM', response) def test_denies_tell_self(self): response = self.tell.tell(create_message_myself('!tell myself to do a thing')) self.assertEqual('Talking to yourself is more efficient in real life than on jabber', response) def test_denies_tell_bot(self): response = self.tell.tell(create_message_myself('!tell Sweetiebot to do a thing')) self.assertEqual('I\'m right here, you know', response) def test_denies_tell_to_unknown_person(self): response = self.tell.tell(create_message('!tell nobody to do a thing')) self.assertEqual('Sorry, I don\'t know who \'nobody\' is', response) def test_can_remember_nicks_not_in_chat(self): response = self.tell.tell(create_message('!tell AfkPerson to do a thing')) self.assertEqual('Sorry, I don\'t know who \'AfkPerson\' is', response) self.tell.nicktojid.on_presence(Presence('[email protected]/AfkPerson', '*****@*****.**', None, None)) response = self.tell.tell(create_message('!tell AfkPerson to do a thing')) self.assertEqual('Message received for [email protected]', response)