|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2016 Gauvain Pocentek <gauvain@pocentek.net> |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU Lesser General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +try: |
| 19 | +importunittest |
| 20 | +exceptImportError: |
| 21 | +importunittest2asunittest |
| 22 | + |
| 23 | +fromhttmockimportHTTMock# noqa |
| 24 | +fromhttmockimportresponse# noqa |
| 25 | +fromhttmockimporturlmatch# noqa |
| 26 | + |
| 27 | +fromgitlabimport*# noqa |
| 28 | +fromgitlab.objectsimportBaseManager# noqa |
| 29 | + |
| 30 | + |
| 31 | +classFakeChildObject(GitlabObject): |
| 32 | +_url="/fake" |
| 33 | + |
| 34 | + |
| 35 | +classFakeChildManager(BaseManager): |
| 36 | +obj_cls=FakeChildObject |
| 37 | + |
| 38 | + |
| 39 | +classFakeObject(GitlabObject): |
| 40 | +_url="/fake" |
| 41 | +managers= [('children',FakeChildManager, [('child_id','id')])] |
| 42 | + |
| 43 | + |
| 44 | +classFakeObjectManager(BaseManager): |
| 45 | +obj_cls=FakeObject |
| 46 | + |
| 47 | + |
| 48 | +classTestGitlabManager(unittest.TestCase): |
| 49 | +defsetUp(self): |
| 50 | +self.gitlab=Gitlab("http://localhost",private_token="private_token", |
| 51 | +email="testuser@test.com", |
| 52 | +password="testpassword",ssl_verify=True) |
| 53 | + |
| 54 | +deftest_constructor(self): |
| 55 | +self.assertRaises(AttributeError,BaseManager,self.gitlab) |
| 56 | + |
| 57 | +@urlmatch(scheme="http",netloc="localhost",path="/api/v3/fake/1", |
| 58 | +method="get") |
| 59 | +defresp_get(url,request): |
| 60 | +headers= {'content-type':'application/json'} |
| 61 | +content='{"id": 1, "name": "fake_name"}'.encode("utf-8") |
| 62 | +returnresponse(200,content,headers,None,5,request) |
| 63 | + |
| 64 | +withHTTMock(resp_get): |
| 65 | +mgr=FakeObjectManager(self.gitlab) |
| 66 | +fake_obj=mgr.get(1) |
| 67 | +self.assertEqual(fake_obj.id,1) |
| 68 | +self.assertEqual(fake_obj.name,"fake_name") |
| 69 | +self.assertEqual(mgr.gitlab,self.gitlab) |
| 70 | +self.assertEqual(mgr.args, []) |
| 71 | +self.assertEqual(mgr.parent,None) |
| 72 | + |
| 73 | +self.assertIsInstance(fake_obj.children,FakeChildManager) |
| 74 | +self.assertEqual(fake_obj.children.gitlab,self.gitlab) |
| 75 | +self.assertEqual(fake_obj.children.parent,fake_obj) |
| 76 | +self.assertEqual(len(fake_obj.children.args),1) |
| 77 | + |
| 78 | +fake_child=fake_obj.children.get(1) |
| 79 | +self.assertEqual(fake_child.id,1) |
| 80 | +self.assertEqual(fake_child.name,"fake_name") |
| 81 | + |
| 82 | +deftest_get(self): |
| 83 | +mgr=FakeObjectManager(self.gitlab) |
| 84 | +FakeObject.canGet=False |
| 85 | +self.assertRaises(NotImplementedError,mgr.get,1) |
| 86 | + |
| 87 | +@urlmatch(scheme="http",netloc="localhost",path="/api/v3/fake/1", |
| 88 | +method="get") |
| 89 | +defresp_get(url,request): |
| 90 | +headers= {'content-type':'application/json'} |
| 91 | +content='{"id": 1, "name": "fake_name"}'.encode("utf-8") |
| 92 | +returnresponse(200,content,headers,None,5,request) |
| 93 | + |
| 94 | +withHTTMock(resp_get): |
| 95 | +FakeObject.canGet=True |
| 96 | +mgr=FakeObjectManager(self.gitlab) |
| 97 | +fake_obj=mgr.get(1) |
| 98 | +self.assertIsInstance(fake_obj,FakeObject) |
| 99 | +self.assertEqual(fake_obj.id,1) |
| 100 | +self.assertEqual(fake_obj.name,"fake_name") |
| 101 | + |
| 102 | +deftest_list(self): |
| 103 | +mgr=FakeObjectManager(self.gitlab) |
| 104 | +FakeObject.canList=False |
| 105 | +self.assertRaises(NotImplementedError,mgr.list) |
| 106 | + |
| 107 | +@urlmatch(scheme="http",netloc="localhost",path="/api/v3/fake", |
| 108 | +method="get") |
| 109 | +defresp_get(url,request): |
| 110 | +headers= {'content-type':'application/json'} |
| 111 | +content= ('[{"id": 1, "name": "fake_name1"},' |
| 112 | +'{"id": 2, "name": "fake_name2"}]') |
| 113 | +content=content.encode("utf-8") |
| 114 | +returnresponse(200,content,headers,None,5,request) |
| 115 | + |
| 116 | +withHTTMock(resp_get): |
| 117 | +FakeObject.canList=True |
| 118 | +mgr=FakeObjectManager(self.gitlab) |
| 119 | +fake_list=mgr.list() |
| 120 | +self.assertEqual(len(fake_list),2) |
| 121 | +self.assertIsInstance(fake_list[0],FakeObject) |
| 122 | +self.assertEqual(fake_list[0].id,1) |
| 123 | +self.assertEqual(fake_list[0].name,"fake_name1") |
| 124 | +self.assertIsInstance(fake_list[1],FakeObject) |
| 125 | +self.assertEqual(fake_list[1].id,2) |
| 126 | +self.assertEqual(fake_list[1].name,"fake_name2") |
| 127 | + |
| 128 | +deftest_create(self): |
| 129 | +mgr=FakeObjectManager(self.gitlab) |
| 130 | +FakeObject.canCreate=False |
| 131 | +self.assertRaises(NotImplementedError,mgr.create, {'foo':'bar'}) |
| 132 | + |
| 133 | +@urlmatch(scheme="http",netloc="localhost",path="/api/v3/fake", |
| 134 | +method="post") |
| 135 | +defresp_post(url,request): |
| 136 | +headers= {'content-type':'application/json'} |
| 137 | +data='{"name": "fake_name"}' |
| 138 | +content='{"id": 1, "name": "fake_name"}'.encode("utf-8") |
| 139 | +returnresponse(201,content,headers,data,5,request) |
| 140 | + |
| 141 | +withHTTMock(resp_post): |
| 142 | +FakeObject.canCreate=True |
| 143 | +mgr=FakeObjectManager(self.gitlab) |
| 144 | +fake_obj=mgr.create({'name':'fake_name'}) |
| 145 | +self.assertIsInstance(fake_obj,FakeObject) |
| 146 | +self.assertEqual(fake_obj.id,1) |
| 147 | +self.assertEqual(fake_obj.name,"fake_name") |