diff --git a/plugins/MoobotFactoids.py b/plugins/MoobotFactoids.py index 84d2d1c06..d27abbefb 100644 --- a/plugins/MoobotFactoids.py +++ b/plugins/MoobotFactoids.py @@ -459,12 +459,14 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp): s += " Locked by %s on %s." % (lock_by, lock_at) irc.reply(s) - def _lock(self, irc, msg, args, lock=True): + def _lock(self, irc, msg, args, locking=True): + self.log.debug('in _lock') try: id = ircdb.users.getUserId(msg.prefix) except KeyError: irc.errorNotRegistered() return + self.log.debug('id: %s' % id) key = privmsgs.getArgs(args, required=1) db = self.dbHandler.getDb() cursor = db.cursor() @@ -475,24 +477,25 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp): return (created_by, locked_by) = cursor.fetchone() # Don't perform redundant operations - if lock: - if locked_by is not None: + if locking and locked_by is not None: irc.error('Factoid "%s" is already locked.' % key) return - else: - if locked_by is None: + if not locking and locked_by is None: irc.error('Factoid "%s" is not locked.' % key) return # Can only lock/unlock own factoids unless you're an admin + self.log.debug('admin?: %s' % ircdb.checkCapability(id, 'admin')) + self.log.debug('created_by: %s' % created_by) if not (ircdb.checkCapability(id, 'admin') or created_by == id): - s = "unlock" - if lock: - s = "lock" + if locking: + s = "lock" + else: + s = "unlock" irc.error("Cannot %s someone else's factoid unless you " "are an admin." % s) return # Okay, we're done, ready to lock/unlock - if lock: + if locking: locked_at = int(time.time()) else: locked_at = None diff --git a/test/test_MoobotFactoids.py b/test/test_MoobotFactoids.py index a5218ae9b..6a8f82424 100644 --- a/test/test_MoobotFactoids.py +++ b/test/test_MoobotFactoids.py @@ -164,24 +164,30 @@ if sqlite is not None: '(?!(request|modif)).*?\.$') def testLockUnlock(self): - self.assertNotError('moo is moo') - self.assertNotError('lock moo') - self.assertRegexp('factinfo moo', - '^moo: Created by tester on' - '.*?\. Locked by tester on .*?\.') - # switch user - self.prefix = 'moo!moo@moo' - self.assertNotError('register nottester moo') - self.assertError('unlock moo') - self.assertRegexp('factinfo moo', - '^moo: Created by tester on' - '.*?\. Locked by tester on .*?\.') - # switch back - self.prefix = 'foo!bar@baz' - self.assertNotError('identify tester moo') - self.assertNotError('unlock moo') - self.assertRegexp('factinfo moo', - '^moo: Created by tester on.*?\.') + # disable world.testing since we want new users to not + # magically be endowed with the admin capability + try: + world.testing = False + self.assertNotError('moo is moo') + self.assertNotError('lock moo') + self.assertRegexp('factinfo moo', + '^moo: Created by tester on' + '.*?\. Locked by tester on .*?\.') + # switch user + self.prefix = 'moo!moo@moo' + self.assertNotError('register nottester moo') + self.assertError('unlock moo') + self.assertRegexp('factinfo moo', + '^moo: Created by tester on' + '.*?\. Locked by tester on .*?\.') + # switch back + self.prefix = 'foo!bar@baz' + self.assertNotError('identify tester moo') + self.assertNotError('unlock moo') + self.assertRegexp('factinfo moo', + '^moo: Created by tester on.*?\.') + finally: + world.testing = True def testChangeFactoid(self): self.assertNotError('moo is moo') @@ -240,7 +246,10 @@ if sqlite is not None: def testListkeys(self): self.assertResponse('listkeys %', 'No keys matching "%" found.') self.assertNotError('moo is moo') - # If only one key, it should respond with the factoid + # With this set, if only one key matches, it should respond with + # the factoid + MFconf = conf.supybot.plugins.MoobotFactoids # looooong! + MFconf.showFactoidIfOnlyOneMatch.setValue(True) self.assertResponse('listkeys moo', 'moo') self.assertResponse('listkeys foo', 'No keys matching "foo" ' 'found.')