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