Fixes to ChannelIdDatabasePlugin as pointed out by @ProgVal

- Quote: factorize tests
- Make supybot.databases.plugins channel specific
This commit is contained in:
James Lu 2015-06-28 11:51:58 -07:00
parent 1fe663ddb2
commit 8e51209c3f
3 changed files with 9 additions and 11 deletions

View File

@ -43,13 +43,9 @@ class QuoteTestCase(ChannelPluginTestCase):
def testUnauthenticatedAdd(self): def testUnauthenticatedAdd(self):
# This should fail because the user isn't registered # This should fail because the user isn't registered
self.assertError('quote add hello world!') self.assertError('quote add hello world!')
original_value = conf.supybot.databases.plugins.requireRegistration() with conf.supybot.databases.plugins.requireRegistration.context(False):
conf.supybot.databases.plugins.requireRegistration.setValue(False)
try:
self.assertNotError('quote add hello world!') self.assertNotError('quote add hello world!')
self.assertRegexp('quote get 1', 'hello') self.assertRegexp('quote get 1', 'hello')
self.assertNotError('quote remove 1') self.assertNotError('quote remove 1')
finally:
conf.supybot.databases.plugins.requireRegistration.setValue(original_value)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -330,6 +330,8 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
(self.name(), id, channel)) (self.name(), id, channel))
def checkChangeAllowed(self, irc, msg, channel, user, record): def checkChangeAllowed(self, irc, msg, channel, user, record):
# Checks and returns True if either the user ID (integer)
# or the hostmask of the caller match.
if (hasattr(user, 'id') and user.id == record.by) or user == record.by: if (hasattr(user, 'id') and user.id == record.by) or user == record.by:
return True return True
cap = ircdb.makeChannelCapability(channel, 'op') cap = ircdb.makeChannelCapability(channel, 'op')
@ -341,12 +343,12 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
"""This should irc.error or raise an exception if text is invalid.""" """This should irc.error or raise an exception if text is invalid."""
pass pass
def getUserId(self, irc, prefix): def getUserId(self, irc, prefix, channel=None):
try: try:
user = ircdb.users.getUser(prefix) user = ircdb.users.getUser(prefix)
return user.id return user.id
except KeyError: except KeyError:
if conf.supybot.databases.plugins.requireRegistration(): if conf.get(conf.supybot.databases.plugins.requireRegistration, channel):
irc.errorNotRegistered(Raise=True) irc.errorNotRegistered(Raise=True)
return return
@ -357,7 +359,7 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
<channel> is only necessary if the message isn't sent in the channel <channel> is only necessary if the message isn't sent in the channel
itself. itself.
""" """
user = self.getUserId(irc, msg.prefix) or msg.prefix user = self.getUserId(irc, msg.prefix, channel) or msg.prefix
at = time.time() at = time.time()
self.addValidator(irc, text) self.addValidator(irc, text)
if text is not None: if text is not None:
@ -372,7 +374,7 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
<channel> is only necessary if the message isn't sent in the channel <channel> is only necessary if the message isn't sent in the channel
itself. itself.
""" """
user = self.getUserId(irc, msg.prefix) or msg.prefix user = self.getUserId(irc, msg.prefix, channel) or msg.prefix
try: try:
record = self.db.get(channel, id) record = self.db.get(channel, id)
self.checkChangeAllowed(irc, msg, channel, user, record) self.checkChangeAllowed(irc, msg, channel, user, record)
@ -448,7 +450,7 @@ class ChannelIdDatabasePlugin(callbacks.Plugin):
<regexp>. <channel> is only necessary if the message isn't sent in the <regexp>. <channel> is only necessary if the message isn't sent in the
channel itself. channel itself.
""" """
user = self.getUserId(irc, msg.prefix) or msg.prefix user = self.getUserId(irc, msg.prefix, channel) or msg.prefix
try: try:
record = self.db.get(channel, id) record = self.db.get(channel, id)
self.checkChangeAllowed(irc, msg, channel, user, record) self.checkChangeAllowed(irc, msg, channel, user, record)

View File

@ -932,7 +932,7 @@ class ChannelSpecific(registry.Boolean):
registerGroup(supybot.databases, 'plugins') registerGroup(supybot.databases, 'plugins')
registerGlobalValue(supybot.databases.plugins, 'requireRegistration', registerChannelValue(supybot.databases.plugins, 'requireRegistration',
registry.Boolean(True, _("""Determines whether the bot will require user registry.Boolean(True, _("""Determines whether the bot will require user
registration to use 'add' commands in database-based Supybot registration to use 'add' commands in database-based Supybot
plugins."""))) plugins.""")))