|
| 1 | +from typing import Dict |
| 2 | +import pytest |
| 3 | +import requests |
| 4 | + |
| 5 | +from aerie_cli.aerie_host import AerieHost, COMPATIBLE_AERIE_VERSIONS, AerieJWT |
| 6 | + |
| 7 | + |
| 8 | +class MockJWT: |
| 9 | + def __init__(self, *args, **kwargs): |
| 10 | + self.default_role = 'viewer' |
| 11 | + |
| 12 | +class MockResponse: |
| 13 | + def __init__(self, json: Dict, text: str = None, ok: bool = True) -> None: |
| 14 | + self.json_data = json |
| 15 | + self.text = text |
| 16 | + self.ok = ok |
| 17 | + |
| 18 | + def json(self) -> Dict: |
| 19 | + if self.json_data is None: |
| 20 | + raise requests.exceptions.JSONDecodeError("", "", 0) |
| 21 | + return self.json_data |
| 22 | + |
| 23 | + |
| 24 | +class MockSession: |
| 25 | + |
| 26 | + def __init__(self, mock_response: MockResponse) -> None: |
| 27 | + self.mock_response = mock_response |
| 28 | + |
| 29 | + def get(self, *args, **kwargs) -> MockResponse: |
| 30 | + return self.mock_response |
| 31 | + |
| 32 | + def post(self, *args, **kwargs) -> MockResponse: |
| 33 | + return self.mock_response |
| 34 | + |
| 35 | + |
| 36 | +def get_mock_aerie_host(json: Dict = None, text: str = None, ok: bool = True) -> AerieHost: |
| 37 | + mock_response = MockResponse(json, text, ok) |
| 38 | + mock_session = MockSession(mock_response) |
| 39 | + return AerieHost("", "", mock_session) |
| 40 | + |
| 41 | + |
| 42 | +def test_check_aerie_version(): |
| 43 | + aerie_host = get_mock_aerie_host( |
| 44 | + json={"version": COMPATIBLE_AERIE_VERSIONS[0]}) |
| 45 | + |
| 46 | + aerie_host.check_aerie_version() |
| 47 | + |
| 48 | + |
| 49 | +def test_authenticate_invalid_version(capsys, monkeypatch): |
| 50 | + ah = AerieHost("", "") |
| 51 | + |
| 52 | + def mock_get(*_, **__): |
| 53 | + return MockResponse({"version": "1.0.0"}) |
| 54 | + def mock_post(*_, **__): |
| 55 | + return MockResponse({"token": ""}) |
| 56 | + def mock_check_auth(*_, **__): |
| 57 | + return True |
| 58 | + |
| 59 | + monkeypatch.setattr(requests.Session, "get", mock_get) |
| 60 | + monkeypatch.setattr(requests.Session, "post", mock_post) |
| 61 | + monkeypatch.setattr(AerieHost, "check_auth", mock_check_auth) |
| 62 | + monkeypatch.setattr(AerieJWT, "__init__", MockJWT.__init__) |
| 63 | + |
| 64 | + with pytest.raises(RuntimeError) as e: |
| 65 | + ah.authenticate("") |
| 66 | + |
| 67 | + assert "Incompatible Aerie version: 1.0.0" in str(e.value) |
| 68 | + |
| 69 | + |
| 70 | +def test_authenticate_invalid_version_force(capsys, monkeypatch): |
| 71 | + ah = AerieHost("", "") |
| 72 | + |
| 73 | + def mock_get(*_, **__): |
| 74 | + return MockResponse({"version": "1.0.0"}) |
| 75 | + def mock_post(*_, **__): |
| 76 | + return MockResponse({"token": ""}) |
| 77 | + def mock_check_auth(*_, **__): |
| 78 | + return True |
| 79 | + |
| 80 | + monkeypatch.setattr(requests.Session, "get", mock_get) |
| 81 | + monkeypatch.setattr(requests.Session, "post", mock_post) |
| 82 | + monkeypatch.setattr(AerieHost, "check_auth", mock_check_auth) |
| 83 | + monkeypatch.setattr(AerieJWT, "__init__", MockJWT.__init__) |
| 84 | + |
| 85 | + ah.authenticate("", force=True) |
| 86 | + |
| 87 | + assert capsys.readouterr().out == "Warning: Incompatible Aerie version: 1.0.0\n" |
| 88 | + |
| 89 | + |
| 90 | +def test_no_version_endpoint(): |
| 91 | + aerie_host = get_mock_aerie_host(text="blah Aerie Gateway blah", ok=True) |
| 92 | + |
| 93 | + with pytest.raises(RuntimeError) as e: |
| 94 | + aerie_host.check_aerie_version() |
| 95 | + |
| 96 | + assert "Incompatible Aerie version: host version unknown" in str(e.value) |
| 97 | + |
| 98 | + |
| 99 | +def test_version_broken_gateway(): |
| 100 | + aerie_host = get_mock_aerie_host( |
| 101 | + text="502 Bad Gateway or something", ok=True) |
| 102 | + |
| 103 | + with pytest.raises(RuntimeError) as e: |
| 104 | + aerie_host.check_aerie_version() |
| 105 | + |
| 106 | + assert "Bad response from Aerie Gateway" in str(e.value) |
0 commit comments