From 145bf842962dabef860b545708ea06499110df96 Mon Sep 17 00:00:00 2001 From: m-peko Date: Tue, 19 Aug 2025 15:51:55 +0200 Subject: [PATCH] Improve empty API key error message --- src/atlas/_client.py | 4 ++-- tests/test_client.py | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/atlas/_client.py b/src/atlas/_client.py index 881cfc0..704da92 100644 --- a/src/atlas/_client.py +++ b/src/atlas/_client.py @@ -44,7 +44,7 @@ def __init__( """ if api_key is None: api_key = os.environ.get("LAYERLENS_ATLAS_API_KEY") - if api_key is None: + if api_key is None or api_key == "": raise AtlasError( "The api_key client option must be set either by passing api_key to the client or by setting the LAYERLENS_ATLAS_API_KEY environment variable" ) @@ -190,7 +190,7 @@ def __init__( """ if api_key is None: api_key = os.environ.get("LAYERLENS_ATLAS_API_KEY") - if api_key is None: + if api_key is None or api_key == "": raise AtlasError( "The api_key client option must be set either by passing api_key to the client " "or by setting the LAYERLENS_ATLAS_API_KEY environment variable" diff --git a/tests/test_client.py b/tests/test_client.py index af4a995..af34dfe 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -3,6 +3,7 @@ import pytest from atlas import Atlas +from atlas._exceptions import AtlasError class TestAtlasClientInitialization: @@ -52,20 +53,20 @@ def test_auth_headers_with_api_key(self, mock_org): def test_auth_headers_without_api_key(self, mock_org): """auth_headers property returns empty dict when no API key.""" with patch("atlas.Atlas._get_organization", return_value=mock_org): - client = Atlas(api_key="") - - headers = client.auth_headers - - assert headers == {} + with pytest.raises( + AtlasError, + match="The api_key client option must be set either by passing api_key to the client or by setting the LAYERLENS_ATLAS_API_KEY environment variable", + ): + Atlas(api_key="") def test_auth_headers_with_empty_api_key(self, mock_org): """auth_headers property returns empty dict when API key is empty string.""" with patch("atlas.Atlas._get_organization", return_value=mock_org): - client = Atlas(api_key="") - - headers = client.auth_headers - - assert headers == {} + with pytest.raises( + AtlasError, + match="The api_key client option must be set either by passing api_key to the client or by setting the LAYERLENS_ATLAS_API_KEY environment variable", + ): + Atlas(api_key="") def test_copy_method(self, mock_org): """copy method creates new client with overridden parameters."""