annotate tests/test_auth.py @ 500:d3cd7d8d6b25 default tip

web: Breaking changes in flask-login API.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 07 Jun 2020 00:56:00 -0700
parents 36c3e9b1d1e3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
451
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import pytest
497
36c3e9b1d1e3 cm: Upgrade all dependencies and fix deprecation issues.
Ludovic Chabant <ludovic@chabant.com>
parents: 451
diff changeset
2 from configparser import ConfigParser
451
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from wikked.auth import (
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 UserManager, PERM_NAMES,
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 NoSuchGroupOrUserError, MultipleGroupMembershipError,
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 CyclicUserGroupError, InvalidPermissionError)
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 def _user_manager_from_str(txt):
497
36c3e9b1d1e3 cm: Upgrade all dependencies and fix deprecation issues.
Ludovic Chabant <ludovic@chabant.com>
parents: 451
diff changeset
10 config = ConfigParser()
451
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 config.read_string(txt)
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 return UserManager(config)
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 def _p(name):
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 return PERM_NAMES[name]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 def test_empty_auth():
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 m = _user_manager_from_str("")
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 assert list(m.getUserNames()) == ['anonymous']
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 assert list(m.getGroupNames()) == ['*']
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 def test_missing_user1():
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 with pytest.raises(NoSuchGroupOrUserError):
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 m = _user_manager_from_str("""
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 [permissions]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 dorothy = read
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 """)
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 def test_missing_user2():
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 with pytest.raises(NoSuchGroupOrUserError):
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 m = _user_manager_from_str("""
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 [groups]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 mygroup = dorothy
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 """)
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 def test_multiple_group_membership1():
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 with pytest.raises(MultipleGroupMembershipError):
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 m = _user_manager_from_str("""
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 [users]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 dorothy = pass
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 [groups]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 one = dorothy
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 two = dorothy
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 """)
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 def test_multiple_group_membership2():
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 with pytest.raises(MultipleGroupMembershipError):
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 m = _user_manager_from_str("""
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 [users]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 dorothy = pass
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 [groups]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 one = dorothy
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 two = one
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 three = one
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 """)
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 def test_auth1():
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 m = _user_manager_from_str("""
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 [users]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 dorothy = pass
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 [permissions]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 dorothy = read,edit
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 """)
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 assert m.hasPermission('dorothy', _p('read'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 assert m.hasPermission('dorothy', _p('edit'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 assert not m.hasPermission('dorothy', _p('create'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 def test_auth2():
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 m = _user_manager_from_str("""
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 [users]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 dorothy = pass
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 toto = pass
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 tinman = pass
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 [groups]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 humans = dorothy
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 others = toto, tinman
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 [permissions]
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 humans = read,edit
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 others = read
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 tinman = create
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 """)
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 assert m.hasPermission('dorothy', _p('read'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 assert m.hasPermission('dorothy', _p('edit'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 assert not m.hasPermission('dorothy', _p('create'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 assert m.hasPermission('toto', _p('read'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 assert not m.hasPermission('toto', _p('edit'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 assert not m.hasPermission('toto', _p('create'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 assert m.hasPermission('tinman', _p('read'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 assert not m.hasPermission('tinman', _p('edit'))
6cd51ea6dfcf auth: Rewrite permission system and improve support for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 assert m.hasPermission('tinman', _p('create'))