Skip to content

Commit 520e1c9

Browse files
Require concrete client api_key inputs
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent ae82b75 commit 520e1c9

2 files changed

Lines changed: 6 additions & 52 deletions

File tree

hyperbrowser/client/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(
3636
raise HyperbrowserError(
3737
"API key must be provided via `api_key` or HYPERBROWSER_API_KEY"
3838
)
39-
if not isinstance(resolved_api_key, str):
39+
if type(resolved_api_key) is not str:
4040
raise HyperbrowserError("api_key must be a string")
4141
try:
4242
normalized_resolved_api_key = resolved_api_key.strip()

tests/test_client_api_key.py

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -201,58 +201,12 @@ def _broken_get(env_name: str, default=None):
201201

202202

203203
@pytest.mark.parametrize("client_class", [Hyperbrowser, AsyncHyperbrowser])
204-
def test_client_wraps_api_key_strip_runtime_errors(client_class):
205-
class _BrokenStripApiKey(str):
206-
def strip(self, chars=None): # type: ignore[override]
207-
_ = chars
208-
raise RuntimeError("api key strip exploded")
204+
def test_client_rejects_string_subclass_api_key_input(client_class):
205+
class _ApiKey(str):
206+
pass
209207

210-
with pytest.raises(HyperbrowserError, match="Failed to normalize api_key") as exc_info:
211-
client_class(api_key=_BrokenStripApiKey("test-key"))
212-
213-
assert isinstance(exc_info.value.original_error, RuntimeError)
214-
215-
216-
@pytest.mark.parametrize("client_class", [Hyperbrowser, AsyncHyperbrowser])
217-
def test_client_preserves_hyperbrowser_api_key_strip_errors(client_class):
218-
class _BrokenStripApiKey(str):
219-
def strip(self, chars=None): # type: ignore[override]
220-
_ = chars
221-
raise HyperbrowserError("custom strip failure")
222-
223-
with pytest.raises(HyperbrowserError, match="custom strip failure") as exc_info:
224-
client_class(api_key=_BrokenStripApiKey("test-key"))
225-
226-
assert exc_info.value.original_error is None
227-
228-
229-
@pytest.mark.parametrize("client_class", [Hyperbrowser, AsyncHyperbrowser])
230-
def test_client_wraps_non_string_api_key_strip_results(client_class):
231-
class _NonStringStripResultApiKey(str):
232-
def strip(self, chars=None): # type: ignore[override]
233-
_ = chars
234-
return object()
235-
236-
with pytest.raises(HyperbrowserError, match="Failed to normalize api_key") as exc_info:
237-
client_class(api_key=_NonStringStripResultApiKey("test-key"))
238-
239-
assert isinstance(exc_info.value.original_error, TypeError)
240-
241-
242-
@pytest.mark.parametrize("client_class", [Hyperbrowser, AsyncHyperbrowser])
243-
def test_client_wraps_api_key_string_subclass_strip_results(client_class):
244-
class _BrokenLengthApiKey(str):
245-
class _NormalizedKey(str):
246-
pass
247-
248-
def strip(self, chars=None): # type: ignore[override]
249-
_ = chars
250-
return self._NormalizedKey("test-key")
251-
252-
with pytest.raises(HyperbrowserError, match="Failed to normalize api_key") as exc_info:
253-
client_class(api_key=_BrokenLengthApiKey("test-key"))
254-
255-
assert isinstance(exc_info.value.original_error, TypeError)
208+
with pytest.raises(HyperbrowserError, match="api_key must be a string"):
209+
client_class(api_key=_ApiKey("test-key"))
256210

257211

258212
@pytest.mark.parametrize("client_class", [Hyperbrowser, AsyncHyperbrowser])

0 commit comments

Comments
 (0)