summaryrefslogtreecommitdiff
path: root/gitlab/tests
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain.pocentek@objectif-libre.com>2015-05-11 20:36:21 +0200
committerGauvain Pocentek <gauvain.pocentek@objectif-libre.com>2015-05-11 20:36:21 +0200
commit03bbfa0fbc6d113b8b744f4901571593d1cabd13 (patch)
treea3aacc5d14c74501a1e4475922a8859da89d4aed /gitlab/tests
parent04432561cb0e1a7e658cf771fd530835b4f463c8 (diff)
downloadgitlab-03bbfa0fbc6d113b8b744f4901571593d1cabd13.tar.gz
move the tests inside the package
Diffstat (limited to 'gitlab/tests')
-rw-r--r--gitlab/tests/test_gitlab.py569
-rw-r--r--gitlab/tests/test_gitlabobject.py432
2 files changed, 1001 insertions, 0 deletions
diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py
new file mode 100644
index 0000000..76b1db6
--- /dev/null
+++ b/gitlab/tests/test_gitlab.py
@@ -0,0 +1,569 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2014 Mika Mäenpää <mika.j.maenpaa@tut.fi>, Tampere University of Technology
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import print_function
+
+try:
+ from unittest import TestCase, main
+except ImportError:
+ from unittest2 import TestCase, main
+
+from gitlab import Gitlab, GitlabConnectionError, GitlabAuthenticationError,\
+ GitlabListError, GitlabGetError, GitlabCreateError, GitlabDeleteError,\
+ GitlabUpdateError, Project, ProjectBranch, Group, User, CurrentUser,\
+ Hook, UserProject, Issue, Team, GroupMember, ProjectSnippet
+
+from httmock import response, HTTMock, urlmatch
+
+class TestGitLabRawMethods(TestCase):
+ def setUp(self):
+ self.gl = Gitlab("http://localhost", private_token="private_token",
+ email="testuser@test.com", password="testpassword",
+ ssl_verify=True)
+
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/known_path",
+ method="get")
+ def resp_get(self, url, request):
+ headers = {'content-type': 'application/json'}
+ content = 'response'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ def test_rawGet_unknown_path(self):
+ self.assertRaises(GitlabConnectionError, self.gl.rawGet,
+ "/unknown_path")
+
+ def test_rawGet_without_kwargs(self):
+ with HTTMock(self.resp_get):
+ resp = self.gl.rawGet("/known_path")
+ self.assertEqual(resp.content, b'response')
+ self.assertEqual(resp.status_code, 200)
+
+ def test_rawGet_with_kwargs(self):
+ with HTTMock(self.resp_get):
+ resp = self.gl.rawGet("/known_path", sudo="testing")
+ self.assertEqual(resp.content, b'response')
+ self.assertEqual(resp.status_code, 200)
+
+ def test_rawPost(self):
+
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/known_path",
+ method="post")
+ def resp_post(url, request):
+ headers = {'content-type': 'application/json'}
+ content = 'response'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_post):
+ resp = self.gl.rawPost("/known_path")
+ self.assertEqual(resp.content, b'response')
+ self.assertEqual(resp.status_code, 200)
+
+ def test_rawPost_unknown_path(self):
+ self.assertRaises(GitlabConnectionError, self.gl.rawPost,
+ "/unknown_path")
+
+ def test_rawPut(self):
+
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/known_path",
+ method="put")
+ def resp_put(url, request):
+ headers = {'content-type': 'application/json'}
+ content = 'response'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_put):
+ resp = self.gl.rawPut("/known_path")
+ self.assertEqual(resp.content, b'response')
+ self.assertEqual(resp.status_code, 200)
+
+ def test_rawPut_unknown_path(self):
+ self.assertRaises(GitlabConnectionError, self.gl.rawPut,
+ "/unknown_path")
+
+ def test_rawDelete(self):
+
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/known_path",
+ method="delete")
+ def resp_delete(url, request):
+ headers = {'content-type': 'application/json'}
+ content = 'response'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_delete):
+ resp = self.gl.rawDelete("/known_path")
+ self.assertEqual(resp.content, b'response')
+ self.assertEqual(resp.status_code, 200)
+
+ def test_rawDelete_unknown_path(self):
+ self.assertRaises(GitlabConnectionError, self.gl.rawDelete,
+ "/unknown_path")
+
+class TestGitLabMethods(TestCase):
+ def setUp(self):
+ self.gl = Gitlab("http://localhost", private_token="private_token",
+ email="testuser@test.com", password="testpassword",
+ ssl_verify=True)
+
+
+ def test_list(self):
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/1/repository/branches", method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '[{"branch_name": "testbranch", "project_id": 1, "ref": "a"}]'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ data = self.gl.list(ProjectBranch, project_id=1, page=1,
+ per_page=20)
+ self.assertEqual(len(data), 1)
+ data = data[0]
+ self.assertEqual(data.branch_name, "testbranch")
+ self.assertEqual(data.project_id, 1)
+ self.assertEqual(data.ref, "a")
+
+ def test_list_401(self):
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/1/repository/branches", method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message":"message"}'.encode("utf-8")
+ return response(401, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabAuthenticationError, self.gl.list,
+ ProjectBranch, project_id=1)
+
+ def test_list_unknown_error(self):
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/1/repository/branches", method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message":"message"}'.encode("utf-8")
+ return response(405, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabGetError, self.gl.list,
+ ProjectBranch, project_id=1)
+
+ def test_list_kw_missing(self):
+ self.assertRaises(GitlabListError, self.gl.list, ProjectBranch)
+
+ def test_list_no_connection(self):
+ self.assertRaises(GitlabConnectionError, self.gl.list, ProjectBranch,
+ project_id=1)
+
+ def test_get(self):
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/1", method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "testproject"}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ data = self.gl.get(Project, id=1)
+ expected = {"name": "testproject"}
+ self.assertEqual(expected, data)
+
+ def test_get_unknown_path(self):
+ self.assertRaises(GitlabConnectionError, self.gl.get, Project, 1)
+
+ def test_get_missing_kw(self):
+ self.assertRaises(GitlabGetError, self.gl.get, ProjectBranch)
+
+ def test_get_401(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1",
+ method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(401, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabAuthenticationError, self.gl.get,
+ Project, 1)
+
+ def test_get_404(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1",
+ method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(404, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabGetError, self.gl.get,
+ Project, 1)
+
+ def test_get_unknown_error(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1",
+ method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(405, content, headers, None, 5, request)
+
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabGetError, self.gl.get,
+ Project, 1)
+
+ def test_delete(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1",
+ method="delete")
+ def resp_delete_group(url, request):
+ headers = {'content-type': 'application/json'}
+ content = ''.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ obj = Group(self.gl, data={"name": "testname", "id": 1})
+ with HTTMock(resp_delete_group):
+ data = self.gl.delete(obj)
+ self.assertIs(data, True)
+
+ def test_delete_unknown_path(self):
+ obj = Project(self.gl, data={"name": "testname", "id": 1})
+ self.assertRaises(GitlabConnectionError, self.gl.delete, obj)
+
+ def test_delete_401(self):
+ obj = Project(self.gl, data={"name": "testname", "id": 1})
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1",
+ method="delete")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(401, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabAuthenticationError, self.gl.delete, obj)
+
+ def test_delete_unknown_error(self):
+ obj = Project(self.gl, data={"name": "testname", "id": 1})
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1",
+ method="delete")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(405, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabDeleteError, self.gl.delete, obj)
+
+ def test_create(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects",
+ method="post")
+ def resp_create_project(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "testname", "id": 1}'.encode("utf-8")
+ return response(201, content, headers, None, 5, request)
+
+ obj = Project(self.gl, data={"name": "testname"})
+
+ with HTTMock(resp_create_project):
+ data = self.gl.create(obj)
+ expected = {u"name": u"testname", u"id": 1}
+ self.assertEqual(expected, data)
+
+ def test_create_kw_missing(self):
+ obj = Group(self.gl, data={"name": "testgroup"})
+ self.assertRaises(GitlabCreateError, self.gl.create, obj)
+
+ def test_create_unknown_path(self):
+ obj = User(self.gl, data={"email": "email", "password": "password",
+ "username": "username", "name": "name",
+ "can_create_group": True})
+ self.assertRaises(GitlabConnectionError, self.gl.create, obj)
+
+ def test_create_401(self):
+ obj = Group(self.gl, data={"name": "testgroup", "path": "testpath"})
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups",
+ method="post")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(401, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabAuthenticationError, self.gl.create, obj)
+
+ def test_create_unknown_error(self):
+ obj = Group(self.gl, data={"name": "testgroup", "path": "testpath"})
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups",
+ method="post")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(405, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabCreateError, self.gl.create, obj)
+
+ def test_update(self):
+ obj = User(self.gl, data={"email": "testuser@testmail.com",
+ "password": "testpassword",
+ "name": u"testuser",
+ "username": "testusername",
+ "can_create_group": True,
+ "id": 1})
+
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/users/1",
+ method="put")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"first": "return1"}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+
+ with HTTMock(resp_cont):
+ data = self.gl.update(obj)
+ expected = {"first": "return1"}
+ self.assertEqual(expected, data)
+
+ def test_update_kw_missing(self):
+ obj = Group(self.gl, data={"name": "testgroup"})
+ self.assertRaises(GitlabUpdateError, self.gl.update, obj)
+
+ def test_update_401(self):
+ obj = Group(self.gl, data={"name": "testgroup", "path": "testpath",
+ "id": 1})
+
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1",
+ method="put")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(401, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabAuthenticationError, self.gl.update, obj)
+
+ def test_update_unknown_error(self):
+ obj = Group(self.gl, data={"name": "testgroup", "path": "testpath",
+ "id": 1})
+
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1",
+ method="put")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(405, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabUpdateError, self.gl.update, obj)
+
+ def test_update_unknown_path(self):
+ obj = Group(self.gl, data={"name": "testgroup", "path": "testpath",
+ "id": 1})
+ self.assertRaises(GitlabConnectionError, self.gl.update, obj)
+
+
+class TestGitLab(TestCase):
+
+ def setUp(self):
+ self.gl = Gitlab("http://localhost", private_token="private_token",
+ email="testuser@test.com", password="testpassword",
+ ssl_verify=True)
+
+ def test_setUrl(self):
+ self.gl.setUrl("http://new_url")
+ self.assertEqual(self.gl._url, "http://new_url/api/v3")
+
+ def test_setToken(self):
+ token = "newtoken"
+ expected = {"PRIVATE-TOKEN": token}
+ self.gl.setToken(token)
+ self.assertEqual(self.gl.private_token, token)
+ self.assertDictContainsSubset(expected, self.gl.headers)
+
+ def test_setCredentials(self):
+ email = "credentialuser@test.com"
+ password = "credentialpassword"
+ self.gl.setCredentials(email=email, password=password)
+ self.assertEqual(self.gl.email, email)
+ self.assertEqual(self.gl.password, password)
+
+ def test_credentials_auth_nopassword(self):
+ self.gl.setCredentials(email=None, password=None)
+ self.assertRaises(GitlabAuthenticationError, self.gl.credentials_auth)
+
+ def test_credentials_auth_notok(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/session",
+ method="post")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "message"}'.encode("utf-8")
+ return response(404, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ self.assertRaises(GitlabAuthenticationError,
+ self.gl.credentials_auth)
+
+ def test_auth_with_credentials(self):
+ self.gl.setToken(None)
+ self.test_credentials_auth(callback=self.gl.auth)
+
+ def test_auth_with_token(self):
+ self.test_token_auth(callback=self.gl.auth)
+
+ def test_credentials_auth(self, callback=None):
+ if callback is None:
+ callback = self.gl.credentials_auth
+ token = "credauthtoken"
+ id_ = 1
+ expected = {"PRIVATE-TOKEN": token }
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/session",
+ method="post")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{{"id": {0:d}, "private_token": "{1:s}"}}'.format(
+ id_, token).encode("utf-8")
+ return response(201, content, headers, None, 5, request)
+
+
+ with HTTMock(resp_cont):
+ callback()
+ self.assertEqual(self.gl.private_token, token)
+ self.assertDictContainsSubset(expected, self.gl.headers)
+ self.assertEqual(self.gl.user.id, id_)
+
+ def test_token_auth(self, callback=None):
+ if callback is None:
+ callback = self.gl.token_auth
+ name = "username"
+ id_ = 1
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/user",
+ method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{{"id": {0:d}, "username": "{1:s}"}}'.format(
+ id_,name).encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ callback()
+ self.assertEqual(self.gl.user.username, name)
+ self.assertEqual(self.gl.user.id, id_)
+ self.assertEqual(type(self.gl.user), CurrentUser)
+
+ def test_getListOrObject_without_id(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects",
+ method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '[{"name": "testproject", "id": 1}]'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ projs = self.gl._getListOrObject(Project, None)
+ self.assertEqual(len(projs), 1)
+ proj = projs[0]
+ self.assertEqual(proj.id, 1)
+ self.assertEqual(proj.name, "testproject")
+
+ def test_getListOrObject_with_id(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1",
+ method="get")
+ def resp_cont(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "testproject", "id": 1}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_cont):
+ proj = self.gl._getListOrObject(Project, 1)
+ self.assertEqual(proj.id, 1)
+ self.assertEqual(proj.name, "testproject")
+
+ def test_Hook(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/hooks/1",
+ method="get")
+ def resp_get_hook(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"url": "testurl", "id": 1}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_get_hook):
+ data = self.gl.Hook(id=1)
+ self.assertEqual(type(data), Hook)
+ self.assertEqual(data.url, "testurl")
+ self.assertEqual(data.id, 1)
+
+ def test_Project(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1",
+ method="get")
+ def resp_get_project(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "name", "id": 1}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_get_project):
+ data = self.gl.Project(id=1)
+ self.assertEqual(type(data), Project)
+ self.assertEqual(data.name, "name")
+ self.assertEqual(data.id, 1)
+
+ def test_UserProject(self):
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/user/2", method="get")
+ def resp_get_userproject(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "name", "id": 1, "user_id": 2}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_get_userproject):
+ self.assertRaises(GitlabGetError, self.gl.UserProject, id=1,
+ user_id=2)
+
+ def test_Group(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1",
+ method="get")
+ def resp_get_group(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "name", "id": 1, "path": "path"}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_get_group):
+ data = self.gl.Group(id=1)
+ self.assertEqual(type(data), Group)
+ self.assertEqual(data.name, "name")
+ self.assertEqual(data.path, "path")
+ self.assertEqual(data.id, 1)
+
+ def test_Issue(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/issues/1",
+ method="get")
+ def resp_get_issue(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "name", "id": 1}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_get_issue):
+ self.assertRaises(GitlabGetError, self.gl.Issue, id=1)
+
+ def test_User(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/users/1",
+ method="get")
+ def resp_get_user(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "name", "id": 1, "password": "password", "username": "username", "email": "email"}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_get_user):
+ user = self.gl.User(id=1)
+ self.assertEqual(type(user), User)
+ self.assertEqual(user.name, "name")
+ self.assertEqual(user.id, 1)
diff --git a/gitlab/tests/test_gitlabobject.py b/gitlab/tests/test_gitlabobject.py
new file mode 100644
index 0000000..9cc2d28
--- /dev/null
+++ b/gitlab/tests/test_gitlabobject.py
@@ -0,0 +1,432 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2014 Mika Mäenpää <mika.j.maenpaa@tut.fi>, Tampere University of Technology
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import print_function, division, absolute_import
+
+try:
+ from unittest import TestCase, main
+except ImportError:
+ from unittest2 import TestCase, main
+
+from gitlab import Gitlab, GitlabConnectionError, GitlabAuthenticationError,\
+ GitlabListError, GitlabGetError, GitlabCreateError, GitlabDeleteError,\
+ GitlabUpdateError, Project, ProjectBranch, Group, User, CurrentUser,\
+ Hook, UserProject, Issue, Team, GroupMember, ProjectSnippet,\
+ GitlabTransferProjectError, GitlabProtectError, ProjectCommit,\
+ ProjectSnippet
+
+from httmock import response, HTTMock, urlmatch
+
+@urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1",
+ method="get")
+def resp_get_project(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "name", "id": 1}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects",
+ method="get")
+def resp_list_project(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '[{"name": "name", "id": 1}]'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost", path="/api/v3/issues/1",
+ method="get")
+def resp_get_issue(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "name", "id": 1}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost", path="/api/v3/users/1",
+ method="put")
+def resp_update_user(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "newname", "id": 1, "password": "password", "username": "username", "email": "email"}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects",
+ method="post")
+def resp_create_project(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"name": "testname", "id": 1}'.encode("utf-8")
+ return response(201, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/2/members",
+ method="post")
+def resp_create_groupmember(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"access_level": 50, "id": 3}'.encode("utf-8")
+ return response(201, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/snippets/3", method="get")
+def resp_get_projectsnippet(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"title": "test", "id": 3}'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1",
+ method="delete")
+def resp_delete_group(url, request):
+ headers = {'content-type': 'application/json'}
+ content = ''.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/2/projects/3",
+ method="post")
+def resp_transfer_project(url, request):
+ headers = {'content-type': 'application/json'}
+ content = ''.encode("utf-8")
+ return response(201, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/2/projects/3",
+ method="post")
+def resp_transfer_project_fail(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "messagecontent"}'.encode("utf-8")
+ return response(400, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/repository/branches/branchname/protect",
+ method="put")
+def resp_protect_branch(url, request):
+ headers = {'content-type': 'application/json'}
+ content = ''.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/repository/branches/branchname/unprotect",
+ method="put")
+def resp_unprotect_branch(url, request):
+ headers = {'content-type': 'application/json'}
+ content = ''.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+@urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/repository/branches/branchname/protect",
+ method="put")
+def resp_protect_branch_fail(url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "messagecontent"}'.encode("utf-8")
+ return response(400, content, headers, None, 5, request)
+
+
+class TestGitLabObject(TestCase):
+
+ def setUp(self):
+ self.gl = Gitlab("http://localhost", private_token="private_token",
+ email="testuser@test.com", password="testpassword",
+ ssl_verify=True)
+
+ def test_list_not_implemented(self):
+ self.assertRaises(NotImplementedError, CurrentUser.list, self.gl)
+
+ def test_list(self):
+ with HTTMock(resp_list_project):
+ data = Project.list(self.gl, id=1)
+ self.assertEqual(type(data), list)
+ self.assertEqual(len(data), 1)
+ self.assertEqual(type(data[0]), Project)
+ self.assertEqual(data[0].name, "name")
+ self.assertEqual(data[0].id, 1)
+
+ def test_getListOrObject_with_list(self):
+ with HTTMock(resp_list_project):
+ gl_object = Project(self.gl, data={"name": "name"})
+ data = gl_object._getListOrObject(Project, id=None)
+ self.assertEqual(type(data), list)
+ self.assertEqual(len(data), 1)
+ self.assertEqual(type(data[0]), Project)
+ self.assertEqual(data[0].name, "name")
+ self.assertEqual(data[0].id, 1)
+
+ def test_getListOrObject_with_get(self):
+ with HTTMock(resp_get_project):
+ gl_object = Project(self.gl, data={"name": "name"})
+ data = gl_object._getListOrObject(Project, id=1)
+ self.assertEqual(type(data), Project)
+ self.assertEqual(data.name, "name")
+ self.assertEqual(data.id, 1)
+
+ def test_getListOrObject_cant_get(self):
+ with HTTMock(resp_get_issue):
+ gl_object = Project(self.gl, data={"name": "name"})
+ self.assertRaises(NotImplementedError, gl_object._getListOrObject,
+ Issue, id=1)
+
+ def test_getListOrObject_cantlist(self):
+ gl_object = Project(self.gl, data={"name": "name"})
+ self.assertRaises(NotImplementedError, gl_object._getListOrObject,
+ CurrentUser, id=None)
+
+ def test_getListOrObject_cantcreate(self):
+ gl_object = Project(self.gl, data={"name": "name"})
+ self.assertRaises(NotImplementedError, gl_object._getListOrObject,
+ CurrentUser, id={})
+
+ def test_getListOrObject_create(self):
+ data = {"name": "name"}
+ gl_object = Project(self.gl, data=data)
+ data = gl_object._getListOrObject(Project, id=data)
+ self.assertEqual(type(data), Project)
+ self.assertEqual(data.name, "name")
+
+ def test_create_cantcreate(self):
+ gl_object = CurrentUser(self.gl, data={"username": "testname"})
+ self.assertRaises(NotImplementedError, gl_object._create)
+
+ def test_create(self):
+ obj = Project(self.gl, data={"name": "testname"})
+ with HTTMock(resp_create_project):
+ obj._create()
+ self.assertEqual(obj.id, 1)
+
+ def test_create_with_kw(self):
+ obj = GroupMember(self.gl, data={"access_level": 50, "user_id": 3},
+ group_id=2)
+ with HTTMock(resp_create_groupmember):
+ obj._create()
+ self.assertEqual(obj.id, 3)
+ self.assertEqual(obj.group_id, 2)
+ self.assertEqual(obj.user_id, 3)
+ self.assertEqual(obj.access_level, 50)
+
+ def test_get_with_kw(self):
+ with HTTMock(resp_get_projectsnippet):
+ obj = ProjectSnippet(self.gl, data=3, project_id=2)
+ self.assertEqual(obj.id, 3)
+ self.assertEqual(obj.project_id, 2)
+ self.assertEqual(obj.title, "test")
+
+ def test_create_cantupdate(self):
+ gl_object = CurrentUser(self.gl, data={"username": "testname"})
+ self.assertRaises(NotImplementedError, gl_object._update)
+
+ def test_update(self):
+ obj = User(self.gl, data={"name": "testname", "email": "email",
+ "password": "password", "id": 1,
+ "username": "username"})
+ self.assertEqual(obj.name, "testname")
+ obj.name = "newname"
+ with HTTMock(resp_update_user):
+ obj._update()
+ self.assertEqual(obj.name, "newname")
+
+ def test_save_with_id(self):
+ obj = User(self.gl, data={"name": "testname", "email": "email",
+ "password": "password", "id": 1,
+ "username": "username"})
+ self.assertEqual(obj.name, "testname")
+ obj.created = True
+ obj.name = "newname"
+ with HTTMock(resp_update_user):
+ obj.save()
+ self.assertEqual(obj.name, "newname")
+
+ def test_save_without_id(self):
+ obj = Project(self.gl, data={"name": "testname"})
+ with HTTMock(resp_create_project):
+ obj.save()
+ self.assertEqual(obj.id, 1)
+
+ def test_delete(self):
+ obj = Group(self.gl, data={"name": "testname", "id": 1,
+ "created": True})
+ with HTTMock(resp_delete_group):
+ data = obj.delete()
+ self.assertIs(data, True)
+
+ def test_delete_with_no_id(self):
+ obj = Group(self.gl, data={"name": "testname"})
+ self.assertRaises(GitlabDeleteError, obj.delete)
+
+ def test_delete_cant_delete(self):
+ obj = CurrentUser(self.gl, data={"name": "testname", "id": 1})
+ self.assertRaises(NotImplementedError, obj.delete)
+
+ def test_setFromDict_BooleanTrue(self):
+ obj = Project(self.gl, data={"name": "testname"})
+ data = {"issues_enabled": True}
+ obj._setFromDict(data)
+ self.assertIs(obj.issues_enabled, True)
+
+ def test_setFromDict_BooleanFalse(self):
+ obj = Project(self.gl, data={"name": "testname"})
+ data = {"issues_enabled": False}
+ obj._setFromDict(data)
+ self.assertIs(obj.issues_enabled, False)
+
+ def test_setFromDict_None(self):
+ obj = Project(self.gl, data={"name": "testname"})
+ data = {"issues_enabled": None}
+ obj._setFromDict(data)
+ self.assertIsNone(obj.issues_enabled)
+
+class TestGroup(TestCase):
+ def setUp(self):
+ self.gl = Gitlab("http://localhost", private_token="private_token",
+ email="testuser@test.com", password="testpassword",
+ ssl_verify=True)
+
+ def test_transfer_project(self):
+ obj = Group(self.gl, data={"name": "testname", "path": "testpath",
+ "id": 2})
+ with HTTMock(resp_transfer_project):
+ obj.transfer_project(3)
+
+ def test_transfer_project_fail(self):
+ obj = Group(self.gl, data={"name": "testname", "path": "testpath",
+ "id": 2})
+ with HTTMock(resp_transfer_project_fail):
+ self.assertRaises(GitlabTransferProjectError,
+ obj.transfer_project, 3)
+
+
+class TestProjectBranch(TestCase):
+ def setUp(self):
+ self.gl = Gitlab("http://localhost", private_token="private_token",
+ email="testuser@test.com", password="testpassword",
+ ssl_verify=True)
+ self.obj = ProjectBranch(self.gl, data={"name": "branchname",
+ "ref": "ref_name", "id": 3,
+ "project_id": 2})
+
+ def test_protect(self):
+ self.assertRaises(AttributeError, getattr, self.obj, 'protected')
+ with HTTMock(resp_protect_branch):
+ self.obj.protect(True)
+ self.assertIs(self.obj.protected, True)
+
+ def test_protect_unprotect(self):
+ self.obj.protected = True
+ with HTTMock(resp_unprotect_branch):
+ self.obj.protect(False)
+ self.assertRaises(AttributeError, getattr, self.obj, 'protected')
+
+ def test_protect_unprotect_again(self):
+ self.assertRaises(AttributeError, getattr, self.obj, 'protected')
+ with HTTMock(resp_unprotect_branch):
+ self.obj.protect(False)
+ self.assertRaises(AttributeError, getattr, sef.obj, 'protected')
+
+ def test_protect_protect_fail(self):
+ with HTTMock(resp_protect_branch_fail):
+ self.assertRaises(GitlabProtectError, self.obj.protect)
+
+ def test_unprotect(self):
+ self.obj.protected = True
+ with HTTMock(resp_unprotect_branch):
+ self.obj.unprotect()
+ self.assertRaises(AttributeError, getattr, self.obj, 'protected')
+
+class TestProjectCommit(TestCase):
+ def setUp(self):
+ self.gl = Gitlab("http://localhost", private_token="private_token",
+ email="testuser@test.com", password="testpassword",
+ ssl_verify=True)
+ self.obj = ProjectCommit(self.gl, data={"id": 3, "project_id": 2})
+
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/repository/commits/3/diff",
+ method="get")
+ def resp_diff(self, url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"json": 2 }'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/repository/commits/3/diff",
+ method="get")
+ def resp_diff_fail(self, url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "messagecontent" }'.encode("utf-8")
+ return response(400, content, headers, None, 5, request)
+
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/repository/blobs/3",
+ method="get")
+ def resp_blob(self, url, request):
+ headers = {'content-type': 'application/json'}
+ content = 'blob'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/repository/blobs/3",
+ method="get")
+ def resp_blob_fail(self, url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "messagecontent" }'.encode("utf-8")
+ return response(400, content, headers, None, 5, request)
+
+ def test_diff(self):
+ with HTTMock(self.resp_diff):
+ data = {"json": 2}
+ diff = self.obj.diff()
+ self.assertEqual(diff, data)
+
+ def test_diff_fail(self):
+ with HTTMock(self.resp_diff_fail):
+ self.assertRaises(GitlabGetError, self.obj.diff)
+
+ def test_blob(self):
+ with HTTMock(self.resp_blob):
+ data = {"json": 2}
+ blob = self.obj.blob("testing")
+ self.assertEqual(blob, b'blob')
+
+ def test_blob_fail(self):
+ with HTTMock(self.resp_blob_fail):
+ self.assertRaises(GitlabGetError, self.obj.blob, "testing")
+
+class TestProjectSnippet(TestCase):
+ def setUp(self):
+ self.gl = Gitlab("http://localhost", private_token="private_token",
+ email="testuser@test.com", password="testpassword",
+ ssl_verify=True)
+ self.obj = ProjectSnippet(self.gl, data={"id": 3, "project_id": 2})
+
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/snippets/3/raw",
+ method="get")
+ def resp_content(self, url, request):
+ headers = {'content-type': 'application/json'}
+ content = 'content'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/projects/2/snippets/3/raw",
+ method="get")
+ def resp_content_fail(self, url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "messagecontent" }'.encode("utf-8")
+ return response(400, content, headers, None, 5, request)
+
+ def test_content(self):
+ with HTTMock(self.resp_content):
+ data = b'content'
+ content = self.obj.Content()
+ self.assertEqual(content, data)
+
+ def test_blob_fail(self):
+ with HTTMock(self.resp_content_fail):
+ self.assertRaises(GitlabGetError, self.obj.Content)
+
+
+if __name__ == "__main__":
+ main()