summaryrefslogtreecommitdiff
path: root/ick2/client_tests.py
blob: 856f4c339af44d4624220e63be81870a7bf9cba6 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Copyright (C) 2018  Lars Wirzenius
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import json
import unittest


import ick2


json_type = 'application/json'


class HttpAPITests(unittest.TestCase):

    def setUp(self):
        self.session = FakeHttpSession()
        self.session.token = 'SECRET-TOKEN'
        self.client = ick2.HttpAPI()
        self.client.set_session(self.session)
        self.client.set_token(self.session.token)

    def test_get_dict_raises_exception_on_error(self):
        self.session.response = FakeResponse(400)
        with self.assertRaises(ick2.HttpError):
            self.client.get_dict('http://controller/version')

    def test_get_dict_raises_exception_on_not_json(self):
        self.session.response = FakeResponse(200, body=json.dumps('{}'))
        with self.assertRaises(ick2.HttpError):
            self.client.get_dict('http://controller/version')

    def test_get_dict_raises_exception_on_malformed_json(self):
        self.session.response = FakeResponse(
            200, body='this is not really JSON', content_type=json_type)
        with self.assertRaises(ick2.HttpError):
            self.client.get_dict('http://controller/version')

    def test_get_dict_returns_response(self):
        version = {
            'version': '1.0',
        }

        self.session.response = FakeResponse(
            200, body=json.dumps(version), content_type=json_type)
        obj = self.client.get_dict('http://controller/version')
        self.assertEqual(obj, version)

    def test_get_blob_raises_exception_on_error(self):
        self.session.response = FakeResponse(404)
        with self.assertRaises(ick2.HttpError):
            self.client.get_blob('http://blobs/blob/foo')

    def test_get_blob_returns_response(self):
        blob = b'hello, world\n'
        self.session.response = FakeResponse(200, body=blob)
        obj = self.client.get_blob('http://blobs/blob/foo')
        self.assertEqual(obj, blob)

    def test_post_raises_exception_on_error(self):
        self.session.response = FakeResponse(400)
        with self.assertRaises(ick2.HttpError):
            self.client.post('http://controller/work', body='')

    def test_post_succeeds(self):
        work = {
            'project': 'foo',
            'stdout': 'hello, world\n',
        }

        self.session.response = FakeResponse(
            201, body=json.dumps(work), content_type=json_type)
        obj = self.client.post('http://controller/work', body=work)
        self.assertEqual(obj, None)

    def test_put_raises_exception_on_error(self):
        blob = b'fooblob'
        self.session.response = FakeResponse(400)
        with self.assertRaises(ick2.HttpError):
            self.client.put('http://blobs/blob/foo', body=blob)

    def test_put_succeeds(self):
        blob = b'fooblob'
        self.session.response = FakeResponse(201, body=None)
        obj = self.client.put('http://controller/work', body=blob)
        self.assertEqual(obj, None)


class ControllerClientTests(unittest.TestCase):

    def setUp(self):
        self.session = FakeHttpSession()
        self.session.token = 'SECRET-TOKEN'

        self.client = ick2.HttpAPI()
        self.client.set_session(self.session)
        self.client.set_token(self.session.token)

        self.controller = ick2.ControllerClient()
        self.controller.set_http_api(self.client)
        self.controller.set_controller_url('https://controller')
        self.controller.set_token('SECRET-TOKEN')
        self.controller.set_client_name('asterix')

    def test_register_succeeds_on_error(self):
        self.session.response = FakeResponse(400)
        self.assertEqual(self.controller.register(), None)

    def test_register_succeeds(self):
        self.session.response = FakeResponse(200)
        self.assertEqual(self.controller.register(), None)

    def test_get_work_raises_exception_on_error(self):
        self.session.response = FakeResponse(400)
        with self.assertRaises(ick2.HttpError):
            self.controller.get_work()

    def test_get_work_succeeds(self):
        work = {
            'build_id': 'rome/1'
        }
        self.session.response = FakeResponse(
            200, body=json.dumps(work), content_type=json_type)
        self.assertEqual(self.controller.get_work(), work)

    def test_report_work_raises_exception_on_error(self):
        work = {
            'stdout': 'hello, world',
        }
        self.session.response = FakeResponse(400)
        with self.assertRaises(ick2.HttpError):
            self.controller.report_work(work)

    def test_report_work_succeeds(self):
        work = {
            'stdout': 'hello, world',
        }
        self.session.response = FakeResponse(200)
        self.assertEqual(self.controller.report_work(work), None)

    def test_get_blob_service_url_raises_exception_on_error(self):
        self.session.response = FakeResponse(400)
        with self.assertRaises(ick2.HttpError):
            self.controller.get_blob_service_url()

    def test_get_blob_service_url_succeeds(self):
        url = 'https://blobs'
        version = {
            'blob_service': url,
        }
        self.session.response = FakeResponse(
            200, body=json.dumps(version), content_type=json_type)
        self.assertEqual(self.controller.get_blob_service_url(), url)


class FakeHttpSession:

    def __init__(self):
        self.response = None
        self.token = None

    def get(self, url, headers=None, verify=None):
        assert self.response is not None
        assert self.is_authorized(headers)
        return self.response

    def post(self, url, headers=None, data=None, verify=None):
        assert self.response is not None
        assert self.is_authorized(headers)
        return self.response

    def put(self, url, headers=None, data=None, verify=None):
        assert self.response is not None
        assert self.is_authorized(headers)
        return self.response

    def is_authorized(self, headers):
        assert self.token is not None
        assert 'Authorization' in headers
        v = headers['Authorization']
        return v == 'Bearer {}'.format(self.token)


class FakeResponse:

    def __init__(self, status_code, body=None, content_type=None):
        if content_type is None:
            content_type = 'application/octet-stream'

        self.status_code = status_code
        self.headers = {
            'Content-Type': content_type,
        }
        self.content = body

    @property
    def ok(self):
        return self.status_code in (200, 201)

    def json(self):
        return json.loads(self.content)