diff options
author | Gauvain Pocentek <gauvain.pocentek@objectif-libre.com> | 2015-05-12 12:36:49 +0200 |
---|---|---|
committer | Gauvain Pocentek <gauvain.pocentek@objectif-libre.com> | 2015-05-12 12:36:49 +0200 |
commit | 0032d468b5dc93b5bf9e639f382b4c869c5ef14c (patch) | |
tree | 989c2df3dafb9e00ea5b62f0c0e084f54547b1d0 | |
parent | 8634a4dba13a42abb54b968896810ecbd264a2a3 (diff) | |
download | gitlab-0032d468b5dc93b5bf9e639f382b4c869c5ef14c.tar.gz |
make the tests pass
-rw-r--r-- | gitlab/__init__.py | 62 | ||||
-rw-r--r-- | gitlab/tests/test_gitlab.py | 187 | ||||
-rw-r--r-- | gitlab/tests/test_gitlabobject.py | 87 | ||||
-rw-r--r-- | tox.ini | 4 |
4 files changed, 218 insertions, 122 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 33245bf..09184cb 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -18,7 +18,7 @@ from __future__ import print_function from __future__ import division from __future__ import absolute_import -from itertools import chain +import itertools import json import sys import warnings @@ -179,13 +179,13 @@ class Gitlab(object): else: _raiseErrorFromResponse(r, GitlabAuthenticationError) - self.setToken(self.user.private_token) + self.set_token(self.user.private_token) def token_auth(self): self.user = CurrentUser(self) def setUrl(self, url): - """Updates the gitlab URL""" + """Updates the gitlab URL.""" self._url = '%s/api/v3' % url def constructUrl(self, id_, obj, parameters): @@ -205,13 +205,13 @@ class Gitlab(object): return request_headers def setToken(self, token): - """(DEPRECATED) Sets the private token for authentication""" + """(DEPRECATED) Sets the private token for authentication.""" warnings.warn("setToken is deprecated, use set_token instead", DeprecationWarning) self.set_token(token) def set_token(self, token): - """Sets the private token for authentication""" + """Sets the private token for authentication.""" self.private_token = token if token else None if token: self.headers["PRIVATE-TOKEN"] = token @@ -219,13 +219,14 @@ class Gitlab(object): del self.headers["PRIVATE-TOKEN"] def setCredentials(self, email, password): - """(DEPRECATED) Sets the email/login and password for authentication""" - warnings.warn("setToken is deprecated, use set_credentials instead", + """(DEPRECATED) Sets the login and password for authentication.""" + warnings.warn("setCredential is deprecated, use set_credentials " + "instead", DeprecationWarning) self.set_credentials(email, password) def set_credentials(self, email, password): - """Sets the email/login and password for authentication""" + """Sets the email/login and password for authentication.""" self.email = email self.password = password @@ -300,8 +301,8 @@ class Gitlab(object): def list(self, obj_class, **kwargs): missing = [] - for k in chain(obj_class.requiredUrlAttrs, - obj_class.requiredListAttrs): + for k in itertools.chain(obj_class.requiredUrlAttrs, + obj_class.requiredListAttrs): if k not in kwargs: missing.append(k) if missing: @@ -348,8 +349,8 @@ class Gitlab(object): def get(self, obj_class, id=None, **kwargs): missing = [] - for k in chain(obj_class.requiredUrlAttrs, - obj_class.requiredGetAttrs): + for k in itertools.chain(obj_class.requiredUrlAttrs, + obj_class.requiredGetAttrs): if k not in kwargs: missing.append(k) if missing: @@ -381,7 +382,8 @@ class Gitlab(object): params = obj.__dict__.copy() params.update(kwargs) missing = [] - for k in chain(obj.requiredUrlAttrs, obj.requiredDeleteAttrs): + for k in itertools.chain(obj.requiredUrlAttrs, + obj.requiredDeleteAttrs): if k not in params: missing.append(k) if missing: @@ -415,7 +417,8 @@ class Gitlab(object): params = obj.__dict__.copy() params.update(kwargs) missing = [] - for k in chain(obj.requiredUrlAttrs, obj.requiredCreateAttrs): + for k in itertools.chain(obj.requiredUrlAttrs, + obj.requiredCreateAttrs): if k not in params: missing.append(k) if missing: @@ -446,7 +449,8 @@ class Gitlab(object): params = obj.__dict__.copy() params.update(kwargs) missing = [] - for k in chain(obj.requiredUrlAttrs, obj.requiredCreateAttrs): + for k in itertools.chain(obj.requiredUrlAttrs, + obj.requiredCreateAttrs): if k not in params: missing.append(k) if missing: @@ -642,8 +646,8 @@ class GitlabObject(object): def _dataForGitlab(self, extra_parameters={}): data = {} - for attribute in chain(self.requiredCreateAttrs, - self.optionalCreateAttrs): + for attribute in itertools.chain(self.requiredCreateAttrs, + self.optionalCreateAttrs): if hasattr(self, attribute): data[attribute] = getattr(self, attribute) @@ -719,8 +723,8 @@ class GitlabObject(object): self._created = False self.gitlab = gl - if data is None or isinstance(data, six.integer_types) or\ - isinstance(data, six.string_types): + if (data is None or isinstance(data, six.integer_types) or + isinstance(data, six.string_types)): if not self.canGet: raise NotImplementedError data = self.gitlab.get(self.__class__, data, **kwargs) @@ -938,8 +942,8 @@ class ProjectCommit(GitlabObject): _raiseErrorFromResponse(r, GitlabGetError) def blob(self, filepath, **kwargs): - url = '/projects/%(project_id)s/repository/blobs/%(commit_id)s' % \ - {'project_id': self.project_id, 'commit_id': self.id} + url = ('/projects/%(project_id)s/repository/blobs/%(commit_id)s' % + {'project_id': self.project_id, 'commit_id': self.id}) url += '?filepath=%s' % filepath r = self.gitlab._raw_get(url, **kwargs) if r.status_code == 200: @@ -1115,8 +1119,8 @@ class ProjectSnippet(GitlabObject): shortPrintAttr = 'title' def Content(self, **kwargs): - url = "/projects/%(project_id)s/snippets/%(snippet_id)s/raw" % \ - {'project_id': self.project_id, 'snippet_id': self.id} + url = ("/projects/%(project_id)s/snippets/%(snippet_id)s/raw" % + {'project_id': self.project_id, 'snippet_id': self.id}) r = self.gitlab._raw_get(url, **kwargs) if r.status_code == 200: @@ -1271,24 +1275,24 @@ class Project(GitlabObject): GitlabConnectionError: Connection to GitLab-server failed """ url = "/projects/%s/repository/files" % self.id - url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \ - (path, branch, content, message) + url += ("?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % + (path, branch, content, message)) r = self.gitlab._raw_post(url, data=None, content_type=None, **kwargs) if r.status_code != 201: _raiseErrorFromResponse(r, GitlabCreateError) def update_file(self, path, branch, content, message, **kwargs): url = "/projects/%s/repository/files" % self.id - url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \ - (path, branch, content, message) + url += ("?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % + (path, branch, content, message)) r = self.gitlab._raw_put(url, data=None, content_type=None, **kwargs) if r.status_code != 200: _raiseErrorFromResponse(r, GitlabUpdateError) def delete_file(self, path, branch, message, **kwargs): url = "/projects/%s/repository/files" % self.id - url += "?file_path=%s&branch_name=%s&commit_message=%s" % \ - (path, branch, message) + url += ("?file_path=%s&branch_name=%s&commit_message=%s" % + (path, branch, message)) r = self.gitlab._raw_delete(url, **kwargs) if r.status_code != 200: _raiseErrorFromResponse(r, GitlabDeleteError) diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py index 76b1db6..0c7687c 100644 --- a/gitlab/tests/test_gitlab.py +++ b/gitlab/tests/test_gitlab.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2014 Mika Mäenpää <mika.j.maenpaa@tut.fi>, Tampere University of Technology +# 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 @@ -18,18 +19,18 @@ from __future__ import print_function try: - from unittest import TestCase, main + import unittest except ImportError: - from unittest2 import TestCase, main + import unittest2 as unittest -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 HTTMock # noqa +from httmock import response # noqa +from httmock import urlmatch # noqa -from httmock import response, HTTMock, urlmatch +from gitlab import * # noqa -class TestGitLabRawMethods(TestCase): + +class TestGitLabRawMethods(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token", email="testuser@test.com", password="testpassword", @@ -42,23 +43,33 @@ class TestGitLabRawMethods(TestCase): 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_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_rawGet_without_kwargs(self): + def test_raw_get_without_kwargs(self): with HTTMock(self.resp_get): - resp = self.gl.rawGet("/known_path") + resp = self.gl._raw_get("/known_path") self.assertEqual(resp.content, b'response') self.assertEqual(resp.status_code, 200) - def test_rawGet_with_kwargs(self): + def test_raw_get_with_kwargs(self): with HTTMock(self.resp_get): - resp = self.gl.rawGet("/known_path", sudo="testing") + resp = self.gl._raw_get("/known_path", sudo="testing") self.assertEqual(resp.content, b'response') self.assertEqual(resp.status_code, 200) - def test_rawPost(self): + def test_raw_post(self): @urlmatch(scheme="http", netloc="localhost", path="/api/v3/known_path", method="post") @@ -68,15 +79,25 @@ class TestGitLabRawMethods(TestCase): return response(200, content, headers, None, 5, request) with HTTMock(resp_post): - resp = self.gl.rawPost("/known_path") + resp = self.gl._raw_post("/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_raw_post_unknown_path(self): - def test_rawPut(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") @@ -86,15 +107,25 @@ class TestGitLabRawMethods(TestCase): return response(200, content, headers, None, 5, request) with HTTMock(resp_put): - resp = self.gl.rawPut("/known_path") + resp = self.gl._raw_put("/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_raw_put_unknown_path(self): - def test_rawDelete(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") @@ -104,27 +135,38 @@ class TestGitLabRawMethods(TestCase): return response(200, content, headers, None, 5, request) with HTTMock(resp_delete): - resp = self.gl.rawDelete("/known_path") + resp = self.gl._raw_delete("/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") + def test_raw_delete_unknown_path(self): -class TestGitLabMethods(TestCase): + @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 TestGitLabMethods(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_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") + content = ('[{"branch_name": "testbranch", ' + '"project_id": 1, "ref": "a"}]').encode("utf-8") return response(200, content, headers, None, 5, request) with HTTMock(resp_cont): @@ -157,7 +199,7 @@ class TestGitLabMethods(TestCase): return response(405, content, headers, None, 5, request) with HTTMock(resp_cont): - self.assertRaises(GitlabGetError, self.gl.list, + self.assertRaises(GitlabListError, self.gl.list, ProjectBranch, project_id=1) def test_list_kw_missing(self): @@ -181,7 +223,15 @@ class TestGitLabMethods(TestCase): self.assertEqual(expected, data) def test_get_unknown_path(self): - self.assertRaises(GitlabConnectionError, self.gl.get, Project, 1) + @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) @@ -218,7 +268,6 @@ class TestGitLabMethods(TestCase): 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) @@ -238,10 +287,21 @@ class TestGitLabMethods(TestCase): def test_delete_unknown_path(self): obj = Project(self.gl, data={"name": "testname", "id": 1}) - self.assertRaises(GitlabConnectionError, self.gl.delete, obj) + obj._created = 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): @@ -254,6 +314,7 @@ class TestGitLabMethods(TestCase): 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): @@ -291,6 +352,7 @@ class TestGitLabMethods(TestCase): 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): @@ -303,6 +365,7 @@ class TestGitLabMethods(TestCase): 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): @@ -328,7 +391,6 @@ class TestGitLabMethods(TestCase): 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"} @@ -369,10 +431,19 @@ class TestGitLabMethods(TestCase): def test_update_unknown_path(self): obj = Group(self.gl, data={"name": "testgroup", "path": "testpath", "id": 1}) - self.assertRaises(GitlabConnectionError, self.gl.update, obj) + + @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 TestGitLab(TestCase): +class TestGitLab(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token", @@ -383,22 +454,22 @@ class TestGitLab(TestCase): self.gl.setUrl("http://new_url") self.assertEqual(self.gl._url, "http://new_url/api/v3") - def test_setToken(self): + def test_set_token(self): token = "newtoken" expected = {"PRIVATE-TOKEN": token} - self.gl.setToken(token) + self.gl.set_token(token) self.assertEqual(self.gl.private_token, token) self.assertDictContainsSubset(expected, self.gl.headers) - def test_setCredentials(self): + def test_set_credentials(self): email = "credentialuser@test.com" password = "credentialpassword" - self.gl.setCredentials(email=email, password=password) + self.gl.set_credentials(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.gl.set_credentials(email=None, password=None) self.assertRaises(GitlabAuthenticationError, self.gl.credentials_auth) def test_credentials_auth_notok(self): @@ -414,7 +485,7 @@ class TestGitLab(TestCase): self.gl.credentials_auth) def test_auth_with_credentials(self): - self.gl.setToken(None) + self.gl.set_token(None) self.test_credentials_auth(callback=self.gl.auth) def test_auth_with_token(self): @@ -425,7 +496,8 @@ class TestGitLab(TestCase): callback = self.gl.credentials_auth token = "credauthtoken" id_ = 1 - expected = {"PRIVATE-TOKEN": token } + expected = {"PRIVATE-TOKEN": token} + @urlmatch(scheme="http", netloc="localhost", path="/api/v3/session", method="post") def resp_cont(url, request): @@ -434,7 +506,6 @@ class TestGitLab(TestCase): 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) @@ -446,12 +517,13 @@ class TestGitLab(TestCase): 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") + id_, name).encode("utf-8") return response(200, content, headers, None, 5, request) with HTTMock(resp_cont): @@ -469,7 +541,7 @@ class TestGitLab(TestCase): return response(200, content, headers, None, 5, request) with HTTMock(resp_cont): - projs = self.gl._getListOrObject(Project, None) + projs = Project._getListOrObject(self.gl, None) self.assertEqual(len(projs), 1) proj = projs[0] self.assertEqual(proj.id, 1) @@ -484,7 +556,7 @@ class TestGitLab(TestCase): return response(200, content, headers, None, 5, request) with HTTMock(resp_cont): - proj = self.gl._getListOrObject(Project, 1) + proj = Project._getListOrObject(self.gl, 1) self.assertEqual(proj.id, 1) self.assertEqual(proj.name, "testproject") @@ -525,7 +597,7 @@ class TestGitLab(TestCase): return response(200, content, headers, None, 5, request) with HTTMock(resp_get_userproject): - self.assertRaises(GitlabGetError, self.gl.UserProject, id=1, + self.assertRaises(NotImplementedError, self.gl.UserProject, id=1, user_id=2) def test_Group(self): @@ -533,7 +605,8 @@ class TestGitLab(TestCase): method="get") def resp_get_group(url, request): headers = {'content-type': 'application/json'} - content = '{"name": "name", "id": 1, "path": "path"}'.encode("utf-8") + 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): @@ -552,14 +625,16 @@ class TestGitLab(TestCase): return response(200, content, headers, None, 5, request) with HTTMock(resp_get_issue): - self.assertRaises(GitlabGetError, self.gl.Issue, id=1) + self.assertRaises(NotImplementedError, 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") + content = ('{"name": "name", "id": 1, "password": "password", ' + '"username": "username", "email": "email"}') + content = content.encode("utf-8") return response(200, content, headers, None, 5, request) with HTTMock(resp_get_user): diff --git a/gitlab/tests/test_gitlabobject.py b/gitlab/tests/test_gitlabobject.py index 9cc2d28..f260b18 100644 --- a/gitlab/tests/test_gitlabobject.py +++ b/gitlab/tests/test_gitlabobject.py @@ -1,7 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # -# Copyright (C) 2014 Mika Mäenpää <mika.j.maenpaa@tut.fi>, Tampere University of Technology +# 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 @@ -16,21 +17,21 @@ # 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 +from __future__ import print_function +from __future__ import division +from __future__ import absolute_import try: - from unittest import TestCase, main + import unittest except ImportError: - from unittest2 import TestCase, main + import unittest2 as unittest -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 HTTMock # noqa +from httmock import response # noqa +from httmock import urlmatch # noqa + +from gitlab import * # noqa -from httmock import response, HTTMock, urlmatch @urlmatch(scheme="http", netloc="localhost", path="/api/v3/projects/1", method="get") @@ -39,6 +40,7 @@ def resp_get_project(url, request): 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): @@ -46,6 +48,7 @@ def resp_list_project(url, request): 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): @@ -53,13 +56,16 @@ def resp_get_issue(url, request): 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") + 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): @@ -67,6 +73,7 @@ def resp_create_project(url, request): 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): @@ -74,6 +81,7 @@ def resp_create_groupmember(url, request): 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): @@ -81,6 +89,7 @@ def resp_get_projectsnippet(url, request): 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): @@ -88,20 +97,25 @@ def resp_delete_group(url, request): content = ''.encode("utf-8") return response(200, content, headers, None, 5, request) -@urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/2/projects/3", + +@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", + +@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") @@ -110,6 +124,7 @@ def resp_protect_branch(url, request): 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") @@ -118,6 +133,7 @@ def resp_unprotect_branch(url, request): 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") @@ -127,7 +143,7 @@ def resp_protect_branch_fail(url, request): return response(400, content, headers, None, 5, request) -class TestGitLabObject(TestCase): +class TestGitLabObject(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token", @@ -149,7 +165,7 @@ class TestGitLabObject(TestCase): 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) + data = gl_object._getListOrObject(self.gl, id=None) self.assertEqual(type(data), list) self.assertEqual(len(data), 1) self.assertEqual(type(data[0]), Project) @@ -159,26 +175,21 @@ class TestGitLabObject(TestCase): 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) + data = gl_object._getListOrObject(self.gl, 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"}) + gl_object = Issue(self.gl, data={"name": "name"}) self.assertRaises(NotImplementedError, gl_object._getListOrObject, - Issue, id=1) + self.gl, id=1) def test_getListOrObject_cantlist(self): - gl_object = Project(self.gl, data={"name": "name"}) + gl_object = CurrentUser(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={}) + self.gl, id=None) def test_getListOrObject_create(self): data = {"name": "name"} @@ -233,7 +244,7 @@ class TestGitLabObject(TestCase): "password": "password", "id": 1, "username": "username"}) self.assertEqual(obj.name, "testname") - obj.created = True + obj._created = True obj.name = "newname" with HTTMock(resp_update_user): obj.save() @@ -246,8 +257,8 @@ class TestGitLabObject(TestCase): self.assertEqual(obj.id, 1) def test_delete(self): - obj = Group(self.gl, data={"name": "testname", "id": 1, - "created": True}) + obj = Group(self.gl, data={"name": "testname", "id": 1}) + obj._created = True with HTTMock(resp_delete_group): data = obj.delete() self.assertIs(data, True) @@ -278,7 +289,8 @@ class TestGitLabObject(TestCase): obj._setFromDict(data) self.assertIsNone(obj.issues_enabled) -class TestGroup(TestCase): + +class TestGroup(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token", email="testuser@test.com", password="testpassword", @@ -298,7 +310,7 @@ class TestGroup(TestCase): obj.transfer_project, 3) -class TestProjectBranch(TestCase): +class TestProjectBranch(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token", email="testuser@test.com", password="testpassword", @@ -321,9 +333,13 @@ class TestProjectBranch(TestCase): 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, sef.obj, 'protected') + self.assertRaises(AttributeError, getattr, self.obj, 'protected') def test_protect_protect_fail(self): with HTTMock(resp_protect_branch_fail): @@ -335,7 +351,8 @@ class TestProjectBranch(TestCase): self.obj.unprotect() self.assertRaises(AttributeError, getattr, self.obj, 'protected') -class TestProjectCommit(TestCase): + +class TestProjectCommit(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token", email="testuser@test.com", password="testpassword", @@ -386,7 +403,6 @@ class TestProjectCommit(TestCase): def test_blob(self): with HTTMock(self.resp_blob): - data = {"json": 2} blob = self.obj.blob("testing") self.assertEqual(blob, b'blob') @@ -394,7 +410,8 @@ class TestProjectCommit(TestCase): with HTTMock(self.resp_blob_fail): self.assertRaises(GitlabGetError, self.obj.blob, "testing") -class TestProjectSnippet(TestCase): + +class TestProjectSnippet(unittest.TestCase): def setUp(self): self.gl = Gitlab("http://localhost", private_token="private_token", email="testuser@test.com", password="testpassword", @@ -1,7 +1,7 @@ [tox] minversion = 1.6 skipsdist = True -envlist = py27,py34,pep8 +envlist = py34,py27,pep8 [testenv] setenv = VIRTUAL_ENV={envdir} @@ -15,7 +15,7 @@ commands = [testenv:pep8] commands = - flake8 {posargs} gitlab gitlab.py + flake8 {posargs} gitlab bin [testenv:venv] commands = {posargs} |