Mercurial > kallithea
comparison rhodecode/tests/api/api_base.py @ 4116:ffd45b185016 rhodecode-2.2.5-gpl
Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
This imports changes between changesets 21af6c4eab3d and 6177597791c2 in
RhodeCode's original repository, including only changes to Python files and HTML.
RhodeCode clearly licensed its changes to these files under GPLv3
in their /LICENSE file, which states the following:
The Python code and integrated HTML are licensed under the GPLv3 license.
(See:
https://code.rhodecode.com/rhodecode/files/v2.2.5/LICENSE
or
http://web.archive.org/web/20140512193334/https://code.rhodecode.com/rhodecode/files/f3b123159901f15426d18e3dc395e8369f70ebe0/LICENSE
for an online copy of that LICENSE file)
Conservancy reviewed these changes and confirmed that they can be licensed as
a whole to the Kallithea project under GPLv3-only.
While some of the contents committed herein are clearly licensed
GPLv3-or-later, on the whole we must assume the are GPLv3-only, since the
statement above from RhodeCode indicates that they intend GPLv3-only as their
license, per GPLv3ยง14 and other relevant sections of GPLv3.
author | Bradley M. Kuhn <bkuhn@sfconservancy.org> |
---|---|
date | Wed, 02 Jul 2014 19:03:13 -0400 |
parents | aff5dd957f35 |
children | da3c57422ee6 |
comparison
equal
deleted
inserted
replaced
4115:8b7294a804a0 | 4116:ffd45b185016 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 # This program is free software: you can redistribute it and/or modify | |
3 # it under the terms of the GNU General Public License as published by | |
4 # the Free Software Foundation, either version 3 of the License, or | |
5 # (at your option) any later version. | |
6 # | |
7 # This program is distributed in the hope that it will be useful, | |
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 # GNU General Public License for more details. | |
11 # | |
12 # You should have received a copy of the GNU General Public License | |
13 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | |
15 """ | |
16 tests for api. run with:: | |
17 | |
18 RC_WHOOSH_TEST_DISABLE=1 nosetests --with-coverage --cover-package=rhodecode.controllers.api.api -x rhodecode/tests/api | |
19 """ | |
20 | |
1 from __future__ import with_statement | 21 from __future__ import with_statement |
22 import os | |
2 import random | 23 import random |
3 import mock | 24 import mock |
4 | 25 |
5 from rhodecode.tests import * | 26 from rhodecode.tests import * |
6 from rhodecode.tests.fixture import Fixture | 27 from rhodecode.tests.fixture import Fixture |
7 from rhodecode.lib.compat import json | 28 from rhodecode.lib.compat import json |
8 from rhodecode.lib.auth import AuthUser | 29 from rhodecode.lib.auth import AuthUser |
9 from rhodecode.model.user import UserModel | 30 from rhodecode.model.user import UserModel |
10 from rhodecode.model.users_group import UserGroupModel | 31 from rhodecode.model.user_group import UserGroupModel |
11 from rhodecode.model.repo import RepoModel | 32 from rhodecode.model.repo import RepoModel |
33 from rhodecode.model.repo_group import RepoGroupModel | |
12 from rhodecode.model.meta import Session | 34 from rhodecode.model.meta import Session |
13 from rhodecode.model.scm import ScmModel | 35 from rhodecode.model.scm import ScmModel |
14 from rhodecode.model.db import Repository, User | 36 from rhodecode.model.gist import GistModel |
15 from rhodecode.lib.utils2 import time_to_datetime | 37 from rhodecode.model.db import Repository, User, RhodeCodeSetting |
38 from rhodecode.lib.utils2 import time_to_datetime | |
16 | 39 |
17 | 40 |
18 API_URL = '/_admin/api' | 41 API_URL = '/_admin/api' |
19 TEST_USER_GROUP = 'test_users_group' | 42 TEST_USER_GROUP = 'test_user_group' |
43 TEST_REPO_GROUP = 'test_repo_group' | |
20 | 44 |
21 fixture = Fixture() | 45 fixture = Fixture() |
22 | 46 |
23 | 47 |
24 def _build_data(apikey, method, **kw): | 48 def _build_data(apikey, method, **kw): |
33 "api_key": apikey, | 57 "api_key": apikey, |
34 "method": method, | 58 "method": method, |
35 "args": kw | 59 "args": kw |
36 }) | 60 }) |
37 | 61 |
62 | |
38 jsonify = lambda obj: json.loads(json.dumps(obj)) | 63 jsonify = lambda obj: json.loads(json.dumps(obj)) |
39 | 64 |
40 | 65 |
41 def crash(*args, **kwargs): | 66 def crash(*args, **kwargs): |
42 raise Exception('Total Crash !') | 67 raise Exception('Total Crash !') |
47 params=params) | 72 params=params) |
48 return response | 73 return response |
49 | 74 |
50 | 75 |
51 ## helpers | 76 ## helpers |
52 def make_users_group(name=TEST_USER_GROUP): | 77 def make_user_group(name=TEST_USER_GROUP): |
53 gr = fixture.create_user_group(name, cur_user=TEST_USER_ADMIN_LOGIN) | 78 gr = fixture.create_user_group(name, cur_user=TEST_USER_ADMIN_LOGIN) |
54 UserGroupModel().add_user_to_group(users_group=gr, | 79 UserGroupModel().add_user_to_group(user_group=gr, |
55 user=TEST_USER_ADMIN_LOGIN) | 80 user=TEST_USER_ADMIN_LOGIN) |
56 Session().commit() | 81 Session().commit() |
57 return gr | 82 return gr |
58 | 83 |
59 | 84 |
60 def destroy_users_group(name=TEST_USER_GROUP): | 85 def make_repo_group(name=TEST_REPO_GROUP): |
61 UserGroupModel().delete(users_group=name, force=True) | 86 gr = fixture.create_repo_group(name, cur_user=TEST_USER_ADMIN_LOGIN) |
62 Session().commit() | 87 Session().commit() |
88 return gr | |
63 | 89 |
64 | 90 |
65 class BaseTestApi(object): | 91 class BaseTestApi(object): |
66 REPO = None | 92 REPO = None |
67 REPO_TYPE = None | 93 REPO_TYPE = None |
68 | 94 |
69 @classmethod | 95 @classmethod |
70 def setUpClass(cls): | 96 def setup_class(cls): |
71 cls.usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) | 97 cls.usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
72 cls.apikey = cls.usr.api_key | 98 cls.apikey = cls.usr.api_key |
73 cls.test_user = UserModel().create_or_update( | 99 cls.test_user = UserModel().create_or_update( |
74 username='test-api', | 100 username='test-api', |
75 password='test', | 101 password='test', |
80 Session().commit() | 106 Session().commit() |
81 cls.TEST_USER_LOGIN = cls.test_user.username | 107 cls.TEST_USER_LOGIN = cls.test_user.username |
82 cls.apikey_regular = cls.test_user.api_key | 108 cls.apikey_regular = cls.test_user.api_key |
83 | 109 |
84 @classmethod | 110 @classmethod |
85 def teardownClass(cls): | 111 def teardown_class(cls): |
86 pass | 112 pass |
87 | 113 |
88 def setUp(self): | 114 def setUp(self): |
89 self.maxDiff = None | 115 self.maxDiff = None |
90 make_users_group() | 116 make_user_group() |
117 make_repo_group() | |
91 | 118 |
92 def tearDown(self): | 119 def tearDown(self): |
93 destroy_users_group() | 120 fixture.destroy_user_group(TEST_USER_GROUP) |
121 fixture.destroy_gists() | |
122 fixture.destroy_repo_group(TEST_REPO_GROUP) | |
94 | 123 |
95 def _compare_ok(self, id_, expected, given): | 124 def _compare_ok(self, id_, expected, given): |
96 expected = jsonify({ | 125 expected = jsonify({ |
97 'id': id_, | 126 'id': id_, |
98 'error': None, | 127 'error': None, |
108 'result': None | 137 'result': None |
109 }) | 138 }) |
110 given = json.loads(given) | 139 given = json.loads(given) |
111 self.assertEqual(expected, given) | 140 self.assertEqual(expected, given) |
112 | 141 |
113 # def test_Optional(self): | 142 def test_Optional_object(self): |
114 # from rhodecode.controllers.api.api import Optional | 143 from rhodecode.controllers.api.api import Optional |
115 # option1 = Optional(None) | 144 |
116 # self.assertEqual('<Optional:%s>' % None, repr(option1)) | 145 option1 = Optional(None) |
117 # | 146 self.assertEqual('<Optional:%s>' % None, repr(option1)) |
118 # self.assertEqual(1, Optional.extract(Optional(1))) | 147 self.assertEqual(option1(), None) |
119 # self.assertEqual('trololo', Optional.extract('trololo')) | 148 |
149 self.assertEqual(1, Optional.extract(Optional(1))) | |
150 self.assertEqual('trololo', Optional.extract('trololo')) | |
151 | |
152 def test_Optional_OAttr(self): | |
153 from rhodecode.controllers.api.api import Optional, OAttr | |
154 | |
155 option1 = Optional(OAttr('apiuser')) | |
156 self.assertEqual('apiuser', Optional.extract(option1)) | |
157 | |
158 def test_OAttr_object(self): | |
159 from rhodecode.controllers.api.api import OAttr | |
160 | |
161 oattr1 = OAttr('apiuser') | |
162 self.assertEqual('<OptionalAttr:apiuser>', repr(oattr1)) | |
163 self.assertEqual(oattr1(), oattr1) | |
120 | 164 |
121 def test_api_wrong_key(self): | 165 def test_api_wrong_key(self): |
122 id_, params = _build_data('trololo', 'get_user') | 166 id_, params = _build_data('trololo', 'get_user') |
123 response = api_call(self, params) | 167 response = api_call(self, params) |
124 | 168 |
147 | 191 |
148 expected = 'Missing non optional `repoid` arg in JSON DATA' | 192 expected = 'Missing non optional `repoid` arg in JSON DATA' |
149 self._compare_error(id_, expected, given=response.body) | 193 self._compare_error(id_, expected, given=response.body) |
150 | 194 |
151 def test_api_args_is_null(self): | 195 def test_api_args_is_null(self): |
152 id_, params = _build_data(self.apikey, 'get_users',) | 196 id_, params = _build_data(self.apikey, 'get_users', ) |
153 params = params.replace('"args": {}', '"args": null') | 197 params = params.replace('"args": {}', '"args": null') |
154 response = api_call(self, params) | 198 response = api_call(self, params) |
155 self.assertEqual(response.status, '200 OK') | 199 self.assertEqual(response.status, '200 OK') |
156 | 200 |
157 def test_api_args_is_bad(self): | 201 def test_api_args_is_bad(self): |
158 id_, params = _build_data(self.apikey, 'get_users',) | 202 id_, params = _build_data(self.apikey, 'get_users', ) |
159 params = params.replace('"args": {}', '"args": 1') | 203 params = params.replace('"args": {}', '"args": 1') |
160 response = api_call(self, params) | 204 response = api_call(self, params) |
161 self.assertEqual(response.status, '200 OK') | 205 self.assertEqual(response.status, '200 OK') |
162 | 206 |
207 def test_api_args_different_args(self): | |
208 import string | |
209 expected = { | |
210 'ascii_letters': string.ascii_letters, | |
211 'ws': string.whitespace, | |
212 'printables': string.printable | |
213 } | |
214 id_, params = _build_data(self.apikey, 'test', args=expected) | |
215 response = api_call(self, params) | |
216 self.assertEqual(response.status, '200 OK') | |
217 self._compare_ok(id_, expected, response.body) | |
218 | |
163 def test_api_get_users(self): | 219 def test_api_get_users(self): |
164 id_, params = _build_data(self.apikey, 'get_users',) | 220 id_, params = _build_data(self.apikey, 'get_users', ) |
165 response = api_call(self, params) | 221 response = api_call(self, params) |
166 ret_all = [] | 222 ret_all = [] |
167 _users = User.query().filter(User.username != User.DEFAULT_USER)\ | 223 _users = User.query().filter(User.username != User.DEFAULT_USER) \ |
168 .order_by(User.username).all() | 224 .order_by(User.username).all() |
169 for usr in _users: | 225 for usr in _users: |
170 ret = usr.get_api_data() | 226 ret = usr.get_api_data() |
171 ret_all.append(jsonify(ret)) | 227 ret_all.append(jsonify(ret)) |
172 expected = ret_all | 228 expected = ret_all |
173 self._compare_ok(id_, expected, given=response.body) | 229 self._compare_ok(id_, expected, given=response.body) |
221 | 277 |
222 expected = 'userid is not the same as your user' | 278 expected = 'userid is not the same as your user' |
223 self._compare_error(id_, expected, given=response.body) | 279 self._compare_error(id_, expected, given=response.body) |
224 | 280 |
225 def test_api_pull(self): | 281 def test_api_pull(self): |
226 #TODO: issues with rhodecode_extras here.. not sure why ! | 282 repo_name = 'test_pull' |
227 pass | 283 r = fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) |
228 | 284 r.clone_uri = os.path.join(TESTS_TMP_PATH, self.REPO) |
229 # repo_name = 'test_pull' | 285 Session.add(r) |
230 # r = fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) | 286 Session.commit() |
231 # r.clone_uri = TEST_self.REPO | 287 |
232 # Session.add(r) | 288 id_, params = _build_data(self.apikey, 'pull', |
233 # Session.commit() | 289 repoid=repo_name,) |
234 # | 290 response = api_call(self, params) |
235 # id_, params = _build_data(self.apikey, 'pull', | 291 |
236 # repoid=repo_name,) | 292 expected = {'msg': 'Pulled from `%s`' % repo_name, |
237 # response = self.app.post(API_URL, content_type='application/json', | 293 'repository': repo_name} |
238 # params=params) | 294 self._compare_ok(id_, expected, given=response.body) |
239 # | 295 |
240 # expected = 'Pulled from `%s`' % repo_name | 296 fixture.destroy_repo(repo_name) |
241 # self._compare_ok(id_, expected, given=response.body) | |
242 # | |
243 # fixture.destroy_repo(repo_name) | |
244 | 297 |
245 def test_api_pull_error(self): | 298 def test_api_pull_error(self): |
246 id_, params = _build_data(self.apikey, 'pull', | 299 id_, params = _build_data(self.apikey, 'pull', |
247 repoid=self.REPO,) | 300 repoid=self.REPO, ) |
248 response = api_call(self, params) | 301 response = api_call(self, params) |
249 | 302 |
250 expected = 'Unable to pull changes from `%s`' % self.REPO | 303 expected = 'Unable to pull changes from `%s`' % self.REPO |
251 self._compare_error(id_, expected, given=response.body) | 304 self._compare_error(id_, expected, given=response.body) |
252 | 305 |
257 expected = {'added': [], 'removed': []} | 310 expected = {'added': [], 'removed': []} |
258 self._compare_ok(id_, expected, given=response.body) | 311 self._compare_ok(id_, expected, given=response.body) |
259 | 312 |
260 @mock.patch.object(ScmModel, 'repo_scan', crash) | 313 @mock.patch.object(ScmModel, 'repo_scan', crash) |
261 def test_api_rescann_error(self): | 314 def test_api_rescann_error(self): |
262 id_, params = _build_data(self.apikey, 'rescan_repos',) | 315 id_, params = _build_data(self.apikey, 'rescan_repos', ) |
263 response = api_call(self, params) | 316 response = api_call(self, params) |
264 | 317 |
265 expected = 'Error occurred during rescan repositories action' | 318 expected = 'Error occurred during rescan repositories action' |
266 self._compare_error(id_, expected, given=response.body) | 319 self._compare_error(id_, expected, given=response.body) |
267 | 320 |
268 def test_api_invalidate_cache(self): | 321 def test_api_invalidate_cache(self): |
269 repo = RepoModel().get_by_repo_name(self.REPO) | 322 repo = RepoModel().get_by_repo_name(self.REPO) |
270 repo.scm_instance_cached() # seed cache | 323 repo.scm_instance_cached() # seed cache |
271 | 324 |
272 id_, params = _build_data(self.apikey, 'invalidate_cache', | 325 id_, params = _build_data(self.apikey, 'invalidate_cache', |
273 repoid=self.REPO) | 326 repoid=self.REPO) |
274 response = api_call(self, params) | 327 response = api_call(self, params) |
275 | 328 |
276 expected = ("Caches of repository `%s` was invalidated" % (self.REPO)) | 329 expected = { |
330 'msg': "Cache for repository `%s` was invalidated" % (self.REPO,), | |
331 'repository': self.REPO | |
332 } | |
277 self._compare_ok(id_, expected, given=response.body) | 333 self._compare_ok(id_, expected, given=response.body) |
278 | 334 |
279 @mock.patch.object(ScmModel, 'mark_for_invalidation', crash) | 335 @mock.patch.object(ScmModel, 'mark_for_invalidation', crash) |
280 def test_api_invalidate_cache_error(self): | 336 def test_api_invalidate_cache_error(self): |
281 id_, params = _build_data(self.apikey, 'invalidate_cache', | 337 id_, params = _build_data(self.apikey, 'invalidate_cache', |
282 repoid=self.REPO) | 338 repoid=self.REPO) |
283 response = api_call(self, params) | 339 response = api_call(self, params) |
284 | 340 |
285 expected = 'Error occurred during cache invalidation action' | 341 expected = 'Error occurred during cache invalidation action' |
342 self._compare_error(id_, expected, given=response.body) | |
343 | |
344 def test_api_invalidate_cache_regular_user_no_permission(self): | |
345 repo = RepoModel().get_by_repo_name(self.REPO) | |
346 repo.scm_instance_cached() # seed cache | |
347 | |
348 id_, params = _build_data(self.apikey_regular, 'invalidate_cache', | |
349 repoid=self.REPO) | |
350 response = api_call(self, params) | |
351 | |
352 expected = "repository `%s` does not exist" % (self.REPO,) | |
286 self._compare_error(id_, expected, given=response.body) | 353 self._compare_error(id_, expected, given=response.body) |
287 | 354 |
288 def test_api_lock_repo_lock_aquire(self): | 355 def test_api_lock_repo_lock_aquire(self): |
289 id_, params = _build_data(self.apikey, 'lock', | 356 id_, params = _build_data(self.apikey, 'lock', |
290 userid=TEST_USER_ADMIN_LOGIN, | 357 userid=TEST_USER_ADMIN_LOGIN, |
291 repoid=self.REPO, | 358 repoid=self.REPO, |
292 locked=True) | 359 locked=True) |
293 response = api_call(self, params) | 360 response = api_call(self, params) |
294 expected = { | 361 expected = { |
295 'repo': self.REPO, | 362 'repo': self.REPO, 'locked': True, |
296 'locked': True, | 363 'locked_since': response.json['result']['locked_since'], |
297 'locked_since': None, | |
298 'locked_by': TEST_USER_ADMIN_LOGIN, | 364 'locked_by': TEST_USER_ADMIN_LOGIN, |
365 'lock_state_changed': True, | |
299 'msg': ('User `%s` set lock state for repo `%s` to `%s`' | 366 'msg': ('User `%s` set lock state for repo `%s` to `%s`' |
300 % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) | 367 % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) |
301 } | 368 } |
302 expected['locked_since'] = json.loads(response.body)['result']['locked_since'] | |
303 self._compare_ok(id_, expected, given=response.body) | 369 self._compare_ok(id_, expected, given=response.body) |
304 | 370 |
305 def test_api_lock_repo_lock_aquire_by_non_admin(self): | 371 def test_api_lock_repo_lock_aquire_by_non_admin(self): |
306 repo_name = 'api_delete_me' | 372 repo_name = 'api_delete_me' |
307 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE, | 373 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE, |
312 locked=True) | 378 locked=True) |
313 response = api_call(self, params) | 379 response = api_call(self, params) |
314 expected = { | 380 expected = { |
315 'repo': repo_name, | 381 'repo': repo_name, |
316 'locked': True, | 382 'locked': True, |
317 'locked_since': None, | 383 'locked_since': response.json['result']['locked_since'], |
318 'locked_by': self.TEST_USER_LOGIN, | 384 'locked_by': self.TEST_USER_LOGIN, |
385 'lock_state_changed': True, | |
319 'msg': ('User `%s` set lock state for repo `%s` to `%s`' | 386 'msg': ('User `%s` set lock state for repo `%s` to `%s`' |
320 % (self.TEST_USER_LOGIN, repo_name, True)) | 387 % (self.TEST_USER_LOGIN, repo_name, True)) |
321 } | 388 } |
322 expected['locked_since'] = json.loads(response.body)['result']['locked_since'] | |
323 self._compare_ok(id_, expected, given=response.body) | 389 self._compare_ok(id_, expected, given=response.body) |
324 finally: | 390 finally: |
325 fixture.destroy_repo(repo_name) | 391 fixture.destroy_repo(repo_name) |
326 | 392 |
327 def test_api_lock_repo_lock_aquire_non_admin_with_userid(self): | 393 def test_api_lock_repo_lock_aquire_non_admin_with_userid(self): |
356 expected = { | 422 expected = { |
357 'repo': self.REPO, | 423 'repo': self.REPO, |
358 'locked': False, | 424 'locked': False, |
359 'locked_since': None, | 425 'locked_since': None, |
360 'locked_by': TEST_USER_ADMIN_LOGIN, | 426 'locked_by': TEST_USER_ADMIN_LOGIN, |
427 'lock_state_changed': True, | |
361 'msg': ('User `%s` set lock state for repo `%s` to `%s`' | 428 'msg': ('User `%s` set lock state for repo `%s` to `%s`' |
362 % (TEST_USER_ADMIN_LOGIN, self.REPO, False)) | 429 % (TEST_USER_ADMIN_LOGIN, self.REPO, False)) |
363 } | 430 } |
364 self._compare_ok(id_, expected, given=response.body) | 431 self._compare_ok(id_, expected, given=response.body) |
365 | 432 |
366 def test_api_lock_repo_lock_aquire_optional_userid(self): | 433 def test_api_lock_repo_lock_aquire_optional_userid(self): |
367 id_, params = _build_data(self.apikey, 'lock', | 434 id_, params = _build_data(self.apikey, 'lock', |
368 repoid=self.REPO, | 435 repoid=self.REPO, |
369 locked=True) | 436 locked=True) |
370 response = api_call(self, params) | 437 response = api_call(self, params) |
438 time_ = response.json['result']['locked_since'] | |
371 expected = { | 439 expected = { |
372 'repo': self.REPO, | 440 'repo': self.REPO, |
373 'locked': True, | 441 'locked': True, |
374 'locked_since': None, | 442 'locked_since': time_, |
375 'locked_by': TEST_USER_ADMIN_LOGIN, | 443 'locked_by': TEST_USER_ADMIN_LOGIN, |
444 'lock_state_changed': True, | |
376 'msg': ('User `%s` set lock state for repo `%s` to `%s`' | 445 'msg': ('User `%s` set lock state for repo `%s` to `%s`' |
377 % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) | 446 % (TEST_USER_ADMIN_LOGIN, self.REPO, True)) |
378 } | 447 } |
379 expected['locked_since'] = json.loads(response.body)['result']['locked_since'] | 448 |
380 self._compare_ok(id_, expected, given=response.body) | 449 self._compare_ok(id_, expected, given=response.body) |
381 | 450 |
382 def test_api_lock_repo_lock_optional_locked(self): | 451 def test_api_lock_repo_lock_optional_locked(self): |
383 id_, params = _build_data(self.apikey, 'lock', | 452 id_, params = _build_data(self.apikey, 'lock', |
384 repoid=self.REPO) | 453 repoid=self.REPO) |
385 response = api_call(self, params) | 454 response = api_call(self, params) |
386 time_ = json.loads(response.body)['result']['locked_since'] | 455 time_ = response.json['result']['locked_since'] |
387 expected = { | 456 expected = { |
388 'repo': self.REPO, | 457 'repo': self.REPO, |
389 'locked': True, | 458 'locked': True, |
390 'locked_since': None, | 459 'locked_since': time_, |
391 'locked_by': TEST_USER_ADMIN_LOGIN, | 460 'locked_by': TEST_USER_ADMIN_LOGIN, |
392 'msg': ('Repo `%s` locked by `%s`. ' | 461 'lock_state_changed': False, |
393 % (self.REPO, | 462 'msg': ('Repo `%s` locked by `%s` on `%s`.' |
394 json.dumps(time_to_datetime(time_)))) | 463 % (self.REPO, TEST_USER_ADMIN_LOGIN, |
395 | 464 json.dumps(time_to_datetime(time_)))) |
396 } | 465 } |
397 expected['locked_since'] = time_ | 466 self._compare_ok(id_, expected, given=response.body) |
398 self._compare_ok(id_, expected, given=response.body) | 467 |
468 def test_api_lock_repo_lock_optional_not_locked(self): | |
469 repo_name = 'api_not_locked' | |
470 repo = fixture.create_repo(repo_name, repo_type=self.REPO_TYPE, | |
471 cur_user=self.TEST_USER_LOGIN) | |
472 self.assertEqual(repo.locked, [None, None]) | |
473 try: | |
474 id_, params = _build_data(self.apikey, 'lock', | |
475 repoid=repo.repo_id) | |
476 response = api_call(self, params) | |
477 expected = { | |
478 'repo': repo_name, | |
479 'locked': False, | |
480 'locked_since': None, | |
481 'locked_by': None, | |
482 'lock_state_changed': False, | |
483 'msg': ('Repo `%s` not locked.' % (repo_name,)) | |
484 } | |
485 self._compare_ok(id_, expected, given=response.body) | |
486 finally: | |
487 fixture.destroy_repo(repo_name) | |
399 | 488 |
400 @mock.patch.object(Repository, 'lock', crash) | 489 @mock.patch.object(Repository, 'lock', crash) |
401 def test_api_lock_error(self): | 490 def test_api_lock_error(self): |
402 id_, params = _build_data(self.apikey, 'lock', | 491 id_, params = _build_data(self.apikey, 'lock', |
403 userid=TEST_USER_ADMIN_LOGIN, | 492 userid=TEST_USER_ADMIN_LOGIN, |
425 id_, params = _build_data(self.apikey, 'get_locks') | 514 id_, params = _build_data(self.apikey, 'get_locks') |
426 response = api_call(self, params) | 515 response = api_call(self, params) |
427 expected = [] | 516 expected = [] |
428 self._compare_ok(id_, expected, given=response.body) | 517 self._compare_ok(id_, expected, given=response.body) |
429 | 518 |
519 def test_api_get_locks_with_one_locked_repo(self): | |
520 repo_name = 'api_delete_me' | |
521 repo = fixture.create_repo(repo_name, repo_type=self.REPO_TYPE, | |
522 cur_user=self.TEST_USER_LOGIN) | |
523 Repository.lock(repo, User.get_by_username(self.TEST_USER_LOGIN).user_id) | |
524 try: | |
525 id_, params = _build_data(self.apikey, 'get_locks') | |
526 response = api_call(self, params) | |
527 expected = [repo.get_api_data()] | |
528 self._compare_ok(id_, expected, given=response.body) | |
529 finally: | |
530 fixture.destroy_repo(repo_name) | |
531 | |
532 def test_api_get_locks_with_one_locked_repo_for_specific_user(self): | |
533 repo_name = 'api_delete_me' | |
534 repo = fixture.create_repo(repo_name, repo_type=self.REPO_TYPE, | |
535 cur_user=self.TEST_USER_LOGIN) | |
536 Repository.lock(repo, User.get_by_username(self.TEST_USER_LOGIN).user_id) | |
537 try: | |
538 id_, params = _build_data(self.apikey, 'get_locks', | |
539 userid=self.TEST_USER_LOGIN) | |
540 response = api_call(self, params) | |
541 expected = [repo.get_api_data()] | |
542 self._compare_ok(id_, expected, given=response.body) | |
543 finally: | |
544 fixture.destroy_repo(repo_name) | |
545 | |
430 def test_api_get_locks_with_userid(self): | 546 def test_api_get_locks_with_userid(self): |
431 id_, params = _build_data(self.apikey, 'get_locks', | 547 id_, params = _build_data(self.apikey, 'get_locks', |
432 userid=TEST_USER_REGULAR_LOGIN) | 548 userid=TEST_USER_REGULAR_LOGIN) |
433 response = api_call(self, params) | 549 response = api_call(self, params) |
434 expected = [] | 550 expected = [] |
468 ret = dict( | 584 ret = dict( |
469 msg='created new user `%s`' % username, | 585 msg='created new user `%s`' % username, |
470 user=jsonify(usr.get_api_data()) | 586 user=jsonify(usr.get_api_data()) |
471 ) | 587 ) |
472 | 588 |
473 expected = ret | 589 try: |
474 self._compare_ok(id_, expected, given=response.body) | 590 expected = ret |
475 | 591 self._compare_ok(id_, expected, given=response.body) |
476 UserModel().delete(usr.user_id) | 592 finally: |
477 Session().commit() | 593 fixture.destroy_user(usr.user_id) |
478 | 594 |
479 def test_api_create_user_without_password(self): | 595 def test_api_create_user_without_password(self): |
480 username = 'test_new_api_user_passwordless' | 596 username = 'test_new_api_user_passwordless' |
481 email = username + "@foo.com" | 597 email = username + "@foo.com" |
482 | 598 |
488 usr = UserModel().get_by_username(username) | 604 usr = UserModel().get_by_username(username) |
489 ret = dict( | 605 ret = dict( |
490 msg='created new user `%s`' % username, | 606 msg='created new user `%s`' % username, |
491 user=jsonify(usr.get_api_data()) | 607 user=jsonify(usr.get_api_data()) |
492 ) | 608 ) |
493 | 609 try: |
494 expected = ret | 610 expected = ret |
495 self._compare_ok(id_, expected, given=response.body) | 611 self._compare_ok(id_, expected, given=response.body) |
496 | 612 finally: |
497 UserModel().delete(usr.user_id) | 613 fixture.destroy_user(usr.user_id) |
498 Session().commit() | 614 |
615 def test_api_create_user_with_extern_name(self): | |
616 username = 'test_new_api_user_passwordless' | |
617 email = username + "@foo.com" | |
618 | |
619 id_, params = _build_data(self.apikey, 'create_user', | |
620 username=username, | |
621 email=email, extern_name='rhodecode') | |
622 response = api_call(self, params) | |
623 | |
624 usr = UserModel().get_by_username(username) | |
625 ret = dict( | |
626 msg='created new user `%s`' % username, | |
627 user=jsonify(usr.get_api_data()) | |
628 ) | |
629 try: | |
630 expected = ret | |
631 self._compare_ok(id_, expected, given=response.body) | |
632 finally: | |
633 fixture.destroy_user(usr.user_id) | |
499 | 634 |
500 @mock.patch.object(UserModel, 'create_or_update', crash) | 635 @mock.patch.object(UserModel, 'create_or_update', crash) |
501 def test_api_create_user_when_exception_happened(self): | 636 def test_api_create_user_when_exception_happened(self): |
502 | 637 |
503 username = 'test_new_api_user' | 638 username = 'test_new_api_user' |
521 email = usr.email | 656 email = usr.email |
522 usr_id = usr.user_id | 657 usr_id = usr.user_id |
523 ## DELETE THIS USER NOW | 658 ## DELETE THIS USER NOW |
524 | 659 |
525 id_, params = _build_data(self.apikey, 'delete_user', | 660 id_, params = _build_data(self.apikey, 'delete_user', |
526 userid=username,) | 661 userid=username, ) |
527 response = api_call(self, params) | 662 response = api_call(self, params) |
528 | 663 |
529 ret = {'msg': 'deleted user ID:%s %s' % (usr_id, username), | 664 ret = {'msg': 'deleted user ID:%s %s' % (usr_id, username), |
530 'user': None} | 665 'user': None} |
531 expected = ret | 666 expected = ret |
539 firstname=u'u1', lastname=u'u1') | 674 firstname=u'u1', lastname=u'u1') |
540 Session().commit() | 675 Session().commit() |
541 username = usr.username | 676 username = usr.username |
542 | 677 |
543 id_, params = _build_data(self.apikey, 'delete_user', | 678 id_, params = _build_data(self.apikey, 'delete_user', |
544 userid=username,) | 679 userid=username, ) |
545 response = api_call(self, params) | 680 response = api_call(self, params) |
546 ret = 'failed to delete ID:%s %s' % (usr.user_id, | 681 ret = 'failed to delete user ID:%s %s' % (usr.user_id, |
547 usr.username) | 682 usr.username) |
548 expected = ret | 683 expected = ret |
549 self._compare_error(id_, expected, given=response.body) | 684 self._compare_error(id_, expected, given=response.body) |
550 | 685 |
551 @parameterized.expand([('firstname', 'new_username'), | 686 @parameterized.expand([('firstname', 'new_username'), |
552 ('lastname', 'new_username'), | 687 ('lastname', 'new_username'), |
553 ('email', 'new_username'), | 688 ('email', 'new_username'), |
554 ('admin', True), | 689 ('admin', True), |
555 ('admin', False), | 690 ('admin', False), |
556 ('ldap_dn', 'test'), | 691 ('extern_type', 'ldap'), |
557 ('ldap_dn', None), | 692 ('extern_type', None), |
693 ('extern_name', 'test'), | |
694 ('extern_name', None), | |
558 ('active', False), | 695 ('active', False), |
559 ('active', True), | 696 ('active', True), |
560 ('password', 'newpass') | 697 ('password', 'newpass') |
561 ]) | 698 ]) |
562 def test_api_update_user(self, name, expected): | 699 def test_api_update_user(self, name, expected): |
563 usr = UserModel().get_by_username(self.TEST_USER_LOGIN) | 700 usr = UserModel().get_by_username(self.TEST_USER_LOGIN) |
564 kw = {name: expected, | 701 kw = {name: expected, |
565 'userid': usr.user_id} | 702 'userid': usr.user_id} |
566 id_, params = _build_data(self.apikey, 'update_user', **kw) | 703 id_, params = _build_data(self.apikey, 'update_user', **kw) |
567 response = api_call(self, params) | 704 response = api_call(self, params) |
568 | 705 |
569 ret = { | 706 ret = { |
570 'msg': 'updated user ID:%s %s' % (usr.user_id, self.TEST_USER_LOGIN), | 707 'msg': 'updated user ID:%s %s' % ( |
571 'user': jsonify(UserModel()\ | 708 usr.user_id, self.TEST_USER_LOGIN), |
572 .get_by_username(self.TEST_USER_LOGIN)\ | 709 'user': jsonify(UserModel() \ |
573 .get_api_data()) | 710 .get_by_username(self.TEST_USER_LOGIN) \ |
711 .get_api_data()) | |
574 } | 712 } |
575 | 713 |
576 expected = ret | 714 expected = ret |
577 self._compare_ok(id_, expected, given=response.body) | 715 self._compare_ok(id_, expected, given=response.body) |
578 | 716 |
582 id_, params = _build_data(self.apikey, 'update_user', | 720 id_, params = _build_data(self.apikey, 'update_user', |
583 userid=TEST_USER_ADMIN_LOGIN) | 721 userid=TEST_USER_ADMIN_LOGIN) |
584 | 722 |
585 response = api_call(self, params) | 723 response = api_call(self, params) |
586 ret = { | 724 ret = { |
587 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN), | 725 'msg': 'updated user ID:%s %s' % ( |
588 'user': ret | 726 usr.user_id, TEST_USER_ADMIN_LOGIN), |
727 'user': ret | |
589 } | 728 } |
590 expected = ret | 729 expected = ret |
591 self._compare_ok(id_, expected, given=response.body) | 730 self._compare_ok(id_, expected, given=response.body) |
592 | 731 |
593 def test_api_update_user_by_user_id(self): | 732 def test_api_update_user_by_user_id(self): |
596 id_, params = _build_data(self.apikey, 'update_user', | 735 id_, params = _build_data(self.apikey, 'update_user', |
597 userid=usr.user_id) | 736 userid=usr.user_id) |
598 | 737 |
599 response = api_call(self, params) | 738 response = api_call(self, params) |
600 ret = { | 739 ret = { |
601 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN), | 740 'msg': 'updated user ID:%s %s' % ( |
602 'user': ret | 741 usr.user_id, TEST_USER_ADMIN_LOGIN), |
603 } | 742 'user': ret |
604 expected = ret | 743 } |
605 self._compare_ok(id_, expected, given=response.body) | 744 expected = ret |
745 self._compare_ok(id_, expected, given=response.body) | |
746 | |
747 def test_api_update_user_default_user(self): | |
748 usr = User.get_default_user() | |
749 id_, params = _build_data(self.apikey, 'update_user', | |
750 userid=usr.user_id) | |
751 | |
752 response = api_call(self, params) | |
753 expected = 'editing default user is forbidden' | |
754 self._compare_error(id_, expected, given=response.body) | |
606 | 755 |
607 @mock.patch.object(UserModel, 'update_user', crash) | 756 @mock.patch.object(UserModel, 'update_user', crash) |
608 def test_api_update_user_when_exception_happens(self): | 757 def test_api_update_user_when_exception_happens(self): |
609 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) | 758 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) |
610 ret = jsonify(usr.get_api_data()) | 759 ret = jsonify(usr.get_api_data()) |
617 expected = ret | 766 expected = ret |
618 self._compare_error(id_, expected, given=response.body) | 767 self._compare_error(id_, expected, given=response.body) |
619 | 768 |
620 def test_api_get_repo(self): | 769 def test_api_get_repo(self): |
621 new_group = 'some_new_group' | 770 new_group = 'some_new_group' |
622 make_users_group(new_group) | 771 make_user_group(new_group) |
623 RepoModel().grant_users_group_permission(repo=self.REPO, | 772 RepoModel().grant_user_group_permission(repo=self.REPO, |
624 group_name=new_group, | 773 group_name=new_group, |
625 perm='repository.read') | 774 perm='repository.read') |
626 Session().commit() | 775 Session().commit() |
627 id_, params = _build_data(self.apikey, 'get_repo', | 776 id_, params = _build_data(self.apikey, 'get_repo', |
628 repoid=self.REPO) | 777 repoid=self.REPO) |
629 response = api_call(self, params) | 778 response = api_call(self, params) |
630 | 779 |
634 members = [] | 783 members = [] |
635 followers = [] | 784 followers = [] |
636 for user in repo.repo_to_perm: | 785 for user in repo.repo_to_perm: |
637 perm = user.permission.permission_name | 786 perm = user.permission.permission_name |
638 user = user.user | 787 user = user.user |
639 user_data = user.get_api_data() | 788 user_data = {'name': user.username, 'type': "user", |
640 user_data['type'] = "user" | 789 'permission': perm} |
641 user_data['permission'] = perm | |
642 members.append(user_data) | 790 members.append(user_data) |
643 | 791 |
644 for users_group in repo.users_group_to_perm: | 792 for user_group in repo.users_group_to_perm: |
645 perm = users_group.permission.permission_name | 793 perm = user_group.permission.permission_name |
646 users_group = users_group.users_group | 794 user_group = user_group.users_group |
647 users_group_data = users_group.get_api_data() | 795 user_group_data = {'name': user_group.users_group_name, |
648 users_group_data['type'] = "users_group" | 796 'type': "user_group", 'permission': perm} |
649 users_group_data['permission'] = perm | 797 members.append(user_group_data) |
650 members.append(users_group_data) | |
651 | 798 |
652 for user in repo.followers: | 799 for user in repo.followers: |
653 followers.append(user.user.get_api_data()) | 800 followers.append(user.user.get_api_data()) |
654 | 801 |
655 ret['members'] = members | 802 ret['members'] = members |
656 ret['followers'] = followers | 803 ret['followers'] = followers |
657 | 804 |
658 expected = ret | 805 expected = ret |
659 self._compare_ok(id_, expected, given=response.body) | 806 self._compare_ok(id_, expected, given=response.body) |
660 destroy_users_group(new_group) | 807 fixture.destroy_user_group(new_group) |
661 | 808 |
662 def test_api_get_repo_by_non_admin(self): | 809 @parameterized.expand([ |
663 id_, params = _build_data(self.apikey, 'get_repo', | 810 ('repository.admin',), |
811 ('repository.write',), | |
812 ('repository.read',), | |
813 ]) | |
814 def test_api_get_repo_by_non_admin(self, grant_perm): | |
815 RepoModel().grant_user_permission(repo=self.REPO, | |
816 user=self.TEST_USER_LOGIN, | |
817 perm=grant_perm) | |
818 Session().commit() | |
819 id_, params = _build_data(self.apikey_regular, 'get_repo', | |
664 repoid=self.REPO) | 820 repoid=self.REPO) |
665 response = api_call(self, params) | 821 response = api_call(self, params) |
666 | 822 |
667 repo = RepoModel().get_by_repo_name(self.REPO) | 823 repo = RepoModel().get_by_repo_name(self.REPO) |
668 ret = repo.get_api_data() | 824 ret = repo.get_api_data() |
669 | 825 |
670 members = [] | 826 members = [] |
671 followers = [] | 827 followers = [] |
828 self.assertEqual(2, len(repo.repo_to_perm)) | |
672 for user in repo.repo_to_perm: | 829 for user in repo.repo_to_perm: |
673 perm = user.permission.permission_name | 830 perm = user.permission.permission_name |
674 user = user.user | 831 user_obj = user.user |
675 user_data = user.get_api_data() | 832 user_data = {'name': user_obj.username, 'type': "user", |
676 user_data['type'] = "user" | 833 'permission': perm} |
677 user_data['permission'] = perm | |
678 members.append(user_data) | 834 members.append(user_data) |
679 | 835 |
680 for users_group in repo.users_group_to_perm: | 836 for user_group in repo.users_group_to_perm: |
681 perm = users_group.permission.permission_name | 837 perm = user_group.permission.permission_name |
682 users_group = users_group.users_group | 838 user_group_obj = user_group.users_group |
683 users_group_data = users_group.get_api_data() | 839 user_group_data = {'name': user_group_obj.users_group_name, |
684 users_group_data['type'] = "users_group" | 840 'type': "user_group", 'permission': perm} |
685 users_group_data['permission'] = perm | 841 members.append(user_group_data) |
686 members.append(users_group_data) | |
687 | 842 |
688 for user in repo.followers: | 843 for user in repo.followers: |
689 followers.append(user.user.get_api_data()) | 844 followers.append(user.user.get_api_data()) |
690 | 845 |
691 ret['members'] = members | 846 ret['members'] = members |
692 ret['followers'] = followers | 847 ret['followers'] = followers |
693 | 848 |
694 expected = ret | 849 expected = ret |
695 self._compare_ok(id_, expected, given=response.body) | 850 try: |
851 self._compare_ok(id_, expected, given=response.body) | |
852 finally: | |
853 RepoModel().revoke_user_permission(self.REPO, self.TEST_USER_LOGIN) | |
696 | 854 |
697 def test_api_get_repo_by_non_admin_no_permission_to_repo(self): | 855 def test_api_get_repo_by_non_admin_no_permission_to_repo(self): |
698 RepoModel().grant_user_permission(repo=self.REPO, | 856 RepoModel().grant_user_permission(repo=self.REPO, |
699 user=self.TEST_USER_LOGIN, | 857 user=self.TEST_USER_LOGIN, |
700 perm='repository.none') | 858 perm='repository.none') |
751 ret_type=ret_type) | 909 ret_type=ret_type) |
752 response = api_call(self, params) | 910 response = api_call(self, params) |
753 | 911 |
754 # we don't the actual return types here since it's tested somewhere | 912 # we don't the actual return types here since it's tested somewhere |
755 # else | 913 # else |
756 expected = json.loads(response.body)['result'] | 914 expected = response.json['result'] |
757 self._compare_ok(id_, expected, given=response.body) | 915 self._compare_ok(id_, expected, given=response.body) |
758 | 916 |
759 def test_api_get_repo_nodes_bad_revisions(self): | 917 def test_api_get_repo_nodes_bad_revisions(self): |
760 rev = 'i-dont-exist' | 918 rev = 'i-dont-exist' |
761 path = '/' | 919 path = '/' |
762 id_, params = _build_data(self.apikey, 'get_repo_nodes', | 920 id_, params = _build_data(self.apikey, 'get_repo_nodes', |
763 repoid=self.REPO, revision=rev, | 921 repoid=self.REPO, revision=rev, |
764 root_path=path,) | 922 root_path=path, ) |
765 response = api_call(self, params) | 923 response = api_call(self, params) |
766 | 924 |
767 expected = 'failed to get repo: `%s` nodes' % self.REPO | 925 expected = 'failed to get repo: `%s` nodes' % self.REPO |
768 self._compare_error(id_, expected, given=response.body) | 926 self._compare_error(id_, expected, given=response.body) |
769 | 927 |
770 def test_api_get_repo_nodes_bad_path(self): | 928 def test_api_get_repo_nodes_bad_path(self): |
771 rev = 'tip' | 929 rev = 'tip' |
772 path = '/idontexits' | 930 path = '/idontexits' |
773 id_, params = _build_data(self.apikey, 'get_repo_nodes', | 931 id_, params = _build_data(self.apikey, 'get_repo_nodes', |
774 repoid=self.REPO, revision=rev, | 932 repoid=self.REPO, revision=rev, |
775 root_path=path,) | 933 root_path=path, ) |
776 response = api_call(self, params) | 934 response = api_call(self, params) |
777 | 935 |
778 expected = 'failed to get repo: `%s` nodes' % self.REPO | 936 expected = 'failed to get repo: `%s` nodes' % self.REPO |
779 self._compare_error(id_, expected, given=response.body) | 937 self._compare_error(id_, expected, given=response.body) |
780 | 938 |
786 repoid=self.REPO, revision=rev, | 944 repoid=self.REPO, revision=rev, |
787 root_path=path, | 945 root_path=path, |
788 ret_type=ret_type) | 946 ret_type=ret_type) |
789 response = api_call(self, params) | 947 response = api_call(self, params) |
790 | 948 |
791 expected = 'ret_type must be one of %s' % (['files', 'dirs', 'all']) | 949 expected = ('ret_type must be one of %s' |
792 self._compare_error(id_, expected, given=response.body) | 950 % (','.join(['files', 'dirs', 'all']))) |
951 self._compare_error(id_, expected, given=response.body) | |
952 | |
953 @parameterized.expand([('all', 'all', 'repository.write'), | |
954 ('dirs', 'dirs', 'repository.admin'), | |
955 ('files', 'files', 'repository.read'), ]) | |
956 def test_api_get_repo_nodes_by_regular_user(self, name, ret_type, grant_perm): | |
957 RepoModel().grant_user_permission(repo=self.REPO, | |
958 user=self.TEST_USER_LOGIN, | |
959 perm=grant_perm) | |
960 Session().commit() | |
961 | |
962 rev = 'tip' | |
963 path = '/' | |
964 id_, params = _build_data(self.apikey_regular, 'get_repo_nodes', | |
965 repoid=self.REPO, revision=rev, | |
966 root_path=path, | |
967 ret_type=ret_type) | |
968 response = api_call(self, params) | |
969 | |
970 # we don't the actual return types here since it's tested somewhere | |
971 # else | |
972 expected = response.json['result'] | |
973 try: | |
974 self._compare_ok(id_, expected, given=response.body) | |
975 finally: | |
976 RepoModel().revoke_user_permission(self.REPO, self.TEST_USER_LOGIN) | |
793 | 977 |
794 def test_api_create_repo(self): | 978 def test_api_create_repo(self): |
795 repo_name = 'api-repo' | 979 repo_name = 'api-repo' |
796 id_, params = _build_data(self.apikey, 'create_repo', | 980 id_, params = _build_data(self.apikey, 'create_repo', |
797 repo_name=repo_name, | 981 repo_name=repo_name, |
798 owner=TEST_USER_ADMIN_LOGIN, | 982 owner=TEST_USER_ADMIN_LOGIN, |
799 repo_type='hg', | 983 repo_type=self.REPO_TYPE, |
800 ) | 984 ) |
801 response = api_call(self, params) | 985 response = api_call(self, params) |
802 | 986 |
803 repo = RepoModel().get_by_repo_name(repo_name) | 987 repo = RepoModel().get_by_repo_name(repo_name) |
988 self.assertNotEqual(repo, None) | |
804 ret = { | 989 ret = { |
805 'msg': 'Created new repository `%s`' % repo_name, | 990 'msg': 'Created new repository `%s`' % repo_name, |
806 'repo': jsonify(repo.get_api_data()) | 991 'success': True, |
992 'task': None, | |
807 } | 993 } |
808 expected = ret | 994 expected = ret |
809 self._compare_ok(id_, expected, given=response.body) | 995 self._compare_ok(id_, expected, given=response.body) |
810 fixture.destroy_repo(repo_name) | 996 fixture.destroy_repo(repo_name) |
997 | |
998 def test_api_create_repo_in_group(self): | |
999 repo_name = 'my_gr/api-repo' | |
1000 id_, params = _build_data(self.apikey, 'create_repo', | |
1001 repo_name=repo_name, | |
1002 owner=TEST_USER_ADMIN_LOGIN, | |
1003 repo_type=self.REPO_TYPE,) | |
1004 response = api_call(self, params) | |
1005 print params | |
1006 repo = RepoModel().get_by_repo_name(repo_name) | |
1007 self.assertNotEqual(repo, None) | |
1008 ret = { | |
1009 'msg': 'Created new repository `%s`' % repo_name, | |
1010 'success': True, | |
1011 'task': None, | |
1012 } | |
1013 expected = ret | |
1014 self._compare_ok(id_, expected, given=response.body) | |
1015 fixture.destroy_repo(repo_name) | |
1016 fixture.destroy_repo_group('my_gr') | |
811 | 1017 |
812 def test_api_create_repo_unknown_owner(self): | 1018 def test_api_create_repo_unknown_owner(self): |
813 repo_name = 'api-repo' | 1019 repo_name = 'api-repo' |
814 owner = 'i-dont-exist' | 1020 owner = 'i-dont-exist' |
815 id_, params = _build_data(self.apikey, 'create_repo', | 1021 id_, params = _build_data(self.apikey, 'create_repo', |
816 repo_name=repo_name, | 1022 repo_name=repo_name, |
817 owner=owner, | 1023 owner=owner, |
818 repo_type='hg', | 1024 repo_type=self.REPO_TYPE, |
819 ) | 1025 ) |
820 response = api_call(self, params) | 1026 response = api_call(self, params) |
821 expected = 'user `%s` does not exist' % owner | 1027 expected = 'user `%s` does not exist' % owner |
822 self._compare_error(id_, expected, given=response.body) | 1028 self._compare_error(id_, expected, given=response.body) |
823 | 1029 |
824 def test_api_create_repo_dont_specify_owner(self): | 1030 def test_api_create_repo_dont_specify_owner(self): |
825 repo_name = 'api-repo' | 1031 repo_name = 'api-repo' |
826 owner = 'i-dont-exist' | 1032 owner = 'i-dont-exist' |
827 id_, params = _build_data(self.apikey, 'create_repo', | 1033 id_, params = _build_data(self.apikey, 'create_repo', |
828 repo_name=repo_name, | 1034 repo_name=repo_name, |
829 repo_type='hg', | 1035 repo_type=self.REPO_TYPE, |
830 ) | 1036 ) |
831 response = api_call(self, params) | 1037 response = api_call(self, params) |
832 | 1038 |
833 repo = RepoModel().get_by_repo_name(repo_name) | 1039 repo = RepoModel().get_by_repo_name(repo_name) |
1040 self.assertNotEqual(repo, None) | |
834 ret = { | 1041 ret = { |
835 'msg': 'Created new repository `%s`' % repo_name, | 1042 'msg': 'Created new repository `%s`' % repo_name, |
836 'repo': jsonify(repo.get_api_data()) | 1043 'success': True, |
1044 'task': None, | |
837 } | 1045 } |
838 expected = ret | 1046 expected = ret |
839 self._compare_ok(id_, expected, given=response.body) | 1047 self._compare_ok(id_, expected, given=response.body) |
840 fixture.destroy_repo(repo_name) | 1048 fixture.destroy_repo(repo_name) |
841 | 1049 |
842 def test_api_create_repo_by_non_admin(self): | 1050 def test_api_create_repo_by_non_admin(self): |
843 repo_name = 'api-repo' | 1051 repo_name = 'api-repo' |
844 owner = 'i-dont-exist' | 1052 owner = 'i-dont-exist' |
845 id_, params = _build_data(self.apikey_regular, 'create_repo', | 1053 id_, params = _build_data(self.apikey_regular, 'create_repo', |
846 repo_name=repo_name, | 1054 repo_name=repo_name, |
847 repo_type='hg', | 1055 repo_type=self.REPO_TYPE, |
848 ) | 1056 ) |
849 response = api_call(self, params) | 1057 response = api_call(self, params) |
850 | 1058 |
851 repo = RepoModel().get_by_repo_name(repo_name) | 1059 repo = RepoModel().get_by_repo_name(repo_name) |
1060 self.assertNotEqual(repo, None) | |
852 ret = { | 1061 ret = { |
853 'msg': 'Created new repository `%s`' % repo_name, | 1062 'msg': 'Created new repository `%s`' % repo_name, |
854 'repo': jsonify(repo.get_api_data()) | 1063 'success': True, |
1064 'task': None, | |
855 } | 1065 } |
856 expected = ret | 1066 expected = ret |
857 self._compare_ok(id_, expected, given=response.body) | 1067 self._compare_ok(id_, expected, given=response.body) |
858 fixture.destroy_repo(repo_name) | 1068 fixture.destroy_repo(repo_name) |
859 | 1069 |
860 def test_api_create_repo_by_non_admin_specify_owner(self): | 1070 def test_api_create_repo_by_non_admin_specify_owner(self): |
861 repo_name = 'api-repo' | 1071 repo_name = 'api-repo' |
862 owner = 'i-dont-exist' | 1072 owner = 'i-dont-exist' |
863 id_, params = _build_data(self.apikey_regular, 'create_repo', | 1073 id_, params = _build_data(self.apikey_regular, 'create_repo', |
864 repo_name=repo_name, | 1074 repo_name=repo_name, |
865 repo_type='hg', | 1075 repo_type=self.REPO_TYPE, |
866 owner=owner | 1076 owner=owner) |
867 ) | |
868 response = api_call(self, params) | 1077 response = api_call(self, params) |
869 | 1078 |
870 expected = 'Only RhodeCode admin can specify `owner` param' | 1079 expected = 'Only RhodeCode admin can specify `owner` param' |
871 self._compare_error(id_, expected, given=response.body) | 1080 self._compare_error(id_, expected, given=response.body) |
872 fixture.destroy_repo(repo_name) | 1081 fixture.destroy_repo(repo_name) |
873 | 1082 |
874 def test_api_create_repo_exists(self): | 1083 def test_api_create_repo_exists(self): |
875 repo_name = self.REPO | 1084 repo_name = self.REPO |
876 id_, params = _build_data(self.apikey, 'create_repo', | 1085 id_, params = _build_data(self.apikey, 'create_repo', |
877 repo_name=repo_name, | 1086 repo_name=repo_name, |
878 owner=TEST_USER_ADMIN_LOGIN, | 1087 owner=TEST_USER_ADMIN_LOGIN, |
879 repo_type='hg', | 1088 repo_type=self.REPO_TYPE,) |
880 ) | |
881 response = api_call(self, params) | 1089 response = api_call(self, params) |
882 expected = "repo `%s` already exist" % repo_name | 1090 expected = "repo `%s` already exist" % repo_name |
883 self._compare_error(id_, expected, given=response.body) | 1091 self._compare_error(id_, expected, given=response.body) |
884 | 1092 |
885 @mock.patch.object(RepoModel, 'create_repo', crash) | 1093 @mock.patch.object(RepoModel, 'create', crash) |
886 def test_api_create_repo_exception_occurred(self): | 1094 def test_api_create_repo_exception_occurred(self): |
887 repo_name = 'api-repo' | 1095 repo_name = 'api-repo' |
888 id_, params = _build_data(self.apikey, 'create_repo', | 1096 id_, params = _build_data(self.apikey, 'create_repo', |
889 repo_name=repo_name, | 1097 repo_name=repo_name, |
890 owner=TEST_USER_ADMIN_LOGIN, | 1098 owner=TEST_USER_ADMIN_LOGIN, |
891 repo_type='hg', | 1099 repo_type=self.REPO_TYPE,) |
892 ) | |
893 response = api_call(self, params) | 1100 response = api_call(self, params) |
894 expected = 'failed to create repository `%s`' % repo_name | 1101 expected = 'failed to create repository `%s`' % repo_name |
895 self._compare_error(id_, expected, given=response.body) | 1102 self._compare_error(id_, expected, given=response.body) |
1103 | |
1104 @parameterized.expand([ | |
1105 ('owner', {'owner': TEST_USER_REGULAR_LOGIN}), | |
1106 ('description', {'description': 'new description'}), | |
1107 ('active', {'active': True}), | |
1108 ('active', {'active': False}), | |
1109 ('clone_uri', {'clone_uri': 'http://foo.com/repo'}), | |
1110 ('clone_uri', {'clone_uri': None}), | |
1111 ('landing_rev', {'landing_rev': 'branch:master'}), | |
1112 ('enable_statistics', {'enable_statistics': True}), | |
1113 ('enable_locking', {'enable_locking': True}), | |
1114 ('enable_downloads', {'enable_downloads': True}), | |
1115 ('name', {'name': 'new_repo_name'}), | |
1116 ('repo_group', {'group': 'test_group_for_update'}), | |
1117 ]) | |
1118 def test_api_update_repo(self, changing_attr, updates): | |
1119 repo_name = 'api_update_me' | |
1120 repo = fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) | |
1121 if changing_attr == 'repo_group': | |
1122 fixture.create_repo_group(updates['group']) | |
1123 | |
1124 id_, params = _build_data(self.apikey, 'update_repo', | |
1125 repoid=repo_name, **updates) | |
1126 response = api_call(self, params) | |
1127 if changing_attr == 'name': | |
1128 repo_name = updates['name'] | |
1129 if changing_attr == 'repo_group': | |
1130 repo_name = '/'.join([updates['group'], repo_name]) | |
1131 try: | |
1132 expected = { | |
1133 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo_name), | |
1134 'repository': repo.get_api_data() | |
1135 } | |
1136 self._compare_ok(id_, expected, given=response.body) | |
1137 finally: | |
1138 fixture.destroy_repo(repo_name) | |
1139 if changing_attr == 'repo_group': | |
1140 fixture.destroy_repo_group(updates['group']) | |
1141 | |
1142 def test_api_update_repo_repo_group_does_not_exist(self): | |
1143 repo_name = 'admin_owned' | |
1144 fixture.create_repo(repo_name) | |
1145 updates = {'group': 'test_group_for_update'} | |
1146 id_, params = _build_data(self.apikey, 'update_repo', | |
1147 repoid=repo_name, **updates) | |
1148 response = api_call(self, params) | |
1149 try: | |
1150 expected = 'repository group `%s` does not exist' % updates['group'] | |
1151 self._compare_error(id_, expected, given=response.body) | |
1152 finally: | |
1153 fixture.destroy_repo(repo_name) | |
1154 | |
1155 def test_api_update_repo_regular_user_not_allowed(self): | |
1156 repo_name = 'admin_owned' | |
1157 fixture.create_repo(repo_name) | |
1158 updates = {'active': False} | |
1159 id_, params = _build_data(self.apikey_regular, 'update_repo', | |
1160 repoid=repo_name, **updates) | |
1161 response = api_call(self, params) | |
1162 try: | |
1163 expected = 'repository `%s` does not exist' % repo_name | |
1164 self._compare_error(id_, expected, given=response.body) | |
1165 finally: | |
1166 fixture.destroy_repo(repo_name) | |
1167 | |
1168 @mock.patch.object(RepoModel, 'update', crash) | |
1169 def test_api_update_repo_exception_occured(self): | |
1170 repo_name = 'api_update_me' | |
1171 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) | |
1172 id_, params = _build_data(self.apikey, 'update_repo', | |
1173 repoid=repo_name, owner=TEST_USER_ADMIN_LOGIN,) | |
1174 response = api_call(self, params) | |
1175 try: | |
1176 expected = 'failed to update repo `%s`' % repo_name | |
1177 self._compare_error(id_, expected, given=response.body) | |
1178 finally: | |
1179 fixture.destroy_repo(repo_name) | |
896 | 1180 |
897 def test_api_delete_repo(self): | 1181 def test_api_delete_repo(self): |
898 repo_name = 'api_delete_me' | 1182 repo_name = 'api_delete_me' |
899 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) | 1183 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) |
900 | 1184 |
901 id_, params = _build_data(self.apikey, 'delete_repo', | 1185 id_, params = _build_data(self.apikey, 'delete_repo', |
902 repoid=repo_name,) | 1186 repoid=repo_name, ) |
903 response = api_call(self, params) | 1187 response = api_call(self, params) |
904 | 1188 |
905 ret = { | 1189 ret = { |
906 'msg': 'Deleted repository `%s`' % repo_name, | 1190 'msg': 'Deleted repository `%s`' % repo_name, |
907 'success': True | 1191 'success': True |
908 } | 1192 } |
909 expected = ret | 1193 try: |
910 self._compare_ok(id_, expected, given=response.body) | 1194 expected = ret |
1195 self._compare_ok(id_, expected, given=response.body) | |
1196 finally: | |
1197 fixture.destroy_repo(repo_name) | |
911 | 1198 |
912 def test_api_delete_repo_by_non_admin(self): | 1199 def test_api_delete_repo_by_non_admin(self): |
913 repo_name = 'api_delete_me' | 1200 repo_name = 'api_delete_me' |
914 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE, | 1201 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE, |
915 cur_user=self.TEST_USER_LOGIN) | 1202 cur_user=self.TEST_USER_LOGIN) |
916 try: | 1203 id_, params = _build_data(self.apikey_regular, 'delete_repo', |
917 id_, params = _build_data(self.apikey_regular, 'delete_repo', | 1204 repoid=repo_name, ) |
918 repoid=repo_name,) | 1205 response = api_call(self, params) |
919 response = api_call(self, params) | 1206 |
920 | 1207 ret = { |
921 ret = { | 1208 'msg': 'Deleted repository `%s`' % repo_name, |
922 'msg': 'Deleted repository `%s`' % repo_name, | 1209 'success': True |
923 'success': True | 1210 } |
924 } | 1211 try: |
925 expected = ret | 1212 expected = ret |
926 self._compare_ok(id_, expected, given=response.body) | 1213 self._compare_ok(id_, expected, given=response.body) |
927 finally: | 1214 finally: |
928 fixture.destroy_repo(repo_name) | 1215 fixture.destroy_repo(repo_name) |
929 | 1216 |
930 def test_api_delete_repo_by_non_admin_no_permission(self): | 1217 def test_api_delete_repo_by_non_admin_no_permission(self): |
931 repo_name = 'api_delete_me' | 1218 repo_name = 'api_delete_me' |
932 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) | 1219 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) |
933 try: | 1220 try: |
934 id_, params = _build_data(self.apikey_regular, 'delete_repo', | 1221 id_, params = _build_data(self.apikey_regular, 'delete_repo', |
935 repoid=repo_name,) | 1222 repoid=repo_name, ) |
936 response = api_call(self, params) | 1223 response = api_call(self, params) |
937 expected = 'repository `%s` does not exist' % (repo_name) | 1224 expected = 'repository `%s` does not exist' % (repo_name) |
938 self._compare_error(id_, expected, given=response.body) | 1225 self._compare_error(id_, expected, given=response.body) |
939 finally: | 1226 finally: |
940 fixture.destroy_repo(repo_name) | 1227 fixture.destroy_repo(repo_name) |
943 repo_name = 'api_delete_me' | 1230 repo_name = 'api_delete_me' |
944 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) | 1231 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE) |
945 try: | 1232 try: |
946 with mock.patch.object(RepoModel, 'delete', crash): | 1233 with mock.patch.object(RepoModel, 'delete', crash): |
947 id_, params = _build_data(self.apikey, 'delete_repo', | 1234 id_, params = _build_data(self.apikey, 'delete_repo', |
948 repoid=repo_name,) | 1235 repoid=repo_name, ) |
949 response = api_call(self, params) | 1236 response = api_call(self, params) |
950 | 1237 |
951 expected = 'failed to delete repository `%s`' % repo_name | 1238 expected = 'failed to delete repository `%s`' % repo_name |
952 self._compare_error(id_, expected, given=response.body) | 1239 self._compare_error(id_, expected, given=response.body) |
953 finally: | 1240 finally: |
954 fixture.destroy_repo(repo_name) | 1241 fixture.destroy_repo(repo_name) |
955 | 1242 |
956 def test_api_fork_repo(self): | 1243 def test_api_fork_repo(self): |
957 fork_name = 'api-repo-fork' | 1244 fork_name = 'api-repo-fork' |
958 id_, params = _build_data(self.apikey, 'fork_repo', | 1245 id_, params = _build_data(self.apikey, 'fork_repo', |
959 repoid=self.REPO, | 1246 repoid=self.REPO, |
960 fork_name=fork_name, | 1247 fork_name=fork_name, |
961 owner=TEST_USER_ADMIN_LOGIN, | 1248 owner=TEST_USER_ADMIN_LOGIN, |
962 ) | 1249 ) |
963 response = api_call(self, params) | 1250 response = api_call(self, params) |
964 | 1251 |
965 ret = { | 1252 ret = { |
966 'msg': 'Created fork of `%s` as `%s`' % (self.REPO, | 1253 'msg': 'Created fork of `%s` as `%s`' % (self.REPO, |
967 fork_name), | 1254 fork_name), |
968 'success': True | 1255 'success': True, |
1256 'task': None, | |
969 } | 1257 } |
970 expected = ret | 1258 expected = ret |
971 self._compare_ok(id_, expected, given=response.body) | 1259 self._compare_ok(id_, expected, given=response.body) |
972 fixture.destroy_repo(fork_name) | 1260 fixture.destroy_repo(fork_name) |
973 | 1261 |
974 def test_api_fork_repo_non_admin(self): | 1262 def test_api_fork_repo_non_admin(self): |
975 fork_name = 'api-repo-fork' | 1263 fork_name = 'api-repo-fork' |
976 id_, params = _build_data(self.apikey_regular, 'fork_repo', | 1264 id_, params = _build_data(self.apikey_regular, 'fork_repo', |
977 repoid=self.REPO, | 1265 repoid=self.REPO, |
978 fork_name=fork_name, | 1266 fork_name=fork_name, |
979 ) | 1267 ) |
980 response = api_call(self, params) | 1268 response = api_call(self, params) |
981 | 1269 |
982 ret = { | 1270 ret = { |
983 'msg': 'Created fork of `%s` as `%s`' % (self.REPO, | 1271 'msg': 'Created fork of `%s` as `%s`' % (self.REPO, |
984 fork_name), | 1272 fork_name), |
985 'success': True | 1273 'success': True, |
1274 'task': None, | |
986 } | 1275 } |
987 expected = ret | 1276 expected = ret |
988 self._compare_ok(id_, expected, given=response.body) | 1277 self._compare_ok(id_, expected, given=response.body) |
989 fixture.destroy_repo(fork_name) | 1278 fixture.destroy_repo(fork_name) |
990 | 1279 |
991 def test_api_fork_repo_non_admin_specify_owner(self): | 1280 def test_api_fork_repo_non_admin_specify_owner(self): |
992 fork_name = 'api-repo-fork' | 1281 fork_name = 'api-repo-fork' |
993 id_, params = _build_data(self.apikey_regular, 'fork_repo', | 1282 id_, params = _build_data(self.apikey_regular, 'fork_repo', |
994 repoid=self.REPO, | 1283 repoid=self.REPO, |
995 fork_name=fork_name, | 1284 fork_name=fork_name, |
996 owner=TEST_USER_ADMIN_LOGIN, | 1285 owner=TEST_USER_ADMIN_LOGIN, |
997 ) | 1286 ) |
998 response = api_call(self, params) | 1287 response = api_call(self, params) |
999 expected = 'Only RhodeCode admin can specify `owner` param' | 1288 expected = 'Only RhodeCode admin can specify `owner` param' |
1000 self._compare_error(id_, expected, given=response.body) | 1289 self._compare_error(id_, expected, given=response.body) |
1001 fixture.destroy_repo(fork_name) | 1290 fixture.destroy_repo(fork_name) |
1002 | 1291 |
1004 RepoModel().grant_user_permission(repo=self.REPO, | 1293 RepoModel().grant_user_permission(repo=self.REPO, |
1005 user=self.TEST_USER_LOGIN, | 1294 user=self.TEST_USER_LOGIN, |
1006 perm='repository.none') | 1295 perm='repository.none') |
1007 fork_name = 'api-repo-fork' | 1296 fork_name = 'api-repo-fork' |
1008 id_, params = _build_data(self.apikey_regular, 'fork_repo', | 1297 id_, params = _build_data(self.apikey_regular, 'fork_repo', |
1009 repoid=self.REPO, | 1298 repoid=self.REPO, |
1010 fork_name=fork_name, | 1299 fork_name=fork_name, |
1011 ) | 1300 ) |
1012 response = api_call(self, params) | 1301 response = api_call(self, params) |
1013 expected = 'repository `%s` does not exist' % (self.REPO) | 1302 expected = 'repository `%s` does not exist' % (self.REPO) |
1014 self._compare_error(id_, expected, given=response.body) | 1303 self._compare_error(id_, expected, given=response.body) |
1015 fixture.destroy_repo(fork_name) | 1304 fixture.destroy_repo(fork_name) |
1016 | 1305 |
1017 def test_api_fork_repo_unknown_owner(self): | 1306 def test_api_fork_repo_unknown_owner(self): |
1018 fork_name = 'api-repo-fork' | 1307 fork_name = 'api-repo-fork' |
1019 owner = 'i-dont-exist' | 1308 owner = 'i-dont-exist' |
1020 id_, params = _build_data(self.apikey, 'fork_repo', | 1309 id_, params = _build_data(self.apikey, 'fork_repo', |
1021 repoid=self.REPO, | 1310 repoid=self.REPO, |
1022 fork_name=fork_name, | 1311 fork_name=fork_name, |
1023 owner=owner, | 1312 owner=owner, |
1024 ) | 1313 ) |
1025 response = api_call(self, params) | 1314 response = api_call(self, params) |
1026 expected = 'user `%s` does not exist' % owner | 1315 expected = 'user `%s` does not exist' % owner |
1027 self._compare_error(id_, expected, given=response.body) | 1316 self._compare_error(id_, expected, given=response.body) |
1028 | 1317 |
1029 def test_api_fork_repo_fork_exists(self): | 1318 def test_api_fork_repo_fork_exists(self): |
1032 | 1321 |
1033 try: | 1322 try: |
1034 fork_name = 'api-repo-fork' | 1323 fork_name = 'api-repo-fork' |
1035 | 1324 |
1036 id_, params = _build_data(self.apikey, 'fork_repo', | 1325 id_, params = _build_data(self.apikey, 'fork_repo', |
1037 repoid=self.REPO, | 1326 repoid=self.REPO, |
1038 fork_name=fork_name, | 1327 fork_name=fork_name, |
1039 owner=TEST_USER_ADMIN_LOGIN, | 1328 owner=TEST_USER_ADMIN_LOGIN, |
1040 ) | 1329 ) |
1041 response = api_call(self, params) | 1330 response = api_call(self, params) |
1042 | 1331 |
1043 expected = "fork `%s` already exist" % fork_name | 1332 expected = "fork `%s` already exist" % fork_name |
1044 self._compare_error(id_, expected, given=response.body) | 1333 self._compare_error(id_, expected, given=response.body) |
1045 finally: | 1334 finally: |
1047 | 1336 |
1048 def test_api_fork_repo_repo_exists(self): | 1337 def test_api_fork_repo_repo_exists(self): |
1049 fork_name = self.REPO | 1338 fork_name = self.REPO |
1050 | 1339 |
1051 id_, params = _build_data(self.apikey, 'fork_repo', | 1340 id_, params = _build_data(self.apikey, 'fork_repo', |
1052 repoid=self.REPO, | 1341 repoid=self.REPO, |
1053 fork_name=fork_name, | 1342 fork_name=fork_name, |
1054 owner=TEST_USER_ADMIN_LOGIN, | 1343 owner=TEST_USER_ADMIN_LOGIN, |
1055 ) | 1344 ) |
1056 response = api_call(self, params) | 1345 response = api_call(self, params) |
1057 | 1346 |
1058 expected = "repo `%s` already exist" % fork_name | 1347 expected = "repo `%s` already exist" % fork_name |
1059 self._compare_error(id_, expected, given=response.body) | 1348 self._compare_error(id_, expected, given=response.body) |
1060 | 1349 |
1061 @mock.patch.object(RepoModel, 'create_fork', crash) | 1350 @mock.patch.object(RepoModel, 'create_fork', crash) |
1062 def test_api_fork_repo_exception_occurred(self): | 1351 def test_api_fork_repo_exception_occurred(self): |
1063 fork_name = 'api-repo-fork' | 1352 fork_name = 'api-repo-fork' |
1064 id_, params = _build_data(self.apikey, 'fork_repo', | 1353 id_, params = _build_data(self.apikey, 'fork_repo', |
1065 repoid=self.REPO, | 1354 repoid=self.REPO, |
1066 fork_name=fork_name, | 1355 fork_name=fork_name, |
1067 owner=TEST_USER_ADMIN_LOGIN, | 1356 owner=TEST_USER_ADMIN_LOGIN, |
1068 ) | 1357 ) |
1069 response = api_call(self, params) | 1358 response = api_call(self, params) |
1070 | 1359 |
1071 expected = 'failed to fork repository `%s` as `%s`' % (self.REPO, | 1360 expected = 'failed to fork repository `%s` as `%s`' % (self.REPO, |
1072 fork_name) | 1361 fork_name) |
1073 self._compare_error(id_, expected, given=response.body) | 1362 self._compare_error(id_, expected, given=response.body) |
1074 | 1363 |
1075 def test_api_get_users_group(self): | 1364 def test_api_get_user_group(self): |
1076 id_, params = _build_data(self.apikey, 'get_users_group', | 1365 id_, params = _build_data(self.apikey, 'get_user_group', |
1077 usersgroupid=TEST_USER_GROUP) | 1366 usergroupid=TEST_USER_GROUP) |
1078 response = api_call(self, params) | 1367 response = api_call(self, params) |
1079 | 1368 |
1080 users_group = UserGroupModel().get_group(TEST_USER_GROUP) | 1369 user_group = UserGroupModel().get_group(TEST_USER_GROUP) |
1081 members = [] | 1370 members = [] |
1082 for user in users_group.members: | 1371 for user in user_group.members: |
1083 user = user.user | 1372 user = user.user |
1084 members.append(user.get_api_data()) | 1373 members.append(user.get_api_data()) |
1085 | 1374 |
1086 ret = users_group.get_api_data() | 1375 ret = user_group.get_api_data() |
1087 ret['members'] = members | 1376 ret['members'] = members |
1088 expected = ret | 1377 expected = ret |
1089 self._compare_ok(id_, expected, given=response.body) | 1378 self._compare_ok(id_, expected, given=response.body) |
1090 | 1379 |
1091 def test_api_get_users_groups(self): | 1380 def test_api_get_user_groups(self): |
1092 | 1381 gr_name = 'test_user_group2' |
1093 make_users_group('test_users_group2') | 1382 make_user_group(gr_name) |
1094 | 1383 |
1095 id_, params = _build_data(self.apikey, 'get_users_groups',) | 1384 id_, params = _build_data(self.apikey, 'get_user_groups', ) |
1096 response = api_call(self, params) | 1385 response = api_call(self, params) |
1097 | 1386 |
1098 expected = [] | 1387 try: |
1099 for gr_name in [TEST_USER_GROUP, 'test_users_group2']: | 1388 expected = [] |
1100 users_group = UserGroupModel().get_group(gr_name) | 1389 for gr_name in [TEST_USER_GROUP, 'test_user_group2']: |
1101 ret = users_group.get_api_data() | 1390 user_group = UserGroupModel().get_group(gr_name) |
1102 expected.append(ret) | 1391 ret = user_group.get_api_data() |
1103 self._compare_ok(id_, expected, given=response.body) | 1392 expected.append(ret) |
1104 | 1393 self._compare_ok(id_, expected, given=response.body) |
1105 UserGroupModel().delete(users_group='test_users_group2') | 1394 finally: |
1106 Session().commit() | 1395 fixture.destroy_user_group(gr_name) |
1107 | 1396 |
1108 def test_api_create_users_group(self): | 1397 def test_api_create_user_group(self): |
1109 group_name = 'some_new_group' | 1398 group_name = 'some_new_group' |
1110 id_, params = _build_data(self.apikey, 'create_users_group', | 1399 id_, params = _build_data(self.apikey, 'create_user_group', |
1111 group_name=group_name) | 1400 group_name=group_name) |
1112 response = api_call(self, params) | 1401 response = api_call(self, params) |
1113 | 1402 |
1114 ret = { | 1403 ret = { |
1115 'msg': 'created new user group `%s`' % group_name, | 1404 'msg': 'created new user group `%s`' % group_name, |
1116 'users_group': jsonify(UserGroupModel()\ | 1405 'user_group': jsonify(UserGroupModel() \ |
1117 .get_by_name(group_name)\ | 1406 .get_by_name(group_name) \ |
1118 .get_api_data()) | 1407 .get_api_data()) |
1119 } | 1408 } |
1120 expected = ret | 1409 expected = ret |
1121 self._compare_ok(id_, expected, given=response.body) | 1410 self._compare_ok(id_, expected, given=response.body) |
1122 | 1411 |
1123 destroy_users_group(group_name) | 1412 fixture.destroy_user_group(group_name) |
1124 | 1413 |
1125 def test_api_get_users_group_that_exist(self): | 1414 def test_api_get_user_group_that_exist(self): |
1126 id_, params = _build_data(self.apikey, 'create_users_group', | 1415 id_, params = _build_data(self.apikey, 'create_user_group', |
1127 group_name=TEST_USER_GROUP) | 1416 group_name=TEST_USER_GROUP) |
1128 response = api_call(self, params) | 1417 response = api_call(self, params) |
1129 | 1418 |
1130 expected = "user group `%s` already exist" % TEST_USER_GROUP | 1419 expected = "user group `%s` already exist" % TEST_USER_GROUP |
1131 self._compare_error(id_, expected, given=response.body) | 1420 self._compare_error(id_, expected, given=response.body) |
1132 | 1421 |
1133 @mock.patch.object(UserGroupModel, 'create', crash) | 1422 @mock.patch.object(UserGroupModel, 'create', crash) |
1134 def test_api_get_users_group_exception_occurred(self): | 1423 def test_api_get_user_group_exception_occurred(self): |
1135 group_name = 'exception_happens' | 1424 group_name = 'exception_happens' |
1136 id_, params = _build_data(self.apikey, 'create_users_group', | 1425 id_, params = _build_data(self.apikey, 'create_user_group', |
1137 group_name=group_name) | 1426 group_name=group_name) |
1138 response = api_call(self, params) | 1427 response = api_call(self, params) |
1139 | 1428 |
1140 expected = 'failed to create group `%s`' % group_name | 1429 expected = 'failed to create group `%s`' % group_name |
1141 self._compare_error(id_, expected, given=response.body) | 1430 self._compare_error(id_, expected, given=response.body) |
1142 | 1431 |
1143 def test_api_add_user_to_users_group(self): | 1432 @parameterized.expand([('group_name', {'group_name': 'new_group_name'}), |
1433 ('group_name', {'group_name': 'test_group_for_update'}), | |
1434 ('owner', {'owner': TEST_USER_REGULAR_LOGIN}), | |
1435 ('active', {'active': False}), | |
1436 ('active', {'active': True})]) | |
1437 def test_api_update_user_group(self, changing_attr, updates): | |
1438 gr_name = 'test_group_for_update' | |
1439 user_group = fixture.create_user_group(gr_name) | |
1440 id_, params = _build_data(self.apikey, 'update_user_group', | |
1441 usergroupid=gr_name, **updates) | |
1442 response = api_call(self, params) | |
1443 try: | |
1444 expected = { | |
1445 'msg': 'updated user group ID:%s %s' % (user_group.users_group_id, | |
1446 user_group.users_group_name), | |
1447 'user_group': user_group.get_api_data() | |
1448 } | |
1449 self._compare_ok(id_, expected, given=response.body) | |
1450 finally: | |
1451 if changing_attr == 'group_name': | |
1452 # switch to updated name for proper cleanup | |
1453 gr_name = updates['group_name'] | |
1454 fixture.destroy_user_group(gr_name) | |
1455 | |
1456 @mock.patch.object(UserGroupModel, 'update', crash) | |
1457 def test_api_update_user_group_exception_occured(self): | |
1144 gr_name = 'test_group' | 1458 gr_name = 'test_group' |
1145 fixture.create_user_group(gr_name) | 1459 fixture.create_user_group(gr_name) |
1146 id_, params = _build_data(self.apikey, 'add_user_to_users_group', | 1460 id_, params = _build_data(self.apikey, 'update_user_group', |
1147 usersgroupid=gr_name, | 1461 usergroupid=gr_name) |
1148 userid=TEST_USER_ADMIN_LOGIN) | 1462 response = api_call(self, params) |
1149 response = api_call(self, params) | 1463 try: |
1150 | 1464 expected = 'failed to update user group `%s`' % gr_name |
1151 expected = { | 1465 self._compare_error(id_, expected, given=response.body) |
1152 'msg': 'added member `%s` to user group `%s`' % ( | 1466 finally: |
1153 TEST_USER_ADMIN_LOGIN, gr_name | 1467 fixture.destroy_user_group(gr_name) |
1154 ), | 1468 |
1155 'success': True} | 1469 def test_api_add_user_to_user_group(self): |
1156 self._compare_ok(id_, expected, given=response.body) | |
1157 | |
1158 UserGroupModel().delete(users_group=gr_name) | |
1159 Session().commit() | |
1160 | |
1161 def test_api_add_user_to_users_group_that_doesnt_exist(self): | |
1162 id_, params = _build_data(self.apikey, 'add_user_to_users_group', | |
1163 usersgroupid='false-group', | |
1164 userid=TEST_USER_ADMIN_LOGIN) | |
1165 response = api_call(self, params) | |
1166 | |
1167 expected = 'user group `%s` does not exist' % 'false-group' | |
1168 self._compare_error(id_, expected, given=response.body) | |
1169 | |
1170 @mock.patch.object(UserGroupModel, 'add_user_to_group', crash) | |
1171 def test_api_add_user_to_users_group_exception_occurred(self): | |
1172 gr_name = 'test_group' | 1470 gr_name = 'test_group' |
1173 fixture.create_user_group(gr_name) | 1471 fixture.create_user_group(gr_name) |
1174 id_, params = _build_data(self.apikey, 'add_user_to_users_group', | 1472 id_, params = _build_data(self.apikey, 'add_user_to_user_group', |
1175 usersgroupid=gr_name, | 1473 usergroupid=gr_name, |
1176 userid=TEST_USER_ADMIN_LOGIN) | 1474 userid=TEST_USER_ADMIN_LOGIN) |
1177 response = api_call(self, params) | 1475 response = api_call(self, params) |
1178 | 1476 try: |
1179 expected = 'failed to add member to user group `%s`' % gr_name | 1477 expected = { |
1180 self._compare_error(id_, expected, given=response.body) | 1478 'msg': 'added member `%s` to user group `%s`' % ( |
1181 | 1479 TEST_USER_ADMIN_LOGIN, gr_name), |
1182 UserGroupModel().delete(users_group=gr_name) | 1480 'success': True |
1183 Session().commit() | 1481 } |
1184 | 1482 self._compare_ok(id_, expected, given=response.body) |
1185 def test_api_remove_user_from_users_group(self): | 1483 finally: |
1484 fixture.destroy_user_group(gr_name) | |
1485 | |
1486 def test_api_add_user_to_user_group_that_doesnt_exist(self): | |
1487 id_, params = _build_data(self.apikey, 'add_user_to_user_group', | |
1488 usergroupid='false-group', | |
1489 userid=TEST_USER_ADMIN_LOGIN) | |
1490 response = api_call(self, params) | |
1491 | |
1492 expected = 'user group `%s` does not exist' % 'false-group' | |
1493 self._compare_error(id_, expected, given=response.body) | |
1494 | |
1495 @mock.patch.object(UserGroupModel, 'add_user_to_group', crash) | |
1496 def test_api_add_user_to_user_group_exception_occurred(self): | |
1497 gr_name = 'test_group' | |
1498 fixture.create_user_group(gr_name) | |
1499 id_, params = _build_data(self.apikey, 'add_user_to_user_group', | |
1500 usergroupid=gr_name, | |
1501 userid=TEST_USER_ADMIN_LOGIN) | |
1502 response = api_call(self, params) | |
1503 | |
1504 try: | |
1505 expected = 'failed to add member to user group `%s`' % gr_name | |
1506 self._compare_error(id_, expected, given=response.body) | |
1507 finally: | |
1508 fixture.destroy_user_group(gr_name) | |
1509 | |
1510 def test_api_remove_user_from_user_group(self): | |
1186 gr_name = 'test_group_3' | 1511 gr_name = 'test_group_3' |
1187 gr = fixture.create_user_group(gr_name) | 1512 gr = fixture.create_user_group(gr_name) |
1188 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN) | 1513 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN) |
1189 Session().commit() | 1514 Session().commit() |
1190 id_, params = _build_data(self.apikey, 'remove_user_from_users_group', | 1515 id_, params = _build_data(self.apikey, 'remove_user_from_user_group', |
1191 usersgroupid=gr_name, | 1516 usergroupid=gr_name, |
1192 userid=TEST_USER_ADMIN_LOGIN) | 1517 userid=TEST_USER_ADMIN_LOGIN) |
1193 response = api_call(self, params) | 1518 response = api_call(self, params) |
1194 | 1519 |
1195 expected = { | 1520 try: |
1196 'msg': 'removed member `%s` from user group `%s`' % ( | 1521 expected = { |
1197 TEST_USER_ADMIN_LOGIN, gr_name | 1522 'msg': 'removed member `%s` from user group `%s`' % ( |
1198 ), | 1523 TEST_USER_ADMIN_LOGIN, gr_name |
1199 'success': True} | 1524 ), |
1200 self._compare_ok(id_, expected, given=response.body) | 1525 'success': True} |
1201 | 1526 self._compare_ok(id_, expected, given=response.body) |
1202 UserGroupModel().delete(users_group=gr_name) | 1527 finally: |
1203 Session().commit() | 1528 fixture.destroy_user_group(gr_name) |
1204 | 1529 |
1205 @mock.patch.object(UserGroupModel, 'remove_user_from_group', crash) | 1530 @mock.patch.object(UserGroupModel, 'remove_user_from_group', crash) |
1206 def test_api_remove_user_from_users_group_exception_occurred(self): | 1531 def test_api_remove_user_from_user_group_exception_occurred(self): |
1207 gr_name = 'test_group_3' | 1532 gr_name = 'test_group_3' |
1208 gr = fixture.create_user_group(gr_name) | 1533 gr = fixture.create_user_group(gr_name) |
1209 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN) | 1534 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN) |
1210 Session().commit() | 1535 Session().commit() |
1211 id_, params = _build_data(self.apikey, 'remove_user_from_users_group', | 1536 id_, params = _build_data(self.apikey, 'remove_user_from_user_group', |
1212 usersgroupid=gr_name, | 1537 usergroupid=gr_name, |
1213 userid=TEST_USER_ADMIN_LOGIN) | 1538 userid=TEST_USER_ADMIN_LOGIN) |
1214 response = api_call(self, params) | 1539 response = api_call(self, params) |
1215 | 1540 try: |
1216 expected = 'failed to remove member from user group `%s`' % gr_name | 1541 expected = 'failed to remove member from user group `%s`' % gr_name |
1217 self._compare_error(id_, expected, given=response.body) | 1542 self._compare_error(id_, expected, given=response.body) |
1218 | 1543 finally: |
1219 UserGroupModel().delete(users_group=gr_name) | 1544 fixture.destroy_user_group(gr_name) |
1220 Session().commit() | 1545 |
1546 def test_api_delete_user_group(self): | |
1547 gr_name = 'test_group' | |
1548 ugroup = fixture.create_user_group(gr_name) | |
1549 gr_id = ugroup.users_group_id | |
1550 id_, params = _build_data(self.apikey, 'delete_user_group', | |
1551 usergroupid=gr_name, | |
1552 userid=TEST_USER_ADMIN_LOGIN) | |
1553 response = api_call(self, params) | |
1554 | |
1555 try: | |
1556 expected = { | |
1557 'user_group': None, | |
1558 'msg': 'deleted user group ID:%s %s' % (gr_id, gr_name) | |
1559 } | |
1560 self._compare_ok(id_, expected, given=response.body) | |
1561 finally: | |
1562 if UserGroupModel().get_by_name(gr_name): | |
1563 fixture.destroy_user_group(gr_name) | |
1564 | |
1565 def test_api_delete_user_group_that_is_assigned(self): | |
1566 gr_name = 'test_group' | |
1567 ugroup = fixture.create_user_group(gr_name) | |
1568 gr_id = ugroup.users_group_id | |
1569 | |
1570 ugr_to_perm = RepoModel().grant_user_group_permission(self.REPO, gr_name, 'repository.write') | |
1571 msg = 'RepoGroup assigned to [%s]' % (ugr_to_perm) | |
1572 | |
1573 id_, params = _build_data(self.apikey, 'delete_user_group', | |
1574 usergroupid=gr_name, | |
1575 userid=TEST_USER_ADMIN_LOGIN) | |
1576 response = api_call(self, params) | |
1577 | |
1578 try: | |
1579 expected = msg | |
1580 self._compare_error(id_, expected, given=response.body) | |
1581 finally: | |
1582 if UserGroupModel().get_by_name(gr_name): | |
1583 fixture.destroy_user_group(gr_name) | |
1584 | |
1585 def test_api_delete_user_group_exception_occured(self): | |
1586 gr_name = 'test_group' | |
1587 ugroup = fixture.create_user_group(gr_name) | |
1588 gr_id = ugroup.users_group_id | |
1589 id_, params = _build_data(self.apikey, 'delete_user_group', | |
1590 usergroupid=gr_name, | |
1591 userid=TEST_USER_ADMIN_LOGIN) | |
1592 | |
1593 try: | |
1594 with mock.patch.object(UserGroupModel, 'delete', crash): | |
1595 response = api_call(self, params) | |
1596 expected = 'failed to delete user group ID:%s %s' % (gr_id, gr_name) | |
1597 self._compare_error(id_, expected, given=response.body) | |
1598 finally: | |
1599 fixture.destroy_user_group(gr_name) | |
1221 | 1600 |
1222 @parameterized.expand([('none', 'repository.none'), | 1601 @parameterized.expand([('none', 'repository.none'), |
1223 ('read', 'repository.read'), | 1602 ('read', 'repository.read'), |
1224 ('write', 'repository.write'), | 1603 ('write', 'repository.write'), |
1225 ('admin', 'repository.admin')]) | 1604 ('admin', 'repository.admin')]) |
1226 def test_api_grant_user_permission(self, name, perm): | 1605 def test_api_grant_user_permission(self, name, perm): |
1227 id_, params = _build_data(self.apikey, 'grant_user_permission', | 1606 id_, params = _build_data(self.apikey, |
1607 'grant_user_permission', | |
1228 repoid=self.REPO, | 1608 repoid=self.REPO, |
1229 userid=TEST_USER_ADMIN_LOGIN, | 1609 userid=TEST_USER_ADMIN_LOGIN, |
1230 perm=perm) | 1610 perm=perm) |
1231 response = api_call(self, params) | 1611 response = api_call(self, params) |
1232 | 1612 |
1233 ret = { | 1613 ret = { |
1234 'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % ( | 1614 'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % ( |
1235 perm, TEST_USER_ADMIN_LOGIN, self.REPO | 1615 perm, TEST_USER_ADMIN_LOGIN, self.REPO |
1236 ), | 1616 ), |
1237 'success': True | 1617 'success': True |
1238 } | 1618 } |
1239 expected = ret | 1619 expected = ret |
1240 self._compare_ok(id_, expected, given=response.body) | 1620 self._compare_ok(id_, expected, given=response.body) |
1241 | 1621 |
1242 def test_api_grant_user_permission_wrong_permission(self): | 1622 def test_api_grant_user_permission_wrong_permission(self): |
1243 perm = 'haha.no.permission' | 1623 perm = 'haha.no.permission' |
1244 id_, params = _build_data(self.apikey, 'grant_user_permission', | 1624 id_, params = _build_data(self.apikey, |
1625 'grant_user_permission', | |
1245 repoid=self.REPO, | 1626 repoid=self.REPO, |
1246 userid=TEST_USER_ADMIN_LOGIN, | 1627 userid=TEST_USER_ADMIN_LOGIN, |
1247 perm=perm) | 1628 perm=perm) |
1248 response = api_call(self, params) | 1629 response = api_call(self, params) |
1249 | 1630 |
1251 self._compare_error(id_, expected, given=response.body) | 1632 self._compare_error(id_, expected, given=response.body) |
1252 | 1633 |
1253 @mock.patch.object(RepoModel, 'grant_user_permission', crash) | 1634 @mock.patch.object(RepoModel, 'grant_user_permission', crash) |
1254 def test_api_grant_user_permission_exception_when_adding(self): | 1635 def test_api_grant_user_permission_exception_when_adding(self): |
1255 perm = 'repository.read' | 1636 perm = 'repository.read' |
1256 id_, params = _build_data(self.apikey, 'grant_user_permission', | 1637 id_, params = _build_data(self.apikey, |
1638 'grant_user_permission', | |
1257 repoid=self.REPO, | 1639 repoid=self.REPO, |
1258 userid=TEST_USER_ADMIN_LOGIN, | 1640 userid=TEST_USER_ADMIN_LOGIN, |
1259 perm=perm) | 1641 perm=perm) |
1260 response = api_call(self, params) | 1642 response = api_call(self, params) |
1261 | 1643 |
1262 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % ( | 1644 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % ( |
1263 TEST_USER_ADMIN_LOGIN, self.REPO | 1645 TEST_USER_ADMIN_LOGIN, self.REPO |
1264 ) | 1646 ) |
1265 self._compare_error(id_, expected, given=response.body) | 1647 self._compare_error(id_, expected, given=response.body) |
1266 | 1648 |
1267 def test_api_revoke_user_permission(self): | 1649 def test_api_revoke_user_permission(self): |
1268 id_, params = _build_data(self.apikey, 'revoke_user_permission', | 1650 id_, params = _build_data(self.apikey, |
1651 'revoke_user_permission', | |
1269 repoid=self.REPO, | 1652 repoid=self.REPO, |
1270 userid=TEST_USER_ADMIN_LOGIN,) | 1653 userid=TEST_USER_ADMIN_LOGIN, ) |
1271 response = api_call(self, params) | 1654 response = api_call(self, params) |
1272 | 1655 |
1273 expected = { | 1656 expected = { |
1274 'msg': 'Revoked perm for user: `%s` in repo: `%s`' % ( | 1657 'msg': 'Revoked perm for user: `%s` in repo: `%s`' % ( |
1275 TEST_USER_ADMIN_LOGIN, self.REPO | 1658 TEST_USER_ADMIN_LOGIN, self.REPO |
1278 } | 1661 } |
1279 self._compare_ok(id_, expected, given=response.body) | 1662 self._compare_ok(id_, expected, given=response.body) |
1280 | 1663 |
1281 @mock.patch.object(RepoModel, 'revoke_user_permission', crash) | 1664 @mock.patch.object(RepoModel, 'revoke_user_permission', crash) |
1282 def test_api_revoke_user_permission_exception_when_adding(self): | 1665 def test_api_revoke_user_permission_exception_when_adding(self): |
1283 id_, params = _build_data(self.apikey, 'revoke_user_permission', | 1666 id_, params = _build_data(self.apikey, |
1667 'revoke_user_permission', | |
1284 repoid=self.REPO, | 1668 repoid=self.REPO, |
1285 userid=TEST_USER_ADMIN_LOGIN,) | 1669 userid=TEST_USER_ADMIN_LOGIN, ) |
1286 response = api_call(self, params) | 1670 response = api_call(self, params) |
1287 | 1671 |
1288 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % ( | 1672 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % ( |
1289 TEST_USER_ADMIN_LOGIN, self.REPO | 1673 TEST_USER_ADMIN_LOGIN, self.REPO |
1290 ) | 1674 ) |
1291 self._compare_error(id_, expected, given=response.body) | 1675 self._compare_error(id_, expected, given=response.body) |
1292 | 1676 |
1293 @parameterized.expand([('none', 'repository.none'), | 1677 @parameterized.expand([('none', 'repository.none'), |
1294 ('read', 'repository.read'), | 1678 ('read', 'repository.read'), |
1295 ('write', 'repository.write'), | 1679 ('write', 'repository.write'), |
1296 ('admin', 'repository.admin')]) | 1680 ('admin', 'repository.admin')]) |
1297 def test_api_grant_users_group_permission(self, name, perm): | 1681 def test_api_grant_user_group_permission(self, name, perm): |
1298 id_, params = _build_data(self.apikey, 'grant_users_group_permission', | 1682 id_, params = _build_data(self.apikey, |
1683 'grant_user_group_permission', | |
1299 repoid=self.REPO, | 1684 repoid=self.REPO, |
1300 usersgroupid=TEST_USER_GROUP, | 1685 usergroupid=TEST_USER_GROUP, |
1301 perm=perm) | 1686 perm=perm) |
1302 response = api_call(self, params) | 1687 response = api_call(self, params) |
1303 | 1688 |
1304 ret = { | 1689 ret = { |
1305 'msg': 'Granted perm: `%s` for user group: `%s` in repo: `%s`' % ( | 1690 'msg': 'Granted perm: `%s` for user group: `%s` in repo: `%s`' % ( |
1308 'success': True | 1693 'success': True |
1309 } | 1694 } |
1310 expected = ret | 1695 expected = ret |
1311 self._compare_ok(id_, expected, given=response.body) | 1696 self._compare_ok(id_, expected, given=response.body) |
1312 | 1697 |
1313 def test_api_grant_users_group_permission_wrong_permission(self): | 1698 def test_api_grant_user_group_permission_wrong_permission(self): |
1314 perm = 'haha.no.permission' | 1699 perm = 'haha.no.permission' |
1315 id_, params = _build_data(self.apikey, 'grant_users_group_permission', | 1700 id_, params = _build_data(self.apikey, |
1701 'grant_user_group_permission', | |
1316 repoid=self.REPO, | 1702 repoid=self.REPO, |
1317 usersgroupid=TEST_USER_GROUP, | 1703 usergroupid=TEST_USER_GROUP, |
1318 perm=perm) | 1704 perm=perm) |
1319 response = api_call(self, params) | 1705 response = api_call(self, params) |
1320 | 1706 |
1321 expected = 'permission `%s` does not exist' % perm | 1707 expected = 'permission `%s` does not exist' % perm |
1322 self._compare_error(id_, expected, given=response.body) | 1708 self._compare_error(id_, expected, given=response.body) |
1323 | 1709 |
1324 @mock.patch.object(RepoModel, 'grant_users_group_permission', crash) | 1710 @mock.patch.object(RepoModel, 'grant_user_group_permission', crash) |
1325 def test_api_grant_users_group_permission_exception_when_adding(self): | 1711 def test_api_grant_user_group_permission_exception_when_adding(self): |
1326 perm = 'repository.read' | 1712 perm = 'repository.read' |
1327 id_, params = _build_data(self.apikey, 'grant_users_group_permission', | 1713 id_, params = _build_data(self.apikey, |
1714 'grant_user_group_permission', | |
1328 repoid=self.REPO, | 1715 repoid=self.REPO, |
1329 usersgroupid=TEST_USER_GROUP, | 1716 usergroupid=TEST_USER_GROUP, |
1330 perm=perm) | 1717 perm=perm) |
1331 response = api_call(self, params) | 1718 response = api_call(self, params) |
1332 | 1719 |
1333 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % ( | 1720 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % ( |
1334 TEST_USER_GROUP, self.REPO | 1721 TEST_USER_GROUP, self.REPO |
1335 ) | 1722 ) |
1336 self._compare_error(id_, expected, given=response.body) | 1723 self._compare_error(id_, expected, given=response.body) |
1337 | 1724 |
1338 def test_api_revoke_users_group_permission(self): | 1725 def test_api_revoke_user_group_permission(self): |
1339 RepoModel().grant_users_group_permission(repo=self.REPO, | 1726 RepoModel().grant_user_group_permission(repo=self.REPO, |
1340 group_name=TEST_USER_GROUP, | 1727 group_name=TEST_USER_GROUP, |
1341 perm='repository.read') | 1728 perm='repository.read') |
1342 Session().commit() | 1729 Session().commit() |
1343 id_, params = _build_data(self.apikey, 'revoke_users_group_permission', | 1730 id_, params = _build_data(self.apikey, |
1731 'revoke_user_group_permission', | |
1344 repoid=self.REPO, | 1732 repoid=self.REPO, |
1345 usersgroupid=TEST_USER_GROUP,) | 1733 usergroupid=TEST_USER_GROUP, ) |
1346 response = api_call(self, params) | 1734 response = api_call(self, params) |
1347 | 1735 |
1348 expected = { | 1736 expected = { |
1349 'msg': 'Revoked perm for user group: `%s` in repo: `%s`' % ( | 1737 'msg': 'Revoked perm for user group: `%s` in repo: `%s`' % ( |
1350 TEST_USER_GROUP, self.REPO | 1738 TEST_USER_GROUP, self.REPO |
1351 ), | 1739 ), |
1352 'success': True | 1740 'success': True |
1353 } | 1741 } |
1354 self._compare_ok(id_, expected, given=response.body) | 1742 self._compare_ok(id_, expected, given=response.body) |
1355 | 1743 |
1356 @mock.patch.object(RepoModel, 'revoke_users_group_permission', crash) | 1744 @mock.patch.object(RepoModel, 'revoke_user_group_permission', crash) |
1357 def test_api_revoke_users_group_permission_exception_when_adding(self): | 1745 def test_api_revoke_user_group_permission_exception_when_adding(self): |
1358 | 1746 id_, params = _build_data(self.apikey, |
1359 id_, params = _build_data(self.apikey, 'revoke_users_group_permission', | 1747 'revoke_user_group_permission', |
1360 repoid=self.REPO, | 1748 repoid=self.REPO, |
1361 usersgroupid=TEST_USER_GROUP,) | 1749 usergroupid=TEST_USER_GROUP, ) |
1362 response = api_call(self, params) | 1750 response = api_call(self, params) |
1363 | 1751 |
1364 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % ( | 1752 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % ( |
1365 TEST_USER_GROUP, self.REPO | 1753 TEST_USER_GROUP, self.REPO |
1366 ) | 1754 ) |
1367 self._compare_error(id_, expected, given=response.body) | 1755 self._compare_error(id_, expected, given=response.body) |
1756 | |
1757 @parameterized.expand([ | |
1758 ('none', 'group.none', 'none'), | |
1759 ('read', 'group.read', 'none'), | |
1760 ('write', 'group.write', 'none'), | |
1761 ('admin', 'group.admin', 'none'), | |
1762 | |
1763 ('none', 'group.none', 'all'), | |
1764 ('read', 'group.read', 'all'), | |
1765 ('write', 'group.write', 'all'), | |
1766 ('admin', 'group.admin', 'all'), | |
1767 | |
1768 ('none', 'group.none', 'repos'), | |
1769 ('read', 'group.read', 'repos'), | |
1770 ('write', 'group.write', 'repos'), | |
1771 ('admin', 'group.admin', 'repos'), | |
1772 | |
1773 ('none', 'group.none', 'groups'), | |
1774 ('read', 'group.read', 'groups'), | |
1775 ('write', 'group.write', 'groups'), | |
1776 ('admin', 'group.admin', 'groups'), | |
1777 ]) | |
1778 def test_api_grant_user_permission_to_repo_group(self, name, perm, apply_to_children): | |
1779 id_, params = _build_data(self.apikey, | |
1780 'grant_user_permission_to_repo_group', | |
1781 repogroupid=TEST_REPO_GROUP, | |
1782 userid=TEST_USER_ADMIN_LOGIN, | |
1783 perm=perm, apply_to_children=apply_to_children) | |
1784 response = api_call(self, params) | |
1785 | |
1786 ret = { | |
1787 'msg': 'Granted perm: `%s` (recursive:%s) for user: `%s` in repo group: `%s`' % ( | |
1788 perm, apply_to_children, TEST_USER_ADMIN_LOGIN, TEST_REPO_GROUP | |
1789 ), | |
1790 'success': True | |
1791 } | |
1792 expected = ret | |
1793 self._compare_ok(id_, expected, given=response.body) | |
1794 | |
1795 @parameterized.expand([ | |
1796 ('none_fails', 'group.none', 'none', False, False), | |
1797 ('read_fails', 'group.read', 'none', False, False), | |
1798 ('write_fails', 'group.write', 'none', False, False), | |
1799 ('admin_fails', 'group.admin', 'none', False, False), | |
1800 | |
1801 # with granted perms | |
1802 ('none_ok', 'group.none', 'none', True, True), | |
1803 ('read_ok', 'group.read', 'none', True, True), | |
1804 ('write_ok', 'group.write', 'none', True, True), | |
1805 ('admin_ok', 'group.admin', 'none', True, True), | |
1806 ]) | |
1807 def test_api_grant_user_permission_to_repo_group_by_regular_user( | |
1808 self, name, perm, apply_to_children, grant_admin, access_ok): | |
1809 if grant_admin: | |
1810 RepoGroupModel().grant_user_permission(TEST_REPO_GROUP, | |
1811 self.TEST_USER_LOGIN, | |
1812 'group.admin') | |
1813 Session().commit() | |
1814 | |
1815 id_, params = _build_data(self.apikey_regular, | |
1816 'grant_user_permission_to_repo_group', | |
1817 repogroupid=TEST_REPO_GROUP, | |
1818 userid=TEST_USER_ADMIN_LOGIN, | |
1819 perm=perm, apply_to_children=apply_to_children) | |
1820 response = api_call(self, params) | |
1821 if access_ok: | |
1822 ret = { | |
1823 'msg': 'Granted perm: `%s` (recursive:%s) for user: `%s` in repo group: `%s`' % ( | |
1824 perm, apply_to_children, TEST_USER_ADMIN_LOGIN, TEST_REPO_GROUP | |
1825 ), | |
1826 'success': True | |
1827 } | |
1828 expected = ret | |
1829 self._compare_ok(id_, expected, given=response.body) | |
1830 else: | |
1831 expected = 'repository group `%s` does not exist' % TEST_REPO_GROUP | |
1832 self._compare_error(id_, expected, given=response.body) | |
1833 | |
1834 def test_api_grant_user_permission_to_repo_group_wrong_permission(self): | |
1835 perm = 'haha.no.permission' | |
1836 id_, params = _build_data(self.apikey, | |
1837 'grant_user_permission_to_repo_group', | |
1838 repogroupid=TEST_REPO_GROUP, | |
1839 userid=TEST_USER_ADMIN_LOGIN, | |
1840 perm=perm) | |
1841 response = api_call(self, params) | |
1842 | |
1843 expected = 'permission `%s` does not exist' % perm | |
1844 self._compare_error(id_, expected, given=response.body) | |
1845 | |
1846 @mock.patch.object(RepoGroupModel, 'grant_user_permission', crash) | |
1847 def test_api_grant_user_permission_to_repo_group_exception_when_adding(self): | |
1848 perm = 'group.read' | |
1849 id_, params = _build_data(self.apikey, | |
1850 'grant_user_permission_to_repo_group', | |
1851 repogroupid=TEST_REPO_GROUP, | |
1852 userid=TEST_USER_ADMIN_LOGIN, | |
1853 perm=perm) | |
1854 response = api_call(self, params) | |
1855 | |
1856 expected = 'failed to edit permission for user: `%s` in repo group: `%s`' % ( | |
1857 TEST_USER_ADMIN_LOGIN, TEST_REPO_GROUP | |
1858 ) | |
1859 self._compare_error(id_, expected, given=response.body) | |
1860 | |
1861 @parameterized.expand([ | |
1862 ('none', 'none'), | |
1863 ('all', 'all'), | |
1864 ('repos', 'repos'), | |
1865 ('groups', 'groups'), | |
1866 ]) | |
1867 def test_api_revoke_user_permission_from_repo_group(self, name, apply_to_children): | |
1868 RepoGroupModel().grant_user_permission(repo_group=TEST_REPO_GROUP, | |
1869 user=TEST_USER_ADMIN_LOGIN, | |
1870 perm='group.read',) | |
1871 Session().commit() | |
1872 | |
1873 id_, params = _build_data(self.apikey, | |
1874 'revoke_user_permission_from_repo_group', | |
1875 repogroupid=TEST_REPO_GROUP, | |
1876 userid=TEST_USER_ADMIN_LOGIN, | |
1877 apply_to_children=apply_to_children,) | |
1878 response = api_call(self, params) | |
1879 | |
1880 expected = { | |
1881 'msg': 'Revoked perm (recursive:%s) for user: `%s` in repo group: `%s`' % ( | |
1882 apply_to_children, TEST_USER_ADMIN_LOGIN, TEST_REPO_GROUP | |
1883 ), | |
1884 'success': True | |
1885 } | |
1886 self._compare_ok(id_, expected, given=response.body) | |
1887 | |
1888 @parameterized.expand([ | |
1889 ('none', 'none', False, False), | |
1890 ('all', 'all', False, False), | |
1891 ('repos', 'repos', False, False), | |
1892 ('groups', 'groups', False, False), | |
1893 | |
1894 # after granting admin rights | |
1895 ('none', 'none', False, False), | |
1896 ('all', 'all', False, False), | |
1897 ('repos', 'repos', False, False), | |
1898 ('groups', 'groups', False, False), | |
1899 ]) | |
1900 def test_api_revoke_user_permission_from_repo_group_by_regular_user( | |
1901 self, name, apply_to_children, grant_admin, access_ok): | |
1902 RepoGroupModel().grant_user_permission(repo_group=TEST_REPO_GROUP, | |
1903 user=TEST_USER_ADMIN_LOGIN, | |
1904 perm='group.read',) | |
1905 Session().commit() | |
1906 | |
1907 if grant_admin: | |
1908 RepoGroupModel().grant_user_permission(TEST_REPO_GROUP, | |
1909 self.TEST_USER_LOGIN, | |
1910 'group.admin') | |
1911 Session().commit() | |
1912 | |
1913 id_, params = _build_data(self.apikey_regular, | |
1914 'revoke_user_permission_from_repo_group', | |
1915 repogroupid=TEST_REPO_GROUP, | |
1916 userid=TEST_USER_ADMIN_LOGIN, | |
1917 apply_to_children=apply_to_children,) | |
1918 response = api_call(self, params) | |
1919 if access_ok: | |
1920 expected = { | |
1921 'msg': 'Revoked perm (recursive:%s) for user: `%s` in repo group: `%s`' % ( | |
1922 apply_to_children, TEST_USER_ADMIN_LOGIN, TEST_REPO_GROUP | |
1923 ), | |
1924 'success': True | |
1925 } | |
1926 self._compare_ok(id_, expected, given=response.body) | |
1927 else: | |
1928 expected = 'repository group `%s` does not exist' % TEST_REPO_GROUP | |
1929 self._compare_error(id_, expected, given=response.body) | |
1930 | |
1931 @mock.patch.object(RepoGroupModel, 'revoke_user_permission', crash) | |
1932 def test_api_revoke_user_permission_from_repo_group_exception_when_adding(self): | |
1933 id_, params = _build_data(self.apikey, | |
1934 'revoke_user_permission_from_repo_group', | |
1935 repogroupid=TEST_REPO_GROUP, | |
1936 userid=TEST_USER_ADMIN_LOGIN, ) | |
1937 response = api_call(self, params) | |
1938 | |
1939 expected = 'failed to edit permission for user: `%s` in repo group: `%s`' % ( | |
1940 TEST_USER_ADMIN_LOGIN, TEST_REPO_GROUP | |
1941 ) | |
1942 self._compare_error(id_, expected, given=response.body) | |
1943 | |
1944 @parameterized.expand([ | |
1945 ('none', 'group.none', 'none'), | |
1946 ('read', 'group.read', 'none'), | |
1947 ('write', 'group.write', 'none'), | |
1948 ('admin', 'group.admin', 'none'), | |
1949 | |
1950 ('none', 'group.none', 'all'), | |
1951 ('read', 'group.read', 'all'), | |
1952 ('write', 'group.write', 'all'), | |
1953 ('admin', 'group.admin', 'all'), | |
1954 | |
1955 ('none', 'group.none', 'repos'), | |
1956 ('read', 'group.read', 'repos'), | |
1957 ('write', 'group.write', 'repos'), | |
1958 ('admin', 'group.admin', 'repos'), | |
1959 | |
1960 ('none', 'group.none', 'groups'), | |
1961 ('read', 'group.read', 'groups'), | |
1962 ('write', 'group.write', 'groups'), | |
1963 ('admin', 'group.admin', 'groups'), | |
1964 ]) | |
1965 def test_api_grant_user_group_permission_to_repo_group(self, name, perm, apply_to_children): | |
1966 id_, params = _build_data(self.apikey, | |
1967 'grant_user_group_permission_to_repo_group', | |
1968 repogroupid=TEST_REPO_GROUP, | |
1969 usergroupid=TEST_USER_GROUP, | |
1970 perm=perm, | |
1971 apply_to_children=apply_to_children,) | |
1972 response = api_call(self, params) | |
1973 | |
1974 ret = { | |
1975 'msg': 'Granted perm: `%s` (recursive:%s) for user group: `%s` in repo group: `%s`' % ( | |
1976 perm, apply_to_children, TEST_USER_GROUP, TEST_REPO_GROUP | |
1977 ), | |
1978 'success': True | |
1979 } | |
1980 expected = ret | |
1981 self._compare_ok(id_, expected, given=response.body) | |
1982 | |
1983 @parameterized.expand([ | |
1984 ('none_fails', 'group.none', 'none', False, False), | |
1985 ('read_fails', 'group.read', 'none', False, False), | |
1986 ('write_fails', 'group.write', 'none', False, False), | |
1987 ('admin_fails', 'group.admin', 'none', False, False), | |
1988 | |
1989 # with granted perms | |
1990 ('none_ok', 'group.none', 'none', True, True), | |
1991 ('read_ok', 'group.read', 'none', True, True), | |
1992 ('write_ok', 'group.write', 'none', True, True), | |
1993 ('admin_ok', 'group.admin', 'none', True, True), | |
1994 ]) | |
1995 def test_api_grant_user_group_permission_to_repo_group_by_regular_user( | |
1996 self, name, perm, apply_to_children, grant_admin, access_ok): | |
1997 if grant_admin: | |
1998 RepoGroupModel().grant_user_permission(TEST_REPO_GROUP, | |
1999 self.TEST_USER_LOGIN, | |
2000 'group.admin') | |
2001 Session().commit() | |
2002 | |
2003 id_, params = _build_data(self.apikey_regular, | |
2004 'grant_user_group_permission_to_repo_group', | |
2005 repogroupid=TEST_REPO_GROUP, | |
2006 usergroupid=TEST_USER_GROUP, | |
2007 perm=perm, | |
2008 apply_to_children=apply_to_children,) | |
2009 response = api_call(self, params) | |
2010 if access_ok: | |
2011 ret = { | |
2012 'msg': 'Granted perm: `%s` (recursive:%s) for user group: `%s` in repo group: `%s`' % ( | |
2013 perm, apply_to_children, TEST_USER_GROUP, TEST_REPO_GROUP | |
2014 ), | |
2015 'success': True | |
2016 } | |
2017 expected = ret | |
2018 self._compare_ok(id_, expected, given=response.body) | |
2019 else: | |
2020 expected = 'repository group `%s` does not exist' % TEST_REPO_GROUP | |
2021 self._compare_error(id_, expected, given=response.body) | |
2022 | |
2023 def test_api_grant_user_group_permission_to_repo_group_wrong_permission(self): | |
2024 perm = 'haha.no.permission' | |
2025 id_, params = _build_data(self.apikey, | |
2026 'grant_user_group_permission_to_repo_group', | |
2027 repogroupid=TEST_REPO_GROUP, | |
2028 usergroupid=TEST_USER_GROUP, | |
2029 perm=perm) | |
2030 response = api_call(self, params) | |
2031 | |
2032 expected = 'permission `%s` does not exist' % perm | |
2033 self._compare_error(id_, expected, given=response.body) | |
2034 | |
2035 @mock.patch.object(RepoGroupModel, 'grant_user_group_permission', crash) | |
2036 def test_api_grant_user_group_permission_exception_when_adding(self): | |
2037 perm = 'group.read' | |
2038 id_, params = _build_data(self.apikey, | |
2039 'grant_user_group_permission_to_repo_group', | |
2040 repogroupid=TEST_REPO_GROUP, | |
2041 usergroupid=TEST_USER_GROUP, | |
2042 perm=perm) | |
2043 response = api_call(self, params) | |
2044 | |
2045 expected = 'failed to edit permission for user group: `%s` in repo group: `%s`' % ( | |
2046 TEST_USER_GROUP, TEST_REPO_GROUP | |
2047 ) | |
2048 self._compare_error(id_, expected, given=response.body) | |
2049 | |
2050 @parameterized.expand([ | |
2051 ('none', 'none'), | |
2052 ('all', 'all'), | |
2053 ('repos', 'repos'), | |
2054 ('groups', 'groups'), | |
2055 ]) | |
2056 def test_api_revoke_user_group_permission_from_repo_group(self, name, apply_to_children): | |
2057 RepoGroupModel().grant_user_group_permission(repo_group=TEST_REPO_GROUP, | |
2058 group_name=TEST_USER_GROUP, | |
2059 perm='group.read',) | |
2060 Session().commit() | |
2061 id_, params = _build_data(self.apikey, | |
2062 'revoke_user_group_permission_from_repo_group', | |
2063 repogroupid=TEST_REPO_GROUP, | |
2064 usergroupid=TEST_USER_GROUP, | |
2065 apply_to_children=apply_to_children,) | |
2066 response = api_call(self, params) | |
2067 | |
2068 expected = { | |
2069 'msg': 'Revoked perm (recursive:%s) for user group: `%s` in repo group: `%s`' % ( | |
2070 apply_to_children, TEST_USER_GROUP, TEST_REPO_GROUP | |
2071 ), | |
2072 'success': True | |
2073 } | |
2074 self._compare_ok(id_, expected, given=response.body) | |
2075 | |
2076 @parameterized.expand([ | |
2077 ('none', 'none', False, False), | |
2078 ('all', 'all', False, False), | |
2079 ('repos', 'repos', False, False), | |
2080 ('groups', 'groups', False, False), | |
2081 | |
2082 # after granting admin rights | |
2083 ('none', 'none', False, False), | |
2084 ('all', 'all', False, False), | |
2085 ('repos', 'repos', False, False), | |
2086 ('groups', 'groups', False, False), | |
2087 ]) | |
2088 def test_api_revoke_user_group_permission_from_repo_group_by_regular_user( | |
2089 self, name, apply_to_children, grant_admin, access_ok): | |
2090 RepoGroupModel().grant_user_permission(repo_group=TEST_REPO_GROUP, | |
2091 user=TEST_USER_ADMIN_LOGIN, | |
2092 perm='group.read',) | |
2093 Session().commit() | |
2094 | |
2095 if grant_admin: | |
2096 RepoGroupModel().grant_user_permission(TEST_REPO_GROUP, | |
2097 self.TEST_USER_LOGIN, | |
2098 'group.admin') | |
2099 Session().commit() | |
2100 | |
2101 id_, params = _build_data(self.apikey_regular, | |
2102 'revoke_user_group_permission_from_repo_group', | |
2103 repogroupid=TEST_REPO_GROUP, | |
2104 usergroupid=TEST_USER_GROUP, | |
2105 apply_to_children=apply_to_children,) | |
2106 response = api_call(self, params) | |
2107 if access_ok: | |
2108 expected = { | |
2109 'msg': 'Revoked perm (recursive:%s) for user group: `%s` in repo group: `%s`' % ( | |
2110 apply_to_children, TEST_USER_ADMIN_LOGIN, TEST_REPO_GROUP | |
2111 ), | |
2112 'success': True | |
2113 } | |
2114 self._compare_ok(id_, expected, given=response.body) | |
2115 else: | |
2116 expected = 'repository group `%s` does not exist' % TEST_REPO_GROUP | |
2117 self._compare_error(id_, expected, given=response.body) | |
2118 | |
2119 @mock.patch.object(RepoGroupModel, 'revoke_user_group_permission', crash) | |
2120 def test_api_revoke_user_group_permission_from_repo_group_exception_when_adding(self): | |
2121 id_, params = _build_data(self.apikey, 'revoke_user_group_permission_from_repo_group', | |
2122 repogroupid=TEST_REPO_GROUP, | |
2123 usergroupid=TEST_USER_GROUP,) | |
2124 response = api_call(self, params) | |
2125 | |
2126 expected = 'failed to edit permission for user group: `%s` in repo group: `%s`' % ( | |
2127 TEST_USER_GROUP, TEST_REPO_GROUP | |
2128 ) | |
2129 self._compare_error(id_, expected, given=response.body) | |
2130 | |
2131 def test_api_get_gist(self): | |
2132 gist = fixture.create_gist() | |
2133 gist_id = gist.gist_access_id | |
2134 gist_created_on = gist.created_on | |
2135 id_, params = _build_data(self.apikey, 'get_gist', | |
2136 gistid=gist_id, ) | |
2137 response = api_call(self, params) | |
2138 | |
2139 expected = { | |
2140 'access_id': gist_id, | |
2141 'created_on': gist_created_on, | |
2142 'description': 'new-gist', | |
2143 'expires': -1.0, | |
2144 'gist_id': int(gist_id), | |
2145 'type': 'public', | |
2146 'url': 'http://localhost:80/_admin/gists/%s' % gist_id | |
2147 } | |
2148 | |
2149 self._compare_ok(id_, expected, given=response.body) | |
2150 | |
2151 def test_api_get_gist_that_does_not_exist(self): | |
2152 id_, params = _build_data(self.apikey_regular, 'get_gist', | |
2153 gistid='12345', ) | |
2154 response = api_call(self, params) | |
2155 expected = 'gist `%s` does not exist' % ('12345',) | |
2156 self._compare_error(id_, expected, given=response.body) | |
2157 | |
2158 def test_api_get_gist_private_gist_without_permission(self): | |
2159 gist = fixture.create_gist() | |
2160 gist_id = gist.gist_access_id | |
2161 gist_created_on = gist.created_on | |
2162 id_, params = _build_data(self.apikey_regular, 'get_gist', | |
2163 gistid=gist_id, ) | |
2164 response = api_call(self, params) | |
2165 | |
2166 expected = 'gist `%s` does not exist' % gist_id | |
2167 self._compare_error(id_, expected, given=response.body) | |
2168 | |
2169 def test_api_get_gists(self): | |
2170 fixture.create_gist() | |
2171 fixture.create_gist() | |
2172 | |
2173 id_, params = _build_data(self.apikey, 'get_gists') | |
2174 response = api_call(self, params) | |
2175 expected = response.json | |
2176 self.assertEqual(len(response.json['result']), 2) | |
2177 #self._compare_ok(id_, expected, given=response.body) | |
2178 | |
2179 def test_api_get_gists_regular_user(self): | |
2180 # by admin | |
2181 fixture.create_gist() | |
2182 fixture.create_gist() | |
2183 | |
2184 # by reg user | |
2185 fixture.create_gist(owner=self.TEST_USER_LOGIN) | |
2186 fixture.create_gist(owner=self.TEST_USER_LOGIN) | |
2187 fixture.create_gist(owner=self.TEST_USER_LOGIN) | |
2188 | |
2189 id_, params = _build_data(self.apikey_regular, 'get_gists') | |
2190 response = api_call(self, params) | |
2191 expected = response.json | |
2192 self.assertEqual(len(response.json['result']), 3) | |
2193 #self._compare_ok(id_, expected, given=response.body) | |
2194 | |
2195 def test_api_get_gists_only_for_regular_user(self): | |
2196 # by admin | |
2197 fixture.create_gist() | |
2198 fixture.create_gist() | |
2199 | |
2200 # by reg user | |
2201 fixture.create_gist(owner=self.TEST_USER_LOGIN) | |
2202 fixture.create_gist(owner=self.TEST_USER_LOGIN) | |
2203 fixture.create_gist(owner=self.TEST_USER_LOGIN) | |
2204 | |
2205 id_, params = _build_data(self.apikey, 'get_gists', | |
2206 userid=self.TEST_USER_LOGIN) | |
2207 response = api_call(self, params) | |
2208 expected = response.json | |
2209 self.assertEqual(len(response.json['result']), 3) | |
2210 #self._compare_ok(id_, expected, given=response.body) | |
2211 | |
2212 def test_api_get_gists_regular_user_with_different_userid(self): | |
2213 id_, params = _build_data(self.apikey_regular, 'get_gists', | |
2214 userid=TEST_USER_ADMIN_LOGIN) | |
2215 response = api_call(self, params) | |
2216 expected = 'userid is not the same as your user' | |
2217 self._compare_error(id_, expected, given=response.body) | |
2218 | |
2219 def test_api_create_gist(self): | |
2220 id_, params = _build_data(self.apikey_regular, 'create_gist', | |
2221 lifetime=10, | |
2222 description='foobar-gist', | |
2223 gist_type='public', | |
2224 files={'foobar': {'content': 'foo'}}) | |
2225 response = api_call(self, params) | |
2226 response_json = response.json | |
2227 expected = { | |
2228 'gist': { | |
2229 'access_id': response_json['result']['gist']['access_id'], | |
2230 'created_on': response_json['result']['gist']['created_on'], | |
2231 'description': 'foobar-gist', | |
2232 'expires': response_json['result']['gist']['expires'], | |
2233 'gist_id': response_json['result']['gist']['gist_id'], | |
2234 'type': 'public', | |
2235 'url': response_json['result']['gist']['url'] | |
2236 }, | |
2237 'msg': 'created new gist' | |
2238 } | |
2239 self._compare_ok(id_, expected, given=response.body) | |
2240 | |
2241 @mock.patch.object(GistModel, 'create', crash) | |
2242 def test_api_create_gist_exception_occured(self): | |
2243 id_, params = _build_data(self.apikey_regular, 'create_gist', | |
2244 files={}) | |
2245 response = api_call(self, params) | |
2246 expected = 'failed to create gist' | |
2247 self._compare_error(id_, expected, given=response.body) | |
2248 | |
2249 def test_api_delete_gist(self): | |
2250 gist_id = fixture.create_gist().gist_access_id | |
2251 id_, params = _build_data(self.apikey, 'delete_gist', | |
2252 gistid=gist_id) | |
2253 response = api_call(self, params) | |
2254 expected = {'gist': None, 'msg': 'deleted gist ID:%s' % gist_id} | |
2255 self._compare_ok(id_, expected, given=response.body) | |
2256 | |
2257 def test_api_delete_gist_regular_user(self): | |
2258 gist_id = fixture.create_gist(owner=self.TEST_USER_LOGIN).gist_access_id | |
2259 id_, params = _build_data(self.apikey_regular, 'delete_gist', | |
2260 gistid=gist_id) | |
2261 response = api_call(self, params) | |
2262 expected = {'gist': None, 'msg': 'deleted gist ID:%s' % gist_id} | |
2263 self._compare_ok(id_, expected, given=response.body) | |
2264 | |
2265 def test_api_delete_gist_regular_user_no_permission(self): | |
2266 gist_id = fixture.create_gist().gist_access_id | |
2267 id_, params = _build_data(self.apikey_regular, 'delete_gist', | |
2268 gistid=gist_id) | |
2269 response = api_call(self, params) | |
2270 expected = 'gist `%s` does not exist' % (gist_id,) | |
2271 self._compare_error(id_, expected, given=response.body) | |
2272 | |
2273 @mock.patch.object(GistModel, 'delete', crash) | |
2274 def test_api_delete_gist_exception_occured(self): | |
2275 gist_id = fixture.create_gist().gist_access_id | |
2276 id_, params = _build_data(self.apikey, 'delete_gist', | |
2277 gistid=gist_id) | |
2278 response = api_call(self, params) | |
2279 expected = 'failed to delete gist ID:%s' % (gist_id,) | |
2280 self._compare_error(id_, expected, given=response.body) | |
2281 | |
2282 def test_api_get_ip(self): | |
2283 id_, params = _build_data(self.apikey, 'get_ip') | |
2284 response = api_call(self, params) | |
2285 expected = { | |
2286 'server_ip_addr': '0.0.0.0', | |
2287 'user_ips': [] | |
2288 } | |
2289 self._compare_ok(id_, expected, given=response.body) | |
2290 | |
2291 def test_api_get_server_info(self): | |
2292 id_, params = _build_data(self.apikey, 'get_server_info') | |
2293 response = api_call(self, params) | |
2294 expected = RhodeCodeSetting.get_server_info() | |
2295 self._compare_ok(id_, expected, given=response.body) |