|
| 1 | +import os |
| 2 | +import subprocess |
1 | 3 | from dataclasses import dataclass |
2 | 4 | from itertools import combinations |
| 5 | +from tempfile import mkstemp |
| 6 | +from time import sleep |
3 | 7 | from typing import Dict |
4 | 8 | from urllib.parse import parse_qs, urlparse |
5 | 9 |
|
6 | 10 | import pytest |
7 | | -from requests.exceptions import ConnectionError |
8 | | -from suzieq.engines.rest.engineobj import SqRestEngine |
9 | | -from time import sleep |
10 | | -import subprocess |
11 | | -import os |
12 | | - |
13 | 11 | import yaml |
14 | | -import pytest |
| 12 | +from requests.exceptions import ConnectionError |
15 | 13 |
|
16 | | -from tests.conftest import suzieq_rest_server_path |
| 14 | +from suzieq.engines.rest.engineobj import SqRestEngine |
17 | 15 | from suzieq.shared.utils import load_sq_config |
18 | | -from tests.conftest import get_free_port |
| 16 | +from tests.conftest import get_free_port, suzieq_rest_server_path |
19 | 17 |
|
20 | 18 |
|
21 | 19 | @dataclass |
@@ -81,10 +79,10 @@ def req_error(param: str, exp: str, got: str) -> str: |
81 | 79 |
|
82 | 80 | # check query parameters |
83 | 81 | url_query = parse_qs(url.query) |
84 | | - for query_param, query_value in url_query.items(): |
85 | | - assert len(query_value) == 1, \ |
| 82 | + for query_param, query_values in url_query.items(): |
| 83 | + assert len(query_values) == 1, \ |
86 | 84 | f'Got more than 1 value for {query_param}' |
87 | | - query_value = query_value[0] |
| 85 | + query_value = query_values[0] |
88 | 86 | if query_param == 'access_token': |
89 | 87 | # access_token needs a special validation |
90 | 88 | assert query_value == engine.ctxt.rest_api_key, \ |
@@ -122,13 +120,11 @@ def test_request_params(): |
122 | 120 | validate_args(engine, req_params) |
123 | 121 |
|
124 | 122 |
|
125 | | - |
126 | 123 | @pytest.mark.rest |
127 | 124 | @pytest.mark.skipif(not os.environ.get('TEST_SERVER', None), |
128 | 125 | reason='causes github action hang') |
129 | 126 | def test_server_cert(): |
130 | 127 | '''Can we can get a valid response with & without certificate''' |
131 | | - from tempfile import mkstemp |
132 | 128 |
|
133 | 129 | # We need to change the port used to avoid conflicts |
134 | 130 | config = {'data-directory': './tests/data/parquet', |
@@ -172,67 +168,60 @@ def open_rest_server(cfgfile): |
172 | 168 |
|
173 | 169 | def make_get_response_request(sqcfg): |
174 | 170 | ctxt = SqContextMock( |
175 | | - rest_api_key=sqcfg['rest']['API_KEY'], |
176 | | - rest_transport= \ |
177 | | - 'http' if sqcfg['rest']['no-https'] == True else 'https', |
178 | | - rest_server_ip=sqcfg['rest']['address'], |
179 | | - rest_server_port=sqcfg['rest']['port'], |
180 | | - cfg={'rest': sqcfg['rest']}) |
| 171 | + rest_api_key=sqcfg['rest']['API_KEY'], |
| 172 | + rest_transport='http' if sqcfg['rest']['no-https'] is True |
| 173 | + else 'https', |
| 174 | + rest_server_ip=sqcfg['rest']['address'], |
| 175 | + rest_server_port=sqcfg['rest']['port'], |
| 176 | + cfg={'rest': sqcfg['rest']}) |
181 | 177 |
|
182 | 178 | sqobj = SqObjMock(ctxt, '', '', 'default', |
183 | | - 'default', 'latest', 'device', 'default') |
| 179 | + 'default', 'latest', 'device', 'default') |
184 | 180 |
|
185 | | - print(ctxt) |
186 | | - print(sqobj) |
187 | 181 | engine = SqRestEngine(sqobj) |
188 | 182 | try: |
189 | 183 | response = engine._get_response('show') |
190 | 184 | print(f'responsein test: {response}') |
191 | 185 | return 200 |
192 | | - except: |
| 186 | + except Exception: |
193 | 187 | return 400 |
194 | 188 |
|
195 | | - |
196 | 189 | def close_session(proc, cfgfile): |
197 | 190 | proc.kill() |
198 | 191 | os.remove(cfgfile) |
199 | 192 |
|
200 | | - #test with http verify None |
| 193 | + # test with http verify None |
201 | 194 | sqcfg, cfgfile = create_config(config) |
202 | 195 | proc = open_rest_server(cfgfile) |
203 | 196 | response = make_get_response_request(sqcfg) |
204 | 197 | assert response == 200 |
205 | 198 | close_session(proc, cfgfile) |
206 | 199 |
|
207 | | - |
208 | | - #test with https verify None |
| 200 | + # test with https verify None |
209 | 201 | config['rest']['no-https'] = False |
210 | 202 | sqcfg, cfgfile = create_config(config) |
211 | 203 | proc = open_rest_server(cfgfile) |
212 | 204 | response = make_get_response_request(sqcfg) |
213 | 205 | assert response == 400 |
214 | 206 | close_session(proc, cfgfile) |
215 | 207 |
|
216 | | - |
217 | | - #test with https verify False |
| 208 | + # test with https verify False |
218 | 209 | config['rest']['cert-verify'] = False |
219 | 210 | sqcfg, cfgfile = create_config(config) |
220 | 211 | proc = open_rest_server(cfgfile) |
221 | 212 | response = make_get_response_request(sqcfg) |
222 | 213 | assert response == 200 |
223 | 214 | close_session(proc, cfgfile) |
224 | 215 |
|
225 | | - |
226 | | - #test with https verify True |
| 216 | + # test with https verify True |
227 | 217 | config['rest']['cert-verify'] = True |
228 | 218 | sqcfg, cfgfile = create_config(config) |
229 | 219 | proc = open_rest_server(cfgfile) |
230 | 220 | response = make_get_response_request(sqcfg) |
231 | 221 | assert response == 400 |
232 | 222 | close_session(proc, cfgfile) |
233 | 223 |
|
234 | | - |
235 | | - #test with https verify CA |
| 224 | + # test with https verify CA |
236 | 225 | config['rest']['cert-verify'] =\ |
237 | 226 | './tests/test_cert_CA/ca-cert.pem' |
238 | 227 | sqcfg, cfgfile = create_config(config) |
|
0 commit comments