##// END OF EJS Templates
fixed broken API tests after excluding default user...
marcink -
r3803:e2ebd880 beta
parent child Browse files
Show More
@@ -1,1310 +1,1312 b''
1 1 from __future__ import with_statement
2 2 import random
3 3 import mock
4 4
5 5 from rhodecode.tests import *
6 6 from rhodecode.tests.fixture import Fixture
7 7 from rhodecode.lib.compat import json
8 8 from rhodecode.lib.auth import AuthUser
9 9 from rhodecode.model.user import UserModel
10 10 from rhodecode.model.users_group import UserGroupModel
11 11 from rhodecode.model.repo import RepoModel
12 12 from rhodecode.model.meta import Session
13 13 from rhodecode.model.scm import ScmModel
14 from rhodecode.model.db import Repository
14 from rhodecode.model.db import Repository, User
15 15
16 16
17 17 API_URL = '/_admin/api'
18 18 TEST_USER_GROUP = 'test_users_group'
19 19
20 20 fixture = Fixture()
21 21
22 22
23 23 def _build_data(apikey, method, **kw):
24 24 """
25 25 Builds API data with given random ID
26 26
27 27 :param random_id:
28 28 :type random_id:
29 29 """
30 30 random_id = random.randrange(1, 9999)
31 31 return random_id, json.dumps({
32 32 "id": random_id,
33 33 "api_key": apikey,
34 34 "method": method,
35 35 "args": kw
36 36 })
37 37
38 38 jsonify = lambda obj: json.loads(json.dumps(obj))
39 39
40 40
41 41 def crash(*args, **kwargs):
42 42 raise Exception('Total Crash !')
43 43
44 44
45 45 def api_call(test_obj, params):
46 46 response = test_obj.app.post(API_URL, content_type='application/json',
47 47 params=params)
48 48 return response
49 49
50 50
51 51 ## helpers
52 52 def make_users_group(name=TEST_USER_GROUP):
53 53 gr = fixture.create_user_group(name, cur_user=TEST_USER_ADMIN_LOGIN)
54 54 UserGroupModel().add_user_to_group(users_group=gr,
55 55 user=TEST_USER_ADMIN_LOGIN)
56 56 Session().commit()
57 57 return gr
58 58
59 59
60 60 def destroy_users_group(name=TEST_USER_GROUP):
61 61 UserGroupModel().delete(users_group=name, force=True)
62 62 Session().commit()
63 63
64 64
65 65 class BaseTestApi(object):
66 66 REPO = None
67 67 REPO_TYPE = None
68 68
69 69 @classmethod
70 70 def setUpClass(self):
71 71 self.usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
72 72 self.apikey = self.usr.api_key
73 73 self.test_user = UserModel().create_or_update(
74 74 username='test-api',
75 75 password='test',
76 76 email='test@api.rhodecode.org',
77 77 firstname='first',
78 78 lastname='last'
79 79 )
80 80 Session().commit()
81 81 self.TEST_USER_LOGIN = self.test_user.username
82 82 self.apikey_regular = self.test_user.api_key
83 83
84 84 @classmethod
85 85 def teardownClass(self):
86 86 pass
87 87
88 88 def setUp(self):
89 89 self.maxDiff = None
90 90 make_users_group()
91 91
92 92 def tearDown(self):
93 93 destroy_users_group()
94 94
95 95 def _compare_ok(self, id_, expected, given):
96 96 expected = jsonify({
97 97 'id': id_,
98 98 'error': None,
99 99 'result': expected
100 100 })
101 101 given = json.loads(given)
102 102 self.assertEqual(expected, given)
103 103
104 104 def _compare_error(self, id_, expected, given):
105 105 expected = jsonify({
106 106 'id': id_,
107 107 'error': expected,
108 108 'result': None
109 109 })
110 110 given = json.loads(given)
111 111 self.assertEqual(expected, given)
112 112
113 113 # def test_Optional(self):
114 114 # from rhodecode.controllers.api.api import Optional
115 115 # option1 = Optional(None)
116 116 # self.assertEqual('<Optional:%s>' % None, repr(option1))
117 117 #
118 118 # self.assertEqual(1, Optional.extract(Optional(1)))
119 119 # self.assertEqual('trololo', Optional.extract('trololo'))
120 120
121 121 def test_api_wrong_key(self):
122 122 id_, params = _build_data('trololo', 'get_user')
123 123 response = api_call(self, params)
124 124
125 125 expected = 'Invalid API KEY'
126 126 self._compare_error(id_, expected, given=response.body)
127 127
128 128 def test_api_missing_non_optional_param(self):
129 129 id_, params = _build_data(self.apikey, 'get_repo')
130 130 response = api_call(self, params)
131 131
132 132 expected = 'Missing non optional `repoid` arg in JSON DATA'
133 133 self._compare_error(id_, expected, given=response.body)
134 134
135 135 def test_api_missing_non_optional_param_args_null(self):
136 136 id_, params = _build_data(self.apikey, 'get_repo')
137 137 params = params.replace('"args": {}', '"args": null')
138 138 response = api_call(self, params)
139 139
140 140 expected = 'Missing non optional `repoid` arg in JSON DATA'
141 141 self._compare_error(id_, expected, given=response.body)
142 142
143 143 def test_api_missing_non_optional_param_args_bad(self):
144 144 id_, params = _build_data(self.apikey, 'get_repo')
145 145 params = params.replace('"args": {}', '"args": 1')
146 146 response = api_call(self, params)
147 147
148 148 expected = 'Missing non optional `repoid` arg in JSON DATA'
149 149 self._compare_error(id_, expected, given=response.body)
150 150
151 151 def test_api_args_is_null(self):
152 152 id_, params = _build_data(self.apikey, 'get_users',)
153 153 params = params.replace('"args": {}', '"args": null')
154 154 response = api_call(self, params)
155 155 self.assertEqual(response.status, '200 OK')
156 156
157 157 def test_api_args_is_bad(self):
158 158 id_, params = _build_data(self.apikey, 'get_users',)
159 159 params = params.replace('"args": {}', '"args": 1')
160 160 response = api_call(self, params)
161 161 self.assertEqual(response.status, '200 OK')
162 162
163 163 def test_api_get_users(self):
164 164 id_, params = _build_data(self.apikey, 'get_users',)
165 165 response = api_call(self, params)
166 166 ret_all = []
167 for usr in UserModel().get_all():
167 _users = User.query().filter(User.username != User.DEFAULT_USER)\
168 .order_by(User.username).all()
169 for usr in _users:
168 170 ret = usr.get_api_data()
169 171 ret_all.append(jsonify(ret))
170 172 expected = ret_all
171 173 self._compare_ok(id_, expected, given=response.body)
172 174
173 175 def test_api_get_user(self):
174 176 id_, params = _build_data(self.apikey, 'get_user',
175 177 userid=TEST_USER_ADMIN_LOGIN)
176 178 response = api_call(self, params)
177 179
178 180 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
179 181 ret = usr.get_api_data()
180 182 ret['permissions'] = AuthUser(usr.user_id).permissions
181 183
182 184 expected = ret
183 185 self._compare_ok(id_, expected, given=response.body)
184 186
185 187 def test_api_get_user_that_does_not_exist(self):
186 188 id_, params = _build_data(self.apikey, 'get_user',
187 189 userid='trololo')
188 190 response = api_call(self, params)
189 191
190 192 expected = "user `%s` does not exist" % 'trololo'
191 193 self._compare_error(id_, expected, given=response.body)
192 194
193 195 def test_api_get_user_without_giving_userid(self):
194 196 id_, params = _build_data(self.apikey, 'get_user')
195 197 response = api_call(self, params)
196 198
197 199 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
198 200 ret = usr.get_api_data()
199 201 ret['permissions'] = AuthUser(usr.user_id).permissions
200 202
201 203 expected = ret
202 204 self._compare_ok(id_, expected, given=response.body)
203 205
204 206 def test_api_get_user_without_giving_userid_non_admin(self):
205 207 id_, params = _build_data(self.apikey_regular, 'get_user')
206 208 response = api_call(self, params)
207 209
208 210 usr = UserModel().get_by_username(self.TEST_USER_LOGIN)
209 211 ret = usr.get_api_data()
210 212 ret['permissions'] = AuthUser(usr.user_id).permissions
211 213
212 214 expected = ret
213 215 self._compare_ok(id_, expected, given=response.body)
214 216
215 217 def test_api_get_user_with_giving_userid_non_admin(self):
216 218 id_, params = _build_data(self.apikey_regular, 'get_user',
217 219 userid=self.TEST_USER_LOGIN)
218 220 response = api_call(self, params)
219 221
220 222 expected = 'userid is not the same as your user'
221 223 self._compare_error(id_, expected, given=response.body)
222 224
223 225 def test_api_pull(self):
224 226 #TODO: issues with rhodecode_extras here.. not sure why !
225 227 pass
226 228
227 229 # repo_name = 'test_pull'
228 230 # r = fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
229 231 # r.clone_uri = TEST_self.REPO
230 232 # Session.add(r)
231 233 # Session.commit()
232 234 #
233 235 # id_, params = _build_data(self.apikey, 'pull',
234 236 # repoid=repo_name,)
235 237 # response = self.app.post(API_URL, content_type='application/json',
236 238 # params=params)
237 239 #
238 240 # expected = 'Pulled from `%s`' % repo_name
239 241 # self._compare_ok(id_, expected, given=response.body)
240 242 #
241 243 # fixture.destroy_repo(repo_name)
242 244
243 245 def test_api_pull_error(self):
244 246 id_, params = _build_data(self.apikey, 'pull',
245 247 repoid=self.REPO,)
246 248 response = api_call(self, params)
247 249
248 250 expected = 'Unable to pull changes from `%s`' % self.REPO
249 251 self._compare_error(id_, expected, given=response.body)
250 252
251 253 def test_api_rescan_repos(self):
252 254 id_, params = _build_data(self.apikey, 'rescan_repos')
253 255 response = api_call(self, params)
254 256
255 257 expected = {'added': [], 'removed': []}
256 258 self._compare_ok(id_, expected, given=response.body)
257 259
258 260 @mock.patch.object(ScmModel, 'repo_scan', crash)
259 261 def test_api_rescann_error(self):
260 262 id_, params = _build_data(self.apikey, 'rescan_repos',)
261 263 response = api_call(self, params)
262 264
263 265 expected = 'Error occurred during rescan repositories action'
264 266 self._compare_error(id_, expected, given=response.body)
265 267
266 268 def test_api_invalidate_cache(self):
267 269 repo = RepoModel().get_by_repo_name(self.REPO)
268 270 repo.scm_instance_cached() # seed cache
269 271
270 272 id_, params = _build_data(self.apikey, 'invalidate_cache',
271 273 repoid=self.REPO)
272 274 response = api_call(self, params)
273 275
274 276 expected = ("Caches of repository `%s` was invalidated" % (self.REPO))
275 277 self._compare_ok(id_, expected, given=response.body)
276 278
277 279 @mock.patch.object(ScmModel, 'mark_for_invalidation', crash)
278 280 def test_api_invalidate_cache_error(self):
279 281 id_, params = _build_data(self.apikey, 'invalidate_cache',
280 282 repoid=self.REPO)
281 283 response = api_call(self, params)
282 284
283 285 expected = 'Error occurred during cache invalidation action'
284 286 self._compare_error(id_, expected, given=response.body)
285 287
286 288 def test_api_lock_repo_lock_aquire(self):
287 289 id_, params = _build_data(self.apikey, 'lock',
288 290 userid=TEST_USER_ADMIN_LOGIN,
289 291 repoid=self.REPO,
290 292 locked=True)
291 293 response = api_call(self, params)
292 294 expected = ('User `%s` set lock state for repo `%s` to `%s`'
293 295 % (TEST_USER_ADMIN_LOGIN, self.REPO, True))
294 296 self._compare_ok(id_, expected, given=response.body)
295 297
296 298 def test_api_lock_repo_lock_aquire_by_non_admin(self):
297 299 repo_name = 'api_delete_me'
298 300 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE,
299 301 cur_user=self.TEST_USER_LOGIN)
300 302 try:
301 303 id_, params = _build_data(self.apikey_regular, 'lock',
302 304 repoid=repo_name,
303 305 locked=True)
304 306 response = api_call(self, params)
305 307 expected = ('User `%s` set lock state for repo `%s` to `%s`'
306 308 % (self.TEST_USER_LOGIN, repo_name, True))
307 309 self._compare_ok(id_, expected, given=response.body)
308 310 finally:
309 311 fixture.destroy_repo(repo_name)
310 312
311 313 def test_api_lock_repo_lock_aquire_non_admin_with_userid(self):
312 314 repo_name = 'api_delete_me'
313 315 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE,
314 316 cur_user=self.TEST_USER_LOGIN)
315 317 try:
316 318 id_, params = _build_data(self.apikey_regular, 'lock',
317 319 userid=TEST_USER_ADMIN_LOGIN,
318 320 repoid=repo_name,
319 321 locked=True)
320 322 response = api_call(self, params)
321 323 expected = 'userid is not the same as your user'
322 324 self._compare_error(id_, expected, given=response.body)
323 325 finally:
324 326 fixture.destroy_repo(repo_name)
325 327
326 328 def test_api_lock_repo_lock_aquire_non_admin_not_his_repo(self):
327 329 id_, params = _build_data(self.apikey_regular, 'lock',
328 330 repoid=self.REPO,
329 331 locked=True)
330 332 response = api_call(self, params)
331 333 expected = 'repository `%s` does not exist' % (self.REPO)
332 334 self._compare_error(id_, expected, given=response.body)
333 335
334 336 def test_api_lock_repo_lock_release(self):
335 337 id_, params = _build_data(self.apikey, 'lock',
336 338 userid=TEST_USER_ADMIN_LOGIN,
337 339 repoid=self.REPO,
338 340 locked=False)
339 341 response = api_call(self, params)
340 342 expected = ('User `%s` set lock state for repo `%s` to `%s`'
341 343 % (TEST_USER_ADMIN_LOGIN, self.REPO, False))
342 344 self._compare_ok(id_, expected, given=response.body)
343 345
344 346 def test_api_lock_repo_lock_aquire_optional_userid(self):
345 347 id_, params = _build_data(self.apikey, 'lock',
346 348 repoid=self.REPO,
347 349 locked=True)
348 350 response = api_call(self, params)
349 351 expected = ('User `%s` set lock state for repo `%s` to `%s`'
350 352 % (TEST_USER_ADMIN_LOGIN, self.REPO, True))
351 353 self._compare_ok(id_, expected, given=response.body)
352 354
353 355 def test_api_lock_repo_lock_optional_locked(self):
354 356 from rhodecode.lib.utils2 import time_to_datetime
355 357 _locked_since = json.dumps(time_to_datetime(Repository\
356 358 .get_by_repo_name(self.REPO).locked[1]))
357 359 id_, params = _build_data(self.apikey, 'lock',
358 360 repoid=self.REPO)
359 361 response = api_call(self, params)
360 362 expected = ('Repo `%s` locked by `%s`. Locked=`True`. Locked since: `%s`'
361 363 % (self.REPO, TEST_USER_ADMIN_LOGIN, _locked_since))
362 364 self._compare_ok(id_, expected, given=response.body)
363 365
364 366 @mock.patch.object(Repository, 'lock', crash)
365 367 def test_api_lock_error(self):
366 368 id_, params = _build_data(self.apikey, 'lock',
367 369 userid=TEST_USER_ADMIN_LOGIN,
368 370 repoid=self.REPO,
369 371 locked=True)
370 372 response = api_call(self, params)
371 373
372 374 expected = 'Error occurred locking repository `%s`' % self.REPO
373 375 self._compare_error(id_, expected, given=response.body)
374 376
375 377 def test_api_get_locks_regular_user(self):
376 378 id_, params = _build_data(self.apikey_regular, 'get_locks')
377 379 response = api_call(self, params)
378 380 expected = []
379 381 self._compare_ok(id_, expected, given=response.body)
380 382
381 383 def test_api_get_locks_with_userid_regular_user(self):
382 384 id_, params = _build_data(self.apikey_regular, 'get_locks',
383 385 userid=TEST_USER_ADMIN_LOGIN)
384 386 response = api_call(self, params)
385 387 expected = 'userid is not the same as your user'
386 388 self._compare_error(id_, expected, given=response.body)
387 389
388 390 def test_api_get_locks(self):
389 391 id_, params = _build_data(self.apikey, 'get_locks')
390 392 response = api_call(self, params)
391 393 expected = []
392 394 self._compare_ok(id_, expected, given=response.body)
393 395
394 396 def test_api_get_locks_with_userid(self):
395 397 id_, params = _build_data(self.apikey, 'get_locks',
396 398 userid=TEST_USER_REGULAR_LOGIN)
397 399 response = api_call(self, params)
398 400 expected = []
399 401 self._compare_ok(id_, expected, given=response.body)
400 402
401 403 def test_api_create_existing_user(self):
402 404 id_, params = _build_data(self.apikey, 'create_user',
403 405 username=TEST_USER_ADMIN_LOGIN,
404 406 email='test@foo.com',
405 407 password='trololo')
406 408 response = api_call(self, params)
407 409
408 410 expected = "user `%s` already exist" % TEST_USER_ADMIN_LOGIN
409 411 self._compare_error(id_, expected, given=response.body)
410 412
411 413 def test_api_create_user_with_existing_email(self):
412 414 id_, params = _build_data(self.apikey, 'create_user',
413 415 username=TEST_USER_ADMIN_LOGIN + 'new',
414 416 email=TEST_USER_REGULAR_EMAIL,
415 417 password='trololo')
416 418 response = api_call(self, params)
417 419
418 420 expected = "email `%s` already exist" % TEST_USER_REGULAR_EMAIL
419 421 self._compare_error(id_, expected, given=response.body)
420 422
421 423 def test_api_create_user(self):
422 424 username = 'test_new_api_user'
423 425 email = username + "@foo.com"
424 426
425 427 id_, params = _build_data(self.apikey, 'create_user',
426 428 username=username,
427 429 email=email,
428 430 password='trololo')
429 431 response = api_call(self, params)
430 432
431 433 usr = UserModel().get_by_username(username)
432 434 ret = dict(
433 435 msg='created new user `%s`' % username,
434 436 user=jsonify(usr.get_api_data())
435 437 )
436 438
437 439 expected = ret
438 440 self._compare_ok(id_, expected, given=response.body)
439 441
440 442 UserModel().delete(usr.user_id)
441 443 Session().commit()
442 444
443 445 @mock.patch.object(UserModel, 'create_or_update', crash)
444 446 def test_api_create_user_when_exception_happened(self):
445 447
446 448 username = 'test_new_api_user'
447 449 email = username + "@foo.com"
448 450
449 451 id_, params = _build_data(self.apikey, 'create_user',
450 452 username=username,
451 453 email=email,
452 454 password='trololo')
453 455 response = api_call(self, params)
454 456 expected = 'failed to create user `%s`' % username
455 457 self._compare_error(id_, expected, given=response.body)
456 458
457 459 def test_api_delete_user(self):
458 460 usr = UserModel().create_or_update(username=u'test_user',
459 461 password=u'qweqwe',
460 462 email=u'u232@rhodecode.org',
461 463 firstname=u'u1', lastname=u'u1')
462 464 Session().commit()
463 465 username = usr.username
464 466 email = usr.email
465 467 usr_id = usr.user_id
466 468 ## DELETE THIS USER NOW
467 469
468 470 id_, params = _build_data(self.apikey, 'delete_user',
469 471 userid=username,)
470 472 response = api_call(self, params)
471 473
472 474 ret = {'msg': 'deleted user ID:%s %s' % (usr_id, username),
473 475 'user': None}
474 476 expected = ret
475 477 self._compare_ok(id_, expected, given=response.body)
476 478
477 479 @mock.patch.object(UserModel, 'delete', crash)
478 480 def test_api_delete_user_when_exception_happened(self):
479 481 usr = UserModel().create_or_update(username=u'test_user',
480 482 password=u'qweqwe',
481 483 email=u'u232@rhodecode.org',
482 484 firstname=u'u1', lastname=u'u1')
483 485 Session().commit()
484 486 username = usr.username
485 487
486 488 id_, params = _build_data(self.apikey, 'delete_user',
487 489 userid=username,)
488 490 response = api_call(self, params)
489 491 ret = 'failed to delete ID:%s %s' % (usr.user_id,
490 492 usr.username)
491 493 expected = ret
492 494 self._compare_error(id_, expected, given=response.body)
493 495
494 496 @parameterized.expand([('firstname', 'new_username'),
495 497 ('lastname', 'new_username'),
496 498 ('email', 'new_username'),
497 499 ('admin', True),
498 500 ('admin', False),
499 501 ('ldap_dn', 'test'),
500 502 ('ldap_dn', None),
501 503 ('active', False),
502 504 ('active', True),
503 505 ('password', 'newpass')
504 506 ])
505 507 def test_api_update_user(self, name, expected):
506 508 usr = UserModel().get_by_username(self.TEST_USER_LOGIN)
507 509 kw = {name: expected,
508 510 'userid': usr.user_id}
509 511 id_, params = _build_data(self.apikey, 'update_user', **kw)
510 512 response = api_call(self, params)
511 513
512 514 ret = {
513 515 'msg': 'updated user ID:%s %s' % (usr.user_id, self.TEST_USER_LOGIN),
514 516 'user': jsonify(UserModel()\
515 517 .get_by_username(self.TEST_USER_LOGIN)\
516 518 .get_api_data())
517 519 }
518 520
519 521 expected = ret
520 522 self._compare_ok(id_, expected, given=response.body)
521 523
522 524 def test_api_update_user_no_changed_params(self):
523 525 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
524 526 ret = jsonify(usr.get_api_data())
525 527 id_, params = _build_data(self.apikey, 'update_user',
526 528 userid=TEST_USER_ADMIN_LOGIN)
527 529
528 530 response = api_call(self, params)
529 531 ret = {
530 532 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN),
531 533 'user': ret
532 534 }
533 535 expected = ret
534 536 self._compare_ok(id_, expected, given=response.body)
535 537
536 538 def test_api_update_user_by_user_id(self):
537 539 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
538 540 ret = jsonify(usr.get_api_data())
539 541 id_, params = _build_data(self.apikey, 'update_user',
540 542 userid=usr.user_id)
541 543
542 544 response = api_call(self, params)
543 545 ret = {
544 546 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN),
545 547 'user': ret
546 548 }
547 549 expected = ret
548 550 self._compare_ok(id_, expected, given=response.body)
549 551
550 552 @mock.patch.object(UserModel, 'update_user', crash)
551 553 def test_api_update_user_when_exception_happens(self):
552 554 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
553 555 ret = jsonify(usr.get_api_data())
554 556 id_, params = _build_data(self.apikey, 'update_user',
555 557 userid=usr.user_id)
556 558
557 559 response = api_call(self, params)
558 560 ret = 'failed to update user `%s`' % usr.user_id
559 561
560 562 expected = ret
561 563 self._compare_error(id_, expected, given=response.body)
562 564
563 565 def test_api_get_repo(self):
564 566 new_group = 'some_new_group'
565 567 make_users_group(new_group)
566 568 RepoModel().grant_users_group_permission(repo=self.REPO,
567 569 group_name=new_group,
568 570 perm='repository.read')
569 571 Session().commit()
570 572 id_, params = _build_data(self.apikey, 'get_repo',
571 573 repoid=self.REPO)
572 574 response = api_call(self, params)
573 575
574 576 repo = RepoModel().get_by_repo_name(self.REPO)
575 577 ret = repo.get_api_data()
576 578
577 579 members = []
578 580 followers = []
579 581 for user in repo.repo_to_perm:
580 582 perm = user.permission.permission_name
581 583 user = user.user
582 584 user_data = user.get_api_data()
583 585 user_data['type'] = "user"
584 586 user_data['permission'] = perm
585 587 members.append(user_data)
586 588
587 589 for users_group in repo.users_group_to_perm:
588 590 perm = users_group.permission.permission_name
589 591 users_group = users_group.users_group
590 592 users_group_data = users_group.get_api_data()
591 593 users_group_data['type'] = "users_group"
592 594 users_group_data['permission'] = perm
593 595 members.append(users_group_data)
594 596
595 597 for user in repo.followers:
596 598 followers.append(user.user.get_api_data())
597 599
598 600 ret['members'] = members
599 601 ret['followers'] = followers
600 602
601 603 expected = ret
602 604 self._compare_ok(id_, expected, given=response.body)
603 605 destroy_users_group(new_group)
604 606
605 607 def test_api_get_repo_by_non_admin(self):
606 608 id_, params = _build_data(self.apikey, 'get_repo',
607 609 repoid=self.REPO)
608 610 response = api_call(self, params)
609 611
610 612 repo = RepoModel().get_by_repo_name(self.REPO)
611 613 ret = repo.get_api_data()
612 614
613 615 members = []
614 616 followers = []
615 617 for user in repo.repo_to_perm:
616 618 perm = user.permission.permission_name
617 619 user = user.user
618 620 user_data = user.get_api_data()
619 621 user_data['type'] = "user"
620 622 user_data['permission'] = perm
621 623 members.append(user_data)
622 624
623 625 for users_group in repo.users_group_to_perm:
624 626 perm = users_group.permission.permission_name
625 627 users_group = users_group.users_group
626 628 users_group_data = users_group.get_api_data()
627 629 users_group_data['type'] = "users_group"
628 630 users_group_data['permission'] = perm
629 631 members.append(users_group_data)
630 632
631 633 for user in repo.followers:
632 634 followers.append(user.user.get_api_data())
633 635
634 636 ret['members'] = members
635 637 ret['followers'] = followers
636 638
637 639 expected = ret
638 640 self._compare_ok(id_, expected, given=response.body)
639 641
640 642 def test_api_get_repo_by_non_admin_no_permission_to_repo(self):
641 643 RepoModel().grant_user_permission(repo=self.REPO,
642 644 user=self.TEST_USER_LOGIN,
643 645 perm='repository.none')
644 646
645 647 id_, params = _build_data(self.apikey_regular, 'get_repo',
646 648 repoid=self.REPO)
647 649 response = api_call(self, params)
648 650
649 651 expected = 'repository `%s` does not exist' % (self.REPO)
650 652 self._compare_error(id_, expected, given=response.body)
651 653
652 654 def test_api_get_repo_that_doesn_not_exist(self):
653 655 id_, params = _build_data(self.apikey, 'get_repo',
654 656 repoid='no-such-repo')
655 657 response = api_call(self, params)
656 658
657 659 ret = 'repository `%s` does not exist' % 'no-such-repo'
658 660 expected = ret
659 661 self._compare_error(id_, expected, given=response.body)
660 662
661 663 def test_api_get_repos(self):
662 664 id_, params = _build_data(self.apikey, 'get_repos')
663 665 response = api_call(self, params)
664 666
665 667 result = []
666 668 for repo in RepoModel().get_all():
667 669 result.append(repo.get_api_data())
668 670 ret = jsonify(result)
669 671
670 672 expected = ret
671 673 self._compare_ok(id_, expected, given=response.body)
672 674
673 675 def test_api_get_repos_non_admin(self):
674 676 id_, params = _build_data(self.apikey_regular, 'get_repos')
675 677 response = api_call(self, params)
676 678
677 679 result = []
678 680 for repo in RepoModel().get_all_user_repos(self.TEST_USER_LOGIN):
679 681 result.append(repo.get_api_data())
680 682 ret = jsonify(result)
681 683
682 684 expected = ret
683 685 self._compare_ok(id_, expected, given=response.body)
684 686
685 687 @parameterized.expand([('all', 'all'),
686 688 ('dirs', 'dirs'),
687 689 ('files', 'files'), ])
688 690 def test_api_get_repo_nodes(self, name, ret_type):
689 691 rev = 'tip'
690 692 path = '/'
691 693 id_, params = _build_data(self.apikey, 'get_repo_nodes',
692 694 repoid=self.REPO, revision=rev,
693 695 root_path=path,
694 696 ret_type=ret_type)
695 697 response = api_call(self, params)
696 698
697 699 # we don't the actual return types here since it's tested somewhere
698 700 # else
699 701 expected = json.loads(response.body)['result']
700 702 self._compare_ok(id_, expected, given=response.body)
701 703
702 704 def test_api_get_repo_nodes_bad_revisions(self):
703 705 rev = 'i-dont-exist'
704 706 path = '/'
705 707 id_, params = _build_data(self.apikey, 'get_repo_nodes',
706 708 repoid=self.REPO, revision=rev,
707 709 root_path=path,)
708 710 response = api_call(self, params)
709 711
710 712 expected = 'failed to get repo: `%s` nodes' % self.REPO
711 713 self._compare_error(id_, expected, given=response.body)
712 714
713 715 def test_api_get_repo_nodes_bad_path(self):
714 716 rev = 'tip'
715 717 path = '/idontexits'
716 718 id_, params = _build_data(self.apikey, 'get_repo_nodes',
717 719 repoid=self.REPO, revision=rev,
718 720 root_path=path,)
719 721 response = api_call(self, params)
720 722
721 723 expected = 'failed to get repo: `%s` nodes' % self.REPO
722 724 self._compare_error(id_, expected, given=response.body)
723 725
724 726 def test_api_get_repo_nodes_bad_ret_type(self):
725 727 rev = 'tip'
726 728 path = '/'
727 729 ret_type = 'error'
728 730 id_, params = _build_data(self.apikey, 'get_repo_nodes',
729 731 repoid=self.REPO, revision=rev,
730 732 root_path=path,
731 733 ret_type=ret_type)
732 734 response = api_call(self, params)
733 735
734 736 expected = 'ret_type must be one of %s' % (['files', 'dirs', 'all'])
735 737 self._compare_error(id_, expected, given=response.body)
736 738
737 739 def test_api_create_repo(self):
738 740 repo_name = 'api-repo'
739 741 id_, params = _build_data(self.apikey, 'create_repo',
740 742 repo_name=repo_name,
741 743 owner=TEST_USER_ADMIN_LOGIN,
742 744 repo_type='hg',
743 745 )
744 746 response = api_call(self, params)
745 747
746 748 repo = RepoModel().get_by_repo_name(repo_name)
747 749 ret = {
748 750 'msg': 'Created new repository `%s`' % repo_name,
749 751 'repo': jsonify(repo.get_api_data())
750 752 }
751 753 expected = ret
752 754 self._compare_ok(id_, expected, given=response.body)
753 755 fixture.destroy_repo(repo_name)
754 756
755 757 def test_api_create_repo_unknown_owner(self):
756 758 repo_name = 'api-repo'
757 759 owner = 'i-dont-exist'
758 760 id_, params = _build_data(self.apikey, 'create_repo',
759 761 repo_name=repo_name,
760 762 owner=owner,
761 763 repo_type='hg',
762 764 )
763 765 response = api_call(self, params)
764 766 expected = 'user `%s` does not exist' % owner
765 767 self._compare_error(id_, expected, given=response.body)
766 768
767 769 def test_api_create_repo_dont_specify_owner(self):
768 770 repo_name = 'api-repo'
769 771 owner = 'i-dont-exist'
770 772 id_, params = _build_data(self.apikey, 'create_repo',
771 773 repo_name=repo_name,
772 774 repo_type='hg',
773 775 )
774 776 response = api_call(self, params)
775 777
776 778 repo = RepoModel().get_by_repo_name(repo_name)
777 779 ret = {
778 780 'msg': 'Created new repository `%s`' % repo_name,
779 781 'repo': jsonify(repo.get_api_data())
780 782 }
781 783 expected = ret
782 784 self._compare_ok(id_, expected, given=response.body)
783 785 fixture.destroy_repo(repo_name)
784 786
785 787 def test_api_create_repo_by_non_admin(self):
786 788 repo_name = 'api-repo'
787 789 owner = 'i-dont-exist'
788 790 id_, params = _build_data(self.apikey_regular, 'create_repo',
789 791 repo_name=repo_name,
790 792 repo_type='hg',
791 793 )
792 794 response = api_call(self, params)
793 795
794 796 repo = RepoModel().get_by_repo_name(repo_name)
795 797 ret = {
796 798 'msg': 'Created new repository `%s`' % repo_name,
797 799 'repo': jsonify(repo.get_api_data())
798 800 }
799 801 expected = ret
800 802 self._compare_ok(id_, expected, given=response.body)
801 803 fixture.destroy_repo(repo_name)
802 804
803 805 def test_api_create_repo_by_non_admin_specify_owner(self):
804 806 repo_name = 'api-repo'
805 807 owner = 'i-dont-exist'
806 808 id_, params = _build_data(self.apikey_regular, 'create_repo',
807 809 repo_name=repo_name,
808 810 repo_type='hg',
809 811 owner=owner
810 812 )
811 813 response = api_call(self, params)
812 814
813 815 expected = 'Only RhodeCode admin can specify `owner` param'
814 816 self._compare_error(id_, expected, given=response.body)
815 817 fixture.destroy_repo(repo_name)
816 818
817 819 def test_api_create_repo_exists(self):
818 820 repo_name = self.REPO
819 821 id_, params = _build_data(self.apikey, 'create_repo',
820 822 repo_name=repo_name,
821 823 owner=TEST_USER_ADMIN_LOGIN,
822 824 repo_type='hg',
823 825 )
824 826 response = api_call(self, params)
825 827 expected = "repo `%s` already exist" % repo_name
826 828 self._compare_error(id_, expected, given=response.body)
827 829
828 830 @mock.patch.object(RepoModel, 'create_repo', crash)
829 831 def test_api_create_repo_exception_occurred(self):
830 832 repo_name = 'api-repo'
831 833 id_, params = _build_data(self.apikey, 'create_repo',
832 834 repo_name=repo_name,
833 835 owner=TEST_USER_ADMIN_LOGIN,
834 836 repo_type='hg',
835 837 )
836 838 response = api_call(self, params)
837 839 expected = 'failed to create repository `%s`' % repo_name
838 840 self._compare_error(id_, expected, given=response.body)
839 841
840 842 def test_api_delete_repo(self):
841 843 repo_name = 'api_delete_me'
842 844 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
843 845
844 846 id_, params = _build_data(self.apikey, 'delete_repo',
845 847 repoid=repo_name,)
846 848 response = api_call(self, params)
847 849
848 850 ret = {
849 851 'msg': 'Deleted repository `%s`' % repo_name,
850 852 'success': True
851 853 }
852 854 expected = ret
853 855 self._compare_ok(id_, expected, given=response.body)
854 856
855 857 def test_api_delete_repo_by_non_admin(self):
856 858 repo_name = 'api_delete_me'
857 859 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE,
858 860 cur_user=self.TEST_USER_LOGIN)
859 861 try:
860 862 id_, params = _build_data(self.apikey_regular, 'delete_repo',
861 863 repoid=repo_name,)
862 864 response = api_call(self, params)
863 865
864 866 ret = {
865 867 'msg': 'Deleted repository `%s`' % repo_name,
866 868 'success': True
867 869 }
868 870 expected = ret
869 871 self._compare_ok(id_, expected, given=response.body)
870 872 finally:
871 873 fixture.destroy_repo(repo_name)
872 874
873 875 def test_api_delete_repo_by_non_admin_no_permission(self):
874 876 repo_name = 'api_delete_me'
875 877 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
876 878 try:
877 879 id_, params = _build_data(self.apikey_regular, 'delete_repo',
878 880 repoid=repo_name,)
879 881 response = api_call(self, params)
880 882 expected = 'repository `%s` does not exist' % (repo_name)
881 883 self._compare_error(id_, expected, given=response.body)
882 884 finally:
883 885 fixture.destroy_repo(repo_name)
884 886
885 887 def test_api_delete_repo_exception_occurred(self):
886 888 repo_name = 'api_delete_me'
887 889 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
888 890 try:
889 891 with mock.patch.object(RepoModel, 'delete', crash):
890 892 id_, params = _build_data(self.apikey, 'delete_repo',
891 893 repoid=repo_name,)
892 894 response = api_call(self, params)
893 895
894 896 expected = 'failed to delete repository `%s`' % repo_name
895 897 self._compare_error(id_, expected, given=response.body)
896 898 finally:
897 899 fixture.destroy_repo(repo_name)
898 900
899 901 def test_api_fork_repo(self):
900 902 fork_name = 'api-repo-fork'
901 903 id_, params = _build_data(self.apikey, 'fork_repo',
902 904 repoid=self.REPO,
903 905 fork_name=fork_name,
904 906 owner=TEST_USER_ADMIN_LOGIN,
905 907 )
906 908 response = api_call(self, params)
907 909
908 910 ret = {
909 911 'msg': 'Created fork of `%s` as `%s`' % (self.REPO,
910 912 fork_name),
911 913 'success': True
912 914 }
913 915 expected = ret
914 916 self._compare_ok(id_, expected, given=response.body)
915 917 fixture.destroy_repo(fork_name)
916 918
917 919 def test_api_fork_repo_non_admin(self):
918 920 fork_name = 'api-repo-fork'
919 921 id_, params = _build_data(self.apikey_regular, 'fork_repo',
920 922 repoid=self.REPO,
921 923 fork_name=fork_name,
922 924 )
923 925 response = api_call(self, params)
924 926
925 927 ret = {
926 928 'msg': 'Created fork of `%s` as `%s`' % (self.REPO,
927 929 fork_name),
928 930 'success': True
929 931 }
930 932 expected = ret
931 933 self._compare_ok(id_, expected, given=response.body)
932 934 fixture.destroy_repo(fork_name)
933 935
934 936 def test_api_fork_repo_non_admin_specify_owner(self):
935 937 fork_name = 'api-repo-fork'
936 938 id_, params = _build_data(self.apikey_regular, 'fork_repo',
937 939 repoid=self.REPO,
938 940 fork_name=fork_name,
939 941 owner=TEST_USER_ADMIN_LOGIN,
940 942 )
941 943 response = api_call(self, params)
942 944 expected = 'Only RhodeCode admin can specify `owner` param'
943 945 self._compare_error(id_, expected, given=response.body)
944 946 fixture.destroy_repo(fork_name)
945 947
946 948 def test_api_fork_repo_non_admin_no_permission_to_fork(self):
947 949 RepoModel().grant_user_permission(repo=self.REPO,
948 950 user=self.TEST_USER_LOGIN,
949 951 perm='repository.none')
950 952 fork_name = 'api-repo-fork'
951 953 id_, params = _build_data(self.apikey_regular, 'fork_repo',
952 954 repoid=self.REPO,
953 955 fork_name=fork_name,
954 956 )
955 957 response = api_call(self, params)
956 958 expected = 'repository `%s` does not exist' % (self.REPO)
957 959 self._compare_error(id_, expected, given=response.body)
958 960 fixture.destroy_repo(fork_name)
959 961
960 962 def test_api_fork_repo_unknown_owner(self):
961 963 fork_name = 'api-repo-fork'
962 964 owner = 'i-dont-exist'
963 965 id_, params = _build_data(self.apikey, 'fork_repo',
964 966 repoid=self.REPO,
965 967 fork_name=fork_name,
966 968 owner=owner,
967 969 )
968 970 response = api_call(self, params)
969 971 expected = 'user `%s` does not exist' % owner
970 972 self._compare_error(id_, expected, given=response.body)
971 973
972 974 def test_api_fork_repo_fork_exists(self):
973 975 fork_name = 'api-repo-fork'
974 976 fixture.create_fork(self.REPO, fork_name)
975 977
976 978 try:
977 979 fork_name = 'api-repo-fork'
978 980
979 981 id_, params = _build_data(self.apikey, 'fork_repo',
980 982 repoid=self.REPO,
981 983 fork_name=fork_name,
982 984 owner=TEST_USER_ADMIN_LOGIN,
983 985 )
984 986 response = api_call(self, params)
985 987
986 988 expected = "fork `%s` already exist" % fork_name
987 989 self._compare_error(id_, expected, given=response.body)
988 990 finally:
989 991 fixture.destroy_repo(fork_name)
990 992
991 993 def test_api_fork_repo_repo_exists(self):
992 994 fork_name = self.REPO
993 995
994 996 id_, params = _build_data(self.apikey, 'fork_repo',
995 997 repoid=self.REPO,
996 998 fork_name=fork_name,
997 999 owner=TEST_USER_ADMIN_LOGIN,
998 1000 )
999 1001 response = api_call(self, params)
1000 1002
1001 1003 expected = "repo `%s` already exist" % fork_name
1002 1004 self._compare_error(id_, expected, given=response.body)
1003 1005
1004 1006 @mock.patch.object(RepoModel, 'create_fork', crash)
1005 1007 def test_api_fork_repo_exception_occurred(self):
1006 1008 fork_name = 'api-repo-fork'
1007 1009 id_, params = _build_data(self.apikey, 'fork_repo',
1008 1010 repoid=self.REPO,
1009 1011 fork_name=fork_name,
1010 1012 owner=TEST_USER_ADMIN_LOGIN,
1011 1013 )
1012 1014 response = api_call(self, params)
1013 1015
1014 1016 expected = 'failed to fork repository `%s` as `%s`' % (self.REPO,
1015 1017 fork_name)
1016 1018 self._compare_error(id_, expected, given=response.body)
1017 1019
1018 1020 def test_api_get_users_group(self):
1019 1021 id_, params = _build_data(self.apikey, 'get_users_group',
1020 1022 usersgroupid=TEST_USER_GROUP)
1021 1023 response = api_call(self, params)
1022 1024
1023 1025 users_group = UserGroupModel().get_group(TEST_USER_GROUP)
1024 1026 members = []
1025 1027 for user in users_group.members:
1026 1028 user = user.user
1027 1029 members.append(user.get_api_data())
1028 1030
1029 1031 ret = users_group.get_api_data()
1030 1032 ret['members'] = members
1031 1033 expected = ret
1032 1034 self._compare_ok(id_, expected, given=response.body)
1033 1035
1034 1036 def test_api_get_users_groups(self):
1035 1037
1036 1038 make_users_group('test_users_group2')
1037 1039
1038 1040 id_, params = _build_data(self.apikey, 'get_users_groups',)
1039 1041 response = api_call(self, params)
1040 1042
1041 1043 expected = []
1042 1044 for gr_name in [TEST_USER_GROUP, 'test_users_group2']:
1043 1045 users_group = UserGroupModel().get_group(gr_name)
1044 1046 ret = users_group.get_api_data()
1045 1047 expected.append(ret)
1046 1048 self._compare_ok(id_, expected, given=response.body)
1047 1049
1048 1050 UserGroupModel().delete(users_group='test_users_group2')
1049 1051 Session().commit()
1050 1052
1051 1053 def test_api_create_users_group(self):
1052 1054 group_name = 'some_new_group'
1053 1055 id_, params = _build_data(self.apikey, 'create_users_group',
1054 1056 group_name=group_name)
1055 1057 response = api_call(self, params)
1056 1058
1057 1059 ret = {
1058 1060 'msg': 'created new user group `%s`' % group_name,
1059 1061 'users_group': jsonify(UserGroupModel()\
1060 1062 .get_by_name(group_name)\
1061 1063 .get_api_data())
1062 1064 }
1063 1065 expected = ret
1064 1066 self._compare_ok(id_, expected, given=response.body)
1065 1067
1066 1068 destroy_users_group(group_name)
1067 1069
1068 1070 def test_api_get_users_group_that_exist(self):
1069 1071 id_, params = _build_data(self.apikey, 'create_users_group',
1070 1072 group_name=TEST_USER_GROUP)
1071 1073 response = api_call(self, params)
1072 1074
1073 1075 expected = "user group `%s` already exist" % TEST_USER_GROUP
1074 1076 self._compare_error(id_, expected, given=response.body)
1075 1077
1076 1078 @mock.patch.object(UserGroupModel, 'create', crash)
1077 1079 def test_api_get_users_group_exception_occurred(self):
1078 1080 group_name = 'exception_happens'
1079 1081 id_, params = _build_data(self.apikey, 'create_users_group',
1080 1082 group_name=group_name)
1081 1083 response = api_call(self, params)
1082 1084
1083 1085 expected = 'failed to create group `%s`' % group_name
1084 1086 self._compare_error(id_, expected, given=response.body)
1085 1087
1086 1088 def test_api_add_user_to_users_group(self):
1087 1089 gr_name = 'test_group'
1088 1090 fixture.create_user_group(gr_name)
1089 1091 id_, params = _build_data(self.apikey, 'add_user_to_users_group',
1090 1092 usersgroupid=gr_name,
1091 1093 userid=TEST_USER_ADMIN_LOGIN)
1092 1094 response = api_call(self, params)
1093 1095
1094 1096 expected = {
1095 1097 'msg': 'added member `%s` to user group `%s`' % (
1096 1098 TEST_USER_ADMIN_LOGIN, gr_name
1097 1099 ),
1098 1100 'success': True}
1099 1101 self._compare_ok(id_, expected, given=response.body)
1100 1102
1101 1103 UserGroupModel().delete(users_group=gr_name)
1102 1104 Session().commit()
1103 1105
1104 1106 def test_api_add_user_to_users_group_that_doesnt_exist(self):
1105 1107 id_, params = _build_data(self.apikey, 'add_user_to_users_group',
1106 1108 usersgroupid='false-group',
1107 1109 userid=TEST_USER_ADMIN_LOGIN)
1108 1110 response = api_call(self, params)
1109 1111
1110 1112 expected = 'user group `%s` does not exist' % 'false-group'
1111 1113 self._compare_error(id_, expected, given=response.body)
1112 1114
1113 1115 @mock.patch.object(UserGroupModel, 'add_user_to_group', crash)
1114 1116 def test_api_add_user_to_users_group_exception_occurred(self):
1115 1117 gr_name = 'test_group'
1116 1118 fixture.create_user_group(gr_name)
1117 1119 id_, params = _build_data(self.apikey, 'add_user_to_users_group',
1118 1120 usersgroupid=gr_name,
1119 1121 userid=TEST_USER_ADMIN_LOGIN)
1120 1122 response = api_call(self, params)
1121 1123
1122 1124 expected = 'failed to add member to user group `%s`' % gr_name
1123 1125 self._compare_error(id_, expected, given=response.body)
1124 1126
1125 1127 UserGroupModel().delete(users_group=gr_name)
1126 1128 Session().commit()
1127 1129
1128 1130 def test_api_remove_user_from_users_group(self):
1129 1131 gr_name = 'test_group_3'
1130 1132 gr = fixture.create_user_group(gr_name)
1131 1133 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN)
1132 1134 Session().commit()
1133 1135 id_, params = _build_data(self.apikey, 'remove_user_from_users_group',
1134 1136 usersgroupid=gr_name,
1135 1137 userid=TEST_USER_ADMIN_LOGIN)
1136 1138 response = api_call(self, params)
1137 1139
1138 1140 expected = {
1139 1141 'msg': 'removed member `%s` from user group `%s`' % (
1140 1142 TEST_USER_ADMIN_LOGIN, gr_name
1141 1143 ),
1142 1144 'success': True}
1143 1145 self._compare_ok(id_, expected, given=response.body)
1144 1146
1145 1147 UserGroupModel().delete(users_group=gr_name)
1146 1148 Session().commit()
1147 1149
1148 1150 @mock.patch.object(UserGroupModel, 'remove_user_from_group', crash)
1149 1151 def test_api_remove_user_from_users_group_exception_occurred(self):
1150 1152 gr_name = 'test_group_3'
1151 1153 gr = fixture.create_user_group(gr_name)
1152 1154 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN)
1153 1155 Session().commit()
1154 1156 id_, params = _build_data(self.apikey, 'remove_user_from_users_group',
1155 1157 usersgroupid=gr_name,
1156 1158 userid=TEST_USER_ADMIN_LOGIN)
1157 1159 response = api_call(self, params)
1158 1160
1159 1161 expected = 'failed to remove member from user group `%s`' % gr_name
1160 1162 self._compare_error(id_, expected, given=response.body)
1161 1163
1162 1164 UserGroupModel().delete(users_group=gr_name)
1163 1165 Session().commit()
1164 1166
1165 1167 @parameterized.expand([('none', 'repository.none'),
1166 1168 ('read', 'repository.read'),
1167 1169 ('write', 'repository.write'),
1168 1170 ('admin', 'repository.admin')])
1169 1171 def test_api_grant_user_permission(self, name, perm):
1170 1172 id_, params = _build_data(self.apikey, 'grant_user_permission',
1171 1173 repoid=self.REPO,
1172 1174 userid=TEST_USER_ADMIN_LOGIN,
1173 1175 perm=perm)
1174 1176 response = api_call(self, params)
1175 1177
1176 1178 ret = {
1177 1179 'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % (
1178 1180 perm, TEST_USER_ADMIN_LOGIN, self.REPO
1179 1181 ),
1180 1182 'success': True
1181 1183 }
1182 1184 expected = ret
1183 1185 self._compare_ok(id_, expected, given=response.body)
1184 1186
1185 1187 def test_api_grant_user_permission_wrong_permission(self):
1186 1188 perm = 'haha.no.permission'
1187 1189 id_, params = _build_data(self.apikey, 'grant_user_permission',
1188 1190 repoid=self.REPO,
1189 1191 userid=TEST_USER_ADMIN_LOGIN,
1190 1192 perm=perm)
1191 1193 response = api_call(self, params)
1192 1194
1193 1195 expected = 'permission `%s` does not exist' % perm
1194 1196 self._compare_error(id_, expected, given=response.body)
1195 1197
1196 1198 @mock.patch.object(RepoModel, 'grant_user_permission', crash)
1197 1199 def test_api_grant_user_permission_exception_when_adding(self):
1198 1200 perm = 'repository.read'
1199 1201 id_, params = _build_data(self.apikey, 'grant_user_permission',
1200 1202 repoid=self.REPO,
1201 1203 userid=TEST_USER_ADMIN_LOGIN,
1202 1204 perm=perm)
1203 1205 response = api_call(self, params)
1204 1206
1205 1207 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % (
1206 1208 TEST_USER_ADMIN_LOGIN, self.REPO
1207 1209 )
1208 1210 self._compare_error(id_, expected, given=response.body)
1209 1211
1210 1212 def test_api_revoke_user_permission(self):
1211 1213 id_, params = _build_data(self.apikey, 'revoke_user_permission',
1212 1214 repoid=self.REPO,
1213 1215 userid=TEST_USER_ADMIN_LOGIN,)
1214 1216 response = api_call(self, params)
1215 1217
1216 1218 expected = {
1217 1219 'msg': 'Revoked perm for user: `%s` in repo: `%s`' % (
1218 1220 TEST_USER_ADMIN_LOGIN, self.REPO
1219 1221 ),
1220 1222 'success': True
1221 1223 }
1222 1224 self._compare_ok(id_, expected, given=response.body)
1223 1225
1224 1226 @mock.patch.object(RepoModel, 'revoke_user_permission', crash)
1225 1227 def test_api_revoke_user_permission_exception_when_adding(self):
1226 1228 id_, params = _build_data(self.apikey, 'revoke_user_permission',
1227 1229 repoid=self.REPO,
1228 1230 userid=TEST_USER_ADMIN_LOGIN,)
1229 1231 response = api_call(self, params)
1230 1232
1231 1233 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % (
1232 1234 TEST_USER_ADMIN_LOGIN, self.REPO
1233 1235 )
1234 1236 self._compare_error(id_, expected, given=response.body)
1235 1237
1236 1238 @parameterized.expand([('none', 'repository.none'),
1237 1239 ('read', 'repository.read'),
1238 1240 ('write', 'repository.write'),
1239 1241 ('admin', 'repository.admin')])
1240 1242 def test_api_grant_users_group_permission(self, name, perm):
1241 1243 id_, params = _build_data(self.apikey, 'grant_users_group_permission',
1242 1244 repoid=self.REPO,
1243 1245 usersgroupid=TEST_USER_GROUP,
1244 1246 perm=perm)
1245 1247 response = api_call(self, params)
1246 1248
1247 1249 ret = {
1248 1250 'msg': 'Granted perm: `%s` for user group: `%s` in repo: `%s`' % (
1249 1251 perm, TEST_USER_GROUP, self.REPO
1250 1252 ),
1251 1253 'success': True
1252 1254 }
1253 1255 expected = ret
1254 1256 self._compare_ok(id_, expected, given=response.body)
1255 1257
1256 1258 def test_api_grant_users_group_permission_wrong_permission(self):
1257 1259 perm = 'haha.no.permission'
1258 1260 id_, params = _build_data(self.apikey, 'grant_users_group_permission',
1259 1261 repoid=self.REPO,
1260 1262 usersgroupid=TEST_USER_GROUP,
1261 1263 perm=perm)
1262 1264 response = api_call(self, params)
1263 1265
1264 1266 expected = 'permission `%s` does not exist' % perm
1265 1267 self._compare_error(id_, expected, given=response.body)
1266 1268
1267 1269 @mock.patch.object(RepoModel, 'grant_users_group_permission', crash)
1268 1270 def test_api_grant_users_group_permission_exception_when_adding(self):
1269 1271 perm = 'repository.read'
1270 1272 id_, params = _build_data(self.apikey, 'grant_users_group_permission',
1271 1273 repoid=self.REPO,
1272 1274 usersgroupid=TEST_USER_GROUP,
1273 1275 perm=perm)
1274 1276 response = api_call(self, params)
1275 1277
1276 1278 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % (
1277 1279 TEST_USER_GROUP, self.REPO
1278 1280 )
1279 1281 self._compare_error(id_, expected, given=response.body)
1280 1282
1281 1283 def test_api_revoke_users_group_permission(self):
1282 1284 RepoModel().grant_users_group_permission(repo=self.REPO,
1283 1285 group_name=TEST_USER_GROUP,
1284 1286 perm='repository.read')
1285 1287 Session().commit()
1286 1288 id_, params = _build_data(self.apikey, 'revoke_users_group_permission',
1287 1289 repoid=self.REPO,
1288 1290 usersgroupid=TEST_USER_GROUP,)
1289 1291 response = api_call(self, params)
1290 1292
1291 1293 expected = {
1292 1294 'msg': 'Revoked perm for user group: `%s` in repo: `%s`' % (
1293 1295 TEST_USER_GROUP, self.REPO
1294 1296 ),
1295 1297 'success': True
1296 1298 }
1297 1299 self._compare_ok(id_, expected, given=response.body)
1298 1300
1299 1301 @mock.patch.object(RepoModel, 'revoke_users_group_permission', crash)
1300 1302 def test_api_revoke_users_group_permission_exception_when_adding(self):
1301 1303
1302 1304 id_, params = _build_data(self.apikey, 'revoke_users_group_permission',
1303 1305 repoid=self.REPO,
1304 1306 usersgroupid=TEST_USER_GROUP,)
1305 1307 response = api_call(self, params)
1306 1308
1307 1309 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % (
1308 1310 TEST_USER_GROUP, self.REPO
1309 1311 )
1310 1312 self._compare_error(id_, expected, given=response.body)
General Comments 0
You need to be logged in to leave comments. Login now