refactoring and getting ready for production, added testcase

This commit is contained in:
Elián Hanisch 2010-04-03 01:44:57 -03:00
parent dda5154237
commit d1d0fc86ab
2 changed files with 40 additions and 22 deletions

View File

@ -243,8 +243,8 @@ class Bantracker(callbacks.Plugin):
self.get_nicks(irc)
self.pendingReviews = PersistentCache('bt.reviews.db')
self.pendingReviews.open()
# add scheduled event for check bans that need review
schedule.addPeriodicEvent(self.reviewBans, 60*10,
# add scheduled event for check bans that need review, check every hour
schedule.addPeriodicEvent(lambda : self.reviewBans(irc), 60*60,
name=self.name())
def get_nicks(self, irc):
@ -443,7 +443,7 @@ class Bantracker(callbacks.Plugin):
self.log.info('SENDING: %s %s' %(op, s))
irc.reply(s, to=op, private=True)
def reviewBans(self):
def reviewBans(self, irc=None):
self.log.debug('Checking for bans that need review ...')
now = time.mktime(time.gmtime())
lastreview = self.pendingReviews.time
@ -485,20 +485,20 @@ class Bantracker(callbacks.Plugin):
if not ban.id:
ban.id = self.get_banId(ban.mask, channel)
if nickMatch(op, forward):
msgs = []
if not irc:
continue
s = "Hi, please somebody review the ban '%s' set by %s on %s in"\
" %s, link: %s/bans.cgi?log=%s" %(ban.mask, op, ban.ascwhen, channel,
bansite, ban.id)
for chan in fchannels:
msgs.append(ircmsgs.notice(chan, s))
# FIXME forwards should be sent now, not later.
msg = ircmsgs.notice(chan, s)
irc.queueMsg(msg)
else:
s = "Hi, please review the ban '%s' that you set on %s in %s, link:"\
" %s/bans.cgi?log=%s" %(ban.mask, ban.ascwhen, channel, bansite, ban.id)
msgs = [ircmsgs.privmsg(op, s)]
if host not in self.pendingReviews:
self.pendingReviews[host] = []
for msg in msgs:
msg = ircmsgs.privmsg(op, s)
if host not in self.pendingReviews:
self.pendingReviews[host] = []
self.pendingReviews[host].append((op, msg))
elif banTime < reviewAfterTime:
# since we made sure bans are sorted by time, the bans left are more recent
@ -510,12 +510,9 @@ class Bantracker(callbacks.Plugin):
if host in self.pendingReviews:
op = msg.nick
for nick, msg in self.pendingReviews[host]:
if msg.command == 'NOTICE':
self.log.info('SENDING: %s' %msg)
irc.queueMsg(msg)
else:
m = ircmsgs.privmsg(op, msg.args[1])
irc.queueMsg(m)
if op != nick:
msg = ircmsgs.privmsg(op, msg.args[1])
irc.queueMsg(msg)
del self.pendingReviews[host]
def doLog(self, irc, channel, s):

View File

@ -90,7 +90,7 @@ class BantrackerTestCase(ChannelPluginTestCase):
self.irc.feedMsg(ban)
return ban
def testCommentRequest(self):
def testComment(self):
pluginConf.request.ignore.set('')
# test bans
self.feedBan('asd!*@*')
@ -115,21 +115,23 @@ class BantrackerTestCase(ChannelPluginTestCase):
self.assertEqual(str(msg).strip(),
"PRIVMSG op :Please comment on the removal of dude in #test, use: @comment 4"
" <comment>")
# test forwards
def testCommentForward(self):
pluginConf.request.ignore.set('')
pluginConf.request.forward.set('bot')
pluginConf.request.forward.channels.set('#channel')
self.feedBan('qwe!*@*')
msg = self.irc.takeMsg()
self.assertEqual(str(msg).strip(),
"PRIVMSG op :Please comment on the ban of qwe!*@* in #test, use: @comment 5"
"PRIVMSG op :Please comment on the ban of qwe!*@* in #test, use: @comment 1"
" <comment>")
self.feedBan('zxc!*@*', prefix='bot!user@host.com')
msg = self.irc.takeMsg()
self.assertEqual(str(msg).strip(),
"NOTICE #channel :Please somebody comment on the ban of zxc!*@* in #test done by bot,"
" use: @comment 6 <comment>")
" use: @comment 2 <comment>")
def testReviewRequest(self):
def testReview(self):
pluginConf.request.ignore.set('')
cb = self.getCallback()
self.feedBan('asd!*@*')
@ -177,6 +179,26 @@ class BantrackerTestCase(ChannelPluginTestCase):
"PRIVMSG op :Hi, please review the ban 'asd2!*@*' that you set on %s in #test, link: "\
"%s/bans.cgi?log=2" %(cb.bans['#test'][1].ascwhen, pluginConf.bansite()))
def testReviewForward(self):
pluginConf.request.ignore.set('')
pluginConf.request.forward.set('bot')
pluginConf.request.forward.channels.set('#channel')
cb = self.getCallback()
self.feedBan('asd!*@*', prefix='bot!user@host.net')
self.irc.takeMsg() # ignore comment request comment
pluginConf.request.review.setValue(1.0/86400) # one second
cb.reviewBans(self.irc)
self.assertFalse(cb.pendingReviews)
print 'waiting 2 secs..'
time.sleep(2)
cb.reviewBans(self.irc)
# since it's a forward, it was sent already
self.assertFalse(cb.pendingReviews)
msg = self.irc.takeMsg()
self.assertEqual(str(msg).strip(),
"NOTICE #channel :Hi, please somebody review the ban 'asd!*@*' set by bot on %s in #test, link: "\
"%s/bans.cgi?log=1" %(cb.bans['#test'][0].ascwhen, pluginConf.bansite()))
def testPersistentCache(self):
msg1 = ircmsgs.privmsg('nick', 'Hello World')
msg2 = ircmsgs.privmsg('nick', 'Hello World')
@ -220,4 +242,3 @@ class BantrackerTestCase(ChannelPluginTestCase):