summaryrefslogtreecommitdiff
path: root/gitlab/tests/test_manager.py
blob: 837e0136abb6c2eb0ad6ceed0c91d1b1fe8235d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 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.objects import BaseManager  # noqa


class FakeChildObject(GitlabObject):
    _url = "/fake"


class FakeChildManager(BaseManager):
    obj_cls = FakeChildObject


class FakeObject(GitlabObject):
    _url = "/fake"
    managers = [('children', FakeChildManager, [('child_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)

    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, {'foo': 'bar'})

        @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")