|
| 1 | +""" |
| 2 | +Tests for clear connection / auth error messages (issue #1). |
| 3 | +
|
| 4 | +A misconfigured client used to surface a raw ``httpx`` error that never |
| 5 | +mentioned Lightdash. These tests verify the SDK now translates the two reported |
| 6 | +cases — unreachable instance and bad credentials — into descriptive exceptions, |
| 7 | +while leaving the existing API-error handling untouched. |
| 8 | +""" |
| 9 | + |
| 10 | +import httpx |
| 11 | +import pytest |
| 12 | +from lightdash import ( |
| 13 | + Client, |
| 14 | + LightdashError, |
| 15 | + LightdashConnectionError, |
| 16 | + LightdashAuthError, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def _client() -> Client: |
| 21 | + return Client( |
| 22 | + instance_url="https://demo.lightdash.cloud", |
| 23 | + access_token="ldpat_token", |
| 24 | + project_uuid="proj", |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +def _response(status: int, json_body=None, text: str = "") -> httpx.Response: |
| 29 | + request = httpx.Request("GET", "https://demo.lightdash.cloud/api/v1/x") |
| 30 | + if json_body is not None: |
| 31 | + return httpx.Response(status, json=json_body, request=request) |
| 32 | + return httpx.Response(status, text=text, request=request) |
| 33 | + |
| 34 | + |
| 35 | +class TestConnectionErrors: |
| 36 | + def test_connect_error_is_wrapped(self, monkeypatch): |
| 37 | + """A DNS/connection failure names the instance and the likely cause.""" |
| 38 | + def boom(*args, **kwargs): |
| 39 | + raise httpx.ConnectError("nodename nor servname provided, or not known") |
| 40 | + monkeypatch.setattr(httpx.Client, "request", boom) |
| 41 | + |
| 42 | + with pytest.raises(LightdashConnectionError, match="Could not connect to Lightdash") as ei: |
| 43 | + _client()._make_request("GET", "/api/v1/x") |
| 44 | + assert "demo.lightdash.cloud" in str(ei.value) |
| 45 | + assert "instance_url" in str(ei.value) |
| 46 | + |
| 47 | + def test_timeout_is_wrapped(self, monkeypatch): |
| 48 | + def boom(*args, **kwargs): |
| 49 | + raise httpx.ConnectTimeout("timed out") |
| 50 | + monkeypatch.setattr(httpx.Client, "request", boom) |
| 51 | + |
| 52 | + with pytest.raises(LightdashConnectionError, match="timed out"): |
| 53 | + _client()._make_request("GET", "/api/v1/x") |
| 54 | + |
| 55 | + def test_connection_error_subclasses_lightdash_error(self, monkeypatch): |
| 56 | + """Existing `except LightdashError` handlers keep working.""" |
| 57 | + def boom(*args, **kwargs): |
| 58 | + raise httpx.ConnectError("x") |
| 59 | + monkeypatch.setattr(httpx.Client, "request", boom) |
| 60 | + |
| 61 | + with pytest.raises(LightdashError): |
| 62 | + _client()._make_request("GET", "/api/v1/x") |
| 63 | + |
| 64 | + def test_original_error_is_chained(self, monkeypatch): |
| 65 | + """The underlying httpx error is preserved as the cause for debugging.""" |
| 66 | + original = httpx.ConnectError("boom") |
| 67 | + def boom(*args, **kwargs): |
| 68 | + raise original |
| 69 | + monkeypatch.setattr(httpx.Client, "request", boom) |
| 70 | + |
| 71 | + with pytest.raises(LightdashConnectionError) as ei: |
| 72 | + _client()._make_request("GET", "/api/v1/x") |
| 73 | + assert ei.value.__cause__ is original |
| 74 | + |
| 75 | + |
| 76 | +class TestAuthErrors: |
| 77 | + @pytest.mark.parametrize("status", [401, 403]) |
| 78 | + def test_auth_failure_raises_auth_error(self, monkeypatch, status): |
| 79 | + monkeypatch.setattr( |
| 80 | + httpx.Client, "request", lambda *a, **k: _response(status, text="unauthorized") |
| 81 | + ) |
| 82 | + with pytest.raises(LightdashAuthError, match="Authentication failed") as ei: |
| 83 | + _client()._make_request("GET", "/api/v1/x") |
| 84 | + assert ei.value.status_code == status |
| 85 | + assert "access_token" in str(ei.value) |
| 86 | + |
| 87 | + |
| 88 | +class TestExistingBehaviourPreserved: |
| 89 | + """The structured-error and success paths are unchanged by this fix.""" |
| 90 | + |
| 91 | + def test_api_error_in_ok_response_is_surfaced(self, monkeypatch): |
| 92 | + body = {"status": "error", "error": {"message": "Bad query", "name": "QueryError", "statusCode": 400}} |
| 93 | + monkeypatch.setattr(httpx.Client, "request", lambda *a, **k: _response(200, json_body=body)) |
| 94 | + with pytest.raises(LightdashError, match="Bad query") as ei: |
| 95 | + _client()._make_request("GET", "/api/v1/x") |
| 96 | + assert not isinstance(ei.value, LightdashAuthError) |
| 97 | + |
| 98 | + def test_ok_response_returns_results(self, monkeypatch): |
| 99 | + body = {"status": "ok", "results": [{"a": 1}]} |
| 100 | + monkeypatch.setattr(httpx.Client, "request", lambda *a, **k: _response(200, json_body=body)) |
| 101 | + assert _client()._make_request("GET", "/api/v1/x") == [{"a": 1}] |
0 commit comments