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