##// 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,29 +181,56 b' class TestGetRefHash(object):'
181 181 class TestUserByNameOrError(object):
182 182 def test_user_found_by_id(self):
183 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 197 patcher = patch('rhodecode.model.user.UserModel.get_user')
185 198 with patcher as get_user:
186 199 get_user.return_value = fake_user
187 result = utils.get_user_or_error('123')
188 assert result == fake_user
200 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
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 207 def test_user_found_by_name(self):
191 208 fake_user = Mock(id=123)
192 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
193 with patcher as get_by_username:
194 get_by_username.return_value = fake_user
195 result = utils.get_user_or_error('test')
196 assert result == fake_user
209
210 patcher = patch('rhodecode.model.user.UserModel.get_user')
211 with patcher as get_user:
212 get_user.return_value = None
213
214 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
215 with patcher as get_by_username:
216 get_by_username.return_value = fake_user
217
218 result = utils.get_user_or_error('test')
219 assert result == fake_user
197 220
198 221 def test_user_not_found_by_id(self):
199 222 patcher = patch('rhodecode.model.user.UserModel.get_user')
200 223 with patcher as get_user:
201 224 get_user.return_value = None
202 with pytest.raises(JSONRPCError) as excinfo:
203 utils.get_user_or_error('123')
225 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
226 with patcher as get_by_username:
227 get_by_username.return_value = None
204 228
205 expected_message = 'user `123` does not exist'
206 assert excinfo.value.message == expected_message
229 with pytest.raises(JSONRPCError) as excinfo:
230 utils.get_user_or_error(123)
231
232 expected_message = 'user `123` does not exist'
233 assert excinfo.value.message == expected_message
207 234
208 235 def test_user_not_found_by_name(self):
209 236 patcher = patch('rhodecode.model.user.UserModel.get_by_username')
@@ -216,8 +243,7 b' class TestUserByNameOrError(object):'
216 243 assert excinfo.value.message == expected_message
217 244
218 245
219 class TestGetCommitDict:
220
246 class TestGetCommitDict(object):
221 247 @pytest.mark.parametrize('filename, expected', [
222 248 (b'sp\xc3\xa4cial', u'sp\xe4cial'),
223 249 (b'sp\xa4cial', u'sp\ufffdcial'),
@@ -213,15 +213,19 b' def get_user_or_error(userid):'
213 213 :param userid:
214 214 """
215 215 from rhodecode.model.user import UserModel
216 user_model = UserModel()
216 217
217 user_model = UserModel()
218 try:
219 user = user_model.get_user(int(userid))
220 except ValueError:
218 if isinstance(userid, (int, long)):
219 try:
220 user = user_model.get_user(userid)
221 except ValueError:
222 user = None
223 else:
221 224 user = user_model.get_by_username(userid)
222 225
223 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 229 return user
226 230
227 231
@@ -232,10 +236,19 b' def get_repo_or_error(repoid):'
232 236 :param repoid:
233 237 """
234 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 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 252 return repo
240 253
241 254
@@ -246,8 +259,16 b' def get_repo_group_or_error(repogroupid)'
246 259 :param repogroupid:
247 260 """
248 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 272 if repo_group is None:
252 273 raise JSONRPCError(
253 274 'repository group `%s` does not exist' % (repogroupid,))
@@ -261,10 +282,19 b' def get_user_group_or_error(usergroupid)'
261 282 :param usergroupid:
262 283 """
263 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 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 298 return user_group
269 299
270 300
General Comments 0
You need to be logged in to leave comments. Login now