Files
@ c785ad556d44
Branch filter:
Location: kallithea/rhodecode/tests/api/api_base.py
c785ad556d44
40.0 KiB
text/x-python
added extra logging into API calls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 | from __future__ import with_statement
import random
import mock
from rhodecode.tests import *
from rhodecode.lib.compat import json
from rhodecode.lib.auth import AuthUser
from rhodecode.model.user import UserModel
from rhodecode.model.users_group import UsersGroupModel
from rhodecode.model.repo import RepoModel
from rhodecode.model.meta import Session
API_URL = '/_admin/api'
def _build_data(apikey, method, **kw):
"""
Builds API data with given random ID
:param random_id:
:type random_id:
"""
random_id = random.randrange(1, 9999)
return random_id, json.dumps({
"id": random_id,
"api_key": apikey,
"method": method,
"args": kw
})
jsonify = lambda obj: json.loads(json.dumps(obj))
def crash(*args, **kwargs):
raise Exception('Total Crash !')
TEST_USERS_GROUP = 'test_users_group'
def make_users_group(name=TEST_USERS_GROUP):
gr = UsersGroupModel().create(name=name)
UsersGroupModel().add_user_to_group(users_group=gr,
user=TEST_USER_ADMIN_LOGIN)
Session().commit()
return gr
def destroy_users_group(name=TEST_USERS_GROUP):
UsersGroupModel().delete(users_group=name, force=True)
Session().commit()
def create_repo(repo_name, repo_type):
# create new repo
form_data = dict(repo_name=repo_name,
repo_name_full=repo_name,
fork_name=None,
description='description %s' % repo_name,
repo_group=None,
private=False,
repo_type=repo_type,
clone_uri=None,
landing_rev='tip')
cur_user = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
r = RepoModel().create(form_data, cur_user)
Session().commit()
return r
def create_fork(fork_name, fork_type, fork_of):
fork = RepoModel(Session())._get_repo(fork_of)
r = create_repo(fork_name, fork_type)
r.fork = fork
Session().add(r)
Session().commit()
return r
def destroy_repo(repo_name):
RepoModel().delete(repo_name)
Session().commit()
class BaseTestApi(object):
REPO = None
REPO_TYPE = None
@classmethod
def setUpClass(self):
self.usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
self.apikey = self.usr.api_key
self.TEST_USER = UserModel().create_or_update(
username='test-api',
password='test',
email='test@api.rhodecode.org',
firstname='first',
lastname='last'
)
Session().commit()
self.TEST_USER_LOGIN = self.TEST_USER.username
@classmethod
def teardownClass(self):
pass
def setUp(self):
self.maxDiff = None
make_users_group()
def tearDown(self):
destroy_users_group()
def _compare_ok(self, id_, expected, given):
expected = jsonify({
'id': id_,
'error': None,
'result': expected
})
given = json.loads(given)
self.assertEqual(expected, given)
def _compare_error(self, id_, expected, given):
expected = jsonify({
'id': id_,
'error': expected,
'result': None
})
given = json.loads(given)
self.assertEqual(expected, given)
# def test_Optional(self):
# from rhodecode.controllers.api.api import Optional
# option1 = Optional(None)
# self.assertEqual('<Optional:%s>' % None, repr(option1))
#
# self.assertEqual(1, Optional.extract(Optional(1)))
# self.assertEqual('trololo', Optional.extract('trololo'))
def test_api_wrong_key(self):
id_, params = _build_data('trololo', 'get_user')
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'Invalid API KEY'
self._compare_error(id_, expected, given=response.body)
def test_api_missing_non_optional_param(self):
id_, params = _build_data(self.apikey, 'get_user')
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'Missing non optional `userid` arg in JSON DATA'
self._compare_error(id_, expected, given=response.body)
def test_api_get_users(self):
id_, params = _build_data(self.apikey, 'get_users',)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret_all = []
for usr in UserModel().get_all():
ret = usr.get_api_data()
ret_all.append(jsonify(ret))
expected = ret_all
self._compare_ok(id_, expected, given=response.body)
def test_api_get_user(self):
id_, params = _build_data(self.apikey, 'get_user',
userid=TEST_USER_ADMIN_LOGIN)
response = self.app.post(API_URL, content_type='application/json',
params=params)
usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
ret = usr.get_api_data()
ret['permissions'] = AuthUser(usr.user_id).permissions
expected = ret
self._compare_ok(id_, expected, given=response.body)
def test_api_get_user_that_does_not_exist(self):
id_, params = _build_data(self.apikey, 'get_user',
userid='trololo')
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = "user `%s` does not exist" % 'trololo'
self._compare_error(id_, expected, given=response.body)
def test_api_pull(self):
#TODO: issues with rhodecode_extras here.. not sure why !
pass
# repo_name = 'test_pull'
# r = create_repo(repo_name, self.REPO_TYPE)
# r.clone_uri = TEST_self.REPO
# Session.add(r)
# Session.commit()
#
# id_, params = _build_data(self.apikey, 'pull',
# repoid=repo_name,)
# response = self.app.post(API_URL, content_type='application/json',
# params=params)
#
# expected = 'Pulled from `%s`' % repo_name
# self._compare_ok(id_, expected, given=response.body)
#
# destroy_repo(repo_name)
def test_api_pull_error(self):
id_, params = _build_data(self.apikey, 'pull',
repoid=self.REPO,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'Unable to pull changes from `%s`' % self.REPO
self._compare_error(id_, expected, given=response.body)
def test_api_create_existing_user(self):
id_, params = _build_data(self.apikey, 'create_user',
username=TEST_USER_ADMIN_LOGIN,
email='test@foo.com',
password='trololo')
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = "user `%s` already exist" % TEST_USER_ADMIN_LOGIN
self._compare_error(id_, expected, given=response.body)
def test_api_create_user_with_existing_email(self):
id_, params = _build_data(self.apikey, 'create_user',
username=TEST_USER_ADMIN_LOGIN + 'new',
email=TEST_USER_REGULAR_EMAIL,
password='trololo')
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = "email `%s` already exist" % TEST_USER_REGULAR_EMAIL
self._compare_error(id_, expected, given=response.body)
def test_api_create_user(self):
username = 'test_new_api_user'
email = username + "@foo.com"
id_, params = _build_data(self.apikey, 'create_user',
username=username,
email=email,
password='trololo')
response = self.app.post(API_URL, content_type='application/json',
params=params)
usr = UserModel().get_by_username(username)
ret = dict(
msg='created new user `%s`' % username,
user=jsonify(usr.get_api_data())
)
expected = ret
self._compare_ok(id_, expected, given=response.body)
UserModel().delete(usr.user_id)
self.Session().commit()
@mock.patch.object(UserModel, 'create_or_update', crash)
def test_api_create_user_when_exception_happened(self):
username = 'test_new_api_user'
email = username + "@foo.com"
id_, params = _build_data(self.apikey, 'create_user',
username=username,
email=email,
password='trololo')
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to create user `%s`' % username
self._compare_error(id_, expected, given=response.body)
def test_api_delete_user(self):
usr = UserModel().create_or_update(username=u'test_user',
password=u'qweqwe',
email=u'u232@rhodecode.org',
firstname=u'u1', lastname=u'u1')
self.Session().commit()
username = usr.username
email = usr.email
usr_id = usr.user_id
## DELETE THIS USER NOW
id_, params = _build_data(self.apikey, 'delete_user',
userid=username,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = {'msg': 'deleted user ID:%s %s' % (usr_id, username),
'user': None}
expected = ret
self._compare_ok(id_, expected, given=response.body)
@mock.patch.object(UserModel, 'delete', crash)
def test_api_delete_user_when_exception_happened(self):
usr = UserModel().create_or_update(username=u'test_user',
password=u'qweqwe',
email=u'u232@rhodecode.org',
firstname=u'u1', lastname=u'u1')
self.Session().commit()
username = usr.username
id_, params = _build_data(self.apikey, 'delete_user',
userid=username,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = 'failed to delete ID:%s %s' % (usr.user_id,
usr.username)
expected = ret
self._compare_error(id_, expected, given=response.body)
@parameterized.expand([('firstname', 'new_username'),
('lastname', 'new_username'),
('email', 'new_username'),
('admin', True),
('admin', False),
('ldap_dn', 'test'),
('ldap_dn', None),
('active', False),
('active', True),
('password', 'newpass')
])
def test_api_update_user(self, name, expected):
usr = UserModel().get_by_username(self.TEST_USER_LOGIN)
kw = {name: expected,
'userid': usr.user_id}
id_, params = _build_data(self.apikey, 'update_user', **kw)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = {
'msg': 'updated user ID:%s %s' % (usr.user_id, self.TEST_USER_LOGIN),
'user': jsonify(UserModel()\
.get_by_username(self.TEST_USER_LOGIN)\
.get_api_data())
}
expected = ret
self._compare_ok(id_, expected, given=response.body)
def test_api_update_user_no_changed_params(self):
usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
ret = jsonify(usr.get_api_data())
id_, params = _build_data(self.apikey, 'update_user',
userid=TEST_USER_ADMIN_LOGIN)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = {
'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN),
'user': ret
}
expected = ret
self._compare_ok(id_, expected, given=response.body)
def test_api_update_user_by_user_id(self):
usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
ret = jsonify(usr.get_api_data())
id_, params = _build_data(self.apikey, 'update_user',
userid=usr.user_id)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = {
'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN),
'user': ret
}
expected = ret
self._compare_ok(id_, expected, given=response.body)
@mock.patch.object(UserModel, 'create_or_update', crash)
def test_api_update_user_when_exception_happens(self):
usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
ret = jsonify(usr.get_api_data())
id_, params = _build_data(self.apikey, 'update_user',
userid=usr.user_id)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = 'failed to update user `%s`' % usr.user_id
expected = ret
self._compare_error(id_, expected, given=response.body)
def test_api_get_repo(self):
new_group = 'some_new_group'
make_users_group(new_group)
RepoModel().grant_users_group_permission(repo=self.REPO,
group_name=new_group,
perm='repository.read')
self.Session().commit()
id_, params = _build_data(self.apikey, 'get_repo',
repoid=self.REPO)
response = self.app.post(API_URL, content_type='application/json',
params=params)
repo = RepoModel().get_by_repo_name(self.REPO)
ret = repo.get_api_data()
members = []
for user in repo.repo_to_perm:
perm = user.permission.permission_name
user = user.user
user_data = user.get_api_data()
user_data['type'] = "user"
user_data['permission'] = perm
members.append(user_data)
for users_group in repo.users_group_to_perm:
perm = users_group.permission.permission_name
users_group = users_group.users_group
users_group_data = users_group.get_api_data()
users_group_data['type'] = "users_group"
users_group_data['permission'] = perm
members.append(users_group_data)
ret['members'] = members
expected = ret
self._compare_ok(id_, expected, given=response.body)
destroy_users_group(new_group)
def test_api_get_repo_that_doesn_not_exist(self):
id_, params = _build_data(self.apikey, 'get_repo',
repoid='no-such-repo')
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = 'repository `%s` does not exist' % 'no-such-repo'
expected = ret
self._compare_error(id_, expected, given=response.body)
def test_api_get_repos(self):
id_, params = _build_data(self.apikey, 'get_repos')
response = self.app.post(API_URL, content_type='application/json',
params=params)
result = []
for repo in RepoModel().get_all():
result.append(repo.get_api_data())
ret = jsonify(result)
expected = ret
self._compare_ok(id_, expected, given=response.body)
@parameterized.expand([('all', 'all'),
('dirs', 'dirs'),
('files', 'files'), ])
def test_api_get_repo_nodes(self, name, ret_type):
rev = 'tip'
path = '/'
id_, params = _build_data(self.apikey, 'get_repo_nodes',
repoid=self.REPO, revision=rev,
root_path=path,
ret_type=ret_type)
response = self.app.post(API_URL, content_type='application/json',
params=params)
# we don't the actual return types here since it's tested somewhere
# else
expected = json.loads(response.body)['result']
self._compare_ok(id_, expected, given=response.body)
def test_api_get_repo_nodes_bad_revisions(self):
rev = 'i-dont-exist'
path = '/'
id_, params = _build_data(self.apikey, 'get_repo_nodes',
repoid=self.REPO, revision=rev,
root_path=path,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to get repo: `%s` nodes' % self.REPO
self._compare_error(id_, expected, given=response.body)
def test_api_get_repo_nodes_bad_path(self):
rev = 'tip'
path = '/idontexits'
id_, params = _build_data(self.apikey, 'get_repo_nodes',
repoid=self.REPO, revision=rev,
root_path=path,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to get repo: `%s` nodes' % self.REPO
self._compare_error(id_, expected, given=response.body)
def test_api_get_repo_nodes_bad_ret_type(self):
rev = 'tip'
path = '/'
ret_type = 'error'
id_, params = _build_data(self.apikey, 'get_repo_nodes',
repoid=self.REPO, revision=rev,
root_path=path,
ret_type=ret_type)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'ret_type must be one of %s' % (['files', 'dirs', 'all'])
self._compare_error(id_, expected, given=response.body)
def test_api_create_repo(self):
repo_name = 'api-repo'
id_, params = _build_data(self.apikey, 'create_repo',
repo_name=repo_name,
owner=TEST_USER_ADMIN_LOGIN,
repo_type='hg',
)
response = self.app.post(API_URL, content_type='application/json',
params=params)
repo = RepoModel().get_by_repo_name(repo_name)
ret = {
'msg': 'Created new repository `%s`' % repo_name,
'repo': jsonify(repo.get_api_data())
}
expected = ret
self._compare_ok(id_, expected, given=response.body)
destroy_repo(repo_name)
def test_api_create_repo_unknown_owner(self):
repo_name = 'api-repo'
owner = 'i-dont-exist'
id_, params = _build_data(self.apikey, 'create_repo',
repo_name=repo_name,
owner=owner,
repo_type='hg',
)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'user `%s` does not exist' % owner
self._compare_error(id_, expected, given=response.body)
def test_api_create_repo_exists(self):
repo_name = self.REPO
id_, params = _build_data(self.apikey, 'create_repo',
repo_name=repo_name,
owner=TEST_USER_ADMIN_LOGIN,
repo_type='hg',
)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = "repo `%s` already exist" % repo_name
self._compare_error(id_, expected, given=response.body)
@mock.patch.object(RepoModel, 'create_repo', crash)
def test_api_create_repo_exception_occurred(self):
repo_name = 'api-repo'
id_, params = _build_data(self.apikey, 'create_repo',
repo_name=repo_name,
owner=TEST_USER_ADMIN_LOGIN,
repo_type='hg',
)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to create repository `%s`' % repo_name
self._compare_error(id_, expected, given=response.body)
def test_api_delete_repo(self):
repo_name = 'api_delete_me'
create_repo(repo_name, self.REPO_TYPE)
id_, params = _build_data(self.apikey, 'delete_repo',
repoid=repo_name,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = {
'msg': 'Deleted repository `%s`' % repo_name,
'success': True
}
expected = ret
self._compare_ok(id_, expected, given=response.body)
def test_api_delete_repo_exception_occurred(self):
repo_name = 'api_delete_me'
create_repo(repo_name, self.REPO_TYPE)
try:
with mock.patch.object(RepoModel, 'delete', crash):
id_, params = _build_data(self.apikey, 'delete_repo',
repoid=repo_name,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to delete repository `%s`' % repo_name
self._compare_error(id_, expected, given=response.body)
finally:
destroy_repo(repo_name)
def test_api_fork_repo(self):
fork_name = 'api-repo-fork'
id_, params = _build_data(self.apikey, 'fork_repo',
repoid=self.REPO,
fork_name=fork_name,
owner=TEST_USER_ADMIN_LOGIN,
)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = {
'msg': 'Created fork of `%s` as `%s`' % (self.REPO,
fork_name),
'success': True
}
expected = ret
self._compare_ok(id_, expected, given=response.body)
destroy_repo(fork_name)
def test_api_fork_repo_unknown_owner(self):
fork_name = 'api-repo-fork'
owner = 'i-dont-exist'
id_, params = _build_data(self.apikey, 'fork_repo',
repoid=self.REPO,
fork_name=fork_name,
owner=owner,
)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'user `%s` does not exist' % owner
self._compare_error(id_, expected, given=response.body)
def test_api_fork_repo_fork_exists(self):
fork_name = 'api-repo-fork'
create_fork(fork_name, self.REPO_TYPE, self.REPO)
try:
fork_name = 'api-repo-fork'
id_, params = _build_data(self.apikey, 'fork_repo',
repoid=self.REPO,
fork_name=fork_name,
owner=TEST_USER_ADMIN_LOGIN,
)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = "fork `%s` already exist" % fork_name
self._compare_error(id_, expected, given=response.body)
finally:
destroy_repo(fork_name)
def test_api_fork_repo_repo_exists(self):
fork_name = self.REPO
id_, params = _build_data(self.apikey, 'fork_repo',
repoid=self.REPO,
fork_name=fork_name,
owner=TEST_USER_ADMIN_LOGIN,
)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = "repo `%s` already exist" % fork_name
self._compare_error(id_, expected, given=response.body)
@mock.patch.object(RepoModel, 'create_fork', crash)
def test_api_fork_repo_exception_occurred(self):
fork_name = 'api-repo-fork'
id_, params = _build_data(self.apikey, 'fork_repo',
repoid=self.REPO,
fork_name=fork_name,
owner=TEST_USER_ADMIN_LOGIN,
)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to fork repository `%s` as `%s`' % (self.REPO,
fork_name)
self._compare_error(id_, expected, given=response.body)
def test_api_get_users_group(self):
id_, params = _build_data(self.apikey, 'get_users_group',
usersgroupid=TEST_USERS_GROUP)
response = self.app.post(API_URL, content_type='application/json',
params=params)
users_group = UsersGroupModel().get_group(TEST_USERS_GROUP)
members = []
for user in users_group.members:
user = user.user
members.append(user.get_api_data())
ret = users_group.get_api_data()
ret['members'] = members
expected = ret
self._compare_ok(id_, expected, given=response.body)
def test_api_get_users_groups(self):
make_users_group('test_users_group2')
id_, params = _build_data(self.apikey, 'get_users_groups',)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = []
for gr_name in [TEST_USERS_GROUP, 'test_users_group2']:
users_group = UsersGroupModel().get_group(gr_name)
ret = users_group.get_api_data()
expected.append(ret)
self._compare_ok(id_, expected, given=response.body)
UsersGroupModel().delete(users_group='test_users_group2')
self.Session().commit()
def test_api_create_users_group(self):
group_name = 'some_new_group'
id_, params = _build_data(self.apikey, 'create_users_group',
group_name=group_name)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = {
'msg': 'created new users group `%s`' % group_name,
'users_group': jsonify(UsersGroupModel()\
.get_by_name(group_name)\
.get_api_data())
}
expected = ret
self._compare_ok(id_, expected, given=response.body)
destroy_users_group(group_name)
def test_api_get_users_group_that_exist(self):
id_, params = _build_data(self.apikey, 'create_users_group',
group_name=TEST_USERS_GROUP)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = "users group `%s` already exist" % TEST_USERS_GROUP
self._compare_error(id_, expected, given=response.body)
@mock.patch.object(UsersGroupModel, 'create', crash)
def test_api_get_users_group_exception_occurred(self):
group_name = 'exception_happens'
id_, params = _build_data(self.apikey, 'create_users_group',
group_name=group_name)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to create group `%s`' % group_name
self._compare_error(id_, expected, given=response.body)
def test_api_add_user_to_users_group(self):
gr_name = 'test_group'
UsersGroupModel().create(gr_name)
self.Session().commit()
id_, params = _build_data(self.apikey, 'add_user_to_users_group',
usersgroupid=gr_name,
userid=TEST_USER_ADMIN_LOGIN)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = {
'msg': 'added member `%s` to users group `%s`' % (
TEST_USER_ADMIN_LOGIN, gr_name
),
'success': True}
self._compare_ok(id_, expected, given=response.body)
UsersGroupModel().delete(users_group=gr_name)
self.Session().commit()
def test_api_add_user_to_users_group_that_doesnt_exist(self):
id_, params = _build_data(self.apikey, 'add_user_to_users_group',
usersgroupid='false-group',
userid=TEST_USER_ADMIN_LOGIN)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'users group `%s` does not exist' % 'false-group'
self._compare_error(id_, expected, given=response.body)
@mock.patch.object(UsersGroupModel, 'add_user_to_group', crash)
def test_api_add_user_to_users_group_exception_occurred(self):
gr_name = 'test_group'
UsersGroupModel().create(gr_name)
self.Session().commit()
id_, params = _build_data(self.apikey, 'add_user_to_users_group',
usersgroupid=gr_name,
userid=TEST_USER_ADMIN_LOGIN)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to add member to users group `%s`' % gr_name
self._compare_error(id_, expected, given=response.body)
UsersGroupModel().delete(users_group=gr_name)
self.Session().commit()
def test_api_remove_user_from_users_group(self):
gr_name = 'test_group_3'
gr = UsersGroupModel().create(gr_name)
UsersGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN)
self.Session().commit()
id_, params = _build_data(self.apikey, 'remove_user_from_users_group',
usersgroupid=gr_name,
userid=TEST_USER_ADMIN_LOGIN)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = {
'msg': 'removed member `%s` from users group `%s`' % (
TEST_USER_ADMIN_LOGIN, gr_name
),
'success': True}
self._compare_ok(id_, expected, given=response.body)
UsersGroupModel().delete(users_group=gr_name)
self.Session().commit()
@mock.patch.object(UsersGroupModel, 'remove_user_from_group', crash)
def test_api_remove_user_from_users_group_exception_occurred(self):
gr_name = 'test_group_3'
gr = UsersGroupModel().create(gr_name)
UsersGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN)
self.Session().commit()
id_, params = _build_data(self.apikey, 'remove_user_from_users_group',
usersgroupid=gr_name,
userid=TEST_USER_ADMIN_LOGIN)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to remove member from users group `%s`' % gr_name
self._compare_error(id_, expected, given=response.body)
UsersGroupModel().delete(users_group=gr_name)
self.Session().commit()
@parameterized.expand([('none', 'repository.none'),
('read', 'repository.read'),
('write', 'repository.write'),
('admin', 'repository.admin')])
def test_api_grant_user_permission(self, name, perm):
id_, params = _build_data(self.apikey, 'grant_user_permission',
repoid=self.REPO,
userid=TEST_USER_ADMIN_LOGIN,
perm=perm)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = {
'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % (
perm, TEST_USER_ADMIN_LOGIN, self.REPO
),
'success': True
}
expected = ret
self._compare_ok(id_, expected, given=response.body)
def test_api_grant_user_permission_wrong_permission(self):
perm = 'haha.no.permission'
id_, params = _build_data(self.apikey, 'grant_user_permission',
repoid=self.REPO,
userid=TEST_USER_ADMIN_LOGIN,
perm=perm)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'permission `%s` does not exist' % perm
self._compare_error(id_, expected, given=response.body)
@mock.patch.object(RepoModel, 'grant_user_permission', crash)
def test_api_grant_user_permission_exception_when_adding(self):
perm = 'repository.read'
id_, params = _build_data(self.apikey, 'grant_user_permission',
repoid=self.REPO,
userid=TEST_USER_ADMIN_LOGIN,
perm=perm)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to edit permission for user: `%s` in repo: `%s`' % (
TEST_USER_ADMIN_LOGIN, self.REPO
)
self._compare_error(id_, expected, given=response.body)
def test_api_revoke_user_permission(self):
id_, params = _build_data(self.apikey, 'revoke_user_permission',
repoid=self.REPO,
userid=TEST_USER_ADMIN_LOGIN,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = {
'msg': 'Revoked perm for user: `%s` in repo: `%s`' % (
TEST_USER_ADMIN_LOGIN, self.REPO
),
'success': True
}
self._compare_ok(id_, expected, given=response.body)
@mock.patch.object(RepoModel, 'revoke_user_permission', crash)
def test_api_revoke_user_permission_exception_when_adding(self):
id_, params = _build_data(self.apikey, 'revoke_user_permission',
repoid=self.REPO,
userid=TEST_USER_ADMIN_LOGIN,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to edit permission for user: `%s` in repo: `%s`' % (
TEST_USER_ADMIN_LOGIN, self.REPO
)
self._compare_error(id_, expected, given=response.body)
@parameterized.expand([('none', 'repository.none'),
('read', 'repository.read'),
('write', 'repository.write'),
('admin', 'repository.admin')])
def test_api_grant_users_group_permission(self, name, perm):
id_, params = _build_data(self.apikey, 'grant_users_group_permission',
repoid=self.REPO,
usersgroupid=TEST_USERS_GROUP,
perm=perm)
response = self.app.post(API_URL, content_type='application/json',
params=params)
ret = {
'msg': 'Granted perm: `%s` for users group: `%s` in repo: `%s`' % (
perm, TEST_USERS_GROUP, self.REPO
),
'success': True
}
expected = ret
self._compare_ok(id_, expected, given=response.body)
def test_api_grant_users_group_permission_wrong_permission(self):
perm = 'haha.no.permission'
id_, params = _build_data(self.apikey, 'grant_users_group_permission',
repoid=self.REPO,
usersgroupid=TEST_USERS_GROUP,
perm=perm)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'permission `%s` does not exist' % perm
self._compare_error(id_, expected, given=response.body)
@mock.patch.object(RepoModel, 'grant_users_group_permission', crash)
def test_api_grant_users_group_permission_exception_when_adding(self):
perm = 'repository.read'
id_, params = _build_data(self.apikey, 'grant_users_group_permission',
repoid=self.REPO,
usersgroupid=TEST_USERS_GROUP,
perm=perm)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to edit permission for users group: `%s` in repo: `%s`' % (
TEST_USERS_GROUP, self.REPO
)
self._compare_error(id_, expected, given=response.body)
def test_api_revoke_users_group_permission(self):
RepoModel().grant_users_group_permission(repo=self.REPO,
group_name=TEST_USERS_GROUP,
perm='repository.read')
self.Session().commit()
id_, params = _build_data(self.apikey, 'revoke_users_group_permission',
repoid=self.REPO,
usersgroupid=TEST_USERS_GROUP,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = {
'msg': 'Revoked perm for users group: `%s` in repo: `%s`' % (
TEST_USERS_GROUP, self.REPO
),
'success': True
}
self._compare_ok(id_, expected, given=response.body)
@mock.patch.object(RepoModel, 'revoke_users_group_permission', crash)
def test_api_revoke_users_group_permission_exception_when_adding(self):
id_, params = _build_data(self.apikey, 'revoke_users_group_permission',
repoid=self.REPO,
usersgroupid=TEST_USERS_GROUP,)
response = self.app.post(API_URL, content_type='application/json',
params=params)
expected = 'failed to edit permission for users group: `%s` in repo: `%s`' % (
TEST_USERS_GROUP, self.REPO
)
self._compare_error(id_, expected, given=response.body)
|