rev: 491566fa24ddd88cee7987a361d3a63f466f8c76 Wikked/tests/test_auth.py -rw-r--r-- 2.2 KiB View raw Log this file
491566fa24ddLudovic Chabant cm: Upgrade dependencies and fix deprecated APIs. 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import pytest
from configparser import ConfigParser
from wikked.auth import (
        UserManager, PERM_NAMES,
        NoSuchGroupOrUserError, MultipleGroupMembershipError,
        CyclicUserGroupError, InvalidPermissionError)


def _user_manager_from_str(txt):
    config = ConfigParser()
    config.read_string(txt)
    return UserManager(config)


def _p(name):
    return PERM_NAMES[name]


def test_empty_auth():
    m = _user_manager_from_str("")
    assert list(m.getUserNames()) == ['anonymous']
    assert list(m.getGroupNames()) == ['*']


def test_missing_user1():
    with pytest.raises(NoSuchGroupOrUserError):
        m = _user_manager_from_str("""
[permissions]
dorothy = read
""")


def test_missing_user2():
    with pytest.raises(NoSuchGroupOrUserError):
        m = _user_manager_from_str("""
[groups]
mygroup = dorothy
""")


def test_multiple_group_membership1():
    with pytest.raises(MultipleGroupMembershipError):
        m = _user_manager_from_str("""
[users]
dorothy = pass
[groups]
one = dorothy
two = dorothy
""")


def test_multiple_group_membership2():
    with pytest.raises(MultipleGroupMembershipError):
        m = _user_manager_from_str("""
[users]
dorothy = pass
[groups]
one = dorothy
two = one
three = one
""")


def test_auth1():
    m = _user_manager_from_str("""
[users]
dorothy = pass
[permissions]
dorothy = read,edit
""")
    assert m.hasPermission('dorothy', _p('read'))
    assert m.hasPermission('dorothy', _p('edit'))
    assert not m.hasPermission('dorothy', _p('create'))


def test_auth2():
    m = _user_manager_from_str("""
[users]
dorothy = pass
toto = pass
tinman = pass
[groups]
humans = dorothy
others = toto, tinman
[permissions]
humans = read,edit
others = read
tinman = create
""")
    assert m.hasPermission('dorothy', _p('read'))
    assert m.hasPermission('dorothy', _p('edit'))
    assert not m.hasPermission('dorothy', _p('create'))
    assert m.hasPermission('toto', _p('read'))
    assert not m.hasPermission('toto', _p('edit'))
    assert not m.hasPermission('toto', _p('create'))
    assert m.hasPermission('tinman', _p('read'))
    assert not m.hasPermission('tinman', _p('edit'))
    assert m.hasPermission('tinman', _p('create'))