Пример #1
0
    def test_remove_cap(self):
        r = Role()
        r['name'] = 'test'
        r['caps'] = {'*', '1', '2', }

        r.remove_cap('1')
        r.remove_cap('3')
        r.remove_cap('*')
        assert r['caps'] == {'2', }

        # 这是不能这么用的
        assert_raises(ValueError, r.remove_cap, '')
        assert_raises(ValueError, r.remove_cap, '-123')
Пример #2
0
    def test_ban_cap(self):
        r = Role()
        r['name'] = 'test'
        r['caps'] = {'1', }

        r.ban_cap('1')
        r.ban_cap('2')

        assert r['caps'] == {'1', '-1', '-2', }

        # 不能 ban 掉全能权限和非法的权限名
        assert_raises(ValueError, r.ban_cap, '')
        assert_raises(ValueError, r.ban_cap, '-hehe')
        assert_raises(ValueError, r.ban_cap, '*')
Пример #3
0
    def test_grant_cap(self):
        r = Role()
        r['name'] = 'test'
        r['caps'] = {'1', }

        r.grant_cap('test')
        assert r['caps'] == {'1', 'test', }

        # 不能授予名字以减号开头的权限 (因为有歧义)
        assert_raises(ValueError, r.grant_cap, '-foo')

        # 可以授予全能权限
        r.grant_cap('*')
        assert r['caps'] == {'*', '1', 'test', }
Пример #4
0
    def test_role_hascap(self):
        r = Role.fetch('testuser')

        assert r.hascap('c1')
        assert not r.hascap('c100')
        assert 'c2' in r
        assert 'c200' not in r
Пример #5
0
    def test_hascap(self):
        r = Role.find('用户')

        assert r.hascap('c1')
        assert not r.hascap('c100')
        assert 'c2' in r
        assert 'c200' not in r
Пример #6
0
    def test_caps(self):
        r = Role.find('用户')

        caps = r['caps']
        caps.sort()

        assert tuple(caps) == ('c1', 'c2', )
Пример #7
0
    def test_allcaps(self):
        caps = Role.allcaps(['testuser', 'testadm', ])

        assert isinstance(caps, set)
        assert caps == {'c1', 'c2', 'c3', 'c5', }
Пример #8
0
    def test_caps(self):
        r = Role.fetch('testuser')

        caps = r['caps']
        assert isinstance(caps, set)
        assert caps == {'c1', 'c2', }
Пример #9
0
 def test_fetch(self):
     assert Role.fetch('testuser')
     assert Role.fetch('nonexist') is None
Пример #10
0
 def test_allcaps_exclude_cap(self):
     caps = Role.allcaps(['testuser', 'restricted-user', ])
     assert caps == {'c1', 'c2', '-c1', }
Пример #11
0
    def test_allcaps(self):
        caps = list(Role.allcaps('用户', '鹳狸猿', ))
        caps.sort()

        assert tuple(caps) == ('c1', 'c2', 'c3', 'c5', )
Пример #12
0
 def test_find(self):
     assert Role.find('用户')
     assert_raises(KeyError, Role.find, '不存在')