diff options
| author | Gauvain Pocentek <gauvain@pocentek.net> | 2018-05-19 17:10:08 +0200 |
|---|---|---|
| committer | Gauvain Pocentek <gauvain@pocentek.net> | 2018-05-19 17:10:08 +0200 |
| commit | fe89b949922c028830dd49095432ba627d330186 (patch) | |
| tree | aa14611d11c550a790d7976f2b24fabb77e81a67 /gitlab/tests | |
| parent | 701169441194bf0441cee13f2ab5784ffad7a207 (diff) | |
| download | gitlab-fe89b949922c028830dd49095432ba627d330186.tar.gz | |
Drop API v3 support
Drop the code, the tests, and update the documentation.
Diffstat (limited to 'gitlab/tests')
| -rw-r--r-- | gitlab/tests/test_cli.py | 43 | ||||
| -rw-r--r-- | gitlab/tests/test_gitlab.py | 615 | ||||
| -rw-r--r-- | gitlab/tests/test_gitlabobject.py | 500 | ||||
| -rw-r--r-- | gitlab/tests/test_manager.py | 309 |
4 files changed, 13 insertions, 1454 deletions
diff --git a/gitlab/tests/test_cli.py b/gitlab/tests/test_cli.py index a39ef96..034beed 100644 --- a/gitlab/tests/test_cli.py +++ b/gitlab/tests/test_cli.py @@ -23,14 +23,12 @@ import argparse import os import tempfile -import six try: import unittest except ImportError: import unittest2 as unittest from gitlab import cli -import gitlab.v3.cli import gitlab.v4.cli @@ -121,44 +119,3 @@ class TestV4CLI(unittest.TestCase): actions = user_subparsers.choices['create']._option_string_actions self.assertFalse(actions['--description'].required) self.assertTrue(actions['--name'].required) - - -class TestV3CLI(unittest.TestCase): - def test_parse_args(self): - parser = cli._get_parser(gitlab.v3.cli) - args = parser.parse_args(['project', 'list']) - self.assertEqual(args.what, 'project') - self.assertEqual(args.action, 'list') - - def test_parser(self): - parser = cli._get_parser(gitlab.v3.cli) - subparsers = None - for action in parser._actions: - if type(action) == argparse._SubParsersAction: - subparsers = action - break - self.assertIsNotNone(subparsers) - self.assertIn('user', subparsers.choices) - - user_subparsers = None - for action in subparsers.choices['user']._actions: - if type(action) == argparse._SubParsersAction: - user_subparsers = action - break - self.assertIsNotNone(user_subparsers) - self.assertIn('list', user_subparsers.choices) - self.assertIn('get', user_subparsers.choices) - self.assertIn('delete', user_subparsers.choices) - self.assertIn('update', user_subparsers.choices) - self.assertIn('create', user_subparsers.choices) - self.assertIn('block', user_subparsers.choices) - self.assertIn('unblock', user_subparsers.choices) - - actions = user_subparsers.choices['create']._option_string_actions - self.assertFalse(actions['--twitter'].required) - self.assertTrue(actions['--username'].required) - - def test_extra_actions(self): - for cls, data in six.iteritems(gitlab.v3.cli.EXTRA_ACTIONS): - for key in data: - self.assertIsInstance(data[key], dict) diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py index 1a1f3d8..daa2694 100644 --- a/gitlab/tests/test_gitlab.py +++ b/gitlab/tests/test_gitlab.py @@ -28,10 +28,10 @@ from httmock import HTTMock # noqa from httmock import response # noqa from httmock import urlmatch # noqa import requests -import six import gitlab from gitlab import * # noqa +from gitlab.v4.objects import * # noqa class TestSanitize(unittest.TestCase): @@ -49,130 +49,6 @@ class TestSanitize(unittest.TestCase): self.assertEqual(expected, gitlab._sanitize(source)) -class TestGitlabRawMethods(unittest.TestCase): - def setUp(self): - self.gl = Gitlab("http://localhost", private_token="private_token", - email="testuser@test.com", password="testpassword", - ssl_verify=True, api_version=3) - - @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_raw_get_unknown_path(self): - - @urlmatch(scheme="http", netloc="localhost", - path="/api/v3/unknown_path", - 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): - resp = self.gl._raw_get("/unknown_path") - self.assertEqual(resp.status_code, 404) - - def test_raw_get_without_kwargs(self): - with HTTMock(self.resp_get): - resp = self.gl._raw_get("/known_path") - self.assertEqual(resp.content, b'response') - self.assertEqual(resp.status_code, 200) - - def test_raw_get_with_kwargs(self): - with HTTMock(self.resp_get): - resp = self.gl._raw_get("/known_path", sudo="testing") - self.assertEqual(resp.content, b'response') - self.assertEqual(resp.status_code, 200) - - def test_raw_post(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._raw_post("/known_path") - self.assertEqual(resp.content, b'response') - self.assertEqual(resp.status_code, 200) - - def test_raw_post_unknown_path(self): - - @urlmatch(scheme="http", netloc="localhost", - path="/api/v3/unknown_path", - 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): - resp = self.gl._raw_post("/unknown_path") - self.assertEqual(resp.status_code, 404) - - def test_raw_put(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._raw_put("/known_path") - self.assertEqual(resp.content, b'response') - self.assertEqual(resp.status_code, 200) - - def test_raw_put_unknown_path(self): - - @urlmatch(scheme="http", netloc="localhost", - path="/api/v3/unknown_path", - method="put") - 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): - resp = self.gl._raw_put("/unknown_path") - self.assertEqual(resp.status_code, 404) - - def test_raw_delete(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._raw_delete("/known_path") - self.assertEqual(resp.content, b'response') - self.assertEqual(resp.status_code, 200) - - def test_raw_delete_unknown_path(self): - - @urlmatch(scheme="http", netloc="localhost", - path="/api/v3/unknown_path", - method="delete") - 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): - resp = self.gl._raw_delete("/unknown_path") - self.assertEqual(resp.status_code, 404) - - class TestGitlabList(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token", @@ -450,441 +326,6 @@ class TestGitlabHttpMethods(unittest.TestCase): '/not_there') -class TestGitlabMethods(unittest.TestCase): - def setUp(self): - self.gl = Gitlab("http://localhost", private_token="private_token", - email="testuser@test.com", password="testpassword", - ssl_verify=True, api_version=3) - - 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_next_link(self): - @urlmatch(scheme="http", netloc="localhost", - path='/api/v3/projects/1/repository/branches', method="get") - def resp_one(url, request): - """First request: - - http://localhost/api/v3/projects/1/repository/branches?per_page=1 - """ - headers = { - 'content-type': 'application/json', - 'link': '<http://localhost/api/v3/projects/1/repository/branc' - 'hes?page=2&per_page=0>; rel="next", <http://localhost/api/v3' - '/projects/1/repository/branches?page=2&per_page=0>; rel="las' - 't", <http://localhost/api/v3/projects/1/repository/branches?' - 'page=1&per_page=0>; rel="first"' - } - content = ('[{"branch_name": "otherbranch", ' - '"project_id": 1, "ref": "b"}]').encode("utf-8") - resp = response(200, content, headers, None, 5, request) - return resp - - @urlmatch(scheme="http", netloc="localhost", - path='/api/v3/projects/1/repository/branches', method="get", - query=r'.*page=2.*') - def resp_two(url, request): - headers = { - 'content-type': 'application/json', - 'link': '<http://localhost/api/v3/projects/1/repository/branc' - 'hes?page=1&per_page=0>; rel="prev", <http://localhost/api/v3' - '/projects/1/repository/branches?page=2&per_page=0>; rel="las' - 't", <http://localhost/api/v3/projects/1/repository/branches?' - 'page=1&per_page=0>; rel="first"' - } - content = ('[{"branch_name": "testbranch", ' - '"project_id": 1, "ref": "a"}]').encode("utf-8") - resp = response(200, content, headers, None, 5, request) - return resp - - with HTTMock(resp_two, resp_one): - data = self.gl.list(ProjectBranch, project_id=1, per_page=1, - all=True) - self.assertEqual(data[1].branch_name, "testbranch") - self.assertEqual(data[1].project_id, 1) - self.assertEqual(data[1].ref, "a") - self.assertEqual(data[0].branch_name, "otherbranch") - self.assertEqual(data[0].project_id, 1) - self.assertEqual(data[0].ref, "b") - self.assertEqual(len(data), 2) - - def test_list_recursion_limit_caught(self): - @urlmatch(scheme="http", netloc="localhost", - path='/api/v3/projects/1/repository/branches', method="get") - def resp_one(url, request): - """First request: - - http://localhost/api/v3/projects/1/repository/branches?per_page=1 - """ - headers = { - 'content-type': 'application/json', - 'link': '<http://localhost/api/v3/projects/1/repository/branc' - 'hes?page=2&per_page=0>; rel="next", <http://localhost/api/v3' - '/projects/1/repository/branches?page=2&per_page=0>; rel="las' - 't", <http://localhost/api/v3/projects/1/repository/branches?' - 'page=1&per_page=0>; rel="first"' - } - content = ('[{"branch_name": "otherbranch", ' - '"project_id": 1, "ref": "b"}]').encode("utf-8") - resp = response(200, content, headers, None, 5, request) - return resp - - @urlmatch(scheme="http", netloc="localhost", - path='/api/v3/projects/1/repository/branches', method="get", - query=r'.*page=2.*') - def resp_two(url, request): - # Mock a runtime error - raise RuntimeError("maximum recursion depth exceeded") - - with HTTMock(resp_two, resp_one): - data = self.gl.list(ProjectBranch, project_id=1, per_page=1, - safe_all=True) - self.assertEqual(data[0].branch_name, "otherbranch") - self.assertEqual(data[0].project_id, 1) - self.assertEqual(data[0].ref, "b") - self.assertEqual(len(data), 1) - - def test_list_recursion_limit_not_caught(self): - @urlmatch(scheme="http", netloc="localhost", - path='/api/v3/projects/1/repository/branches', method="get") - def resp_one(url, request): - """First request: - - http://localhost/api/v3/projects/1/repository/branches?per_page=1 - """ - headers = { - 'content-type': 'application/json', - 'link': '<http://localhost/api/v3/projects/1/repository/branc' - 'hes?page=2&per_page=0>; rel="next", <http://localhost/api/v3' - '/projects/1/repository/branches?page=2&per_page=0>; rel="las' - 't", <http://localhost/api/v3/projects/1/repository/branches?' - 'page=1&per_page=0>; rel="first"' - } - content = ('[{"branch_name": "otherbranch", ' - '"project_id": 1, "ref": "b"}]').encode("utf-8") - resp = response(200, content, headers, None, 5, request) - return resp - - @urlmatch(scheme="http", netloc="localhost", - path='/api/v3/projects/1/repository/branches', method="get", - query=r'.*page=2.*') - def resp_two(url, request): - # Mock a runtime error - raise RuntimeError("maximum recursion depth exceeded") - - with HTTMock(resp_two, resp_one): - with six.assertRaisesRegex(self, GitlabError, - "(maximum recursion depth exceeded)"): - self.gl.list(ProjectBranch, project_id=1, per_page=1, all=True) - - 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(GitlabListError, 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.gl._url = 'http://localhost:66000/api/v3' - 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): - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/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, Group, 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_from_object(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_from_invalid_class(self): - class InvalidClass(object): - pass - - self.assertRaises(GitlabError, self.gl.delete, InvalidClass, 1) - - def test_delete_from_class(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) - - with HTTMock(resp_delete_group): - data = self.gl.delete(Group, 1) - self.assertIs(data, True) - - def test_delete_unknown_path(self): - obj = Project(self.gl, data={"name": "testname", "id": 1}) - obj._from_api = True - - @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(404, content, headers, None, 5, request) - - with HTTMock(resp_cont): - self.assertRaises(GitlabDeleteError, 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 = Project(self.gl, data={"name": "name"}) - obj.id = 1 - obj._from_api = True - - @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(404, content, headers, None, 5, request) - - with HTTMock(resp_cont): - self.assertRaises(GitlabDeleteError, self.gl.delete, 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 = Hook(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}) - - @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(404, content, headers, None, 5, request) - - with HTTMock(resp_cont): - self.assertRaises(GitlabUpdateError, self.gl.update, obj) - - class TestGitlabAuth(unittest.TestCase): def test_invalid_auth_args(self): self.assertRaises(ValueError, @@ -938,7 +379,7 @@ class TestGitlab(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token", email="testuser@test.com", password="testpassword", - ssl_verify=True, api_version=3) + ssl_verify=True, api_version=4) def test_pickability(self): original_gl_objects = self.gl._objects @@ -952,7 +393,7 @@ class TestGitlab(unittest.TestCase): self.gl.email = None self.gl.password = None - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/session", + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/session", method="post") def resp_cont(url, request): headers = {'content-type': 'application/json'} @@ -960,11 +401,10 @@ class TestGitlab(unittest.TestCase): return response(404, content, headers, None, 5, request) with HTTMock(resp_cont): - self.assertRaises(GitlabAuthenticationError, - self.gl._credentials_auth) + self.assertRaises(GitlabHttpError, self.gl._credentials_auth) def test_credentials_auth_notok(self): - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/session", + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/session", method="post") def resp_cont(url, request): headers = {'content-type': 'application/json'} @@ -972,8 +412,7 @@ class TestGitlab(unittest.TestCase): return response(404, content, headers, None, 5, request) with HTTMock(resp_cont): - self.assertRaises(GitlabAuthenticationError, - self.gl._credentials_auth) + self.assertRaises(GitlabHttpError, self.gl._credentials_auth) def test_auth_with_credentials(self): self.gl.private_token = None @@ -989,7 +428,7 @@ class TestGitlab(unittest.TestCase): id_ = 1 expected = {"PRIVATE-TOKEN": token} - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/session", + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/session", method="post") def resp_cont(url, request): headers = {'content-type': 'application/json'} @@ -1009,7 +448,7 @@ class TestGitlab(unittest.TestCase): name = "username" id_ = 1 - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/user", + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/user", method="get") def resp_cont(url, request): headers = {'content-type': 'application/json'} @@ -1024,7 +463,7 @@ class TestGitlab(unittest.TestCase): self.assertEqual(type(self.gl.user), CurrentUser) def test_hooks(self): - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/hooks/1", + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/hooks/1", method="get") def resp_get_hook(url, request): headers = {'content-type': 'application/json'} @@ -1038,7 +477,7 @@ class TestGitlab(unittest.TestCase): self.assertEqual(data.id, 1) def test_projects(self): - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1", + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/1", method="get") def resp_get_project(url, request): headers = {'content-type': 'application/json'} @@ -1051,20 +490,8 @@ class TestGitlab(unittest.TestCase): self.assertEqual(data.name, "name") self.assertEqual(data.id, 1) - def test_userprojects(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(NotImplementedError, self.gl.user_projects.get, - 1, user_id=2) - def test_groups(self): - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1", + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1", method="get") def resp_get_group(url, request): headers = {'content-type': 'application/json'} @@ -1080,7 +507,7 @@ class TestGitlab(unittest.TestCase): self.assertEqual(data.id, 1) def test_issues(self): - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/issues", + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/issues", method="get") def resp_get_issue(url, request): headers = {'content-type': 'application/json'} @@ -1095,7 +522,7 @@ class TestGitlab(unittest.TestCase): self.assertEqual(data.name, 'other_name') def test_users(self): - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/users/1", + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/users/1", method="get") def resp_get_user(url, request): headers = {'content-type': 'application/json'} @@ -1109,19 +536,3 @@ class TestGitlab(unittest.TestCase): self.assertEqual(type(user), User) self.assertEqual(user.name, "name") self.assertEqual(user.id, 1) - - def test_teams(self): - @urlmatch(scheme="http", netloc="localhost", - path="/api/v3/user_teams/1", method="get") - def resp_get_group(url, request): - headers = {'content-type': 'application/json'} - content = '{"name": "name", "id": 1, "path": "path"}' - content = content.encode('utf-8') - return response(200, content, headers, None, 5, request) - - with HTTMock(resp_get_group): - data = self.gl.teams.get(1) - self.assertEqual(type(data), Team) - self.assertEqual(data.name, "name") - self.assertEqual(data.path, "path") - self.assertEqual(data.id, 1) diff --git a/gitlab/tests/test_gitlabobject.py b/gitlab/tests/test_gitlabobject.py deleted file mode 100644 index 844ba9e..0000000 --- a/gitlab/tests/test_gitlabobject.py +++ /dev/null @@ -1,500 +0,0 @@ -#!/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 -from __future__ import absolute_import - -import json -import pickle -try: - import unittest -except ImportError: - import unittest2 as unittest - -from httmock import HTTMock # noqa -from httmock import response # noqa -from httmock import urlmatch # noqa - -from gitlab import * # noqa - - -@urlmatch(scheme="http", netloc="localhost", path="/api/v4/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/v4/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/v4/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/v4/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/v4/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/v4/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/v4/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/v4/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/v4/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/v4/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/v4/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/v4/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/v4/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(unittest.TestCase): - - def setUp(self): - self.gl = Gitlab("http://localhost", private_token="private_token", - email="testuser@test.com", password="testpassword", - ssl_verify=True) - - def test_json(self): - gl_object = CurrentUser(self.gl, data={"username": "testname"}) - json_str = gl_object.json() - data = json.loads(json_str) - self.assertIn("id", data) - self.assertEqual(data["username"], "testname") - self.assertEqual(data["gitlab"]["url"], "http://localhost/api/v4") - - def test_pickability(self): - gl_object = CurrentUser(self.gl, data={"username": "testname"}) - original_obj_module = gl_object._module - pickled = pickle.dumps(gl_object) - unpickled = pickle.loads(pickled) - self.assertIsInstance(unpickled, CurrentUser) - self.assertTrue(hasattr(unpickled, '_module')) - self.assertEqual(unpickled._module, original_obj_module) - - def test_data_for_gitlab(self): - class FakeObj1(GitlabObject): - _url = '/fake1' - requiredCreateAttrs = ['create_req'] - optionalCreateAttrs = ['create_opt'] - requiredUpdateAttrs = ['update_req'] - optionalUpdateAttrs = ['update_opt'] - - class FakeObj2(GitlabObject): - _url = '/fake2' - requiredCreateAttrs = ['create_req'] - optionalCreateAttrs = ['create_opt'] - - obj1 = FakeObj1(self.gl, {'update_req': 1, 'update_opt': 1, - 'create_req': 1, 'create_opt': 1}) - obj2 = FakeObj2(self.gl, {'create_req': 1, 'create_opt': 1}) - - obj1_data = json.loads(obj1._data_for_gitlab()) - self.assertIn('create_req', obj1_data) - self.assertIn('create_opt', obj1_data) - self.assertNotIn('update_req', obj1_data) - self.assertNotIn('update_opt', obj1_data) - self.assertNotIn('gitlab', obj1_data) - - obj1_data = json.loads(obj1._data_for_gitlab(update=True)) - self.assertNotIn('create_req', obj1_data) - self.assertNotIn('create_opt', obj1_data) - self.assertIn('update_req', obj1_data) - self.assertIn('update_opt', obj1_data) - - obj1_data = json.loads(obj1._data_for_gitlab( - extra_parameters={'foo': 'bar'})) - self.assertIn('foo', obj1_data) - self.assertEqual(obj1_data['foo'], 'bar') - - obj2_data = json.loads(obj2._data_for_gitlab(update=True)) - self.assertIn('create_req', obj2_data) - self.assertIn('create_opt', obj2_data) - - 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_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._from_api = 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}) - obj._from_api = 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_set_from_dict_BooleanTrue(self): - obj = Project(self.gl, data={"name": "testname"}) - data = {"issues_enabled": True} - obj._set_from_dict(data) - self.assertIs(obj.issues_enabled, True) - - def test_set_from_dict_BooleanFalse(self): - obj = Project(self.gl, data={"name": "testname"}) - data = {"issues_enabled": False} - obj._set_from_dict(data) - self.assertIs(obj.issues_enabled, False) - - def test_set_from_dict_None(self): - obj = Project(self.gl, data={"name": "testname"}) - data = {"issues_enabled": None} - obj._set_from_dict(data) - self.assertIsNone(obj.issues_enabled) - - -class TestGroup(unittest.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(unittest.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_protect_branch): - self.obj.protect(True) - self.assertIs(self.obj.protected, True) - self.assertEqual(True, self.obj.protected) - with HTTMock(resp_unprotect_branch): - self.obj.protect(False) - self.assertRaises(AttributeError, getattr, self.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(unittest.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/v4/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/v4/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/v4/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/v4/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): - 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(unittest.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/v4/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/v4/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) - - -class TestSnippet(unittest.TestCase): - def setUp(self): - self.gl = Gitlab("http://localhost", private_token="private_token", - email="testuser@test.com", password="testpassword", - ssl_verify=True) - self.obj = Snippet(self.gl, data={"id": 3}) - - @urlmatch(scheme="http", netloc="localhost", - path="/api/v4/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/v4/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.raw() - self.assertEqual(content, data) - - def test_blob_fail(self): - with HTTMock(self.resp_content_fail): - self.assertRaises(GitlabGetError, self.obj.raw) diff --git a/gitlab/tests/test_manager.py b/gitlab/tests/test_manager.py deleted file mode 100644 index c6ef299..0000000 --- a/gitlab/tests/test_manager.py +++ /dev/null @@ -1,309 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2016-2017 Gauvain Pocentek <gauvain@pocentek.net> -# -# 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/>. - -try: - import unittest -except ImportError: - import unittest2 as unittest - -from httmock import HTTMock # noqa -from httmock import response # noqa -from httmock import urlmatch # noqa - -from gitlab import * # noqa -from gitlab.v3.objects import BaseManager # noqa - - -class FakeChildObject(GitlabObject): - _url = "/fake/%(parent_id)s/fakechild" - requiredCreateAttrs = ['name'] - requiredUrlAttrs = ['parent_id'] - - -class FakeChildManager(BaseManager): - obj_cls = FakeChildObject - - -class FakeObject(GitlabObject): - _url = "/fake" - requiredCreateAttrs = ['name'] - managers = [('children', FakeChildManager, [('parent_id', 'id')])] - - -class FakeObjectManager(BaseManager): - obj_cls = FakeObject - - -class TestGitlabManager(unittest.TestCase): - def setUp(self): - self.gitlab = Gitlab("http://localhost", private_token="private_token", - email="testuser@test.com", - password="testpassword", ssl_verify=True, - api_version=3) - - def test_set_parent_args(self): - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/fake", - method="POST") - def resp_create(url, request): - headers = {'content-type': 'application/json'} - content = '{"id": 1, "name": "name"}'.encode("utf-8") - return response(201, content, headers, None, 5, request) - - mgr = FakeChildManager(self.gitlab) - args = mgr._set_parent_args(name="name") - self.assertEqual(args, {"name": "name"}) - - with HTTMock(resp_create): - o = FakeObjectManager(self.gitlab).create({"name": "name"}) - args = o.children._set_parent_args(name="name") - self.assertEqual(args, {"name": "name", "parent_id": 1}) - - def test_constructor(self): - self.assertRaises(AttributeError, BaseManager, self.gitlab) - - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/fake/1", - method="get") - def resp_get(url, request): - headers = {'content-type': 'application/json'} - content = '{"id": 1, "name": "fake_name"}'.encode("utf-8") - return response(200, content, headers, None, 5, request) - - with HTTMock(resp_get): - mgr = FakeObjectManager(self.gitlab) - fake_obj = mgr.get(1) - self.assertEqual(fake_obj.id, 1) - self.assertEqual(fake_obj.name, "fake_name") - self.assertEqual(mgr.gitlab, self.gitlab) - self.assertEqual(mgr.args, []) - self.assertEqual(mgr.parent, None) - - self.assertIsInstance(fake_obj.children, FakeChildManager) - self.assertEqual(fake_obj.children.gitlab, self.gitlab) - self.assertEqual(fake_obj.children.parent, fake_obj) - self.assertEqual(len(fake_obj.children.args), 1) - - fake_child = fake_obj.children.get(1) - self.assertEqual(fake_child.id, 1) - self.assertEqual(fake_child.name, "fake_name") - - def test_get(self): - mgr = FakeObjectManager(self.gitlab) - FakeObject.canGet = False - self.assertRaises(NotImplementedError, mgr.get, 1) - - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/fake/1", - method="get") - def resp_get(url, request): - headers = {'content-type': 'application/json'} - content = '{"id": 1, "name": "fake_name"}'.encode("utf-8") - return response(200, content, headers, None, 5, request) - - with HTTMock(resp_get): - FakeObject.canGet = True - mgr = FakeObjectManager(self.gitlab) - fake_obj = mgr.get(1) - self.assertIsInstance(fake_obj, FakeObject) - self.assertEqual(fake_obj.id, 1) - self.assertEqual(fake_obj.name, "fake_name") - - def test_list(self): - mgr = FakeObjectManager(self.gitlab) - FakeObject.canList = False - self.assertRaises(NotImplementedError, mgr.list) - - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/fake", - method="get") - def resp_get(url, request): - headers = {'content-type': 'application/json'} - content = ('[{"id": 1, "name": "fake_name1"},' - '{"id": 2, "name": "fake_name2"}]') - content = content.encode("utf-8") - return response(200, content, headers, None, 5, request) - - with HTTMock(resp_get): - FakeObject.canList = True - mgr = FakeObjectManager(self.gitlab) - fake_list = mgr.list() - self.assertEqual(len(fake_list), 2) - self.assertIsInstance(fake_list[0], FakeObject) - self.assertEqual(fake_list[0].id, 1) - self.assertEqual(fake_list[0].name, "fake_name1") - self.assertIsInstance(fake_list[1], FakeObject) - self.assertEqual(fake_list[1].id, 2) - self.assertEqual(fake_list[1].name, "fake_name2") - - def test_create(self): - mgr = FakeObjectManager(self.gitlab) - FakeObject.canCreate = False - self.assertRaises(NotImplementedError, mgr.create, {'name': 'name'}) - - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/fake", - method="post") - def resp_post(url, request): - headers = {'content-type': 'application/json'} - data = '{"name": "fake_name"}' - content = '{"id": 1, "name": "fake_name"}'.encode("utf-8") - return response(201, content, headers, data, 5, request) - - with HTTMock(resp_post): - FakeObject.canCreate = True - mgr = FakeObjectManager(self.gitlab) - fake_obj = mgr.create({'name': 'fake_name'}) - self.assertIsInstance(fake_obj, FakeObject) - self.assertEqual(fake_obj.id, 1) - self.assertEqual(fake_obj.name, "fake_name") - - def test_project_manager_owned(self): - mgr = ProjectManager(self.gitlab) - - @urlmatch(scheme="http", netloc="localhost", - path="/api/v3/projects/owned", method="get") - def resp_get_all(url, request): - headers = {'content-type': 'application/json'} - content = ('[{"name": "name1", "id": 1}, ' - '{"name": "name2", "id": 2}]') - content = content.encode("utf-8") - return response(200, content, headers, None, 5, request) - - with HTTMock(resp_get_all): - data = mgr.owned() - self.assertEqual(type(data), list) - self.assertEqual(2, len(data)) - self.assertEqual(type(data[0]), Project) - self.assertEqual(type(data[1]), Project) - self.assertEqual(data[0].name, "name1") - self.assertEqual(data[1].name, "name2") - self.assertEqual(data[0].id, 1) - self.assertEqual(data[1].id, 2) - - def test_project_manager_all(self): - mgr = ProjectManager(self.gitlab) - - @urlmatch(scheme="http", netloc="localhost", - path="/api/v3/projects/all", method="get") - def resp_get_all(url, request): - headers = {'content-type': 'application/json'} - content = ('[{"name": "name1", "id": 1}, ' - '{"name": "name2", "id": 2}]') - content = content.encode("utf-8") - return response(200, content, headers, None, 5, request) - - with HTTMock(resp_get_all): - data = mgr.all() - self.assertEqual(type(data), list) - self.assertEqual(2, len(data)) - self.assertEqual(type(data[0]), Project) - self.assertEqual(type(data[1]), Project) - self.assertEqual(data[0].name, "name1") - self.assertEqual(data[1].name, "name2") - self.assertEqual(data[0].id, 1) - self.assertEqual(data[1].id, 2) - - def test_project_manager_search(self): - mgr = ProjectManager(self.gitlab) - - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects", - query="search=foo", method="get") - def resp_get_all(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_all): - data = mgr.list(search='foo') - self.assertEqual(type(data), list) - self.assertEqual(2, len(data)) - self.assertEqual(type(data[0]), Project) - self.assertEqual(type(data[1]), Project) - 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_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_user_manager_get_by_username(self): - mgr = UserManager(self.gitlab) - - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/users", - query="username=foo", method="get") - def resp_get_username(url, request): - headers = {'content-type': 'application/json'} - content = '[{"name": "foo", "id": 1}]'.encode("utf-8") - return response(200, content, headers, None, 5, request) - - with HTTMock(resp_get_username): - data = mgr.get_by_username('foo') - self.assertEqual(type(data), User) - self.assertEqual(data.name, "foo") - self.assertEqual(data.id, 1) - - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/users", - query="username=foo", method="get") - def resp_get_username_nomatch(url, request): - headers = {'content-type': 'application/json'} - content = '[]'.encode("utf-8") - return response(200, content, headers, None, 5, request) - - with HTTMock(resp_get_username_nomatch): - self.assertRaises(GitlabGetError, mgr.get_by_username, 'foo') - - def test_group_manager_search(self): - mgr = GroupManager(self.gitlab) - - @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups", - 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]), Group) - self.assertEqual(type(data[1]), Group) - self.assertEqual(data[0].name, "foo1") - self.assertEqual(data[1].name, "foo2") - self.assertEqual(data[0].id, 1) - self.assertEqual(data[1].id, 2) |
