##// END OF EJS Templates
fixed broken API tests after excluding default user...
marcink -
r3803:e2ebd880 beta
parent child Browse files
Show More
@@ -1,1310 +1,1312 b''
1 from __future__ import with_statement
1 from __future__ import with_statement
2 import random
2 import random
3 import mock
3 import mock
4
4
5 from rhodecode.tests import *
5 from rhodecode.tests import *
6 from rhodecode.tests.fixture import Fixture
6 from rhodecode.tests.fixture import Fixture
7 from rhodecode.lib.compat import json
7 from rhodecode.lib.compat import json
8 from rhodecode.lib.auth import AuthUser
8 from rhodecode.lib.auth import AuthUser
9 from rhodecode.model.user import UserModel
9 from rhodecode.model.user import UserModel
10 from rhodecode.model.users_group import UserGroupModel
10 from rhodecode.model.users_group import UserGroupModel
11 from rhodecode.model.repo import RepoModel
11 from rhodecode.model.repo import RepoModel
12 from rhodecode.model.meta import Session
12 from rhodecode.model.meta import Session
13 from rhodecode.model.scm import ScmModel
13 from rhodecode.model.scm import ScmModel
14 from rhodecode.model.db import Repository
14 from rhodecode.model.db import Repository, User
15
15
16
16
17 API_URL = '/_admin/api'
17 API_URL = '/_admin/api'
18 TEST_USER_GROUP = 'test_users_group'
18 TEST_USER_GROUP = 'test_users_group'
19
19
20 fixture = Fixture()
20 fixture = Fixture()
21
21
22
22
23 def _build_data(apikey, method, **kw):
23 def _build_data(apikey, method, **kw):
24 """
24 """
25 Builds API data with given random ID
25 Builds API data with given random ID
26
26
27 :param random_id:
27 :param random_id:
28 :type random_id:
28 :type random_id:
29 """
29 """
30 random_id = random.randrange(1, 9999)
30 random_id = random.randrange(1, 9999)
31 return random_id, json.dumps({
31 return random_id, json.dumps({
32 "id": random_id,
32 "id": random_id,
33 "api_key": apikey,
33 "api_key": apikey,
34 "method": method,
34 "method": method,
35 "args": kw
35 "args": kw
36 })
36 })
37
37
38 jsonify = lambda obj: json.loads(json.dumps(obj))
38 jsonify = lambda obj: json.loads(json.dumps(obj))
39
39
40
40
41 def crash(*args, **kwargs):
41 def crash(*args, **kwargs):
42 raise Exception('Total Crash !')
42 raise Exception('Total Crash !')
43
43
44
44
45 def api_call(test_obj, params):
45 def api_call(test_obj, params):
46 response = test_obj.app.post(API_URL, content_type='application/json',
46 response = test_obj.app.post(API_URL, content_type='application/json',
47 params=params)
47 params=params)
48 return response
48 return response
49
49
50
50
51 ## helpers
51 ## helpers
52 def make_users_group(name=TEST_USER_GROUP):
52 def make_users_group(name=TEST_USER_GROUP):
53 gr = fixture.create_user_group(name, cur_user=TEST_USER_ADMIN_LOGIN)
53 gr = fixture.create_user_group(name, cur_user=TEST_USER_ADMIN_LOGIN)
54 UserGroupModel().add_user_to_group(users_group=gr,
54 UserGroupModel().add_user_to_group(users_group=gr,
55 user=TEST_USER_ADMIN_LOGIN)
55 user=TEST_USER_ADMIN_LOGIN)
56 Session().commit()
56 Session().commit()
57 return gr
57 return gr
58
58
59
59
60 def destroy_users_group(name=TEST_USER_GROUP):
60 def destroy_users_group(name=TEST_USER_GROUP):
61 UserGroupModel().delete(users_group=name, force=True)
61 UserGroupModel().delete(users_group=name, force=True)
62 Session().commit()
62 Session().commit()
63
63
64
64
65 class BaseTestApi(object):
65 class BaseTestApi(object):
66 REPO = None
66 REPO = None
67 REPO_TYPE = None
67 REPO_TYPE = None
68
68
69 @classmethod
69 @classmethod
70 def setUpClass(self):
70 def setUpClass(self):
71 self.usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
71 self.usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
72 self.apikey = self.usr.api_key
72 self.apikey = self.usr.api_key
73 self.test_user = UserModel().create_or_update(
73 self.test_user = UserModel().create_or_update(
74 username='test-api',
74 username='test-api',
75 password='test',
75 password='test',
76 email='test@api.rhodecode.org',
76 email='test@api.rhodecode.org',
77 firstname='first',
77 firstname='first',
78 lastname='last'
78 lastname='last'
79 )
79 )
80 Session().commit()
80 Session().commit()
81 self.TEST_USER_LOGIN = self.test_user.username
81 self.TEST_USER_LOGIN = self.test_user.username
82 self.apikey_regular = self.test_user.api_key
82 self.apikey_regular = self.test_user.api_key
83
83
84 @classmethod
84 @classmethod
85 def teardownClass(self):
85 def teardownClass(self):
86 pass
86 pass
87
87
88 def setUp(self):
88 def setUp(self):
89 self.maxDiff = None
89 self.maxDiff = None
90 make_users_group()
90 make_users_group()
91
91
92 def tearDown(self):
92 def tearDown(self):
93 destroy_users_group()
93 destroy_users_group()
94
94
95 def _compare_ok(self, id_, expected, given):
95 def _compare_ok(self, id_, expected, given):
96 expected = jsonify({
96 expected = jsonify({
97 'id': id_,
97 'id': id_,
98 'error': None,
98 'error': None,
99 'result': expected
99 'result': expected
100 })
100 })
101 given = json.loads(given)
101 given = json.loads(given)
102 self.assertEqual(expected, given)
102 self.assertEqual(expected, given)
103
103
104 def _compare_error(self, id_, expected, given):
104 def _compare_error(self, id_, expected, given):
105 expected = jsonify({
105 expected = jsonify({
106 'id': id_,
106 'id': id_,
107 'error': expected,
107 'error': expected,
108 'result': None
108 'result': None
109 })
109 })
110 given = json.loads(given)
110 given = json.loads(given)
111 self.assertEqual(expected, given)
111 self.assertEqual(expected, given)
112
112
113 # def test_Optional(self):
113 # def test_Optional(self):
114 # from rhodecode.controllers.api.api import Optional
114 # from rhodecode.controllers.api.api import Optional
115 # option1 = Optional(None)
115 # option1 = Optional(None)
116 # self.assertEqual('<Optional:%s>' % None, repr(option1))
116 # self.assertEqual('<Optional:%s>' % None, repr(option1))
117 #
117 #
118 # self.assertEqual(1, Optional.extract(Optional(1)))
118 # self.assertEqual(1, Optional.extract(Optional(1)))
119 # self.assertEqual('trololo', Optional.extract('trololo'))
119 # self.assertEqual('trololo', Optional.extract('trololo'))
120
120
121 def test_api_wrong_key(self):
121 def test_api_wrong_key(self):
122 id_, params = _build_data('trololo', 'get_user')
122 id_, params = _build_data('trololo', 'get_user')
123 response = api_call(self, params)
123 response = api_call(self, params)
124
124
125 expected = 'Invalid API KEY'
125 expected = 'Invalid API KEY'
126 self._compare_error(id_, expected, given=response.body)
126 self._compare_error(id_, expected, given=response.body)
127
127
128 def test_api_missing_non_optional_param(self):
128 def test_api_missing_non_optional_param(self):
129 id_, params = _build_data(self.apikey, 'get_repo')
129 id_, params = _build_data(self.apikey, 'get_repo')
130 response = api_call(self, params)
130 response = api_call(self, params)
131
131
132 expected = 'Missing non optional `repoid` arg in JSON DATA'
132 expected = 'Missing non optional `repoid` arg in JSON DATA'
133 self._compare_error(id_, expected, given=response.body)
133 self._compare_error(id_, expected, given=response.body)
134
134
135 def test_api_missing_non_optional_param_args_null(self):
135 def test_api_missing_non_optional_param_args_null(self):
136 id_, params = _build_data(self.apikey, 'get_repo')
136 id_, params = _build_data(self.apikey, 'get_repo')
137 params = params.replace('"args": {}', '"args": null')
137 params = params.replace('"args": {}', '"args": null')
138 response = api_call(self, params)
138 response = api_call(self, params)
139
139
140 expected = 'Missing non optional `repoid` arg in JSON DATA'
140 expected = 'Missing non optional `repoid` arg in JSON DATA'
141 self._compare_error(id_, expected, given=response.body)
141 self._compare_error(id_, expected, given=response.body)
142
142
143 def test_api_missing_non_optional_param_args_bad(self):
143 def test_api_missing_non_optional_param_args_bad(self):
144 id_, params = _build_data(self.apikey, 'get_repo')
144 id_, params = _build_data(self.apikey, 'get_repo')
145 params = params.replace('"args": {}', '"args": 1')
145 params = params.replace('"args": {}', '"args": 1')
146 response = api_call(self, params)
146 response = api_call(self, params)
147
147
148 expected = 'Missing non optional `repoid` arg in JSON DATA'
148 expected = 'Missing non optional `repoid` arg in JSON DATA'
149 self._compare_error(id_, expected, given=response.body)
149 self._compare_error(id_, expected, given=response.body)
150
150
151 def test_api_args_is_null(self):
151 def test_api_args_is_null(self):
152 id_, params = _build_data(self.apikey, 'get_users',)
152 id_, params = _build_data(self.apikey, 'get_users',)
153 params = params.replace('"args": {}', '"args": null')
153 params = params.replace('"args": {}', '"args": null')
154 response = api_call(self, params)
154 response = api_call(self, params)
155 self.assertEqual(response.status, '200 OK')
155 self.assertEqual(response.status, '200 OK')
156
156
157 def test_api_args_is_bad(self):
157 def test_api_args_is_bad(self):
158 id_, params = _build_data(self.apikey, 'get_users',)
158 id_, params = _build_data(self.apikey, 'get_users',)
159 params = params.replace('"args": {}', '"args": 1')
159 params = params.replace('"args": {}', '"args": 1')
160 response = api_call(self, params)
160 response = api_call(self, params)
161 self.assertEqual(response.status, '200 OK')
161 self.assertEqual(response.status, '200 OK')
162
162
163 def test_api_get_users(self):
163 def test_api_get_users(self):
164 id_, params = _build_data(self.apikey, 'get_users',)
164 id_, params = _build_data(self.apikey, 'get_users',)
165 response = api_call(self, params)
165 response = api_call(self, params)
166 ret_all = []
166 ret_all = []
167 for usr in UserModel().get_all():
167 _users = User.query().filter(User.username != User.DEFAULT_USER)\
168 .order_by(User.username).all()
169 for usr in _users:
168 ret = usr.get_api_data()
170 ret = usr.get_api_data()
169 ret_all.append(jsonify(ret))
171 ret_all.append(jsonify(ret))
170 expected = ret_all
172 expected = ret_all
171 self._compare_ok(id_, expected, given=response.body)
173 self._compare_ok(id_, expected, given=response.body)
172
174
173 def test_api_get_user(self):
175 def test_api_get_user(self):
174 id_, params = _build_data(self.apikey, 'get_user',
176 id_, params = _build_data(self.apikey, 'get_user',
175 userid=TEST_USER_ADMIN_LOGIN)
177 userid=TEST_USER_ADMIN_LOGIN)
176 response = api_call(self, params)
178 response = api_call(self, params)
177
179
178 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
180 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
179 ret = usr.get_api_data()
181 ret = usr.get_api_data()
180 ret['permissions'] = AuthUser(usr.user_id).permissions
182 ret['permissions'] = AuthUser(usr.user_id).permissions
181
183
182 expected = ret
184 expected = ret
183 self._compare_ok(id_, expected, given=response.body)
185 self._compare_ok(id_, expected, given=response.body)
184
186
185 def test_api_get_user_that_does_not_exist(self):
187 def test_api_get_user_that_does_not_exist(self):
186 id_, params = _build_data(self.apikey, 'get_user',
188 id_, params = _build_data(self.apikey, 'get_user',
187 userid='trololo')
189 userid='trololo')
188 response = api_call(self, params)
190 response = api_call(self, params)
189
191
190 expected = "user `%s` does not exist" % 'trololo'
192 expected = "user `%s` does not exist" % 'trololo'
191 self._compare_error(id_, expected, given=response.body)
193 self._compare_error(id_, expected, given=response.body)
192
194
193 def test_api_get_user_without_giving_userid(self):
195 def test_api_get_user_without_giving_userid(self):
194 id_, params = _build_data(self.apikey, 'get_user')
196 id_, params = _build_data(self.apikey, 'get_user')
195 response = api_call(self, params)
197 response = api_call(self, params)
196
198
197 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
199 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
198 ret = usr.get_api_data()
200 ret = usr.get_api_data()
199 ret['permissions'] = AuthUser(usr.user_id).permissions
201 ret['permissions'] = AuthUser(usr.user_id).permissions
200
202
201 expected = ret
203 expected = ret
202 self._compare_ok(id_, expected, given=response.body)
204 self._compare_ok(id_, expected, given=response.body)
203
205
204 def test_api_get_user_without_giving_userid_non_admin(self):
206 def test_api_get_user_without_giving_userid_non_admin(self):
205 id_, params = _build_data(self.apikey_regular, 'get_user')
207 id_, params = _build_data(self.apikey_regular, 'get_user')
206 response = api_call(self, params)
208 response = api_call(self, params)
207
209
208 usr = UserModel().get_by_username(self.TEST_USER_LOGIN)
210 usr = UserModel().get_by_username(self.TEST_USER_LOGIN)
209 ret = usr.get_api_data()
211 ret = usr.get_api_data()
210 ret['permissions'] = AuthUser(usr.user_id).permissions
212 ret['permissions'] = AuthUser(usr.user_id).permissions
211
213
212 expected = ret
214 expected = ret
213 self._compare_ok(id_, expected, given=response.body)
215 self._compare_ok(id_, expected, given=response.body)
214
216
215 def test_api_get_user_with_giving_userid_non_admin(self):
217 def test_api_get_user_with_giving_userid_non_admin(self):
216 id_, params = _build_data(self.apikey_regular, 'get_user',
218 id_, params = _build_data(self.apikey_regular, 'get_user',
217 userid=self.TEST_USER_LOGIN)
219 userid=self.TEST_USER_LOGIN)
218 response = api_call(self, params)
220 response = api_call(self, params)
219
221
220 expected = 'userid is not the same as your user'
222 expected = 'userid is not the same as your user'
221 self._compare_error(id_, expected, given=response.body)
223 self._compare_error(id_, expected, given=response.body)
222
224
223 def test_api_pull(self):
225 def test_api_pull(self):
224 #TODO: issues with rhodecode_extras here.. not sure why !
226 #TODO: issues with rhodecode_extras here.. not sure why !
225 pass
227 pass
226
228
227 # repo_name = 'test_pull'
229 # repo_name = 'test_pull'
228 # r = fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
230 # r = fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
229 # r.clone_uri = TEST_self.REPO
231 # r.clone_uri = TEST_self.REPO
230 # Session.add(r)
232 # Session.add(r)
231 # Session.commit()
233 # Session.commit()
232 #
234 #
233 # id_, params = _build_data(self.apikey, 'pull',
235 # id_, params = _build_data(self.apikey, 'pull',
234 # repoid=repo_name,)
236 # repoid=repo_name,)
235 # response = self.app.post(API_URL, content_type='application/json',
237 # response = self.app.post(API_URL, content_type='application/json',
236 # params=params)
238 # params=params)
237 #
239 #
238 # expected = 'Pulled from `%s`' % repo_name
240 # expected = 'Pulled from `%s`' % repo_name
239 # self._compare_ok(id_, expected, given=response.body)
241 # self._compare_ok(id_, expected, given=response.body)
240 #
242 #
241 # fixture.destroy_repo(repo_name)
243 # fixture.destroy_repo(repo_name)
242
244
243 def test_api_pull_error(self):
245 def test_api_pull_error(self):
244 id_, params = _build_data(self.apikey, 'pull',
246 id_, params = _build_data(self.apikey, 'pull',
245 repoid=self.REPO,)
247 repoid=self.REPO,)
246 response = api_call(self, params)
248 response = api_call(self, params)
247
249
248 expected = 'Unable to pull changes from `%s`' % self.REPO
250 expected = 'Unable to pull changes from `%s`' % self.REPO
249 self._compare_error(id_, expected, given=response.body)
251 self._compare_error(id_, expected, given=response.body)
250
252
251 def test_api_rescan_repos(self):
253 def test_api_rescan_repos(self):
252 id_, params = _build_data(self.apikey, 'rescan_repos')
254 id_, params = _build_data(self.apikey, 'rescan_repos')
253 response = api_call(self, params)
255 response = api_call(self, params)
254
256
255 expected = {'added': [], 'removed': []}
257 expected = {'added': [], 'removed': []}
256 self._compare_ok(id_, expected, given=response.body)
258 self._compare_ok(id_, expected, given=response.body)
257
259
258 @mock.patch.object(ScmModel, 'repo_scan', crash)
260 @mock.patch.object(ScmModel, 'repo_scan', crash)
259 def test_api_rescann_error(self):
261 def test_api_rescann_error(self):
260 id_, params = _build_data(self.apikey, 'rescan_repos',)
262 id_, params = _build_data(self.apikey, 'rescan_repos',)
261 response = api_call(self, params)
263 response = api_call(self, params)
262
264
263 expected = 'Error occurred during rescan repositories action'
265 expected = 'Error occurred during rescan repositories action'
264 self._compare_error(id_, expected, given=response.body)
266 self._compare_error(id_, expected, given=response.body)
265
267
266 def test_api_invalidate_cache(self):
268 def test_api_invalidate_cache(self):
267 repo = RepoModel().get_by_repo_name(self.REPO)
269 repo = RepoModel().get_by_repo_name(self.REPO)
268 repo.scm_instance_cached() # seed cache
270 repo.scm_instance_cached() # seed cache
269
271
270 id_, params = _build_data(self.apikey, 'invalidate_cache',
272 id_, params = _build_data(self.apikey, 'invalidate_cache',
271 repoid=self.REPO)
273 repoid=self.REPO)
272 response = api_call(self, params)
274 response = api_call(self, params)
273
275
274 expected = ("Caches of repository `%s` was invalidated" % (self.REPO))
276 expected = ("Caches of repository `%s` was invalidated" % (self.REPO))
275 self._compare_ok(id_, expected, given=response.body)
277 self._compare_ok(id_, expected, given=response.body)
276
278
277 @mock.patch.object(ScmModel, 'mark_for_invalidation', crash)
279 @mock.patch.object(ScmModel, 'mark_for_invalidation', crash)
278 def test_api_invalidate_cache_error(self):
280 def test_api_invalidate_cache_error(self):
279 id_, params = _build_data(self.apikey, 'invalidate_cache',
281 id_, params = _build_data(self.apikey, 'invalidate_cache',
280 repoid=self.REPO)
282 repoid=self.REPO)
281 response = api_call(self, params)
283 response = api_call(self, params)
282
284
283 expected = 'Error occurred during cache invalidation action'
285 expected = 'Error occurred during cache invalidation action'
284 self._compare_error(id_, expected, given=response.body)
286 self._compare_error(id_, expected, given=response.body)
285
287
286 def test_api_lock_repo_lock_aquire(self):
288 def test_api_lock_repo_lock_aquire(self):
287 id_, params = _build_data(self.apikey, 'lock',
289 id_, params = _build_data(self.apikey, 'lock',
288 userid=TEST_USER_ADMIN_LOGIN,
290 userid=TEST_USER_ADMIN_LOGIN,
289 repoid=self.REPO,
291 repoid=self.REPO,
290 locked=True)
292 locked=True)
291 response = api_call(self, params)
293 response = api_call(self, params)
292 expected = ('User `%s` set lock state for repo `%s` to `%s`'
294 expected = ('User `%s` set lock state for repo `%s` to `%s`'
293 % (TEST_USER_ADMIN_LOGIN, self.REPO, True))
295 % (TEST_USER_ADMIN_LOGIN, self.REPO, True))
294 self._compare_ok(id_, expected, given=response.body)
296 self._compare_ok(id_, expected, given=response.body)
295
297
296 def test_api_lock_repo_lock_aquire_by_non_admin(self):
298 def test_api_lock_repo_lock_aquire_by_non_admin(self):
297 repo_name = 'api_delete_me'
299 repo_name = 'api_delete_me'
298 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE,
300 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE,
299 cur_user=self.TEST_USER_LOGIN)
301 cur_user=self.TEST_USER_LOGIN)
300 try:
302 try:
301 id_, params = _build_data(self.apikey_regular, 'lock',
303 id_, params = _build_data(self.apikey_regular, 'lock',
302 repoid=repo_name,
304 repoid=repo_name,
303 locked=True)
305 locked=True)
304 response = api_call(self, params)
306 response = api_call(self, params)
305 expected = ('User `%s` set lock state for repo `%s` to `%s`'
307 expected = ('User `%s` set lock state for repo `%s` to `%s`'
306 % (self.TEST_USER_LOGIN, repo_name, True))
308 % (self.TEST_USER_LOGIN, repo_name, True))
307 self._compare_ok(id_, expected, given=response.body)
309 self._compare_ok(id_, expected, given=response.body)
308 finally:
310 finally:
309 fixture.destroy_repo(repo_name)
311 fixture.destroy_repo(repo_name)
310
312
311 def test_api_lock_repo_lock_aquire_non_admin_with_userid(self):
313 def test_api_lock_repo_lock_aquire_non_admin_with_userid(self):
312 repo_name = 'api_delete_me'
314 repo_name = 'api_delete_me'
313 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE,
315 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE,
314 cur_user=self.TEST_USER_LOGIN)
316 cur_user=self.TEST_USER_LOGIN)
315 try:
317 try:
316 id_, params = _build_data(self.apikey_regular, 'lock',
318 id_, params = _build_data(self.apikey_regular, 'lock',
317 userid=TEST_USER_ADMIN_LOGIN,
319 userid=TEST_USER_ADMIN_LOGIN,
318 repoid=repo_name,
320 repoid=repo_name,
319 locked=True)
321 locked=True)
320 response = api_call(self, params)
322 response = api_call(self, params)
321 expected = 'userid is not the same as your user'
323 expected = 'userid is not the same as your user'
322 self._compare_error(id_, expected, given=response.body)
324 self._compare_error(id_, expected, given=response.body)
323 finally:
325 finally:
324 fixture.destroy_repo(repo_name)
326 fixture.destroy_repo(repo_name)
325
327
326 def test_api_lock_repo_lock_aquire_non_admin_not_his_repo(self):
328 def test_api_lock_repo_lock_aquire_non_admin_not_his_repo(self):
327 id_, params = _build_data(self.apikey_regular, 'lock',
329 id_, params = _build_data(self.apikey_regular, 'lock',
328 repoid=self.REPO,
330 repoid=self.REPO,
329 locked=True)
331 locked=True)
330 response = api_call(self, params)
332 response = api_call(self, params)
331 expected = 'repository `%s` does not exist' % (self.REPO)
333 expected = 'repository `%s` does not exist' % (self.REPO)
332 self._compare_error(id_, expected, given=response.body)
334 self._compare_error(id_, expected, given=response.body)
333
335
334 def test_api_lock_repo_lock_release(self):
336 def test_api_lock_repo_lock_release(self):
335 id_, params = _build_data(self.apikey, 'lock',
337 id_, params = _build_data(self.apikey, 'lock',
336 userid=TEST_USER_ADMIN_LOGIN,
338 userid=TEST_USER_ADMIN_LOGIN,
337 repoid=self.REPO,
339 repoid=self.REPO,
338 locked=False)
340 locked=False)
339 response = api_call(self, params)
341 response = api_call(self, params)
340 expected = ('User `%s` set lock state for repo `%s` to `%s`'
342 expected = ('User `%s` set lock state for repo `%s` to `%s`'
341 % (TEST_USER_ADMIN_LOGIN, self.REPO, False))
343 % (TEST_USER_ADMIN_LOGIN, self.REPO, False))
342 self._compare_ok(id_, expected, given=response.body)
344 self._compare_ok(id_, expected, given=response.body)
343
345
344 def test_api_lock_repo_lock_aquire_optional_userid(self):
346 def test_api_lock_repo_lock_aquire_optional_userid(self):
345 id_, params = _build_data(self.apikey, 'lock',
347 id_, params = _build_data(self.apikey, 'lock',
346 repoid=self.REPO,
348 repoid=self.REPO,
347 locked=True)
349 locked=True)
348 response = api_call(self, params)
350 response = api_call(self, params)
349 expected = ('User `%s` set lock state for repo `%s` to `%s`'
351 expected = ('User `%s` set lock state for repo `%s` to `%s`'
350 % (TEST_USER_ADMIN_LOGIN, self.REPO, True))
352 % (TEST_USER_ADMIN_LOGIN, self.REPO, True))
351 self._compare_ok(id_, expected, given=response.body)
353 self._compare_ok(id_, expected, given=response.body)
352
354
353 def test_api_lock_repo_lock_optional_locked(self):
355 def test_api_lock_repo_lock_optional_locked(self):
354 from rhodecode.lib.utils2 import time_to_datetime
356 from rhodecode.lib.utils2 import time_to_datetime
355 _locked_since = json.dumps(time_to_datetime(Repository\
357 _locked_since = json.dumps(time_to_datetime(Repository\
356 .get_by_repo_name(self.REPO).locked[1]))
358 .get_by_repo_name(self.REPO).locked[1]))
357 id_, params = _build_data(self.apikey, 'lock',
359 id_, params = _build_data(self.apikey, 'lock',
358 repoid=self.REPO)
360 repoid=self.REPO)
359 response = api_call(self, params)
361 response = api_call(self, params)
360 expected = ('Repo `%s` locked by `%s`. Locked=`True`. Locked since: `%s`'
362 expected = ('Repo `%s` locked by `%s`. Locked=`True`. Locked since: `%s`'
361 % (self.REPO, TEST_USER_ADMIN_LOGIN, _locked_since))
363 % (self.REPO, TEST_USER_ADMIN_LOGIN, _locked_since))
362 self._compare_ok(id_, expected, given=response.body)
364 self._compare_ok(id_, expected, given=response.body)
363
365
364 @mock.patch.object(Repository, 'lock', crash)
366 @mock.patch.object(Repository, 'lock', crash)
365 def test_api_lock_error(self):
367 def test_api_lock_error(self):
366 id_, params = _build_data(self.apikey, 'lock',
368 id_, params = _build_data(self.apikey, 'lock',
367 userid=TEST_USER_ADMIN_LOGIN,
369 userid=TEST_USER_ADMIN_LOGIN,
368 repoid=self.REPO,
370 repoid=self.REPO,
369 locked=True)
371 locked=True)
370 response = api_call(self, params)
372 response = api_call(self, params)
371
373
372 expected = 'Error occurred locking repository `%s`' % self.REPO
374 expected = 'Error occurred locking repository `%s`' % self.REPO
373 self._compare_error(id_, expected, given=response.body)
375 self._compare_error(id_, expected, given=response.body)
374
376
375 def test_api_get_locks_regular_user(self):
377 def test_api_get_locks_regular_user(self):
376 id_, params = _build_data(self.apikey_regular, 'get_locks')
378 id_, params = _build_data(self.apikey_regular, 'get_locks')
377 response = api_call(self, params)
379 response = api_call(self, params)
378 expected = []
380 expected = []
379 self._compare_ok(id_, expected, given=response.body)
381 self._compare_ok(id_, expected, given=response.body)
380
382
381 def test_api_get_locks_with_userid_regular_user(self):
383 def test_api_get_locks_with_userid_regular_user(self):
382 id_, params = _build_data(self.apikey_regular, 'get_locks',
384 id_, params = _build_data(self.apikey_regular, 'get_locks',
383 userid=TEST_USER_ADMIN_LOGIN)
385 userid=TEST_USER_ADMIN_LOGIN)
384 response = api_call(self, params)
386 response = api_call(self, params)
385 expected = 'userid is not the same as your user'
387 expected = 'userid is not the same as your user'
386 self._compare_error(id_, expected, given=response.body)
388 self._compare_error(id_, expected, given=response.body)
387
389
388 def test_api_get_locks(self):
390 def test_api_get_locks(self):
389 id_, params = _build_data(self.apikey, 'get_locks')
391 id_, params = _build_data(self.apikey, 'get_locks')
390 response = api_call(self, params)
392 response = api_call(self, params)
391 expected = []
393 expected = []
392 self._compare_ok(id_, expected, given=response.body)
394 self._compare_ok(id_, expected, given=response.body)
393
395
394 def test_api_get_locks_with_userid(self):
396 def test_api_get_locks_with_userid(self):
395 id_, params = _build_data(self.apikey, 'get_locks',
397 id_, params = _build_data(self.apikey, 'get_locks',
396 userid=TEST_USER_REGULAR_LOGIN)
398 userid=TEST_USER_REGULAR_LOGIN)
397 response = api_call(self, params)
399 response = api_call(self, params)
398 expected = []
400 expected = []
399 self._compare_ok(id_, expected, given=response.body)
401 self._compare_ok(id_, expected, given=response.body)
400
402
401 def test_api_create_existing_user(self):
403 def test_api_create_existing_user(self):
402 id_, params = _build_data(self.apikey, 'create_user',
404 id_, params = _build_data(self.apikey, 'create_user',
403 username=TEST_USER_ADMIN_LOGIN,
405 username=TEST_USER_ADMIN_LOGIN,
404 email='test@foo.com',
406 email='test@foo.com',
405 password='trololo')
407 password='trololo')
406 response = api_call(self, params)
408 response = api_call(self, params)
407
409
408 expected = "user `%s` already exist" % TEST_USER_ADMIN_LOGIN
410 expected = "user `%s` already exist" % TEST_USER_ADMIN_LOGIN
409 self._compare_error(id_, expected, given=response.body)
411 self._compare_error(id_, expected, given=response.body)
410
412
411 def test_api_create_user_with_existing_email(self):
413 def test_api_create_user_with_existing_email(self):
412 id_, params = _build_data(self.apikey, 'create_user',
414 id_, params = _build_data(self.apikey, 'create_user',
413 username=TEST_USER_ADMIN_LOGIN + 'new',
415 username=TEST_USER_ADMIN_LOGIN + 'new',
414 email=TEST_USER_REGULAR_EMAIL,
416 email=TEST_USER_REGULAR_EMAIL,
415 password='trololo')
417 password='trololo')
416 response = api_call(self, params)
418 response = api_call(self, params)
417
419
418 expected = "email `%s` already exist" % TEST_USER_REGULAR_EMAIL
420 expected = "email `%s` already exist" % TEST_USER_REGULAR_EMAIL
419 self._compare_error(id_, expected, given=response.body)
421 self._compare_error(id_, expected, given=response.body)
420
422
421 def test_api_create_user(self):
423 def test_api_create_user(self):
422 username = 'test_new_api_user'
424 username = 'test_new_api_user'
423 email = username + "@foo.com"
425 email = username + "@foo.com"
424
426
425 id_, params = _build_data(self.apikey, 'create_user',
427 id_, params = _build_data(self.apikey, 'create_user',
426 username=username,
428 username=username,
427 email=email,
429 email=email,
428 password='trololo')
430 password='trololo')
429 response = api_call(self, params)
431 response = api_call(self, params)
430
432
431 usr = UserModel().get_by_username(username)
433 usr = UserModel().get_by_username(username)
432 ret = dict(
434 ret = dict(
433 msg='created new user `%s`' % username,
435 msg='created new user `%s`' % username,
434 user=jsonify(usr.get_api_data())
436 user=jsonify(usr.get_api_data())
435 )
437 )
436
438
437 expected = ret
439 expected = ret
438 self._compare_ok(id_, expected, given=response.body)
440 self._compare_ok(id_, expected, given=response.body)
439
441
440 UserModel().delete(usr.user_id)
442 UserModel().delete(usr.user_id)
441 Session().commit()
443 Session().commit()
442
444
443 @mock.patch.object(UserModel, 'create_or_update', crash)
445 @mock.patch.object(UserModel, 'create_or_update', crash)
444 def test_api_create_user_when_exception_happened(self):
446 def test_api_create_user_when_exception_happened(self):
445
447
446 username = 'test_new_api_user'
448 username = 'test_new_api_user'
447 email = username + "@foo.com"
449 email = username + "@foo.com"
448
450
449 id_, params = _build_data(self.apikey, 'create_user',
451 id_, params = _build_data(self.apikey, 'create_user',
450 username=username,
452 username=username,
451 email=email,
453 email=email,
452 password='trololo')
454 password='trololo')
453 response = api_call(self, params)
455 response = api_call(self, params)
454 expected = 'failed to create user `%s`' % username
456 expected = 'failed to create user `%s`' % username
455 self._compare_error(id_, expected, given=response.body)
457 self._compare_error(id_, expected, given=response.body)
456
458
457 def test_api_delete_user(self):
459 def test_api_delete_user(self):
458 usr = UserModel().create_or_update(username=u'test_user',
460 usr = UserModel().create_or_update(username=u'test_user',
459 password=u'qweqwe',
461 password=u'qweqwe',
460 email=u'u232@rhodecode.org',
462 email=u'u232@rhodecode.org',
461 firstname=u'u1', lastname=u'u1')
463 firstname=u'u1', lastname=u'u1')
462 Session().commit()
464 Session().commit()
463 username = usr.username
465 username = usr.username
464 email = usr.email
466 email = usr.email
465 usr_id = usr.user_id
467 usr_id = usr.user_id
466 ## DELETE THIS USER NOW
468 ## DELETE THIS USER NOW
467
469
468 id_, params = _build_data(self.apikey, 'delete_user',
470 id_, params = _build_data(self.apikey, 'delete_user',
469 userid=username,)
471 userid=username,)
470 response = api_call(self, params)
472 response = api_call(self, params)
471
473
472 ret = {'msg': 'deleted user ID:%s %s' % (usr_id, username),
474 ret = {'msg': 'deleted user ID:%s %s' % (usr_id, username),
473 'user': None}
475 'user': None}
474 expected = ret
476 expected = ret
475 self._compare_ok(id_, expected, given=response.body)
477 self._compare_ok(id_, expected, given=response.body)
476
478
477 @mock.patch.object(UserModel, 'delete', crash)
479 @mock.patch.object(UserModel, 'delete', crash)
478 def test_api_delete_user_when_exception_happened(self):
480 def test_api_delete_user_when_exception_happened(self):
479 usr = UserModel().create_or_update(username=u'test_user',
481 usr = UserModel().create_or_update(username=u'test_user',
480 password=u'qweqwe',
482 password=u'qweqwe',
481 email=u'u232@rhodecode.org',
483 email=u'u232@rhodecode.org',
482 firstname=u'u1', lastname=u'u1')
484 firstname=u'u1', lastname=u'u1')
483 Session().commit()
485 Session().commit()
484 username = usr.username
486 username = usr.username
485
487
486 id_, params = _build_data(self.apikey, 'delete_user',
488 id_, params = _build_data(self.apikey, 'delete_user',
487 userid=username,)
489 userid=username,)
488 response = api_call(self, params)
490 response = api_call(self, params)
489 ret = 'failed to delete ID:%s %s' % (usr.user_id,
491 ret = 'failed to delete ID:%s %s' % (usr.user_id,
490 usr.username)
492 usr.username)
491 expected = ret
493 expected = ret
492 self._compare_error(id_, expected, given=response.body)
494 self._compare_error(id_, expected, given=response.body)
493
495
494 @parameterized.expand([('firstname', 'new_username'),
496 @parameterized.expand([('firstname', 'new_username'),
495 ('lastname', 'new_username'),
497 ('lastname', 'new_username'),
496 ('email', 'new_username'),
498 ('email', 'new_username'),
497 ('admin', True),
499 ('admin', True),
498 ('admin', False),
500 ('admin', False),
499 ('ldap_dn', 'test'),
501 ('ldap_dn', 'test'),
500 ('ldap_dn', None),
502 ('ldap_dn', None),
501 ('active', False),
503 ('active', False),
502 ('active', True),
504 ('active', True),
503 ('password', 'newpass')
505 ('password', 'newpass')
504 ])
506 ])
505 def test_api_update_user(self, name, expected):
507 def test_api_update_user(self, name, expected):
506 usr = UserModel().get_by_username(self.TEST_USER_LOGIN)
508 usr = UserModel().get_by_username(self.TEST_USER_LOGIN)
507 kw = {name: expected,
509 kw = {name: expected,
508 'userid': usr.user_id}
510 'userid': usr.user_id}
509 id_, params = _build_data(self.apikey, 'update_user', **kw)
511 id_, params = _build_data(self.apikey, 'update_user', **kw)
510 response = api_call(self, params)
512 response = api_call(self, params)
511
513
512 ret = {
514 ret = {
513 'msg': 'updated user ID:%s %s' % (usr.user_id, self.TEST_USER_LOGIN),
515 'msg': 'updated user ID:%s %s' % (usr.user_id, self.TEST_USER_LOGIN),
514 'user': jsonify(UserModel()\
516 'user': jsonify(UserModel()\
515 .get_by_username(self.TEST_USER_LOGIN)\
517 .get_by_username(self.TEST_USER_LOGIN)\
516 .get_api_data())
518 .get_api_data())
517 }
519 }
518
520
519 expected = ret
521 expected = ret
520 self._compare_ok(id_, expected, given=response.body)
522 self._compare_ok(id_, expected, given=response.body)
521
523
522 def test_api_update_user_no_changed_params(self):
524 def test_api_update_user_no_changed_params(self):
523 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
525 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
524 ret = jsonify(usr.get_api_data())
526 ret = jsonify(usr.get_api_data())
525 id_, params = _build_data(self.apikey, 'update_user',
527 id_, params = _build_data(self.apikey, 'update_user',
526 userid=TEST_USER_ADMIN_LOGIN)
528 userid=TEST_USER_ADMIN_LOGIN)
527
529
528 response = api_call(self, params)
530 response = api_call(self, params)
529 ret = {
531 ret = {
530 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN),
532 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN),
531 'user': ret
533 'user': ret
532 }
534 }
533 expected = ret
535 expected = ret
534 self._compare_ok(id_, expected, given=response.body)
536 self._compare_ok(id_, expected, given=response.body)
535
537
536 def test_api_update_user_by_user_id(self):
538 def test_api_update_user_by_user_id(self):
537 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
539 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
538 ret = jsonify(usr.get_api_data())
540 ret = jsonify(usr.get_api_data())
539 id_, params = _build_data(self.apikey, 'update_user',
541 id_, params = _build_data(self.apikey, 'update_user',
540 userid=usr.user_id)
542 userid=usr.user_id)
541
543
542 response = api_call(self, params)
544 response = api_call(self, params)
543 ret = {
545 ret = {
544 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN),
546 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN),
545 'user': ret
547 'user': ret
546 }
548 }
547 expected = ret
549 expected = ret
548 self._compare_ok(id_, expected, given=response.body)
550 self._compare_ok(id_, expected, given=response.body)
549
551
550 @mock.patch.object(UserModel, 'update_user', crash)
552 @mock.patch.object(UserModel, 'update_user', crash)
551 def test_api_update_user_when_exception_happens(self):
553 def test_api_update_user_when_exception_happens(self):
552 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
554 usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN)
553 ret = jsonify(usr.get_api_data())
555 ret = jsonify(usr.get_api_data())
554 id_, params = _build_data(self.apikey, 'update_user',
556 id_, params = _build_data(self.apikey, 'update_user',
555 userid=usr.user_id)
557 userid=usr.user_id)
556
558
557 response = api_call(self, params)
559 response = api_call(self, params)
558 ret = 'failed to update user `%s`' % usr.user_id
560 ret = 'failed to update user `%s`' % usr.user_id
559
561
560 expected = ret
562 expected = ret
561 self._compare_error(id_, expected, given=response.body)
563 self._compare_error(id_, expected, given=response.body)
562
564
563 def test_api_get_repo(self):
565 def test_api_get_repo(self):
564 new_group = 'some_new_group'
566 new_group = 'some_new_group'
565 make_users_group(new_group)
567 make_users_group(new_group)
566 RepoModel().grant_users_group_permission(repo=self.REPO,
568 RepoModel().grant_users_group_permission(repo=self.REPO,
567 group_name=new_group,
569 group_name=new_group,
568 perm='repository.read')
570 perm='repository.read')
569 Session().commit()
571 Session().commit()
570 id_, params = _build_data(self.apikey, 'get_repo',
572 id_, params = _build_data(self.apikey, 'get_repo',
571 repoid=self.REPO)
573 repoid=self.REPO)
572 response = api_call(self, params)
574 response = api_call(self, params)
573
575
574 repo = RepoModel().get_by_repo_name(self.REPO)
576 repo = RepoModel().get_by_repo_name(self.REPO)
575 ret = repo.get_api_data()
577 ret = repo.get_api_data()
576
578
577 members = []
579 members = []
578 followers = []
580 followers = []
579 for user in repo.repo_to_perm:
581 for user in repo.repo_to_perm:
580 perm = user.permission.permission_name
582 perm = user.permission.permission_name
581 user = user.user
583 user = user.user
582 user_data = user.get_api_data()
584 user_data = user.get_api_data()
583 user_data['type'] = "user"
585 user_data['type'] = "user"
584 user_data['permission'] = perm
586 user_data['permission'] = perm
585 members.append(user_data)
587 members.append(user_data)
586
588
587 for users_group in repo.users_group_to_perm:
589 for users_group in repo.users_group_to_perm:
588 perm = users_group.permission.permission_name
590 perm = users_group.permission.permission_name
589 users_group = users_group.users_group
591 users_group = users_group.users_group
590 users_group_data = users_group.get_api_data()
592 users_group_data = users_group.get_api_data()
591 users_group_data['type'] = "users_group"
593 users_group_data['type'] = "users_group"
592 users_group_data['permission'] = perm
594 users_group_data['permission'] = perm
593 members.append(users_group_data)
595 members.append(users_group_data)
594
596
595 for user in repo.followers:
597 for user in repo.followers:
596 followers.append(user.user.get_api_data())
598 followers.append(user.user.get_api_data())
597
599
598 ret['members'] = members
600 ret['members'] = members
599 ret['followers'] = followers
601 ret['followers'] = followers
600
602
601 expected = ret
603 expected = ret
602 self._compare_ok(id_, expected, given=response.body)
604 self._compare_ok(id_, expected, given=response.body)
603 destroy_users_group(new_group)
605 destroy_users_group(new_group)
604
606
605 def test_api_get_repo_by_non_admin(self):
607 def test_api_get_repo_by_non_admin(self):
606 id_, params = _build_data(self.apikey, 'get_repo',
608 id_, params = _build_data(self.apikey, 'get_repo',
607 repoid=self.REPO)
609 repoid=self.REPO)
608 response = api_call(self, params)
610 response = api_call(self, params)
609
611
610 repo = RepoModel().get_by_repo_name(self.REPO)
612 repo = RepoModel().get_by_repo_name(self.REPO)
611 ret = repo.get_api_data()
613 ret = repo.get_api_data()
612
614
613 members = []
615 members = []
614 followers = []
616 followers = []
615 for user in repo.repo_to_perm:
617 for user in repo.repo_to_perm:
616 perm = user.permission.permission_name
618 perm = user.permission.permission_name
617 user = user.user
619 user = user.user
618 user_data = user.get_api_data()
620 user_data = user.get_api_data()
619 user_data['type'] = "user"
621 user_data['type'] = "user"
620 user_data['permission'] = perm
622 user_data['permission'] = perm
621 members.append(user_data)
623 members.append(user_data)
622
624
623 for users_group in repo.users_group_to_perm:
625 for users_group in repo.users_group_to_perm:
624 perm = users_group.permission.permission_name
626 perm = users_group.permission.permission_name
625 users_group = users_group.users_group
627 users_group = users_group.users_group
626 users_group_data = users_group.get_api_data()
628 users_group_data = users_group.get_api_data()
627 users_group_data['type'] = "users_group"
629 users_group_data['type'] = "users_group"
628 users_group_data['permission'] = perm
630 users_group_data['permission'] = perm
629 members.append(users_group_data)
631 members.append(users_group_data)
630
632
631 for user in repo.followers:
633 for user in repo.followers:
632 followers.append(user.user.get_api_data())
634 followers.append(user.user.get_api_data())
633
635
634 ret['members'] = members
636 ret['members'] = members
635 ret['followers'] = followers
637 ret['followers'] = followers
636
638
637 expected = ret
639 expected = ret
638 self._compare_ok(id_, expected, given=response.body)
640 self._compare_ok(id_, expected, given=response.body)
639
641
640 def test_api_get_repo_by_non_admin_no_permission_to_repo(self):
642 def test_api_get_repo_by_non_admin_no_permission_to_repo(self):
641 RepoModel().grant_user_permission(repo=self.REPO,
643 RepoModel().grant_user_permission(repo=self.REPO,
642 user=self.TEST_USER_LOGIN,
644 user=self.TEST_USER_LOGIN,
643 perm='repository.none')
645 perm='repository.none')
644
646
645 id_, params = _build_data(self.apikey_regular, 'get_repo',
647 id_, params = _build_data(self.apikey_regular, 'get_repo',
646 repoid=self.REPO)
648 repoid=self.REPO)
647 response = api_call(self, params)
649 response = api_call(self, params)
648
650
649 expected = 'repository `%s` does not exist' % (self.REPO)
651 expected = 'repository `%s` does not exist' % (self.REPO)
650 self._compare_error(id_, expected, given=response.body)
652 self._compare_error(id_, expected, given=response.body)
651
653
652 def test_api_get_repo_that_doesn_not_exist(self):
654 def test_api_get_repo_that_doesn_not_exist(self):
653 id_, params = _build_data(self.apikey, 'get_repo',
655 id_, params = _build_data(self.apikey, 'get_repo',
654 repoid='no-such-repo')
656 repoid='no-such-repo')
655 response = api_call(self, params)
657 response = api_call(self, params)
656
658
657 ret = 'repository `%s` does not exist' % 'no-such-repo'
659 ret = 'repository `%s` does not exist' % 'no-such-repo'
658 expected = ret
660 expected = ret
659 self._compare_error(id_, expected, given=response.body)
661 self._compare_error(id_, expected, given=response.body)
660
662
661 def test_api_get_repos(self):
663 def test_api_get_repos(self):
662 id_, params = _build_data(self.apikey, 'get_repos')
664 id_, params = _build_data(self.apikey, 'get_repos')
663 response = api_call(self, params)
665 response = api_call(self, params)
664
666
665 result = []
667 result = []
666 for repo in RepoModel().get_all():
668 for repo in RepoModel().get_all():
667 result.append(repo.get_api_data())
669 result.append(repo.get_api_data())
668 ret = jsonify(result)
670 ret = jsonify(result)
669
671
670 expected = ret
672 expected = ret
671 self._compare_ok(id_, expected, given=response.body)
673 self._compare_ok(id_, expected, given=response.body)
672
674
673 def test_api_get_repos_non_admin(self):
675 def test_api_get_repos_non_admin(self):
674 id_, params = _build_data(self.apikey_regular, 'get_repos')
676 id_, params = _build_data(self.apikey_regular, 'get_repos')
675 response = api_call(self, params)
677 response = api_call(self, params)
676
678
677 result = []
679 result = []
678 for repo in RepoModel().get_all_user_repos(self.TEST_USER_LOGIN):
680 for repo in RepoModel().get_all_user_repos(self.TEST_USER_LOGIN):
679 result.append(repo.get_api_data())
681 result.append(repo.get_api_data())
680 ret = jsonify(result)
682 ret = jsonify(result)
681
683
682 expected = ret
684 expected = ret
683 self._compare_ok(id_, expected, given=response.body)
685 self._compare_ok(id_, expected, given=response.body)
684
686
685 @parameterized.expand([('all', 'all'),
687 @parameterized.expand([('all', 'all'),
686 ('dirs', 'dirs'),
688 ('dirs', 'dirs'),
687 ('files', 'files'), ])
689 ('files', 'files'), ])
688 def test_api_get_repo_nodes(self, name, ret_type):
690 def test_api_get_repo_nodes(self, name, ret_type):
689 rev = 'tip'
691 rev = 'tip'
690 path = '/'
692 path = '/'
691 id_, params = _build_data(self.apikey, 'get_repo_nodes',
693 id_, params = _build_data(self.apikey, 'get_repo_nodes',
692 repoid=self.REPO, revision=rev,
694 repoid=self.REPO, revision=rev,
693 root_path=path,
695 root_path=path,
694 ret_type=ret_type)
696 ret_type=ret_type)
695 response = api_call(self, params)
697 response = api_call(self, params)
696
698
697 # we don't the actual return types here since it's tested somewhere
699 # we don't the actual return types here since it's tested somewhere
698 # else
700 # else
699 expected = json.loads(response.body)['result']
701 expected = json.loads(response.body)['result']
700 self._compare_ok(id_, expected, given=response.body)
702 self._compare_ok(id_, expected, given=response.body)
701
703
702 def test_api_get_repo_nodes_bad_revisions(self):
704 def test_api_get_repo_nodes_bad_revisions(self):
703 rev = 'i-dont-exist'
705 rev = 'i-dont-exist'
704 path = '/'
706 path = '/'
705 id_, params = _build_data(self.apikey, 'get_repo_nodes',
707 id_, params = _build_data(self.apikey, 'get_repo_nodes',
706 repoid=self.REPO, revision=rev,
708 repoid=self.REPO, revision=rev,
707 root_path=path,)
709 root_path=path,)
708 response = api_call(self, params)
710 response = api_call(self, params)
709
711
710 expected = 'failed to get repo: `%s` nodes' % self.REPO
712 expected = 'failed to get repo: `%s` nodes' % self.REPO
711 self._compare_error(id_, expected, given=response.body)
713 self._compare_error(id_, expected, given=response.body)
712
714
713 def test_api_get_repo_nodes_bad_path(self):
715 def test_api_get_repo_nodes_bad_path(self):
714 rev = 'tip'
716 rev = 'tip'
715 path = '/idontexits'
717 path = '/idontexits'
716 id_, params = _build_data(self.apikey, 'get_repo_nodes',
718 id_, params = _build_data(self.apikey, 'get_repo_nodes',
717 repoid=self.REPO, revision=rev,
719 repoid=self.REPO, revision=rev,
718 root_path=path,)
720 root_path=path,)
719 response = api_call(self, params)
721 response = api_call(self, params)
720
722
721 expected = 'failed to get repo: `%s` nodes' % self.REPO
723 expected = 'failed to get repo: `%s` nodes' % self.REPO
722 self._compare_error(id_, expected, given=response.body)
724 self._compare_error(id_, expected, given=response.body)
723
725
724 def test_api_get_repo_nodes_bad_ret_type(self):
726 def test_api_get_repo_nodes_bad_ret_type(self):
725 rev = 'tip'
727 rev = 'tip'
726 path = '/'
728 path = '/'
727 ret_type = 'error'
729 ret_type = 'error'
728 id_, params = _build_data(self.apikey, 'get_repo_nodes',
730 id_, params = _build_data(self.apikey, 'get_repo_nodes',
729 repoid=self.REPO, revision=rev,
731 repoid=self.REPO, revision=rev,
730 root_path=path,
732 root_path=path,
731 ret_type=ret_type)
733 ret_type=ret_type)
732 response = api_call(self, params)
734 response = api_call(self, params)
733
735
734 expected = 'ret_type must be one of %s' % (['files', 'dirs', 'all'])
736 expected = 'ret_type must be one of %s' % (['files', 'dirs', 'all'])
735 self._compare_error(id_, expected, given=response.body)
737 self._compare_error(id_, expected, given=response.body)
736
738
737 def test_api_create_repo(self):
739 def test_api_create_repo(self):
738 repo_name = 'api-repo'
740 repo_name = 'api-repo'
739 id_, params = _build_data(self.apikey, 'create_repo',
741 id_, params = _build_data(self.apikey, 'create_repo',
740 repo_name=repo_name,
742 repo_name=repo_name,
741 owner=TEST_USER_ADMIN_LOGIN,
743 owner=TEST_USER_ADMIN_LOGIN,
742 repo_type='hg',
744 repo_type='hg',
743 )
745 )
744 response = api_call(self, params)
746 response = api_call(self, params)
745
747
746 repo = RepoModel().get_by_repo_name(repo_name)
748 repo = RepoModel().get_by_repo_name(repo_name)
747 ret = {
749 ret = {
748 'msg': 'Created new repository `%s`' % repo_name,
750 'msg': 'Created new repository `%s`' % repo_name,
749 'repo': jsonify(repo.get_api_data())
751 'repo': jsonify(repo.get_api_data())
750 }
752 }
751 expected = ret
753 expected = ret
752 self._compare_ok(id_, expected, given=response.body)
754 self._compare_ok(id_, expected, given=response.body)
753 fixture.destroy_repo(repo_name)
755 fixture.destroy_repo(repo_name)
754
756
755 def test_api_create_repo_unknown_owner(self):
757 def test_api_create_repo_unknown_owner(self):
756 repo_name = 'api-repo'
758 repo_name = 'api-repo'
757 owner = 'i-dont-exist'
759 owner = 'i-dont-exist'
758 id_, params = _build_data(self.apikey, 'create_repo',
760 id_, params = _build_data(self.apikey, 'create_repo',
759 repo_name=repo_name,
761 repo_name=repo_name,
760 owner=owner,
762 owner=owner,
761 repo_type='hg',
763 repo_type='hg',
762 )
764 )
763 response = api_call(self, params)
765 response = api_call(self, params)
764 expected = 'user `%s` does not exist' % owner
766 expected = 'user `%s` does not exist' % owner
765 self._compare_error(id_, expected, given=response.body)
767 self._compare_error(id_, expected, given=response.body)
766
768
767 def test_api_create_repo_dont_specify_owner(self):
769 def test_api_create_repo_dont_specify_owner(self):
768 repo_name = 'api-repo'
770 repo_name = 'api-repo'
769 owner = 'i-dont-exist'
771 owner = 'i-dont-exist'
770 id_, params = _build_data(self.apikey, 'create_repo',
772 id_, params = _build_data(self.apikey, 'create_repo',
771 repo_name=repo_name,
773 repo_name=repo_name,
772 repo_type='hg',
774 repo_type='hg',
773 )
775 )
774 response = api_call(self, params)
776 response = api_call(self, params)
775
777
776 repo = RepoModel().get_by_repo_name(repo_name)
778 repo = RepoModel().get_by_repo_name(repo_name)
777 ret = {
779 ret = {
778 'msg': 'Created new repository `%s`' % repo_name,
780 'msg': 'Created new repository `%s`' % repo_name,
779 'repo': jsonify(repo.get_api_data())
781 'repo': jsonify(repo.get_api_data())
780 }
782 }
781 expected = ret
783 expected = ret
782 self._compare_ok(id_, expected, given=response.body)
784 self._compare_ok(id_, expected, given=response.body)
783 fixture.destroy_repo(repo_name)
785 fixture.destroy_repo(repo_name)
784
786
785 def test_api_create_repo_by_non_admin(self):
787 def test_api_create_repo_by_non_admin(self):
786 repo_name = 'api-repo'
788 repo_name = 'api-repo'
787 owner = 'i-dont-exist'
789 owner = 'i-dont-exist'
788 id_, params = _build_data(self.apikey_regular, 'create_repo',
790 id_, params = _build_data(self.apikey_regular, 'create_repo',
789 repo_name=repo_name,
791 repo_name=repo_name,
790 repo_type='hg',
792 repo_type='hg',
791 )
793 )
792 response = api_call(self, params)
794 response = api_call(self, params)
793
795
794 repo = RepoModel().get_by_repo_name(repo_name)
796 repo = RepoModel().get_by_repo_name(repo_name)
795 ret = {
797 ret = {
796 'msg': 'Created new repository `%s`' % repo_name,
798 'msg': 'Created new repository `%s`' % repo_name,
797 'repo': jsonify(repo.get_api_data())
799 'repo': jsonify(repo.get_api_data())
798 }
800 }
799 expected = ret
801 expected = ret
800 self._compare_ok(id_, expected, given=response.body)
802 self._compare_ok(id_, expected, given=response.body)
801 fixture.destroy_repo(repo_name)
803 fixture.destroy_repo(repo_name)
802
804
803 def test_api_create_repo_by_non_admin_specify_owner(self):
805 def test_api_create_repo_by_non_admin_specify_owner(self):
804 repo_name = 'api-repo'
806 repo_name = 'api-repo'
805 owner = 'i-dont-exist'
807 owner = 'i-dont-exist'
806 id_, params = _build_data(self.apikey_regular, 'create_repo',
808 id_, params = _build_data(self.apikey_regular, 'create_repo',
807 repo_name=repo_name,
809 repo_name=repo_name,
808 repo_type='hg',
810 repo_type='hg',
809 owner=owner
811 owner=owner
810 )
812 )
811 response = api_call(self, params)
813 response = api_call(self, params)
812
814
813 expected = 'Only RhodeCode admin can specify `owner` param'
815 expected = 'Only RhodeCode admin can specify `owner` param'
814 self._compare_error(id_, expected, given=response.body)
816 self._compare_error(id_, expected, given=response.body)
815 fixture.destroy_repo(repo_name)
817 fixture.destroy_repo(repo_name)
816
818
817 def test_api_create_repo_exists(self):
819 def test_api_create_repo_exists(self):
818 repo_name = self.REPO
820 repo_name = self.REPO
819 id_, params = _build_data(self.apikey, 'create_repo',
821 id_, params = _build_data(self.apikey, 'create_repo',
820 repo_name=repo_name,
822 repo_name=repo_name,
821 owner=TEST_USER_ADMIN_LOGIN,
823 owner=TEST_USER_ADMIN_LOGIN,
822 repo_type='hg',
824 repo_type='hg',
823 )
825 )
824 response = api_call(self, params)
826 response = api_call(self, params)
825 expected = "repo `%s` already exist" % repo_name
827 expected = "repo `%s` already exist" % repo_name
826 self._compare_error(id_, expected, given=response.body)
828 self._compare_error(id_, expected, given=response.body)
827
829
828 @mock.patch.object(RepoModel, 'create_repo', crash)
830 @mock.patch.object(RepoModel, 'create_repo', crash)
829 def test_api_create_repo_exception_occurred(self):
831 def test_api_create_repo_exception_occurred(self):
830 repo_name = 'api-repo'
832 repo_name = 'api-repo'
831 id_, params = _build_data(self.apikey, 'create_repo',
833 id_, params = _build_data(self.apikey, 'create_repo',
832 repo_name=repo_name,
834 repo_name=repo_name,
833 owner=TEST_USER_ADMIN_LOGIN,
835 owner=TEST_USER_ADMIN_LOGIN,
834 repo_type='hg',
836 repo_type='hg',
835 )
837 )
836 response = api_call(self, params)
838 response = api_call(self, params)
837 expected = 'failed to create repository `%s`' % repo_name
839 expected = 'failed to create repository `%s`' % repo_name
838 self._compare_error(id_, expected, given=response.body)
840 self._compare_error(id_, expected, given=response.body)
839
841
840 def test_api_delete_repo(self):
842 def test_api_delete_repo(self):
841 repo_name = 'api_delete_me'
843 repo_name = 'api_delete_me'
842 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
844 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
843
845
844 id_, params = _build_data(self.apikey, 'delete_repo',
846 id_, params = _build_data(self.apikey, 'delete_repo',
845 repoid=repo_name,)
847 repoid=repo_name,)
846 response = api_call(self, params)
848 response = api_call(self, params)
847
849
848 ret = {
850 ret = {
849 'msg': 'Deleted repository `%s`' % repo_name,
851 'msg': 'Deleted repository `%s`' % repo_name,
850 'success': True
852 'success': True
851 }
853 }
852 expected = ret
854 expected = ret
853 self._compare_ok(id_, expected, given=response.body)
855 self._compare_ok(id_, expected, given=response.body)
854
856
855 def test_api_delete_repo_by_non_admin(self):
857 def test_api_delete_repo_by_non_admin(self):
856 repo_name = 'api_delete_me'
858 repo_name = 'api_delete_me'
857 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE,
859 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE,
858 cur_user=self.TEST_USER_LOGIN)
860 cur_user=self.TEST_USER_LOGIN)
859 try:
861 try:
860 id_, params = _build_data(self.apikey_regular, 'delete_repo',
862 id_, params = _build_data(self.apikey_regular, 'delete_repo',
861 repoid=repo_name,)
863 repoid=repo_name,)
862 response = api_call(self, params)
864 response = api_call(self, params)
863
865
864 ret = {
866 ret = {
865 'msg': 'Deleted repository `%s`' % repo_name,
867 'msg': 'Deleted repository `%s`' % repo_name,
866 'success': True
868 'success': True
867 }
869 }
868 expected = ret
870 expected = ret
869 self._compare_ok(id_, expected, given=response.body)
871 self._compare_ok(id_, expected, given=response.body)
870 finally:
872 finally:
871 fixture.destroy_repo(repo_name)
873 fixture.destroy_repo(repo_name)
872
874
873 def test_api_delete_repo_by_non_admin_no_permission(self):
875 def test_api_delete_repo_by_non_admin_no_permission(self):
874 repo_name = 'api_delete_me'
876 repo_name = 'api_delete_me'
875 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
877 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
876 try:
878 try:
877 id_, params = _build_data(self.apikey_regular, 'delete_repo',
879 id_, params = _build_data(self.apikey_regular, 'delete_repo',
878 repoid=repo_name,)
880 repoid=repo_name,)
879 response = api_call(self, params)
881 response = api_call(self, params)
880 expected = 'repository `%s` does not exist' % (repo_name)
882 expected = 'repository `%s` does not exist' % (repo_name)
881 self._compare_error(id_, expected, given=response.body)
883 self._compare_error(id_, expected, given=response.body)
882 finally:
884 finally:
883 fixture.destroy_repo(repo_name)
885 fixture.destroy_repo(repo_name)
884
886
885 def test_api_delete_repo_exception_occurred(self):
887 def test_api_delete_repo_exception_occurred(self):
886 repo_name = 'api_delete_me'
888 repo_name = 'api_delete_me'
887 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
889 fixture.create_repo(repo_name, repo_type=self.REPO_TYPE)
888 try:
890 try:
889 with mock.patch.object(RepoModel, 'delete', crash):
891 with mock.patch.object(RepoModel, 'delete', crash):
890 id_, params = _build_data(self.apikey, 'delete_repo',
892 id_, params = _build_data(self.apikey, 'delete_repo',
891 repoid=repo_name,)
893 repoid=repo_name,)
892 response = api_call(self, params)
894 response = api_call(self, params)
893
895
894 expected = 'failed to delete repository `%s`' % repo_name
896 expected = 'failed to delete repository `%s`' % repo_name
895 self._compare_error(id_, expected, given=response.body)
897 self._compare_error(id_, expected, given=response.body)
896 finally:
898 finally:
897 fixture.destroy_repo(repo_name)
899 fixture.destroy_repo(repo_name)
898
900
899 def test_api_fork_repo(self):
901 def test_api_fork_repo(self):
900 fork_name = 'api-repo-fork'
902 fork_name = 'api-repo-fork'
901 id_, params = _build_data(self.apikey, 'fork_repo',
903 id_, params = _build_data(self.apikey, 'fork_repo',
902 repoid=self.REPO,
904 repoid=self.REPO,
903 fork_name=fork_name,
905 fork_name=fork_name,
904 owner=TEST_USER_ADMIN_LOGIN,
906 owner=TEST_USER_ADMIN_LOGIN,
905 )
907 )
906 response = api_call(self, params)
908 response = api_call(self, params)
907
909
908 ret = {
910 ret = {
909 'msg': 'Created fork of `%s` as `%s`' % (self.REPO,
911 'msg': 'Created fork of `%s` as `%s`' % (self.REPO,
910 fork_name),
912 fork_name),
911 'success': True
913 'success': True
912 }
914 }
913 expected = ret
915 expected = ret
914 self._compare_ok(id_, expected, given=response.body)
916 self._compare_ok(id_, expected, given=response.body)
915 fixture.destroy_repo(fork_name)
917 fixture.destroy_repo(fork_name)
916
918
917 def test_api_fork_repo_non_admin(self):
919 def test_api_fork_repo_non_admin(self):
918 fork_name = 'api-repo-fork'
920 fork_name = 'api-repo-fork'
919 id_, params = _build_data(self.apikey_regular, 'fork_repo',
921 id_, params = _build_data(self.apikey_regular, 'fork_repo',
920 repoid=self.REPO,
922 repoid=self.REPO,
921 fork_name=fork_name,
923 fork_name=fork_name,
922 )
924 )
923 response = api_call(self, params)
925 response = api_call(self, params)
924
926
925 ret = {
927 ret = {
926 'msg': 'Created fork of `%s` as `%s`' % (self.REPO,
928 'msg': 'Created fork of `%s` as `%s`' % (self.REPO,
927 fork_name),
929 fork_name),
928 'success': True
930 'success': True
929 }
931 }
930 expected = ret
932 expected = ret
931 self._compare_ok(id_, expected, given=response.body)
933 self._compare_ok(id_, expected, given=response.body)
932 fixture.destroy_repo(fork_name)
934 fixture.destroy_repo(fork_name)
933
935
934 def test_api_fork_repo_non_admin_specify_owner(self):
936 def test_api_fork_repo_non_admin_specify_owner(self):
935 fork_name = 'api-repo-fork'
937 fork_name = 'api-repo-fork'
936 id_, params = _build_data(self.apikey_regular, 'fork_repo',
938 id_, params = _build_data(self.apikey_regular, 'fork_repo',
937 repoid=self.REPO,
939 repoid=self.REPO,
938 fork_name=fork_name,
940 fork_name=fork_name,
939 owner=TEST_USER_ADMIN_LOGIN,
941 owner=TEST_USER_ADMIN_LOGIN,
940 )
942 )
941 response = api_call(self, params)
943 response = api_call(self, params)
942 expected = 'Only RhodeCode admin can specify `owner` param'
944 expected = 'Only RhodeCode admin can specify `owner` param'
943 self._compare_error(id_, expected, given=response.body)
945 self._compare_error(id_, expected, given=response.body)
944 fixture.destroy_repo(fork_name)
946 fixture.destroy_repo(fork_name)
945
947
946 def test_api_fork_repo_non_admin_no_permission_to_fork(self):
948 def test_api_fork_repo_non_admin_no_permission_to_fork(self):
947 RepoModel().grant_user_permission(repo=self.REPO,
949 RepoModel().grant_user_permission(repo=self.REPO,
948 user=self.TEST_USER_LOGIN,
950 user=self.TEST_USER_LOGIN,
949 perm='repository.none')
951 perm='repository.none')
950 fork_name = 'api-repo-fork'
952 fork_name = 'api-repo-fork'
951 id_, params = _build_data(self.apikey_regular, 'fork_repo',
953 id_, params = _build_data(self.apikey_regular, 'fork_repo',
952 repoid=self.REPO,
954 repoid=self.REPO,
953 fork_name=fork_name,
955 fork_name=fork_name,
954 )
956 )
955 response = api_call(self, params)
957 response = api_call(self, params)
956 expected = 'repository `%s` does not exist' % (self.REPO)
958 expected = 'repository `%s` does not exist' % (self.REPO)
957 self._compare_error(id_, expected, given=response.body)
959 self._compare_error(id_, expected, given=response.body)
958 fixture.destroy_repo(fork_name)
960 fixture.destroy_repo(fork_name)
959
961
960 def test_api_fork_repo_unknown_owner(self):
962 def test_api_fork_repo_unknown_owner(self):
961 fork_name = 'api-repo-fork'
963 fork_name = 'api-repo-fork'
962 owner = 'i-dont-exist'
964 owner = 'i-dont-exist'
963 id_, params = _build_data(self.apikey, 'fork_repo',
965 id_, params = _build_data(self.apikey, 'fork_repo',
964 repoid=self.REPO,
966 repoid=self.REPO,
965 fork_name=fork_name,
967 fork_name=fork_name,
966 owner=owner,
968 owner=owner,
967 )
969 )
968 response = api_call(self, params)
970 response = api_call(self, params)
969 expected = 'user `%s` does not exist' % owner
971 expected = 'user `%s` does not exist' % owner
970 self._compare_error(id_, expected, given=response.body)
972 self._compare_error(id_, expected, given=response.body)
971
973
972 def test_api_fork_repo_fork_exists(self):
974 def test_api_fork_repo_fork_exists(self):
973 fork_name = 'api-repo-fork'
975 fork_name = 'api-repo-fork'
974 fixture.create_fork(self.REPO, fork_name)
976 fixture.create_fork(self.REPO, fork_name)
975
977
976 try:
978 try:
977 fork_name = 'api-repo-fork'
979 fork_name = 'api-repo-fork'
978
980
979 id_, params = _build_data(self.apikey, 'fork_repo',
981 id_, params = _build_data(self.apikey, 'fork_repo',
980 repoid=self.REPO,
982 repoid=self.REPO,
981 fork_name=fork_name,
983 fork_name=fork_name,
982 owner=TEST_USER_ADMIN_LOGIN,
984 owner=TEST_USER_ADMIN_LOGIN,
983 )
985 )
984 response = api_call(self, params)
986 response = api_call(self, params)
985
987
986 expected = "fork `%s` already exist" % fork_name
988 expected = "fork `%s` already exist" % fork_name
987 self._compare_error(id_, expected, given=response.body)
989 self._compare_error(id_, expected, given=response.body)
988 finally:
990 finally:
989 fixture.destroy_repo(fork_name)
991 fixture.destroy_repo(fork_name)
990
992
991 def test_api_fork_repo_repo_exists(self):
993 def test_api_fork_repo_repo_exists(self):
992 fork_name = self.REPO
994 fork_name = self.REPO
993
995
994 id_, params = _build_data(self.apikey, 'fork_repo',
996 id_, params = _build_data(self.apikey, 'fork_repo',
995 repoid=self.REPO,
997 repoid=self.REPO,
996 fork_name=fork_name,
998 fork_name=fork_name,
997 owner=TEST_USER_ADMIN_LOGIN,
999 owner=TEST_USER_ADMIN_LOGIN,
998 )
1000 )
999 response = api_call(self, params)
1001 response = api_call(self, params)
1000
1002
1001 expected = "repo `%s` already exist" % fork_name
1003 expected = "repo `%s` already exist" % fork_name
1002 self._compare_error(id_, expected, given=response.body)
1004 self._compare_error(id_, expected, given=response.body)
1003
1005
1004 @mock.patch.object(RepoModel, 'create_fork', crash)
1006 @mock.patch.object(RepoModel, 'create_fork', crash)
1005 def test_api_fork_repo_exception_occurred(self):
1007 def test_api_fork_repo_exception_occurred(self):
1006 fork_name = 'api-repo-fork'
1008 fork_name = 'api-repo-fork'
1007 id_, params = _build_data(self.apikey, 'fork_repo',
1009 id_, params = _build_data(self.apikey, 'fork_repo',
1008 repoid=self.REPO,
1010 repoid=self.REPO,
1009 fork_name=fork_name,
1011 fork_name=fork_name,
1010 owner=TEST_USER_ADMIN_LOGIN,
1012 owner=TEST_USER_ADMIN_LOGIN,
1011 )
1013 )
1012 response = api_call(self, params)
1014 response = api_call(self, params)
1013
1015
1014 expected = 'failed to fork repository `%s` as `%s`' % (self.REPO,
1016 expected = 'failed to fork repository `%s` as `%s`' % (self.REPO,
1015 fork_name)
1017 fork_name)
1016 self._compare_error(id_, expected, given=response.body)
1018 self._compare_error(id_, expected, given=response.body)
1017
1019
1018 def test_api_get_users_group(self):
1020 def test_api_get_users_group(self):
1019 id_, params = _build_data(self.apikey, 'get_users_group',
1021 id_, params = _build_data(self.apikey, 'get_users_group',
1020 usersgroupid=TEST_USER_GROUP)
1022 usersgroupid=TEST_USER_GROUP)
1021 response = api_call(self, params)
1023 response = api_call(self, params)
1022
1024
1023 users_group = UserGroupModel().get_group(TEST_USER_GROUP)
1025 users_group = UserGroupModel().get_group(TEST_USER_GROUP)
1024 members = []
1026 members = []
1025 for user in users_group.members:
1027 for user in users_group.members:
1026 user = user.user
1028 user = user.user
1027 members.append(user.get_api_data())
1029 members.append(user.get_api_data())
1028
1030
1029 ret = users_group.get_api_data()
1031 ret = users_group.get_api_data()
1030 ret['members'] = members
1032 ret['members'] = members
1031 expected = ret
1033 expected = ret
1032 self._compare_ok(id_, expected, given=response.body)
1034 self._compare_ok(id_, expected, given=response.body)
1033
1035
1034 def test_api_get_users_groups(self):
1036 def test_api_get_users_groups(self):
1035
1037
1036 make_users_group('test_users_group2')
1038 make_users_group('test_users_group2')
1037
1039
1038 id_, params = _build_data(self.apikey, 'get_users_groups',)
1040 id_, params = _build_data(self.apikey, 'get_users_groups',)
1039 response = api_call(self, params)
1041 response = api_call(self, params)
1040
1042
1041 expected = []
1043 expected = []
1042 for gr_name in [TEST_USER_GROUP, 'test_users_group2']:
1044 for gr_name in [TEST_USER_GROUP, 'test_users_group2']:
1043 users_group = UserGroupModel().get_group(gr_name)
1045 users_group = UserGroupModel().get_group(gr_name)
1044 ret = users_group.get_api_data()
1046 ret = users_group.get_api_data()
1045 expected.append(ret)
1047 expected.append(ret)
1046 self._compare_ok(id_, expected, given=response.body)
1048 self._compare_ok(id_, expected, given=response.body)
1047
1049
1048 UserGroupModel().delete(users_group='test_users_group2')
1050 UserGroupModel().delete(users_group='test_users_group2')
1049 Session().commit()
1051 Session().commit()
1050
1052
1051 def test_api_create_users_group(self):
1053 def test_api_create_users_group(self):
1052 group_name = 'some_new_group'
1054 group_name = 'some_new_group'
1053 id_, params = _build_data(self.apikey, 'create_users_group',
1055 id_, params = _build_data(self.apikey, 'create_users_group',
1054 group_name=group_name)
1056 group_name=group_name)
1055 response = api_call(self, params)
1057 response = api_call(self, params)
1056
1058
1057 ret = {
1059 ret = {
1058 'msg': 'created new user group `%s`' % group_name,
1060 'msg': 'created new user group `%s`' % group_name,
1059 'users_group': jsonify(UserGroupModel()\
1061 'users_group': jsonify(UserGroupModel()\
1060 .get_by_name(group_name)\
1062 .get_by_name(group_name)\
1061 .get_api_data())
1063 .get_api_data())
1062 }
1064 }
1063 expected = ret
1065 expected = ret
1064 self._compare_ok(id_, expected, given=response.body)
1066 self._compare_ok(id_, expected, given=response.body)
1065
1067
1066 destroy_users_group(group_name)
1068 destroy_users_group(group_name)
1067
1069
1068 def test_api_get_users_group_that_exist(self):
1070 def test_api_get_users_group_that_exist(self):
1069 id_, params = _build_data(self.apikey, 'create_users_group',
1071 id_, params = _build_data(self.apikey, 'create_users_group',
1070 group_name=TEST_USER_GROUP)
1072 group_name=TEST_USER_GROUP)
1071 response = api_call(self, params)
1073 response = api_call(self, params)
1072
1074
1073 expected = "user group `%s` already exist" % TEST_USER_GROUP
1075 expected = "user group `%s` already exist" % TEST_USER_GROUP
1074 self._compare_error(id_, expected, given=response.body)
1076 self._compare_error(id_, expected, given=response.body)
1075
1077
1076 @mock.patch.object(UserGroupModel, 'create', crash)
1078 @mock.patch.object(UserGroupModel, 'create', crash)
1077 def test_api_get_users_group_exception_occurred(self):
1079 def test_api_get_users_group_exception_occurred(self):
1078 group_name = 'exception_happens'
1080 group_name = 'exception_happens'
1079 id_, params = _build_data(self.apikey, 'create_users_group',
1081 id_, params = _build_data(self.apikey, 'create_users_group',
1080 group_name=group_name)
1082 group_name=group_name)
1081 response = api_call(self, params)
1083 response = api_call(self, params)
1082
1084
1083 expected = 'failed to create group `%s`' % group_name
1085 expected = 'failed to create group `%s`' % group_name
1084 self._compare_error(id_, expected, given=response.body)
1086 self._compare_error(id_, expected, given=response.body)
1085
1087
1086 def test_api_add_user_to_users_group(self):
1088 def test_api_add_user_to_users_group(self):
1087 gr_name = 'test_group'
1089 gr_name = 'test_group'
1088 fixture.create_user_group(gr_name)
1090 fixture.create_user_group(gr_name)
1089 id_, params = _build_data(self.apikey, 'add_user_to_users_group',
1091 id_, params = _build_data(self.apikey, 'add_user_to_users_group',
1090 usersgroupid=gr_name,
1092 usersgroupid=gr_name,
1091 userid=TEST_USER_ADMIN_LOGIN)
1093 userid=TEST_USER_ADMIN_LOGIN)
1092 response = api_call(self, params)
1094 response = api_call(self, params)
1093
1095
1094 expected = {
1096 expected = {
1095 'msg': 'added member `%s` to user group `%s`' % (
1097 'msg': 'added member `%s` to user group `%s`' % (
1096 TEST_USER_ADMIN_LOGIN, gr_name
1098 TEST_USER_ADMIN_LOGIN, gr_name
1097 ),
1099 ),
1098 'success': True}
1100 'success': True}
1099 self._compare_ok(id_, expected, given=response.body)
1101 self._compare_ok(id_, expected, given=response.body)
1100
1102
1101 UserGroupModel().delete(users_group=gr_name)
1103 UserGroupModel().delete(users_group=gr_name)
1102 Session().commit()
1104 Session().commit()
1103
1105
1104 def test_api_add_user_to_users_group_that_doesnt_exist(self):
1106 def test_api_add_user_to_users_group_that_doesnt_exist(self):
1105 id_, params = _build_data(self.apikey, 'add_user_to_users_group',
1107 id_, params = _build_data(self.apikey, 'add_user_to_users_group',
1106 usersgroupid='false-group',
1108 usersgroupid='false-group',
1107 userid=TEST_USER_ADMIN_LOGIN)
1109 userid=TEST_USER_ADMIN_LOGIN)
1108 response = api_call(self, params)
1110 response = api_call(self, params)
1109
1111
1110 expected = 'user group `%s` does not exist' % 'false-group'
1112 expected = 'user group `%s` does not exist' % 'false-group'
1111 self._compare_error(id_, expected, given=response.body)
1113 self._compare_error(id_, expected, given=response.body)
1112
1114
1113 @mock.patch.object(UserGroupModel, 'add_user_to_group', crash)
1115 @mock.patch.object(UserGroupModel, 'add_user_to_group', crash)
1114 def test_api_add_user_to_users_group_exception_occurred(self):
1116 def test_api_add_user_to_users_group_exception_occurred(self):
1115 gr_name = 'test_group'
1117 gr_name = 'test_group'
1116 fixture.create_user_group(gr_name)
1118 fixture.create_user_group(gr_name)
1117 id_, params = _build_data(self.apikey, 'add_user_to_users_group',
1119 id_, params = _build_data(self.apikey, 'add_user_to_users_group',
1118 usersgroupid=gr_name,
1120 usersgroupid=gr_name,
1119 userid=TEST_USER_ADMIN_LOGIN)
1121 userid=TEST_USER_ADMIN_LOGIN)
1120 response = api_call(self, params)
1122 response = api_call(self, params)
1121
1123
1122 expected = 'failed to add member to user group `%s`' % gr_name
1124 expected = 'failed to add member to user group `%s`' % gr_name
1123 self._compare_error(id_, expected, given=response.body)
1125 self._compare_error(id_, expected, given=response.body)
1124
1126
1125 UserGroupModel().delete(users_group=gr_name)
1127 UserGroupModel().delete(users_group=gr_name)
1126 Session().commit()
1128 Session().commit()
1127
1129
1128 def test_api_remove_user_from_users_group(self):
1130 def test_api_remove_user_from_users_group(self):
1129 gr_name = 'test_group_3'
1131 gr_name = 'test_group_3'
1130 gr = fixture.create_user_group(gr_name)
1132 gr = fixture.create_user_group(gr_name)
1131 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN)
1133 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN)
1132 Session().commit()
1134 Session().commit()
1133 id_, params = _build_data(self.apikey, 'remove_user_from_users_group',
1135 id_, params = _build_data(self.apikey, 'remove_user_from_users_group',
1134 usersgroupid=gr_name,
1136 usersgroupid=gr_name,
1135 userid=TEST_USER_ADMIN_LOGIN)
1137 userid=TEST_USER_ADMIN_LOGIN)
1136 response = api_call(self, params)
1138 response = api_call(self, params)
1137
1139
1138 expected = {
1140 expected = {
1139 'msg': 'removed member `%s` from user group `%s`' % (
1141 'msg': 'removed member `%s` from user group `%s`' % (
1140 TEST_USER_ADMIN_LOGIN, gr_name
1142 TEST_USER_ADMIN_LOGIN, gr_name
1141 ),
1143 ),
1142 'success': True}
1144 'success': True}
1143 self._compare_ok(id_, expected, given=response.body)
1145 self._compare_ok(id_, expected, given=response.body)
1144
1146
1145 UserGroupModel().delete(users_group=gr_name)
1147 UserGroupModel().delete(users_group=gr_name)
1146 Session().commit()
1148 Session().commit()
1147
1149
1148 @mock.patch.object(UserGroupModel, 'remove_user_from_group', crash)
1150 @mock.patch.object(UserGroupModel, 'remove_user_from_group', crash)
1149 def test_api_remove_user_from_users_group_exception_occurred(self):
1151 def test_api_remove_user_from_users_group_exception_occurred(self):
1150 gr_name = 'test_group_3'
1152 gr_name = 'test_group_3'
1151 gr = fixture.create_user_group(gr_name)
1153 gr = fixture.create_user_group(gr_name)
1152 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN)
1154 UserGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN)
1153 Session().commit()
1155 Session().commit()
1154 id_, params = _build_data(self.apikey, 'remove_user_from_users_group',
1156 id_, params = _build_data(self.apikey, 'remove_user_from_users_group',
1155 usersgroupid=gr_name,
1157 usersgroupid=gr_name,
1156 userid=TEST_USER_ADMIN_LOGIN)
1158 userid=TEST_USER_ADMIN_LOGIN)
1157 response = api_call(self, params)
1159 response = api_call(self, params)
1158
1160
1159 expected = 'failed to remove member from user group `%s`' % gr_name
1161 expected = 'failed to remove member from user group `%s`' % gr_name
1160 self._compare_error(id_, expected, given=response.body)
1162 self._compare_error(id_, expected, given=response.body)
1161
1163
1162 UserGroupModel().delete(users_group=gr_name)
1164 UserGroupModel().delete(users_group=gr_name)
1163 Session().commit()
1165 Session().commit()
1164
1166
1165 @parameterized.expand([('none', 'repository.none'),
1167 @parameterized.expand([('none', 'repository.none'),
1166 ('read', 'repository.read'),
1168 ('read', 'repository.read'),
1167 ('write', 'repository.write'),
1169 ('write', 'repository.write'),
1168 ('admin', 'repository.admin')])
1170 ('admin', 'repository.admin')])
1169 def test_api_grant_user_permission(self, name, perm):
1171 def test_api_grant_user_permission(self, name, perm):
1170 id_, params = _build_data(self.apikey, 'grant_user_permission',
1172 id_, params = _build_data(self.apikey, 'grant_user_permission',
1171 repoid=self.REPO,
1173 repoid=self.REPO,
1172 userid=TEST_USER_ADMIN_LOGIN,
1174 userid=TEST_USER_ADMIN_LOGIN,
1173 perm=perm)
1175 perm=perm)
1174 response = api_call(self, params)
1176 response = api_call(self, params)
1175
1177
1176 ret = {
1178 ret = {
1177 'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % (
1179 'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % (
1178 perm, TEST_USER_ADMIN_LOGIN, self.REPO
1180 perm, TEST_USER_ADMIN_LOGIN, self.REPO
1179 ),
1181 ),
1180 'success': True
1182 'success': True
1181 }
1183 }
1182 expected = ret
1184 expected = ret
1183 self._compare_ok(id_, expected, given=response.body)
1185 self._compare_ok(id_, expected, given=response.body)
1184
1186
1185 def test_api_grant_user_permission_wrong_permission(self):
1187 def test_api_grant_user_permission_wrong_permission(self):
1186 perm = 'haha.no.permission'
1188 perm = 'haha.no.permission'
1187 id_, params = _build_data(self.apikey, 'grant_user_permission',
1189 id_, params = _build_data(self.apikey, 'grant_user_permission',
1188 repoid=self.REPO,
1190 repoid=self.REPO,
1189 userid=TEST_USER_ADMIN_LOGIN,
1191 userid=TEST_USER_ADMIN_LOGIN,
1190 perm=perm)
1192 perm=perm)
1191 response = api_call(self, params)
1193 response = api_call(self, params)
1192
1194
1193 expected = 'permission `%s` does not exist' % perm
1195 expected = 'permission `%s` does not exist' % perm
1194 self._compare_error(id_, expected, given=response.body)
1196 self._compare_error(id_, expected, given=response.body)
1195
1197
1196 @mock.patch.object(RepoModel, 'grant_user_permission', crash)
1198 @mock.patch.object(RepoModel, 'grant_user_permission', crash)
1197 def test_api_grant_user_permission_exception_when_adding(self):
1199 def test_api_grant_user_permission_exception_when_adding(self):
1198 perm = 'repository.read'
1200 perm = 'repository.read'
1199 id_, params = _build_data(self.apikey, 'grant_user_permission',
1201 id_, params = _build_data(self.apikey, 'grant_user_permission',
1200 repoid=self.REPO,
1202 repoid=self.REPO,
1201 userid=TEST_USER_ADMIN_LOGIN,
1203 userid=TEST_USER_ADMIN_LOGIN,
1202 perm=perm)
1204 perm=perm)
1203 response = api_call(self, params)
1205 response = api_call(self, params)
1204
1206
1205 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % (
1207 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % (
1206 TEST_USER_ADMIN_LOGIN, self.REPO
1208 TEST_USER_ADMIN_LOGIN, self.REPO
1207 )
1209 )
1208 self._compare_error(id_, expected, given=response.body)
1210 self._compare_error(id_, expected, given=response.body)
1209
1211
1210 def test_api_revoke_user_permission(self):
1212 def test_api_revoke_user_permission(self):
1211 id_, params = _build_data(self.apikey, 'revoke_user_permission',
1213 id_, params = _build_data(self.apikey, 'revoke_user_permission',
1212 repoid=self.REPO,
1214 repoid=self.REPO,
1213 userid=TEST_USER_ADMIN_LOGIN,)
1215 userid=TEST_USER_ADMIN_LOGIN,)
1214 response = api_call(self, params)
1216 response = api_call(self, params)
1215
1217
1216 expected = {
1218 expected = {
1217 'msg': 'Revoked perm for user: `%s` in repo: `%s`' % (
1219 'msg': 'Revoked perm for user: `%s` in repo: `%s`' % (
1218 TEST_USER_ADMIN_LOGIN, self.REPO
1220 TEST_USER_ADMIN_LOGIN, self.REPO
1219 ),
1221 ),
1220 'success': True
1222 'success': True
1221 }
1223 }
1222 self._compare_ok(id_, expected, given=response.body)
1224 self._compare_ok(id_, expected, given=response.body)
1223
1225
1224 @mock.patch.object(RepoModel, 'revoke_user_permission', crash)
1226 @mock.patch.object(RepoModel, 'revoke_user_permission', crash)
1225 def test_api_revoke_user_permission_exception_when_adding(self):
1227 def test_api_revoke_user_permission_exception_when_adding(self):
1226 id_, params = _build_data(self.apikey, 'revoke_user_permission',
1228 id_, params = _build_data(self.apikey, 'revoke_user_permission',
1227 repoid=self.REPO,
1229 repoid=self.REPO,
1228 userid=TEST_USER_ADMIN_LOGIN,)
1230 userid=TEST_USER_ADMIN_LOGIN,)
1229 response = api_call(self, params)
1231 response = api_call(self, params)
1230
1232
1231 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % (
1233 expected = 'failed to edit permission for user: `%s` in repo: `%s`' % (
1232 TEST_USER_ADMIN_LOGIN, self.REPO
1234 TEST_USER_ADMIN_LOGIN, self.REPO
1233 )
1235 )
1234 self._compare_error(id_, expected, given=response.body)
1236 self._compare_error(id_, expected, given=response.body)
1235
1237
1236 @parameterized.expand([('none', 'repository.none'),
1238 @parameterized.expand([('none', 'repository.none'),
1237 ('read', 'repository.read'),
1239 ('read', 'repository.read'),
1238 ('write', 'repository.write'),
1240 ('write', 'repository.write'),
1239 ('admin', 'repository.admin')])
1241 ('admin', 'repository.admin')])
1240 def test_api_grant_users_group_permission(self, name, perm):
1242 def test_api_grant_users_group_permission(self, name, perm):
1241 id_, params = _build_data(self.apikey, 'grant_users_group_permission',
1243 id_, params = _build_data(self.apikey, 'grant_users_group_permission',
1242 repoid=self.REPO,
1244 repoid=self.REPO,
1243 usersgroupid=TEST_USER_GROUP,
1245 usersgroupid=TEST_USER_GROUP,
1244 perm=perm)
1246 perm=perm)
1245 response = api_call(self, params)
1247 response = api_call(self, params)
1246
1248
1247 ret = {
1249 ret = {
1248 'msg': 'Granted perm: `%s` for user group: `%s` in repo: `%s`' % (
1250 'msg': 'Granted perm: `%s` for user group: `%s` in repo: `%s`' % (
1249 perm, TEST_USER_GROUP, self.REPO
1251 perm, TEST_USER_GROUP, self.REPO
1250 ),
1252 ),
1251 'success': True
1253 'success': True
1252 }
1254 }
1253 expected = ret
1255 expected = ret
1254 self._compare_ok(id_, expected, given=response.body)
1256 self._compare_ok(id_, expected, given=response.body)
1255
1257
1256 def test_api_grant_users_group_permission_wrong_permission(self):
1258 def test_api_grant_users_group_permission_wrong_permission(self):
1257 perm = 'haha.no.permission'
1259 perm = 'haha.no.permission'
1258 id_, params = _build_data(self.apikey, 'grant_users_group_permission',
1260 id_, params = _build_data(self.apikey, 'grant_users_group_permission',
1259 repoid=self.REPO,
1261 repoid=self.REPO,
1260 usersgroupid=TEST_USER_GROUP,
1262 usersgroupid=TEST_USER_GROUP,
1261 perm=perm)
1263 perm=perm)
1262 response = api_call(self, params)
1264 response = api_call(self, params)
1263
1265
1264 expected = 'permission `%s` does not exist' % perm
1266 expected = 'permission `%s` does not exist' % perm
1265 self._compare_error(id_, expected, given=response.body)
1267 self._compare_error(id_, expected, given=response.body)
1266
1268
1267 @mock.patch.object(RepoModel, 'grant_users_group_permission', crash)
1269 @mock.patch.object(RepoModel, 'grant_users_group_permission', crash)
1268 def test_api_grant_users_group_permission_exception_when_adding(self):
1270 def test_api_grant_users_group_permission_exception_when_adding(self):
1269 perm = 'repository.read'
1271 perm = 'repository.read'
1270 id_, params = _build_data(self.apikey, 'grant_users_group_permission',
1272 id_, params = _build_data(self.apikey, 'grant_users_group_permission',
1271 repoid=self.REPO,
1273 repoid=self.REPO,
1272 usersgroupid=TEST_USER_GROUP,
1274 usersgroupid=TEST_USER_GROUP,
1273 perm=perm)
1275 perm=perm)
1274 response = api_call(self, params)
1276 response = api_call(self, params)
1275
1277
1276 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % (
1278 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % (
1277 TEST_USER_GROUP, self.REPO
1279 TEST_USER_GROUP, self.REPO
1278 )
1280 )
1279 self._compare_error(id_, expected, given=response.body)
1281 self._compare_error(id_, expected, given=response.body)
1280
1282
1281 def test_api_revoke_users_group_permission(self):
1283 def test_api_revoke_users_group_permission(self):
1282 RepoModel().grant_users_group_permission(repo=self.REPO,
1284 RepoModel().grant_users_group_permission(repo=self.REPO,
1283 group_name=TEST_USER_GROUP,
1285 group_name=TEST_USER_GROUP,
1284 perm='repository.read')
1286 perm='repository.read')
1285 Session().commit()
1287 Session().commit()
1286 id_, params = _build_data(self.apikey, 'revoke_users_group_permission',
1288 id_, params = _build_data(self.apikey, 'revoke_users_group_permission',
1287 repoid=self.REPO,
1289 repoid=self.REPO,
1288 usersgroupid=TEST_USER_GROUP,)
1290 usersgroupid=TEST_USER_GROUP,)
1289 response = api_call(self, params)
1291 response = api_call(self, params)
1290
1292
1291 expected = {
1293 expected = {
1292 'msg': 'Revoked perm for user group: `%s` in repo: `%s`' % (
1294 'msg': 'Revoked perm for user group: `%s` in repo: `%s`' % (
1293 TEST_USER_GROUP, self.REPO
1295 TEST_USER_GROUP, self.REPO
1294 ),
1296 ),
1295 'success': True
1297 'success': True
1296 }
1298 }
1297 self._compare_ok(id_, expected, given=response.body)
1299 self._compare_ok(id_, expected, given=response.body)
1298
1300
1299 @mock.patch.object(RepoModel, 'revoke_users_group_permission', crash)
1301 @mock.patch.object(RepoModel, 'revoke_users_group_permission', crash)
1300 def test_api_revoke_users_group_permission_exception_when_adding(self):
1302 def test_api_revoke_users_group_permission_exception_when_adding(self):
1301
1303
1302 id_, params = _build_data(self.apikey, 'revoke_users_group_permission',
1304 id_, params = _build_data(self.apikey, 'revoke_users_group_permission',
1303 repoid=self.REPO,
1305 repoid=self.REPO,
1304 usersgroupid=TEST_USER_GROUP,)
1306 usersgroupid=TEST_USER_GROUP,)
1305 response = api_call(self, params)
1307 response = api_call(self, params)
1306
1308
1307 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % (
1309 expected = 'failed to edit permission for user group: `%s` in repo: `%s`' % (
1308 TEST_USER_GROUP, self.REPO
1310 TEST_USER_GROUP, self.REPO
1309 )
1311 )
1310 self._compare_error(id_, expected, given=response.body)
1312 self._compare_error(id_, expected, given=response.body)
General Comments 0
You need to be logged in to leave comments. Login now