|
| 1 | +"""Tests for platform credential error detection in Semantic Layer tools. |
| 2 | +
|
| 3 | +Verifies that when query_metrics or get_metrics_compiled_sql fail due to |
| 4 | +expired dbt Cloud platform credentials, the error message clearly indicates |
| 5 | +this is a platform-side issue and directs users to the dbt Cloud UI. |
| 6 | +
|
| 7 | +Related: https://github.com/dbt-labs/dbt-mcp/issues/670 |
| 8 | +""" |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from dbt_mcp.semantic_layer.client import ( |
| 13 | + SemanticLayerFetcher, |
| 14 | + _PLATFORM_CREDENTIAL_HINT, |
| 15 | + _is_platform_credential_error, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def fetcher(): |
| 21 | + from unittest.mock import AsyncMock |
| 22 | + |
| 23 | + return SemanticLayerFetcher(client_provider=AsyncMock()) |
| 24 | + |
| 25 | + |
| 26 | +class TestPlatformCredentialErrorDetection: |
| 27 | + """Tests for _is_platform_credential_error.""" |
| 28 | + |
| 29 | + def test_detects_sso_authentication_expired(self) -> None: |
| 30 | + assert _is_platform_credential_error( |
| 31 | + "SSO authentication has expired, please re-connect to Snowflake" |
| 32 | + ) |
| 33 | + |
| 34 | + def test_detects_reconnect_to_snowflake(self) -> None: |
| 35 | + assert _is_platform_credential_error( |
| 36 | + "Please re-connect to Snowflake: https://docs.getdbt.com/faqs" |
| 37 | + ) |
| 38 | + |
| 39 | + def test_detects_refresh_snowflake_oauth_url(self) -> None: |
| 40 | + assert _is_platform_credential_error( |
| 41 | + "https://docs.getdbt.com/faqs/Troubleshooting/refresh-snowflake-oauth-credentials" |
| 42 | + ) |
| 43 | + |
| 44 | + def test_detects_authentication_token_expired(self) -> None: |
| 45 | + assert _is_platform_credential_error( |
| 46 | + "Authentication token has expired for the data warehouse" |
| 47 | + ) |
| 48 | + |
| 49 | + def test_detects_oauth_token_expired(self) -> None: |
| 50 | + assert _is_platform_credential_error( |
| 51 | + "OAuth access token has expired for this connection" |
| 52 | + ) |
| 53 | + |
| 54 | + def test_case_insensitive(self) -> None: |
| 55 | + assert _is_platform_credential_error( |
| 56 | + "sso AUTHENTICATION Has Expired" |
| 57 | + ) |
| 58 | + |
| 59 | + def test_does_not_match_generic_query_error(self) -> None: |
| 60 | + assert not _is_platform_credential_error( |
| 61 | + "column 'revenue' not found in table" |
| 62 | + ) |
| 63 | + |
| 64 | + def test_does_not_match_syntax_error(self) -> None: |
| 65 | + assert not _is_platform_credential_error( |
| 66 | + "SQL compilation error: syntax error at position 42" |
| 67 | + ) |
| 68 | + |
| 69 | + def test_does_not_match_timeout_error(self) -> None: |
| 70 | + assert not _is_platform_credential_error( |
| 71 | + "Query timed out after 60 seconds" |
| 72 | + ) |
| 73 | + |
| 74 | + def test_does_not_match_empty_string(self) -> None: |
| 75 | + assert not _is_platform_credential_error("") |
| 76 | + |
| 77 | + |
| 78 | +class TestFormatSemanticLayerErrorWithCredentialHint: |
| 79 | + """Tests that _format_semantic_layer_error appends the credential hint.""" |
| 80 | + |
| 81 | + def test_sso_expired_includes_platform_hint(self, fetcher) -> None: |
| 82 | + error = Exception( |
| 83 | + "SSO authentication has expired, please re-connect to Snowflake: " |
| 84 | + "https://docs.getdbt.com/faqs/Troubleshooting/refresh-snowflake-oauth-credentials" |
| 85 | + ) |
| 86 | + result = fetcher._format_semantic_layer_error(error) |
| 87 | + assert "dbt Cloud" in result |
| 88 | + assert "Profile → Credentials" in result |
| 89 | + assert "not a local authentication problem" in result |
| 90 | + |
| 91 | + def test_sso_expired_preserves_original_message(self, fetcher) -> None: |
| 92 | + error = Exception( |
| 93 | + "SSO authentication has expired, please re-connect to Snowflake" |
| 94 | + ) |
| 95 | + result = fetcher._format_semantic_layer_error(error) |
| 96 | + assert "SSO authentication has expired" in result |
| 97 | + |
| 98 | + def test_generic_error_does_not_include_platform_hint(self, fetcher) -> None: |
| 99 | + error = Exception("column 'revenue' not found") |
| 100 | + result = fetcher._format_semantic_layer_error(error) |
| 101 | + assert _PLATFORM_CREDENTIAL_HINT not in result |
| 102 | + assert "dbt Cloud UI" not in result |
| 103 | + |
| 104 | + def test_query_failed_error_with_credential_message_includes_hint( |
| 105 | + self, fetcher |
| 106 | + ) -> None: |
| 107 | + """QueryFailedError wrapping a credential expiry should also get the hint.""" |
| 108 | + error = Exception( |
| 109 | + 'QueryFailedError(["SSO authentication has expired, ' |
| 110 | + "please re-connect to Snowflake\"])" |
| 111 | + ) |
| 112 | + result = fetcher._format_semantic_layer_error(error) |
| 113 | + assert "dbt Cloud" in result |
| 114 | + assert "Profile → Credentials" in result |
| 115 | + |
| 116 | + def test_format_query_failed_error_with_credential_message(self, fetcher) -> None: |
| 117 | + """_format_query_failed_error should also include the hint for credential errors.""" |
| 118 | + from dbtsl.error import QueryFailedError |
| 119 | + |
| 120 | + error = QueryFailedError( |
| 121 | + "SSO authentication has expired, please re-connect to Snowflake" |
| 122 | + ) |
| 123 | + result = fetcher._format_query_failed_error(error) |
| 124 | + assert "dbt Cloud" in result.error |
| 125 | + assert "Profile → Credentials" in result.error |
0 commit comments