Show More
@@ -1,525 +1,529 b'' | |||||
1 | import os |
|
1 | import os | |
2 | import unittest |
|
2 | import unittest | |
3 | from rhodecode.tests import * |
|
3 | from rhodecode.tests import * | |
4 | from rhodecode.tests.fixture import Fixture |
|
4 | from rhodecode.tests.fixture import Fixture | |
5 | from rhodecode.model.repos_group import ReposGroupModel |
|
5 | from rhodecode.model.repos_group import ReposGroupModel | |
6 | from rhodecode.model.repo import RepoModel |
|
6 | from rhodecode.model.repo import RepoModel | |
7 | from rhodecode.model.db import RepoGroup, User, UserGroupRepoGroupToPerm,\ |
|
7 | from rhodecode.model.db import RepoGroup, User, UserGroupRepoGroupToPerm,\ | |
8 | Permission, UserToPerm |
|
8 | Permission, UserToPerm | |
9 | from rhodecode.model.user import UserModel |
|
9 | from rhodecode.model.user import UserModel | |
10 |
|
10 | |||
11 | from rhodecode.model.meta import Session |
|
11 | from rhodecode.model.meta import Session | |
12 | from rhodecode.model.users_group import UserGroupModel |
|
12 | from rhodecode.model.users_group import UserGroupModel | |
13 | from rhodecode.lib.auth import AuthUser |
|
13 | from rhodecode.lib.auth import AuthUser | |
14 | from rhodecode.model.permission import PermissionModel |
|
14 | from rhodecode.model.permission import PermissionModel | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | fixture = Fixture() |
|
17 | fixture = Fixture() | |
18 |
|
18 | |||
19 |
|
19 | |||
20 | class TestPermissions(unittest.TestCase): |
|
20 | class TestPermissions(unittest.TestCase): | |
21 | def __init__(self, methodName='runTest'): |
|
21 | def __init__(self, methodName='runTest'): | |
22 | super(TestPermissions, self).__init__(methodName=methodName) |
|
22 | super(TestPermissions, self).__init__(methodName=methodName) | |
23 |
|
23 | |||
24 | def setUp(self): |
|
24 | def setUp(self): | |
25 | self.u1 = UserModel().create_or_update( |
|
25 | self.u1 = UserModel().create_or_update( | |
26 | username=u'u1', password=u'qweqwe', |
|
26 | username=u'u1', password=u'qweqwe', | |
27 | email=u'u1@rhodecode.org', firstname=u'u1', lastname=u'u1' |
|
27 | email=u'u1@rhodecode.org', firstname=u'u1', lastname=u'u1' | |
28 | ) |
|
28 | ) | |
29 | self.u2 = UserModel().create_or_update( |
|
29 | self.u2 = UserModel().create_or_update( | |
30 | username=u'u2', password=u'qweqwe', |
|
30 | username=u'u2', password=u'qweqwe', | |
31 | email=u'u2@rhodecode.org', firstname=u'u2', lastname=u'u2' |
|
31 | email=u'u2@rhodecode.org', firstname=u'u2', lastname=u'u2' | |
32 | ) |
|
32 | ) | |
33 | self.u3 = UserModel().create_or_update( |
|
33 | self.u3 = UserModel().create_or_update( | |
34 | username=u'u3', password=u'qweqwe', |
|
34 | username=u'u3', password=u'qweqwe', | |
35 | email=u'u3@rhodecode.org', firstname=u'u3', lastname=u'u3' |
|
35 | email=u'u3@rhodecode.org', firstname=u'u3', lastname=u'u3' | |
36 | ) |
|
36 | ) | |
37 | self.anon = User.get_default_user() |
|
37 | self.anon = User.get_default_user() | |
38 | self.a1 = UserModel().create_or_update( |
|
38 | self.a1 = UserModel().create_or_update( | |
39 | username=u'a1', password=u'qweqwe', |
|
39 | username=u'a1', password=u'qweqwe', | |
40 | email=u'a1@rhodecode.org', firstname=u'a1', lastname=u'a1', admin=True |
|
40 | email=u'a1@rhodecode.org', firstname=u'a1', lastname=u'a1', admin=True | |
41 | ) |
|
41 | ) | |
42 | Session().commit() |
|
42 | Session().commit() | |
43 |
|
43 | |||
44 | def tearDown(self): |
|
44 | def tearDown(self): | |
45 | if hasattr(self, 'test_repo'): |
|
45 | if hasattr(self, 'test_repo'): | |
46 | RepoModel().delete(repo=self.test_repo) |
|
46 | RepoModel().delete(repo=self.test_repo) | |
47 |
|
47 | |||
48 | UserModel().delete(self.u1) |
|
48 | UserModel().delete(self.u1) | |
49 | UserModel().delete(self.u2) |
|
49 | UserModel().delete(self.u2) | |
50 | UserModel().delete(self.u3) |
|
50 | UserModel().delete(self.u3) | |
51 | UserModel().delete(self.a1) |
|
51 | UserModel().delete(self.a1) | |
52 | if hasattr(self, 'g1'): |
|
52 | if hasattr(self, 'g1'): | |
53 | ReposGroupModel().delete(self.g1.group_id) |
|
53 | ReposGroupModel().delete(self.g1.group_id) | |
54 | if hasattr(self, 'g2'): |
|
54 | if hasattr(self, 'g2'): | |
55 | ReposGroupModel().delete(self.g2.group_id) |
|
55 | ReposGroupModel().delete(self.g2.group_id) | |
56 |
|
56 | |||
57 | if hasattr(self, 'ug1'): |
|
57 | if hasattr(self, 'ug1'): | |
58 | UserGroupModel().delete(self.ug1, force=True) |
|
58 | UserGroupModel().delete(self.ug1, force=True) | |
59 |
|
59 | |||
60 | Session().commit() |
|
60 | Session().commit() | |
61 |
|
61 | |||
62 | def test_default_perms_set(self): |
|
62 | def test_default_perms_set(self): | |
63 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
63 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
64 | perms = { |
|
64 | perms = { | |
65 | 'repositories_groups': {}, |
|
65 | 'repositories_groups': {}, | |
66 | 'global': set([u'hg.create.repository', u'repository.read', |
|
66 | 'global': set([u'hg.create.repository', u'repository.read', | |
67 | u'hg.register.manual_activate']), |
|
67 | u'hg.register.manual_activate']), | |
68 | 'repositories': {u'vcs_test_hg': u'repository.read'} |
|
68 | 'repositories': {u'vcs_test_hg': u'repository.read'} | |
69 | } |
|
69 | } | |
70 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], |
|
70 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], | |
71 | perms['repositories'][HG_REPO]) |
|
71 | perms['repositories'][HG_REPO]) | |
72 | new_perm = 'repository.write' |
|
72 | new_perm = 'repository.write' | |
73 | RepoModel().grant_user_permission(repo=HG_REPO, user=self.u1, |
|
73 | RepoModel().grant_user_permission(repo=HG_REPO, user=self.u1, | |
74 | perm=new_perm) |
|
74 | perm=new_perm) | |
75 | Session().commit() |
|
75 | Session().commit() | |
76 |
|
76 | |||
77 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
77 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
78 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], |
|
78 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], | |
79 | new_perm) |
|
79 | new_perm) | |
80 |
|
80 | |||
81 | def test_default_admin_perms_set(self): |
|
81 | def test_default_admin_perms_set(self): | |
82 | a1_auth = AuthUser(user_id=self.a1.user_id) |
|
82 | a1_auth = AuthUser(user_id=self.a1.user_id) | |
83 | perms = { |
|
83 | perms = { | |
84 | 'repositories_groups': {}, |
|
84 | 'repositories_groups': {}, | |
85 | 'global': set([u'hg.admin']), |
|
85 | 'global': set([u'hg.admin']), | |
86 | 'repositories': {u'vcs_test_hg': u'repository.admin'} |
|
86 | 'repositories': {u'vcs_test_hg': u'repository.admin'} | |
87 | } |
|
87 | } | |
88 | self.assertEqual(a1_auth.permissions['repositories'][HG_REPO], |
|
88 | self.assertEqual(a1_auth.permissions['repositories'][HG_REPO], | |
89 | perms['repositories'][HG_REPO]) |
|
89 | perms['repositories'][HG_REPO]) | |
90 | new_perm = 'repository.write' |
|
90 | new_perm = 'repository.write' | |
91 | RepoModel().grant_user_permission(repo=HG_REPO, user=self.a1, |
|
91 | RepoModel().grant_user_permission(repo=HG_REPO, user=self.a1, | |
92 | perm=new_perm) |
|
92 | perm=new_perm) | |
93 | Session().commit() |
|
93 | Session().commit() | |
94 | # cannot really downgrade admins permissions !? they still get's set as |
|
94 | # cannot really downgrade admins permissions !? they still get's set as | |
95 | # admin ! |
|
95 | # admin ! | |
96 | u1_auth = AuthUser(user_id=self.a1.user_id) |
|
96 | u1_auth = AuthUser(user_id=self.a1.user_id) | |
97 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], |
|
97 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], | |
98 | perms['repositories'][HG_REPO]) |
|
98 | perms['repositories'][HG_REPO]) | |
99 |
|
99 | |||
100 | def test_default_group_perms(self): |
|
100 | def test_default_group_perms(self): | |
101 | self.g1 = fixture.create_group('test1', skip_if_exists=True) |
|
101 | self.g1 = fixture.create_group('test1', skip_if_exists=True) | |
102 | self.g2 = fixture.create_group('test2', skip_if_exists=True) |
|
102 | self.g2 = fixture.create_group('test2', skip_if_exists=True) | |
103 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
103 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
104 | perms = { |
|
104 | perms = { | |
105 | 'repositories_groups': {u'test1': 'group.read', u'test2': 'group.read'}, |
|
105 | 'repositories_groups': {u'test1': 'group.read', u'test2': 'group.read'}, | |
106 | 'global': set(Permission.DEFAULT_USER_PERMISSIONS), |
|
106 | 'global': set(Permission.DEFAULT_USER_PERMISSIONS), | |
107 | 'repositories': {u'vcs_test_hg': u'repository.read'} |
|
107 | 'repositories': {u'vcs_test_hg': u'repository.read'} | |
108 | } |
|
108 | } | |
109 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], |
|
109 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], | |
110 | perms['repositories'][HG_REPO]) |
|
110 | perms['repositories'][HG_REPO]) | |
111 | self.assertEqual(u1_auth.permissions['repositories_groups'], |
|
111 | self.assertEqual(u1_auth.permissions['repositories_groups'], | |
112 | perms['repositories_groups']) |
|
112 | perms['repositories_groups']) | |
113 | self.assertEqual(u1_auth.permissions['global'], |
|
113 | self.assertEqual(u1_auth.permissions['global'], | |
114 | perms['global']) |
|
114 | perms['global']) | |
115 |
|
115 | |||
116 | def test_default_admin_group_perms(self): |
|
116 | def test_default_admin_group_perms(self): | |
117 | self.g1 = fixture.create_group('test1', skip_if_exists=True) |
|
117 | self.g1 = fixture.create_group('test1', skip_if_exists=True) | |
118 | self.g2 = fixture.create_group('test2', skip_if_exists=True) |
|
118 | self.g2 = fixture.create_group('test2', skip_if_exists=True) | |
119 | a1_auth = AuthUser(user_id=self.a1.user_id) |
|
119 | a1_auth = AuthUser(user_id=self.a1.user_id) | |
120 | perms = { |
|
120 | perms = { | |
121 | 'repositories_groups': {u'test1': 'group.admin', u'test2': 'group.admin'}, |
|
121 | 'repositories_groups': {u'test1': 'group.admin', u'test2': 'group.admin'}, | |
122 | 'global': set(['hg.admin']), |
|
122 | 'global': set(['hg.admin']), | |
123 | 'repositories': {u'vcs_test_hg': 'repository.admin'} |
|
123 | 'repositories': {u'vcs_test_hg': 'repository.admin'} | |
124 | } |
|
124 | } | |
125 |
|
125 | |||
126 | self.assertEqual(a1_auth.permissions['repositories'][HG_REPO], |
|
126 | self.assertEqual(a1_auth.permissions['repositories'][HG_REPO], | |
127 | perms['repositories'][HG_REPO]) |
|
127 | perms['repositories'][HG_REPO]) | |
128 | self.assertEqual(a1_auth.permissions['repositories_groups'], |
|
128 | self.assertEqual(a1_auth.permissions['repositories_groups'], | |
129 | perms['repositories_groups']) |
|
129 | perms['repositories_groups']) | |
130 |
|
130 | |||
131 | def test_propagated_permission_from_users_group_by_explicit_perms_exist(self): |
|
131 | def test_propagated_permission_from_users_group_by_explicit_perms_exist(self): | |
132 | # make group |
|
132 | # make group | |
133 | self.ug1 = fixture.create_user_group('G1') |
|
133 | self.ug1 = fixture.create_user_group('G1') | |
134 | UserGroupModel().add_user_to_group(self.ug1, self.u1) |
|
134 | UserGroupModel().add_user_to_group(self.ug1, self.u1) | |
135 |
|
135 | |||
136 | # set permission to lower |
|
136 | # set permission to lower | |
137 | new_perm = 'repository.none' |
|
137 | new_perm = 'repository.none' | |
138 | RepoModel().grant_user_permission(repo=HG_REPO, user=self.u1, perm=new_perm) |
|
138 | RepoModel().grant_user_permission(repo=HG_REPO, user=self.u1, perm=new_perm) | |
139 | Session().commit() |
|
139 | Session().commit() | |
140 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
140 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
141 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], |
|
141 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], | |
142 | new_perm) |
|
142 | new_perm) | |
143 |
|
143 | |||
144 | # grant perm for group this should not override permission from user |
|
144 | # grant perm for group this should not override permission from user | |
145 | # since it has explicitly set |
|
145 | # since it has explicitly set | |
146 | new_perm_gr = 'repository.write' |
|
146 | new_perm_gr = 'repository.write' | |
147 | RepoModel().grant_users_group_permission(repo=HG_REPO, |
|
147 | RepoModel().grant_users_group_permission(repo=HG_REPO, | |
148 | group_name=self.ug1, |
|
148 | group_name=self.ug1, | |
149 | perm=new_perm_gr) |
|
149 | perm=new_perm_gr) | |
150 | # check perms |
|
150 | # check perms | |
151 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
151 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
152 | perms = { |
|
152 | perms = { | |
153 | 'repositories_groups': {}, |
|
153 | 'repositories_groups': {}, | |
154 | 'global': set([u'hg.create.repository', u'repository.read', |
|
154 | 'global': set([u'hg.create.repository', u'repository.read', | |
155 | u'hg.register.manual_activate']), |
|
155 | u'hg.register.manual_activate']), | |
156 | 'repositories': {u'vcs_test_hg': u'repository.read'} |
|
156 | 'repositories': {u'vcs_test_hg': u'repository.read'} | |
157 | } |
|
157 | } | |
158 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], |
|
158 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], | |
159 | new_perm) |
|
159 | new_perm) | |
160 | self.assertEqual(u1_auth.permissions['repositories_groups'], |
|
160 | self.assertEqual(u1_auth.permissions['repositories_groups'], | |
161 | perms['repositories_groups']) |
|
161 | perms['repositories_groups']) | |
162 |
|
162 | |||
163 | def test_propagated_permission_from_users_group(self): |
|
163 | def test_propagated_permission_from_users_group(self): | |
164 | # make group |
|
164 | # make group | |
165 | self.ug1 = fixture.create_user_group('G1') |
|
165 | self.ug1 = fixture.create_user_group('G1') | |
166 | UserGroupModel().add_user_to_group(self.ug1, self.u3) |
|
166 | UserGroupModel().add_user_to_group(self.ug1, self.u3) | |
167 |
|
167 | |||
168 | # grant perm for group this should override default permission from user |
|
168 | # grant perm for group this should override default permission from user | |
169 | new_perm_gr = 'repository.write' |
|
169 | new_perm_gr = 'repository.write' | |
170 | RepoModel().grant_users_group_permission(repo=HG_REPO, |
|
170 | RepoModel().grant_users_group_permission(repo=HG_REPO, | |
171 | group_name=self.ug1, |
|
171 | group_name=self.ug1, | |
172 | perm=new_perm_gr) |
|
172 | perm=new_perm_gr) | |
173 | # check perms |
|
173 | # check perms | |
174 | u3_auth = AuthUser(user_id=self.u3.user_id) |
|
174 | u3_auth = AuthUser(user_id=self.u3.user_id) | |
175 | perms = { |
|
175 | perms = { | |
176 | 'repositories_groups': {}, |
|
176 | 'repositories_groups': {}, | |
177 | 'global': set([u'hg.create.repository', u'repository.read', |
|
177 | 'global': set([u'hg.create.repository', u'repository.read', | |
178 | u'hg.register.manual_activate']), |
|
178 | u'hg.register.manual_activate']), | |
179 | 'repositories': {u'vcs_test_hg': u'repository.read'} |
|
179 | 'repositories': {u'vcs_test_hg': u'repository.read'} | |
180 | } |
|
180 | } | |
181 | self.assertEqual(u3_auth.permissions['repositories'][HG_REPO], |
|
181 | self.assertEqual(u3_auth.permissions['repositories'][HG_REPO], | |
182 | new_perm_gr) |
|
182 | new_perm_gr) | |
183 | self.assertEqual(u3_auth.permissions['repositories_groups'], |
|
183 | self.assertEqual(u3_auth.permissions['repositories_groups'], | |
184 | perms['repositories_groups']) |
|
184 | perms['repositories_groups']) | |
185 |
|
185 | |||
186 | def test_propagated_permission_from_users_group_lower_weight(self): |
|
186 | def test_propagated_permission_from_users_group_lower_weight(self): | |
187 | # make group |
|
187 | # make group | |
188 | self.ug1 = fixture.create_user_group('G1') |
|
188 | self.ug1 = fixture.create_user_group('G1') | |
189 | # add user to group |
|
189 | # add user to group | |
190 | UserGroupModel().add_user_to_group(self.ug1, self.u1) |
|
190 | UserGroupModel().add_user_to_group(self.ug1, self.u1) | |
191 |
|
191 | |||
192 | # set permission to lower |
|
192 | # set permission to lower | |
193 | new_perm_h = 'repository.write' |
|
193 | new_perm_h = 'repository.write' | |
194 | RepoModel().grant_user_permission(repo=HG_REPO, user=self.u1, |
|
194 | RepoModel().grant_user_permission(repo=HG_REPO, user=self.u1, | |
195 | perm=new_perm_h) |
|
195 | perm=new_perm_h) | |
196 | Session().commit() |
|
196 | Session().commit() | |
197 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
197 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
198 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], |
|
198 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], | |
199 | new_perm_h) |
|
199 | new_perm_h) | |
200 |
|
200 | |||
201 | # grant perm for group this should NOT override permission from user |
|
201 | # grant perm for group this should NOT override permission from user | |
202 | # since it's lower than granted |
|
202 | # since it's lower than granted | |
203 | new_perm_l = 'repository.read' |
|
203 | new_perm_l = 'repository.read' | |
204 | RepoModel().grant_users_group_permission(repo=HG_REPO, |
|
204 | RepoModel().grant_users_group_permission(repo=HG_REPO, | |
205 | group_name=self.ug1, |
|
205 | group_name=self.ug1, | |
206 | perm=new_perm_l) |
|
206 | perm=new_perm_l) | |
207 | # check perms |
|
207 | # check perms | |
208 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
208 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
209 | perms = { |
|
209 | perms = { | |
210 | 'repositories_groups': {}, |
|
210 | 'repositories_groups': {}, | |
211 | 'global': set([u'hg.create.repository', u'repository.read', |
|
211 | 'global': set([u'hg.create.repository', u'repository.read', | |
212 | u'hg.register.manual_activate']), |
|
212 | u'hg.register.manual_activate']), | |
213 | 'repositories': {u'vcs_test_hg': u'repository.write'} |
|
213 | 'repositories': {u'vcs_test_hg': u'repository.write'} | |
214 | } |
|
214 | } | |
215 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], |
|
215 | self.assertEqual(u1_auth.permissions['repositories'][HG_REPO], | |
216 | new_perm_h) |
|
216 | new_perm_h) | |
217 | self.assertEqual(u1_auth.permissions['repositories_groups'], |
|
217 | self.assertEqual(u1_auth.permissions['repositories_groups'], | |
218 | perms['repositories_groups']) |
|
218 | perms['repositories_groups']) | |
219 |
|
219 | |||
220 | def test_repo_in_group_permissions(self): |
|
220 | def test_repo_in_group_permissions(self): | |
221 | self.g1 = fixture.create_group('group1', skip_if_exists=True) |
|
221 | self.g1 = fixture.create_group('group1', skip_if_exists=True) | |
222 | self.g2 = fixture.create_group('group2', skip_if_exists=True) |
|
222 | self.g2 = fixture.create_group('group2', skip_if_exists=True) | |
223 | # both perms should be read ! |
|
223 | # both perms should be read ! | |
224 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
224 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
225 | self.assertEqual(u1_auth.permissions['repositories_groups'], |
|
225 | self.assertEqual(u1_auth.permissions['repositories_groups'], | |
226 | {u'group1': u'group.read', u'group2': u'group.read'}) |
|
226 | {u'group1': u'group.read', u'group2': u'group.read'}) | |
227 |
|
227 | |||
228 | a1_auth = AuthUser(user_id=self.anon.user_id) |
|
228 | a1_auth = AuthUser(user_id=self.anon.user_id) | |
229 | self.assertEqual(a1_auth.permissions['repositories_groups'], |
|
229 | self.assertEqual(a1_auth.permissions['repositories_groups'], | |
230 | {u'group1': u'group.read', u'group2': u'group.read'}) |
|
230 | {u'group1': u'group.read', u'group2': u'group.read'}) | |
231 |
|
231 | |||
232 | #Change perms to none for both groups |
|
232 | #Change perms to none for both groups | |
233 | ReposGroupModel().grant_user_permission(repos_group=self.g1, |
|
233 | ReposGroupModel().grant_user_permission(repos_group=self.g1, | |
234 | user=self.anon, |
|
234 | user=self.anon, | |
235 | perm='group.none') |
|
235 | perm='group.none') | |
236 | ReposGroupModel().grant_user_permission(repos_group=self.g2, |
|
236 | ReposGroupModel().grant_user_permission(repos_group=self.g2, | |
237 | user=self.anon, |
|
237 | user=self.anon, | |
238 | perm='group.none') |
|
238 | perm='group.none') | |
239 |
|
239 | |||
240 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
240 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
241 | self.assertEqual(u1_auth.permissions['repositories_groups'], |
|
241 | self.assertEqual(u1_auth.permissions['repositories_groups'], | |
242 | {u'group1': u'group.none', u'group2': u'group.none'}) |
|
242 | {u'group1': u'group.none', u'group2': u'group.none'}) | |
243 |
|
243 | |||
244 | a1_auth = AuthUser(user_id=self.anon.user_id) |
|
244 | a1_auth = AuthUser(user_id=self.anon.user_id) | |
245 | self.assertEqual(a1_auth.permissions['repositories_groups'], |
|
245 | self.assertEqual(a1_auth.permissions['repositories_groups'], | |
246 | {u'group1': u'group.none', u'group2': u'group.none'}) |
|
246 | {u'group1': u'group.none', u'group2': u'group.none'}) | |
247 |
|
247 | |||
248 | # add repo to group |
|
248 | # add repo to group | |
249 | name = RepoGroup.url_sep().join([self.g1.group_name, 'test_perm']) |
|
249 | name = RepoGroup.url_sep().join([self.g1.group_name, 'test_perm']) | |
250 | self.test_repo = fixture.create_repo(name=name, |
|
250 | self.test_repo = fixture.create_repo(name=name, | |
251 | repo_type='hg', |
|
251 | repo_type='hg', | |
252 | repos_group=self.g1, |
|
252 | repos_group=self.g1, | |
253 | cur_user=self.u1,) |
|
253 | cur_user=self.u1,) | |
254 |
|
254 | |||
255 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
255 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
256 | self.assertEqual(u1_auth.permissions['repositories_groups'], |
|
256 | self.assertEqual(u1_auth.permissions['repositories_groups'], | |
257 | {u'group1': u'group.none', u'group2': u'group.none'}) |
|
257 | {u'group1': u'group.none', u'group2': u'group.none'}) | |
258 |
|
258 | |||
259 | a1_auth = AuthUser(user_id=self.anon.user_id) |
|
259 | a1_auth = AuthUser(user_id=self.anon.user_id) | |
260 | self.assertEqual(a1_auth.permissions['repositories_groups'], |
|
260 | self.assertEqual(a1_auth.permissions['repositories_groups'], | |
261 | {u'group1': u'group.none', u'group2': u'group.none'}) |
|
261 | {u'group1': u'group.none', u'group2': u'group.none'}) | |
262 |
|
262 | |||
263 | #grant permission for u2 ! |
|
263 | #grant permission for u2 ! | |
264 | ReposGroupModel().grant_user_permission(repos_group=self.g1, |
|
264 | ReposGroupModel().grant_user_permission(repos_group=self.g1, | |
265 | user=self.u2, |
|
265 | user=self.u2, | |
266 | perm='group.read') |
|
266 | perm='group.read') | |
267 | ReposGroupModel().grant_user_permission(repos_group=self.g2, |
|
267 | ReposGroupModel().grant_user_permission(repos_group=self.g2, | |
268 | user=self.u2, |
|
268 | user=self.u2, | |
269 | perm='group.read') |
|
269 | perm='group.read') | |
270 | Session().commit() |
|
270 | Session().commit() | |
271 | self.assertNotEqual(self.u1, self.u2) |
|
271 | self.assertNotEqual(self.u1, self.u2) | |
272 | #u1 and anon should have not change perms while u2 should ! |
|
272 | #u1 and anon should have not change perms while u2 should ! | |
273 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
273 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
274 | self.assertEqual(u1_auth.permissions['repositories_groups'], |
|
274 | self.assertEqual(u1_auth.permissions['repositories_groups'], | |
275 | {u'group1': u'group.none', u'group2': u'group.none'}) |
|
275 | {u'group1': u'group.none', u'group2': u'group.none'}) | |
276 |
|
276 | |||
277 | u2_auth = AuthUser(user_id=self.u2.user_id) |
|
277 | u2_auth = AuthUser(user_id=self.u2.user_id) | |
278 | self.assertEqual(u2_auth.permissions['repositories_groups'], |
|
278 | self.assertEqual(u2_auth.permissions['repositories_groups'], | |
279 | {u'group1': u'group.read', u'group2': u'group.read'}) |
|
279 | {u'group1': u'group.read', u'group2': u'group.read'}) | |
280 |
|
280 | |||
281 | a1_auth = AuthUser(user_id=self.anon.user_id) |
|
281 | a1_auth = AuthUser(user_id=self.anon.user_id) | |
282 | self.assertEqual(a1_auth.permissions['repositories_groups'], |
|
282 | self.assertEqual(a1_auth.permissions['repositories_groups'], | |
283 | {u'group1': u'group.none', u'group2': u'group.none'}) |
|
283 | {u'group1': u'group.none', u'group2': u'group.none'}) | |
284 |
|
284 | |||
285 | def test_repo_group_user_as_user_group_member(self): |
|
285 | def test_repo_group_user_as_user_group_member(self): | |
286 | # create Group1 |
|
286 | # create Group1 | |
287 | self.g1 = fixture.create_group('group1', skip_if_exists=True) |
|
287 | self.g1 = fixture.create_group('group1', skip_if_exists=True) | |
288 | a1_auth = AuthUser(user_id=self.anon.user_id) |
|
288 | a1_auth = AuthUser(user_id=self.anon.user_id) | |
289 |
|
289 | |||
290 | self.assertEqual(a1_auth.permissions['repositories_groups'], |
|
290 | self.assertEqual(a1_auth.permissions['repositories_groups'], | |
291 | {u'group1': u'group.read'}) |
|
291 | {u'group1': u'group.read'}) | |
292 |
|
292 | |||
293 | # set default permission to none |
|
293 | # set default permission to none | |
294 | ReposGroupModel().grant_user_permission(repos_group=self.g1, |
|
294 | ReposGroupModel().grant_user_permission(repos_group=self.g1, | |
295 | user=self.anon, |
|
295 | user=self.anon, | |
296 | perm='group.none') |
|
296 | perm='group.none') | |
297 | # make group |
|
297 | # make group | |
298 | self.ug1 = fixture.create_user_group('G1') |
|
298 | self.ug1 = fixture.create_user_group('G1') | |
299 | # add user to group |
|
299 | # add user to group | |
300 | UserGroupModel().add_user_to_group(self.ug1, self.u1) |
|
300 | UserGroupModel().add_user_to_group(self.ug1, self.u1) | |
301 | Session().commit() |
|
301 | Session().commit() | |
302 |
|
302 | |||
303 | # check if user is in the group |
|
303 | # check if user is in the group | |
304 | membrs = [x.user_id for x in UserGroupModel().get(self.ug1.users_group_id).members] |
|
304 | membrs = [x.user_id for x in UserGroupModel().get(self.ug1.users_group_id).members] | |
305 | self.assertEqual(membrs, [self.u1.user_id]) |
|
305 | self.assertEqual(membrs, [self.u1.user_id]) | |
306 | # add some user to that group |
|
306 | # add some user to that group | |
307 |
|
307 | |||
308 | # check his permissions |
|
308 | # check his permissions | |
309 | a1_auth = AuthUser(user_id=self.anon.user_id) |
|
309 | a1_auth = AuthUser(user_id=self.anon.user_id) | |
310 | self.assertEqual(a1_auth.permissions['repositories_groups'], |
|
310 | self.assertEqual(a1_auth.permissions['repositories_groups'], | |
311 | {u'group1': u'group.none'}) |
|
311 | {u'group1': u'group.none'}) | |
312 |
|
312 | |||
313 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
313 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
314 | self.assertEqual(u1_auth.permissions['repositories_groups'], |
|
314 | self.assertEqual(u1_auth.permissions['repositories_groups'], | |
315 | {u'group1': u'group.none'}) |
|
315 | {u'group1': u'group.none'}) | |
316 |
|
316 | |||
317 | # grant ug1 read permissions for |
|
317 | # grant ug1 read permissions for | |
318 | ReposGroupModel().grant_users_group_permission(repos_group=self.g1, |
|
318 | ReposGroupModel().grant_users_group_permission(repos_group=self.g1, | |
319 | group_name=self.ug1, |
|
319 | group_name=self.ug1, | |
320 | perm='group.read') |
|
320 | perm='group.read') | |
321 | Session().commit() |
|
321 | Session().commit() | |
322 | # check if the |
|
322 | # check if the | |
323 | obj = Session().query(UserGroupRepoGroupToPerm)\ |
|
323 | obj = Session().query(UserGroupRepoGroupToPerm)\ | |
324 | .filter(UserGroupRepoGroupToPerm.group == self.g1)\ |
|
324 | .filter(UserGroupRepoGroupToPerm.group == self.g1)\ | |
325 | .filter(UserGroupRepoGroupToPerm.users_group == self.ug1)\ |
|
325 | .filter(UserGroupRepoGroupToPerm.users_group == self.ug1)\ | |
326 | .scalar() |
|
326 | .scalar() | |
327 | self.assertEqual(obj.permission.permission_name, 'group.read') |
|
327 | self.assertEqual(obj.permission.permission_name, 'group.read') | |
328 |
|
328 | |||
329 | a1_auth = AuthUser(user_id=self.anon.user_id) |
|
329 | a1_auth = AuthUser(user_id=self.anon.user_id) | |
330 |
|
330 | |||
331 | self.assertEqual(a1_auth.permissions['repositories_groups'], |
|
331 | self.assertEqual(a1_auth.permissions['repositories_groups'], | |
332 | {u'group1': u'group.none'}) |
|
332 | {u'group1': u'group.none'}) | |
333 |
|
333 | |||
334 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
334 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
335 | self.assertEqual(u1_auth.permissions['repositories_groups'], |
|
335 | self.assertEqual(u1_auth.permissions['repositories_groups'], | |
336 | {u'group1': u'group.read'}) |
|
336 | {u'group1': u'group.read'}) | |
337 |
|
337 | |||
338 | def test_inherited_permissions_from_default_on_user_enabled(self): |
|
338 | def test_inherited_permissions_from_default_on_user_enabled(self): | |
339 | user_model = UserModel() |
|
339 | user_model = UserModel() | |
340 | # enable fork and create on default user |
|
340 | # enable fork and create on default user | |
341 | usr = 'default' |
|
341 | usr = 'default' | |
342 | user_model.revoke_perm(usr, 'hg.create.none') |
|
342 | user_model.revoke_perm(usr, 'hg.create.none') | |
343 | user_model.grant_perm(usr, 'hg.create.repository') |
|
343 | user_model.grant_perm(usr, 'hg.create.repository') | |
344 | user_model.revoke_perm(usr, 'hg.fork.none') |
|
344 | user_model.revoke_perm(usr, 'hg.fork.none') | |
345 | user_model.grant_perm(usr, 'hg.fork.repository') |
|
345 | user_model.grant_perm(usr, 'hg.fork.repository') | |
346 | # make sure inherit flag is turned on |
|
346 | # make sure inherit flag is turned on | |
347 | self.u1.inherit_default_permissions = True |
|
347 | self.u1.inherit_default_permissions = True | |
348 | Session().commit() |
|
348 | Session().commit() | |
349 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
349 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
350 | # this user will have inherited permissions from default user |
|
350 | # this user will have inherited permissions from default user | |
351 | self.assertEqual(u1_auth.permissions['global'], |
|
351 | self.assertEqual(u1_auth.permissions['global'], | |
352 | set(['hg.create.repository', 'hg.fork.repository', |
|
352 | set(['hg.create.repository', 'hg.fork.repository', | |
353 | 'hg.register.manual_activate', |
|
353 | 'hg.register.manual_activate', | |
|
354 | 'hg.extern_activate.auto', | |||
354 | 'repository.read', 'group.read', |
|
355 | 'repository.read', 'group.read', | |
355 | 'usergroup.read'])) |
|
356 | 'usergroup.read'])) | |
356 |
|
357 | |||
357 | def test_inherited_permissions_from_default_on_user_disabled(self): |
|
358 | def test_inherited_permissions_from_default_on_user_disabled(self): | |
358 | user_model = UserModel() |
|
359 | user_model = UserModel() | |
359 | # disable fork and create on default user |
|
360 | # disable fork and create on default user | |
360 | usr = 'default' |
|
361 | usr = 'default' | |
361 | user_model.revoke_perm(usr, 'hg.create.repository') |
|
362 | user_model.revoke_perm(usr, 'hg.create.repository') | |
362 | user_model.grant_perm(usr, 'hg.create.none') |
|
363 | user_model.grant_perm(usr, 'hg.create.none') | |
363 | user_model.revoke_perm(usr, 'hg.fork.repository') |
|
364 | user_model.revoke_perm(usr, 'hg.fork.repository') | |
364 | user_model.grant_perm(usr, 'hg.fork.none') |
|
365 | user_model.grant_perm(usr, 'hg.fork.none') | |
365 | # make sure inherit flag is turned on |
|
366 | # make sure inherit flag is turned on | |
366 | self.u1.inherit_default_permissions = True |
|
367 | self.u1.inherit_default_permissions = True | |
367 | Session().commit() |
|
368 | Session().commit() | |
368 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
369 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
369 | # this user will have inherited permissions from default user |
|
370 | # this user will have inherited permissions from default user | |
370 | self.assertEqual(u1_auth.permissions['global'], |
|
371 | self.assertEqual(u1_auth.permissions['global'], | |
371 | set(['hg.create.none', 'hg.fork.none', |
|
372 | set(['hg.create.none', 'hg.fork.none', | |
372 | 'hg.register.manual_activate', |
|
373 | 'hg.register.manual_activate', | |
|
374 | 'hg.extern_activate.auto', | |||
373 | 'repository.read', 'group.read', |
|
375 | 'repository.read', 'group.read', | |
374 | 'usergroup.read'])) |
|
376 | 'usergroup.read'])) | |
375 |
|
377 | |||
376 | def test_non_inherited_permissions_from_default_on_user_enabled(self): |
|
378 | def test_non_inherited_permissions_from_default_on_user_enabled(self): | |
377 | user_model = UserModel() |
|
379 | user_model = UserModel() | |
378 | # enable fork and create on default user |
|
380 | # enable fork and create on default user | |
379 | usr = 'default' |
|
381 | usr = 'default' | |
380 | user_model.revoke_perm(usr, 'hg.create.none') |
|
382 | user_model.revoke_perm(usr, 'hg.create.none') | |
381 | user_model.grant_perm(usr, 'hg.create.repository') |
|
383 | user_model.grant_perm(usr, 'hg.create.repository') | |
382 | user_model.revoke_perm(usr, 'hg.fork.none') |
|
384 | user_model.revoke_perm(usr, 'hg.fork.none') | |
383 | user_model.grant_perm(usr, 'hg.fork.repository') |
|
385 | user_model.grant_perm(usr, 'hg.fork.repository') | |
384 |
|
386 | |||
385 | #disable global perms on specific user |
|
387 | #disable global perms on specific user | |
386 | user_model.revoke_perm(self.u1, 'hg.create.repository') |
|
388 | user_model.revoke_perm(self.u1, 'hg.create.repository') | |
387 | user_model.grant_perm(self.u1, 'hg.create.none') |
|
389 | user_model.grant_perm(self.u1, 'hg.create.none') | |
388 | user_model.revoke_perm(self.u1, 'hg.fork.repository') |
|
390 | user_model.revoke_perm(self.u1, 'hg.fork.repository') | |
389 | user_model.grant_perm(self.u1, 'hg.fork.none') |
|
391 | user_model.grant_perm(self.u1, 'hg.fork.none') | |
390 |
|
392 | |||
391 | # make sure inherit flag is turned off |
|
393 | # make sure inherit flag is turned off | |
392 | self.u1.inherit_default_permissions = False |
|
394 | self.u1.inherit_default_permissions = False | |
393 | Session().commit() |
|
395 | Session().commit() | |
394 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
396 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
395 | # this user will have non inherited permissions from he's |
|
397 | # this user will have non inherited permissions from he's | |
396 | # explicitly set permissions |
|
398 | # explicitly set permissions | |
397 | self.assertEqual(u1_auth.permissions['global'], |
|
399 | self.assertEqual(u1_auth.permissions['global'], | |
398 | set(['hg.create.none', 'hg.fork.none', |
|
400 | set(['hg.create.none', 'hg.fork.none', | |
399 | 'hg.register.manual_activate', |
|
401 | 'hg.register.manual_activate', | |
|
402 | 'hg.extern_activate.auto', | |||
400 | 'repository.read', 'group.read', |
|
403 | 'repository.read', 'group.read', | |
401 | 'usergroup.read'])) |
|
404 | 'usergroup.read'])) | |
402 |
|
405 | |||
403 | def test_non_inherited_permissions_from_default_on_user_disabled(self): |
|
406 | def test_non_inherited_permissions_from_default_on_user_disabled(self): | |
404 | user_model = UserModel() |
|
407 | user_model = UserModel() | |
405 | # disable fork and create on default user |
|
408 | # disable fork and create on default user | |
406 | usr = 'default' |
|
409 | usr = 'default' | |
407 | user_model.revoke_perm(usr, 'hg.create.repository') |
|
410 | user_model.revoke_perm(usr, 'hg.create.repository') | |
408 | user_model.grant_perm(usr, 'hg.create.none') |
|
411 | user_model.grant_perm(usr, 'hg.create.none') | |
409 | user_model.revoke_perm(usr, 'hg.fork.repository') |
|
412 | user_model.revoke_perm(usr, 'hg.fork.repository') | |
410 | user_model.grant_perm(usr, 'hg.fork.none') |
|
413 | user_model.grant_perm(usr, 'hg.fork.none') | |
411 |
|
414 | |||
412 | #enable global perms on specific user |
|
415 | #enable global perms on specific user | |
413 | user_model.revoke_perm(self.u1, 'hg.create.none') |
|
416 | user_model.revoke_perm(self.u1, 'hg.create.none') | |
414 | user_model.grant_perm(self.u1, 'hg.create.repository') |
|
417 | user_model.grant_perm(self.u1, 'hg.create.repository') | |
415 | user_model.revoke_perm(self.u1, 'hg.fork.none') |
|
418 | user_model.revoke_perm(self.u1, 'hg.fork.none') | |
416 | user_model.grant_perm(self.u1, 'hg.fork.repository') |
|
419 | user_model.grant_perm(self.u1, 'hg.fork.repository') | |
417 |
|
420 | |||
418 | # make sure inherit flag is turned off |
|
421 | # make sure inherit flag is turned off | |
419 | self.u1.inherit_default_permissions = False |
|
422 | self.u1.inherit_default_permissions = False | |
420 | Session().commit() |
|
423 | Session().commit() | |
421 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
424 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
422 | # this user will have non inherited permissions from he's |
|
425 | # this user will have non inherited permissions from he's | |
423 | # explicitly set permissions |
|
426 | # explicitly set permissions | |
424 | self.assertEqual(u1_auth.permissions['global'], |
|
427 | self.assertEqual(u1_auth.permissions['global'], | |
425 | set(['hg.create.repository', 'hg.fork.repository', |
|
428 | set(['hg.create.repository', 'hg.fork.repository', | |
426 | 'hg.register.manual_activate', |
|
429 | 'hg.register.manual_activate', | |
|
430 | 'hg.extern_activate.auto', | |||
427 | 'repository.read', 'group.read', |
|
431 | 'repository.read', 'group.read', | |
428 | 'usergroup.read'])) |
|
432 | 'usergroup.read'])) | |
429 |
|
433 | |||
430 | def test_owner_permissions_doesnot_get_overwritten_by_group(self): |
|
434 | def test_owner_permissions_doesnot_get_overwritten_by_group(self): | |
431 | #create repo as USER, |
|
435 | #create repo as USER, | |
432 | self.test_repo = fixture.create_repo(name='myownrepo', |
|
436 | self.test_repo = fixture.create_repo(name='myownrepo', | |
433 | repo_type='hg', |
|
437 | repo_type='hg', | |
434 | cur_user=self.u1) |
|
438 | cur_user=self.u1) | |
435 |
|
439 | |||
436 | #he has permissions of admin as owner |
|
440 | #he has permissions of admin as owner | |
437 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
441 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
438 | self.assertEqual(u1_auth.permissions['repositories']['myownrepo'], |
|
442 | self.assertEqual(u1_auth.permissions['repositories']['myownrepo'], | |
439 | 'repository.admin') |
|
443 | 'repository.admin') | |
440 | #set his permission as user group, he should still be admin |
|
444 | #set his permission as user group, he should still be admin | |
441 | self.ug1 = fixture.create_user_group('G1') |
|
445 | self.ug1 = fixture.create_user_group('G1') | |
442 | UserGroupModel().add_user_to_group(self.ug1, self.u1) |
|
446 | UserGroupModel().add_user_to_group(self.ug1, self.u1) | |
443 | RepoModel().grant_users_group_permission(self.test_repo, |
|
447 | RepoModel().grant_users_group_permission(self.test_repo, | |
444 | group_name=self.ug1, |
|
448 | group_name=self.ug1, | |
445 | perm='repository.none') |
|
449 | perm='repository.none') | |
446 |
|
450 | |||
447 | Session().commit() |
|
451 | Session().commit() | |
448 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
452 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
449 | self.assertEqual(u1_auth.permissions['repositories']['myownrepo'], |
|
453 | self.assertEqual(u1_auth.permissions['repositories']['myownrepo'], | |
450 | 'repository.admin') |
|
454 | 'repository.admin') | |
451 |
|
455 | |||
452 | def test_owner_permissions_doesnot_get_overwritten_by_others(self): |
|
456 | def test_owner_permissions_doesnot_get_overwritten_by_others(self): | |
453 | #create repo as USER, |
|
457 | #create repo as USER, | |
454 | self.test_repo = fixture.create_repo(name='myownrepo', |
|
458 | self.test_repo = fixture.create_repo(name='myownrepo', | |
455 | repo_type='hg', |
|
459 | repo_type='hg', | |
456 | cur_user=self.u1) |
|
460 | cur_user=self.u1) | |
457 |
|
461 | |||
458 | #he has permissions of admin as owner |
|
462 | #he has permissions of admin as owner | |
459 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
463 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
460 | self.assertEqual(u1_auth.permissions['repositories']['myownrepo'], |
|
464 | self.assertEqual(u1_auth.permissions['repositories']['myownrepo'], | |
461 | 'repository.admin') |
|
465 | 'repository.admin') | |
462 | #set his permission as user, he should still be admin |
|
466 | #set his permission as user, he should still be admin | |
463 | RepoModel().grant_user_permission(self.test_repo, user=self.u1, |
|
467 | RepoModel().grant_user_permission(self.test_repo, user=self.u1, | |
464 | perm='repository.none') |
|
468 | perm='repository.none') | |
465 | Session().commit() |
|
469 | Session().commit() | |
466 | u1_auth = AuthUser(user_id=self.u1.user_id) |
|
470 | u1_auth = AuthUser(user_id=self.u1.user_id) | |
467 | self.assertEqual(u1_auth.permissions['repositories']['myownrepo'], |
|
471 | self.assertEqual(u1_auth.permissions['repositories']['myownrepo'], | |
468 | 'repository.admin') |
|
472 | 'repository.admin') | |
469 |
|
473 | |||
470 | def _test_def_perm_equal(self, user, change_factor=0): |
|
474 | def _test_def_perm_equal(self, user, change_factor=0): | |
471 | perms = UserToPerm.query()\ |
|
475 | perms = UserToPerm.query()\ | |
472 | .filter(UserToPerm.user == user)\ |
|
476 | .filter(UserToPerm.user == user)\ | |
473 | .all() |
|
477 | .all() | |
474 | self.assertEqual(len(perms), |
|
478 | self.assertEqual(len(perms), | |
475 | len(Permission.DEFAULT_USER_PERMISSIONS,)+change_factor, |
|
479 | len(Permission.DEFAULT_USER_PERMISSIONS,)+change_factor, | |
476 | msg=perms) |
|
480 | msg=perms) | |
477 |
|
481 | |||
478 | def test_set_default_permissions(self): |
|
482 | def test_set_default_permissions(self): | |
479 | PermissionModel().create_default_permissions(user=self.u1) |
|
483 | PermissionModel().create_default_permissions(user=self.u1) | |
480 | self._test_def_perm_equal(user=self.u1) |
|
484 | self._test_def_perm_equal(user=self.u1) | |
481 |
|
485 | |||
482 | def test_set_default_permissions_after_one_is_missing(self): |
|
486 | def test_set_default_permissions_after_one_is_missing(self): | |
483 | PermissionModel().create_default_permissions(user=self.u1) |
|
487 | PermissionModel().create_default_permissions(user=self.u1) | |
484 | self._test_def_perm_equal(user=self.u1) |
|
488 | self._test_def_perm_equal(user=self.u1) | |
485 | #now we delete one, it should be re-created after another call |
|
489 | #now we delete one, it should be re-created after another call | |
486 | perms = UserToPerm.query()\ |
|
490 | perms = UserToPerm.query()\ | |
487 | .filter(UserToPerm.user == self.u1)\ |
|
491 | .filter(UserToPerm.user == self.u1)\ | |
488 | .all() |
|
492 | .all() | |
489 | Session().delete(perms[0]) |
|
493 | Session().delete(perms[0]) | |
490 | Session().commit() |
|
494 | Session().commit() | |
491 |
|
495 | |||
492 | self._test_def_perm_equal(user=self.u1, change_factor=-1) |
|
496 | self._test_def_perm_equal(user=self.u1, change_factor=-1) | |
493 |
|
497 | |||
494 | #create missing one ! |
|
498 | #create missing one ! | |
495 | PermissionModel().create_default_permissions(user=self.u1) |
|
499 | PermissionModel().create_default_permissions(user=self.u1) | |
496 | self._test_def_perm_equal(user=self.u1) |
|
500 | self._test_def_perm_equal(user=self.u1) | |
497 |
|
501 | |||
498 | @parameterized.expand([ |
|
502 | @parameterized.expand([ | |
499 | ('repository.read', 'repository.none'), |
|
503 | ('repository.read', 'repository.none'), | |
500 | ('group.read', 'group.none'), |
|
504 | ('group.read', 'group.none'), | |
501 | ('usergroup.read', 'usergroup.none'), |
|
505 | ('usergroup.read', 'usergroup.none'), | |
502 | ('hg.create.repository', 'hg.create.none'), |
|
506 | ('hg.create.repository', 'hg.create.none'), | |
503 | ('hg.fork.repository', 'hg.fork.none'), |
|
507 | ('hg.fork.repository', 'hg.fork.none'), | |
504 | ('hg.register.manual_activate', 'hg.register.auto_activate',) |
|
508 | ('hg.register.manual_activate', 'hg.register.auto_activate',) | |
505 | ]) |
|
509 | ]) | |
506 | def test_set_default_permissions_after_modification(self, perm, modify_to): |
|
510 | def test_set_default_permissions_after_modification(self, perm, modify_to): | |
507 | PermissionModel().create_default_permissions(user=self.u1) |
|
511 | PermissionModel().create_default_permissions(user=self.u1) | |
508 | self._test_def_perm_equal(user=self.u1) |
|
512 | self._test_def_perm_equal(user=self.u1) | |
509 |
|
513 | |||
510 | old = Permission.get_by_key(perm) |
|
514 | old = Permission.get_by_key(perm) | |
511 | new = Permission.get_by_key(modify_to) |
|
515 | new = Permission.get_by_key(modify_to) | |
512 | self.assertNotEqual(old, None) |
|
516 | self.assertNotEqual(old, None) | |
513 | self.assertNotEqual(new, None) |
|
517 | self.assertNotEqual(new, None) | |
514 |
|
518 | |||
515 | #now modify permissions |
|
519 | #now modify permissions | |
516 | p = UserToPerm.query()\ |
|
520 | p = UserToPerm.query()\ | |
517 | .filter(UserToPerm.user == self.u1)\ |
|
521 | .filter(UserToPerm.user == self.u1)\ | |
518 | .filter(UserToPerm.permission == old)\ |
|
522 | .filter(UserToPerm.permission == old)\ | |
519 | .one() |
|
523 | .one() | |
520 | p.permission = new |
|
524 | p.permission = new | |
521 | Session().add(p) |
|
525 | Session().add(p) | |
522 | Session().commit() |
|
526 | Session().commit() | |
523 |
|
527 | |||
524 | PermissionModel().create_default_permissions(user=self.u1) |
|
528 | PermissionModel().create_default_permissions(user=self.u1) | |
525 | self._test_def_perm_equal(user=self.u1) |
|
529 | self._test_def_perm_equal(user=self.u1) |
General Comments 0
You need to be logged in to leave comments.
Login now