##// END OF EJS Templates
api: use consistent way to extract users, repos, repo groups and user groups by id or name....
marcink -
r1530:1efcb4ee default
parent child Browse files
Show More
@@ -181,17 +181,40 b' class TestGetRefHash(object):'
181 class TestUserByNameOrError(object):
181 class TestUserByNameOrError(object):
182 def test_user_found_by_id(self):
182 def test_user_found_by_id(self):
183 fake_user = Mock(id=123)
183 fake_user = Mock(id=123)
184
185 patcher = patch('rhodecode.model.user.UserModel.get_user')
186 with patcher as get_user:
187 get_user.return_value = fake_user
188
189 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
190 with patcher as get_by_username:
191 result = utils.get_user_or_error(123)
192 assert result == fake_user
193
194 def test_user_not_found_by_id_as_str(self):
195 fake_user = Mock(id=123)
196
184 patcher = patch('rhodecode.model.user.UserModel.get_user')
197 patcher = patch('rhodecode.model.user.UserModel.get_user')
185 with patcher as get_user:
198 with patcher as get_user:
186 get_user.return_value = fake_user
199 get_user.return_value = fake_user
187 result = utils.get_user_or_error('123')
200 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
188 assert result == fake_user
201 with patcher as get_by_username:
202 get_by_username.return_value = None
203
204 with pytest.raises(JSONRPCError):
205 utils.get_user_or_error('123')
189
206
190 def test_user_found_by_name(self):
207 def test_user_found_by_name(self):
191 fake_user = Mock(id=123)
208 fake_user = Mock(id=123)
209
210 patcher = patch('rhodecode.model.user.UserModel.get_user')
211 with patcher as get_user:
212 get_user.return_value = None
213
192 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
214 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
193 with patcher as get_by_username:
215 with patcher as get_by_username:
194 get_by_username.return_value = fake_user
216 get_by_username.return_value = fake_user
217
195 result = utils.get_user_or_error('test')
218 result = utils.get_user_or_error('test')
196 assert result == fake_user
219 assert result == fake_user
197
220
@@ -199,8 +222,12 b' class TestUserByNameOrError(object):'
199 patcher = patch('rhodecode.model.user.UserModel.get_user')
222 patcher = patch('rhodecode.model.user.UserModel.get_user')
200 with patcher as get_user:
223 with patcher as get_user:
201 get_user.return_value = None
224 get_user.return_value = None
225 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
226 with patcher as get_by_username:
227 get_by_username.return_value = None
228
202 with pytest.raises(JSONRPCError) as excinfo:
229 with pytest.raises(JSONRPCError) as excinfo:
203 utils.get_user_or_error('123')
230 utils.get_user_or_error(123)
204
231
205 expected_message = 'user `123` does not exist'
232 expected_message = 'user `123` does not exist'
206 assert excinfo.value.message == expected_message
233 assert excinfo.value.message == expected_message
@@ -216,8 +243,7 b' class TestUserByNameOrError(object):'
216 assert excinfo.value.message == expected_message
243 assert excinfo.value.message == expected_message
217
244
218
245
219 class TestGetCommitDict:
246 class TestGetCommitDict(object):
220
221 @pytest.mark.parametrize('filename, expected', [
247 @pytest.mark.parametrize('filename, expected', [
222 (b'sp\xc3\xa4cial', u'sp\xe4cial'),
248 (b'sp\xc3\xa4cial', u'sp\xe4cial'),
223 (b'sp\xa4cial', u'sp\ufffdcial'),
249 (b'sp\xa4cial', u'sp\ufffdcial'),
@@ -213,15 +213,19 b' def get_user_or_error(userid):'
213 :param userid:
213 :param userid:
214 """
214 """
215 from rhodecode.model.user import UserModel
215 from rhodecode.model.user import UserModel
216 user_model = UserModel()
216
217
217 user_model = UserModel()
218 if isinstance(userid, (int, long)):
218 try:
219 try:
219 user = user_model.get_user(int(userid))
220 user = user_model.get_user(userid)
220 except ValueError:
221 except ValueError:
222 user = None
223 else:
221 user = user_model.get_by_username(userid)
224 user = user_model.get_by_username(userid)
222
225
223 if user is None:
226 if user is None:
224 raise JSONRPCError("user `%s` does not exist" % (userid,))
227 raise JSONRPCError(
228 'user `%s` does not exist' % (userid,))
225 return user
229 return user
226
230
227
231
@@ -232,10 +236,19 b' def get_repo_or_error(repoid):'
232 :param repoid:
236 :param repoid:
233 """
237 """
234 from rhodecode.model.repo import RepoModel
238 from rhodecode.model.repo import RepoModel
239 repo_model = RepoModel()
235
240
236 repo = RepoModel().get_repo(repoid)
241 if isinstance(repoid, (int, long)):
242 try:
243 repo = repo_model.get_repo(repoid)
244 except ValueError:
245 repo = None
246 else:
247 repo = repo_model.get_by_repo_name(repoid)
248
237 if repo is None:
249 if repo is None:
238 raise JSONRPCError('repository `%s` does not exist' % (repoid,))
250 raise JSONRPCError(
251 'repository `%s` does not exist' % (repoid,))
239 return repo
252 return repo
240
253
241
254
@@ -246,8 +259,16 b' def get_repo_group_or_error(repogroupid)'
246 :param repogroupid:
259 :param repogroupid:
247 """
260 """
248 from rhodecode.model.repo_group import RepoGroupModel
261 from rhodecode.model.repo_group import RepoGroupModel
262 repo_group_model = RepoGroupModel()
249
263
250 repo_group = RepoGroupModel()._get_repo_group(repogroupid)
264 if isinstance(repogroupid, (int, long)):
265 try:
266 repo_group = repo_group_model._get_repo_group(repogroupid)
267 except ValueError:
268 repo_group = None
269 else:
270 repo_group = repo_group_model.get_by_group_name(repogroupid)
271
251 if repo_group is None:
272 if repo_group is None:
252 raise JSONRPCError(
273 raise JSONRPCError(
253 'repository group `%s` does not exist' % (repogroupid,))
274 'repository group `%s` does not exist' % (repogroupid,))
@@ -261,10 +282,19 b' def get_user_group_or_error(usergroupid)'
261 :param usergroupid:
282 :param usergroupid:
262 """
283 """
263 from rhodecode.model.user_group import UserGroupModel
284 from rhodecode.model.user_group import UserGroupModel
285 user_group_model = UserGroupModel()
264
286
265 user_group = UserGroupModel().get_group(usergroupid)
287 if isinstance(usergroupid, (int, long)):
288 try:
289 user_group = user_group_model.get_group(usergroupid)
290 except ValueError:
291 user_group = None
292 else:
293 user_group = user_group_model.get_by_name(usergroupid)
294
266 if user_group is None:
295 if user_group is None:
267 raise JSONRPCError('user group `%s` does not exist' % (usergroupid,))
296 raise JSONRPCError(
297 'user group `%s` does not exist' % (usergroupid,))
268 return user_group
298 return user_group
269
299
270
300
General Comments 0
You need to be logged in to leave comments. Login now