|
1 | 1 | import re |
| 2 | +from dataclasses import dataclass |
2 | 3 | from tempfile import NamedTemporaryFile |
| 4 | +from typing import Dict |
3 | 5 | from unittest.mock import MagicMock, patch |
4 | 6 |
|
5 | 7 | import pytest |
| 8 | + |
| 9 | +from suzieq.cli.sqcmds import context_commands |
| 10 | +from suzieq.shared.context import SqContext |
6 | 11 | from suzieq.shared.utils import (get_sq_install_dir, load_sq_config, |
7 | 12 | validate_sq_config) |
8 | 13 | from tests.conftest import create_dummy_config_file |
@@ -90,3 +95,134 @@ def test_config_validation(monkeypatch): |
90 | 95 | monkeypatch.setenv(env_var, env_key) |
91 | 96 | error = validate_sq_config(cfg) |
92 | 97 | assert error is None, error |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.sq_config |
| 101 | +@pytest.mark.rest |
| 102 | +def test_config_rest() -> None: |
| 103 | + """ |
| 104 | + Test config file |
| 105 | + """ |
| 106 | + # with pandas engine |
| 107 | + cfg: Dict = {'rest': {}} |
| 108 | + |
| 109 | + ctxt = SqContext(cfg=cfg) |
| 110 | + # check that the default value are the same |
| 111 | + assert ctxt.rest_transport == 'https' |
| 112 | + assert ctxt.rest_server_ip == '127.0.0.1' |
| 113 | + assert ctxt.rest_server_port == 8000 |
| 114 | + assert ctxt.rest_api_key == '' |
| 115 | + assert ctxt.engine == 'pandas' |
| 116 | + |
| 117 | + # defining rest engine params |
| 118 | + cfg['rest']['API_KEY'] = '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f' |
| 119 | + cfg['rest']['address'] = '0.0.0.0' |
| 120 | + cfg['rest']['port'] = 8000 |
| 121 | + cfg['rest']['no-https'] = True |
| 122 | + cfg['ux'] = {'engine': 'rest'} |
| 123 | + |
| 124 | + ctxt = SqContext(cfg=cfg) |
| 125 | + assert ctxt.rest_transport == 'http' |
| 126 | + assert ctxt.rest_server_ip == '0.0.0.0' |
| 127 | + assert ctxt.rest_server_port == 8000 |
| 128 | + assert ctxt.rest_api_key == '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f' |
| 129 | + assert ctxt.engine == 'rest' |
| 130 | + |
| 131 | + # https with rest engine |
| 132 | + cfg['rest']['no-https'] = False |
| 133 | + ctxt = SqContext(cfg=cfg) |
| 134 | + assert ctxt.rest_transport == 'https' |
| 135 | + assert ctxt.rest_server_ip == '0.0.0.0' |
| 136 | + assert ctxt.rest_server_port == 8000 |
| 137 | + assert ctxt.rest_api_key == '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f' |
| 138 | + assert ctxt.engine == 'rest' |
| 139 | + |
| 140 | + |
| 141 | +@pytest.mark.sq_config |
| 142 | +@pytest.mark.rest |
| 143 | +def test_context_commands_context(monkeypatch): |
| 144 | + """ |
| 145 | + test context |
| 146 | + """ |
| 147 | + CTXT_REST_ATTRS = { |
| 148 | + 'rest_server_ip': 'address', |
| 149 | + 'rest_server_port': 'port', |
| 150 | + 'rest_transport': 'no-https', |
| 151 | + 'rest_api_key': 'API_KEY'} |
| 152 | + |
| 153 | + @dataclass |
| 154 | + class fake_ctxt_class(): |
| 155 | + engine = 'rest' |
| 156 | + cfg = {'rest': |
| 157 | + { |
| 158 | + 'address': '0.0.0.0', |
| 159 | + 'port': '8000', |
| 160 | + 'no-https': True, |
| 161 | + 'API_KEY': '496157e6e869ef7f3d6ecb24a6f6d847b224ee4f' |
| 162 | + }} |
| 163 | + rest_server_ip: str = 'test_rest_server_ip' |
| 164 | + rest_server_port: int = 8080 |
| 165 | + rest_api_key: str = 'test_rest_api_key' |
| 166 | + rest_transport: str = 'test_rest_transport' |
| 167 | + |
| 168 | + @dataclass |
| 169 | + class fake_context_class(): |
| 170 | + ctxt = fake_ctxt_class() |
| 171 | + |
| 172 | + def change_engine(self, engine): |
| 173 | + self.ctxt.engine = engine |
| 174 | + |
| 175 | + def fake_get_context(): |
| 176 | + return fake_context_class() |
| 177 | + |
| 178 | + # sending set engine: rest when is already selected, nothing should change |
| 179 | + monkeypatch.setattr(context_commands.context, |
| 180 | + 'get_context', |
| 181 | + fake_get_context) |
| 182 | + context_commands.set_ctxt(engine='rest') |
| 183 | + assert context_commands.context.get_context().ctxt.engine == 'rest' |
| 184 | + assert getattr(context_commands.context.get_context().ctxt, |
| 185 | + 'rest_server_ip') == 'test_rest_server_ip' |
| 186 | + assert getattr(context_commands.context.get_context().ctxt, |
| 187 | + 'rest_server_port') == 8080 |
| 188 | + assert getattr(context_commands.context.get_context().ctxt, |
| 189 | + 'rest_api_key') == 'test_rest_api_key' |
| 190 | + assert getattr(context_commands.context.get_context().ctxt, |
| 191 | + 'rest_transport') == 'test_rest_transport' |
| 192 | + |
| 193 | + # sending set engine: rest when is selected engine: pandas with http |
| 194 | + fake_context_class.ctxt.engine = 'pandas' |
| 195 | + monkeypatch.setattr(context_commands.context, |
| 196 | + 'get_context', |
| 197 | + fake_get_context) |
| 198 | + context_commands.set_ctxt(engine='rest') |
| 199 | + assert context_commands.context.get_context().ctxt.engine == 'rest' |
| 200 | + for attr in CTXT_REST_ATTRS: |
| 201 | + # get the expexted value of the rest param |
| 202 | + expected_value = fake_ctxt_class.cfg['rest'][CTXT_REST_ATTRS[attr]] |
| 203 | + if CTXT_REST_ATTRS[attr] == 'no-https': |
| 204 | + expected_value = 'http' if expected_value is True else 'https' |
| 205 | + # check if all the rest attr match |
| 206 | + assert getattr( |
| 207 | + context_commands.context.get_context().ctxt, |
| 208 | + attr) == expected_value, f'{attr} value shold be {expected_value},\ |
| 209 | + not {getattr(context_commands.context.get_context().ctxt, attr)}' |
| 210 | + |
| 211 | + # sending set engine: rest when is selected engine: pandas with https |
| 212 | + fake_context_class.ctxt.engine = 'pandas' |
| 213 | + fake_ctxt_class.cfg['rest']['no-https'] = False |
| 214 | + monkeypatch.setattr(context_commands.context, |
| 215 | + 'get_context', |
| 216 | + fake_get_context) |
| 217 | + context_commands.set_ctxt(engine='rest') |
| 218 | + assert context_commands.context.get_context().ctxt.engine == 'rest' |
| 219 | + for attr in CTXT_REST_ATTRS: |
| 220 | + # get the expexted value of the rest param |
| 221 | + expected_value = fake_ctxt_class.cfg['rest'][CTXT_REST_ATTRS[attr]] |
| 222 | + if CTXT_REST_ATTRS[attr] == 'no-https': |
| 223 | + expected_value = 'http' if expected_value is True else 'https' |
| 224 | + # check if all the rest attr match |
| 225 | + assert getattr( |
| 226 | + context_commands.context.get_context().ctxt, |
| 227 | + attr) == expected_value, f'{attr} value shold be {expected_value},\ |
| 228 | + not {getattr(context_commands.context.get_context().ctxt, attr)}' |
0 commit comments