def test_channel_star_perms(self): acl = ACL(StringIO.StringIO(TEST_ACL.strip('\n')).read()) comm = { 'user': '******', 'channel': '#channel', } self.assertTrue(acl.hasPermission(comm, 'foo'))
def test_channel_everything_but_figlet(self): acl = ACL(StringIO.StringIO(TEST_ACL).read()) comm = { 'user': '******', # not in any groups 'channel': '#channel2', } self.assertTrue(acl.hasPermission(comm, 'cmd')) self.assertTrue(acl.hasPermission(comm, 'anothercmd')) self.assertFalse(acl.hasPermission(comm, 'figlet'))
def test_channel_perms(self): acl = ACL(StringIO.StringIO(TEST_ACL).read()) comm = { 'user': '******', # not in any groups 'channel': '#channel1', } self.assertTrue(acl.hasPermission(comm, 'figlet')) self.assertTrue(acl.hasPermission(comm, 'quote')) self.assertFalse(acl.hasPermission(comm, 'quote.add')) self.assertFalse(acl.hasPermission(comm, 'channel.leave'))
def __init__(self, config): self.channels = [c.split(' ', 1) for c in config['channels']] self.nickname = config['nickname'] self.password = config.get('password', None) self.history = {} self.sent_messages = {} acl_fname = config.get('acl', None) if acl_fname: # Bubble up an IOError if they passed a bad file with open(acl_fname, 'r') as acl_fd: self.acl = ACL(acl_fd.read()) log.info('Loaded ACLs from %s', acl_fname) else: self.acl = AllowAllACL() log.info('Using no-op ACLs.') self.loader = PluginLoader(config) if 'db' in config: log.info('Loading db from config: %s', config['db']) db_engine = sqlalchemy.create_engine(config['db']) else: log.info('Using in-memory db') db_engine = sqlalchemy.create_engine('sqlite:///:memory:') DBSession = orm.sessionmaker(db_engine) session = DBSession() self.loader.db = DB(db_engine, session) self.loader.loadAll()
class TestAddGroups(TestCase): def setUp(self): self.acl = ACL(TEST_ACL) def test_single(self): comm = {'user': '******'} self.assertEqual(self.acl.add_groups(comm)['groups'], ['@ops']) def test_double(self): comm = {'user': '******'} groups = set(self.acl.add_groups(comm)['groups']) expected = set(['@ops', '@7letters']) self.assertEqual(groups, expected) def test_none(self): comm = {'user': '******'} self.assertEqual(self.acl.add_groups(comm)['groups'], [])
class TestPermissionGlobbing(TestCase): def setUp(self): self.acl = ACL(TEST_ACL) def _check(self, perm, pattern, match=ACL.ALLOW): if match: self.assertTrue(self.acl.glob_permission_match(perm, pattern)) else: self.assertFalse(self.acl.glob_permission_match(perm, pattern)) def test_single(self): self._check('foo', 'foo') self._check('foo', 'bar', None) def test_dotted(self): self._check('foo.bar', 'foo.bar') self._check('foo.bar', 'foo.baz', None) def test_partial(self): self._check('foo', 'foo.bar', None) self._check('foo.bar', 'foo', None) def test_end_star(self): self._check('foo.bar', 'foo.*') self._check('foo.bar', 'bar.*', None) def test_middle_star(self): self._check('foo.bar.baz', 'foo.*.baz') self._check('foo.bar.baz', 'foo.*.qux', None) def test_partial_star(self): self._check('foo.bar.baz', 'foo.*') self._check('foo.bar.baz', 'bar.*', None) def test_negation(self): self._check('foo', '-foo', ACL.DENY) self._check('foo.bar', '-foo.bar', ACL.DENY) self._check('foo', '-foo.bar', None)
def _check(self, selector, expected): acl = ACL(TEST_ACL) self.assertEqual(acl.parse_selector(selector), expected)
def setUp(self): self.acl = ACL(TEST_ACL)
def test_empty_acls(self): # This should not error. acl = ACL('{}') acl.has_permission({}, 'foo')
class TestACL(TestCase): def setUp(self): self.acl = ACL(TEST_ACL) def test_channel_star_perms(self): comm = { 'user': '******', 'channel': '#channel2', } self.assertTrue(self.acl.has_permission(comm, 'foo')) def test_channel_perms(self): comm = { 'user': '******', 'channel': '#channel1', } self.assertTrue(self.acl.has_permission(comm, 'figlet')) self.assertTrue(self.acl.has_permission(comm, 'quote')) self.assertFalse(self.acl.has_permission(comm, 'quote.add')) self.assertFalse(self.acl.has_permission(comm, 'channel.leave')) def test_channel_everything_but_figlet(self): comm = { 'user': '******', 'channel': '#channel2', } self.assertTrue(self.acl.has_permission(comm, 'cmd')) self.assertTrue(self.acl.has_permission(comm, 'anothercmd')) self.assertFalse(self.acl.has_permission(comm, 'figlet')) def test_group_permissions(self): comm = { 'user': '******', 'channel': '#channel3', # group will be filled in as '@op' } self.assertTrue(self.acl.has_permission(comm, 'cmd')) comm['channel'] = '#channel2' # Not even ops can use figlet here. self.assertFalse(self.acl.has_permission(comm, 'figlet')) def test_user_permission(self): comm = { 'user': '******', 'channel': '#channel3', } self.assertTrue(self.acl.has_permission(comm, 'quote')) self.assertTrue(self.acl.has_permission(comm, 'quote.add')) self.assertTrue(self.acl.has_permission(comm, 'quote.delete')) def test_empty_acls(self): # This should not error. acl = ACL('{}') acl.has_permission({}, 'foo')