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