summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gitlab/objects.py8
-rw-r--r--gitlab/tests/test_manager.py23
-rw-r--r--tools/python_test.py12
3 files changed, 43 insertions, 0 deletions
diff --git a/gitlab/objects.py b/gitlab/objects.py
index 9849179..7a679fc 100644
--- a/gitlab/objects.py
+++ b/gitlab/objects.py
@@ -565,6 +565,14 @@ class User(GitlabObject):
class UserManager(BaseManager):
obj_cls = User
+ def search(self, query, **kwargs):
+ """Search users.
+
+ Returns a list of matching users.
+ """
+ url = self.obj_cls._url + '?search=' + query
+ return self._custom_list(url, self.obj_cls, **kwargs)
+
class CurrentUserKey(GitlabObject):
_url = '/user/keys'
diff --git a/gitlab/tests/test_manager.py b/gitlab/tests/test_manager.py
index f1286af..1b808a9 100644
--- a/gitlab/tests/test_manager.py
+++ b/gitlab/tests/test_manager.py
@@ -235,6 +235,29 @@ class TestGitlabManager(unittest.TestCase):
self.assertEqual(data[0].id, 1)
self.assertEqual(data[1].id, 2)
+ def test_user_manager_search(self):
+ mgr = UserManager(self.gitlab)
+
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/users",
+ query="search=foo", method="get")
+ def resp_get_search(url, request):
+ headers = {'content-type': 'application/json'}
+ content = ('[{"name": "foo1", "id": 1}, '
+ '{"name": "foo2", "id": 2}]')
+ content = content.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_get_search):
+ data = mgr.search('foo')
+ self.assertEqual(type(data), list)
+ self.assertEqual(2, len(data))
+ self.assertEqual(type(data[0]), User)
+ self.assertEqual(type(data[1]), User)
+ self.assertEqual(data[0].name, "foo1")
+ self.assertEqual(data[1].name, "foo2")
+ self.assertEqual(data[0].id, 1)
+ self.assertEqual(data[1].id, 2)
+
def test_group_manager_search(self):
mgr = GroupManager(self.gitlab)
diff --git a/tools/python_test.py b/tools/python_test.py
index aa881b1..a2e0b15 100644
--- a/tools/python_test.py
+++ b/tools/python_test.py
@@ -43,12 +43,24 @@ assert(new_user.email == user.email)
new_user.block()
new_user.unblock()
+foobar_user = gl.users.create(
+ {'email': 'foobar@example.com', 'username': 'foobar',
+ 'name': 'Foo Bar', 'password': 'foobar_password'})
+
+assert gl.users.search('foobar') == [foobar_user]
+usercmp = lambda x,y: cmp(x.id, y.id)
+expected = sorted([new_user, foobar_user], cmp=usercmp)
+actual = sorted(gl.users.search('foo'), cmp=usercmp)
+assert expected == actual
+assert gl.users.search('asdf') == []
+
# SSH keys
key = new_user.keys.create({'title': 'testkey', 'key': SSH_KEY})
assert(len(new_user.keys.list()) == 1)
key.delete()
new_user.delete()
+foobar_user.delete()
assert(len(gl.users.list()) == 1)
# current user key