Coverage for tests/test_squash.py: 40%
87 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-28 03:06 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-28 03:06 -0700
1# This file is part of verify.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22import json
23import unittest
25import requests
26try:
27 # responses is not a formal LSST Stack dependency (yet), so we'll skip any
28 # tests that require it in environments that don't have it.
29 import responses
30except ImportError:
31 responses = None
33from lsst.verify import squash
36class GetDefaultTimeoutTestCase(unittest.TestCase):
38 def test_get_default_timeout(self):
39 self.assertEqual(
40 squash.get_default_timeout(),
41 squash._TIMEOUT
42 )
45class GetDefaultApiVersionTestCase(unittest.TestCase):
47 def test_get_default_api_version(self):
48 self.assertEqual(
49 squash.get_default_api_version(),
50 squash._API_VERSION
51 )
54class MakeAcceptHeaderTestCase(unittest.TestCase):
56 def test_make_accept_header(self):
57 self.assertEqual(
58 squash.make_accept_header(),
59 'application/json; version=' + squash.get_default_api_version()
60 )
62 self.assertEqual(
63 squash.make_accept_header(1.1),
64 'application/json; version=1.1'
65 )
68class GetEndpointUrlTestCase(unittest.TestCase):
70 def setUp(self):
71 self.api_url = 'https://example.com/api/'
72 self.endpoints = {
73 'jobs': 'https://example.com/api/jobs/'
74 }
75 squash.reset_endpoint_cache()
77 def tearDown(self):
78 squash.reset_endpoint_cache()
80 @unittest.skipIf(responses is None,
81 'Requires `responses` PyPI package')
82 def test_jobs(self):
83 with responses.RequestsMock() as reqmock:
84 reqmock.add(responses.GET,
85 self.api_url,
86 json=self.endpoints,
87 status=200,
88 content_type='application/json')
89 jobs_url = squash.get_endpoint_url(self.api_url, 'jobs')
91 self.assertEqual(jobs_url, self.endpoints['jobs'])
93 # call again, shouldn't register a new request
94 jobs_url = squash.get_endpoint_url(self.api_url, 'jobs')
96 self.assertEqual(jobs_url, self.endpoints['jobs'])
97 self.assertEqual(len(reqmock.calls), 1)
100class GetTestCase(unittest.TestCase):
102 def setUp(self):
103 self.api_url = 'https://example.com/api/'
104 # pre-set the _ENDPOINT_URLS to use the cache, not http get
105 squash._ENDPOINT_URLS = {
106 'jobs': self.api_url + 'jobs'
107 }
109 def tearDown(self):
110 squash.reset_endpoint_cache()
112 @unittest.skipIf(responses is None,
113 'Requires `responses` PyPI package')
114 def test_get(self):
115 with responses.RequestsMock() as reqmock:
116 reqmock.add(responses.GET,
117 squash._ENDPOINT_URLS['jobs'],
118 json={},
119 status=200,
120 content_type='application/json')
122 r = squash.get(self.api_url, api_endpoint='jobs')
124 self.assertIsInstance(r, requests.Response)
125 self.assertEqual(len(reqmock.calls), 1)
126 self.assertEqual(
127 reqmock.calls[0].request.url,
128 squash._ENDPOINT_URLS['jobs'],
129 )
130 self.assertEqual(
131 reqmock.calls[0].request.headers['Accept'],
132 'application/json; version=' + squash._API_VERSION
133 )
135 @unittest.skipIf(responses is None,
136 'Requires `responses` PyPI package')
137 def test_versioned_get(self):
138 with responses.RequestsMock() as reqmock:
139 reqmock.add(responses.GET,
140 squash._ENDPOINT_URLS['jobs'],
141 json={},
142 status=200,
143 content_type='application/json')
145 squash.get(self.api_url, api_endpoint='jobs', version='1.2')
147 self.assertEqual(
148 reqmock.calls[0].request.headers['Accept'],
149 'application/json; version=1.2'
150 )
152 @unittest.skipIf(responses is None,
153 'Requires `responses` PyPI package')
154 def test_raises(self):
155 with responses.RequestsMock() as reqmock:
156 reqmock.add(responses.GET,
157 squash._ENDPOINT_URLS['jobs'],
158 json={},
159 status=404,
160 content_type='application/json')
162 with self.assertRaises(requests.exceptions.RequestException):
163 squash.get(self.api_url, api_endpoint='jobs', version='1.2')
166class PostTestCase(unittest.TestCase):
168 def setUp(self):
169 self.api_url = 'https://example.com/api/'
170 # pre-set the _ENDPOINT_URLS to use the cache, not http get
171 squash._ENDPOINT_URLS = {
172 'jobs': self.api_url + 'jobs'
173 }
175 def tearDown(self):
176 squash.reset_endpoint_cache()
178 @unittest.skipIf(responses is None,
179 'Requires `responses` PyPI package')
180 def test_json_post(self):
181 with responses.RequestsMock() as reqmock:
182 reqmock.add(responses.POST,
183 squash._ENDPOINT_URLS['jobs'],
184 json={},
185 status=201,
186 content_type='application/json')
188 r = squash.post(self.api_url, api_endpoint='jobs',
189 json_doc={'key': 'value'},
190 access_token='mytoken')
192 self.assertIsInstance(r, requests.Response)
193 self.assertEqual(len(reqmock.calls), 1)
194 self.assertEqual(
195 reqmock.calls[0].request.url,
196 squash._ENDPOINT_URLS['jobs'],
197 )
198 self.assertEqual(
199 json.loads(reqmock.calls[0].request.body),
200 {'key': 'value'}
201 )
202 headers = reqmock.calls[0].request.headers
203 self.assertEqual(
204 headers['Accept'],
205 'application/json; version=' + squash._API_VERSION
206 )
207 self.assertEqual(
208 headers['Authorization'],
209 'JWT mytoken'
210 )
212 @unittest.skipIf(responses is None,
213 'Requires `responses` PyPI package')
214 def test_raises(self):
215 with responses.RequestsMock() as reqmock:
216 reqmock.add(responses.POST,
217 squash._ENDPOINT_URLS['jobs'],
218 json={},
219 status=503,
220 content_type='application/json')
222 with self.assertRaises(requests.exceptions.RequestException):
223 squash.post(self.api_url, api_endpoint='jobs',
224 json_doc={})
227if __name__ == "__main__": 227 ↛ 228line 227 didn't jump to line 228, because the condition on line 227 was never true
228 unittest.main()