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