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