| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -573,6 +573,20 @@ def search(self, query, **kwargs): | |||
| 573 | 573 | url = self.obj_cls._url + '?search=' + query | |
| 574 | 574 | return self._custom_list(url, self.obj_cls, **kwargs) | |
| 575 | 575 | ||
| 576 | + def get_by_username(self, username, **kwargs): | ||
| 577 | + """Get a user by its username. | ||
| 578 | + | ||
| 579 | + Returns a User object or None if the named user does not | ||
| 580 | + exist. | ||
| 581 | + """ | ||
| 582 | + url = self.obj_cls._url + '?username=' + username | ||
| 583 | + results = self._custom_list(url, self.obj_cls, **kwargs) | ||
| 584 | + assert len(results) in (0, 1) | ||
| 585 | + try: | ||
| 586 | + return results[0] | ||
| 587 | + except IndexError: | ||
| 588 | + raise GitlabGetError('no such user: ' + username) | ||
| 589 | + | ||
| 576 | 590 | ||
| 577 | 591 | class CurrentUserKey(GitlabObject): | |
| 578 | 592 | _url = '/user/keys' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -258,6 +258,32 @@ def resp_get_search(url, request): | |||
| 258 | 258 | self.assertEqual(data[0].id, 1) | |
| 259 | 259 | self.assertEqual(data[1].id, 2) | |
| 260 | 260 | ||
| 261 | + def test_user_manager_get_by_username(self): | ||
| 262 | + mgr = UserManager(self.gitlab) | ||
| 263 | + | ||
| 264 | + @urlmatch(scheme="http", netloc="localhost", path="/api/v3/users", | ||
| 265 | + query="username=foo", method="get") | ||
| 266 | + def resp_get_username(url, request): | ||
| 267 | + headers = {'content-type': 'application/json'} | ||
| 268 | + content = '[{"name": "foo", "id": 1}]'.encode("utf-8") | ||
| 269 | + return response(200, content, headers, None, 5, request) | ||
| 270 | + | ||
| 271 | + with HTTMock(resp_get_username): | ||
| 272 | + data = mgr.get_by_username('foo') | ||
| 273 | + self.assertEqual(type(data), User) | ||
| 274 | + self.assertEqual(data.name, "foo") | ||
| 275 | + self.assertEqual(data.id, 1) | ||
| 276 | + | ||
| 277 | + @urlmatch(scheme="http", netloc="localhost", path="/api/v3/users", | ||
| 278 | + query="username=foo", method="get") | ||
| 279 | + def resp_get_username_nomatch(url, request): | ||
| 280 | + headers = {'content-type': 'application/json'} | ||
| 281 | + content = '[]'.encode("utf-8") | ||
| 282 | + return response(200, content, headers, None, 5, request) | ||
| 283 | + | ||
| 284 | + with HTTMock(resp_get_username_nomatch): | ||
| 285 | + self.assertRaises(GitlabGetError, mgr.get_by_username, 'foo') | ||
| 286 | + | ||
| 261 | 287 | def test_group_manager_search(self): | |
| 262 | 288 | mgr = GroupManager(self.gitlab) | |
| 263 | 289 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,6 +54,15 @@ | |||
| 54 | 54 | assert expected == actual | |
| 55 | 55 | assert gl.users.search('asdf') == [] | |
| 56 | 56 | ||
| 57 | + assert gl.users.get_by_username('foobar') == foobar_user | ||
| 58 | + assert gl.users.get_by_username('foo') == new_user | ||
| 59 | + try: | ||
| 60 | + gl.users.get_by_username('asdf') | ||
| 61 | + except gitlab.GitlabGetError: | ||
| 62 | + pass | ||
| 63 | + else: | ||
| 64 | + assert False | ||
| 65 | + | ||
| 57 | 66 | # SSH keys | |
| 58 | 67 | key = new_user.keys.create({'title': 'testkey', 'key': SSH_KEY}) | |
| 59 | 68 | assert(len(new_user.keys.list()) == 1) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments