Skip to content

Commit

Permalink
Verify at construction that AzureKeyCredentialPolicy receive somethin…
Browse files Browse the repository at this point in the history
…g it can used (Azure#30380)

* Update _authentication.py

* Feedback

* Black

* Tests

* ChangeLog
  • Loading branch information
lmazuel authored May 26, 2023
1 parent 15b8822 commit 41748f3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

### Bugs Fixed

- Improve error message when providing the wrong credential type for AzureKeyCredential #30380

### Other Changes

## 1.26.4 (2023-04-06)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,14 @@ def __init__(
prefix: Optional[str] = None,
**kwargs, # pylint: disable=unused-argument
) -> None:
super(AzureKeyCredentialPolicy, self).__init__()
self._credential = credential
super().__init__()
if not hasattr(credential, "key"):
raise TypeError("String is not a supported credential input type. Use an instance of AzureKeyCredential.")
if not name:
raise ValueError("name can not be None or empty")
if not isinstance(name, str):
raise TypeError("name must be a string.")
self._credential = credential
self._name = name
self._prefix = prefix + " " if prefix else ""

Expand Down
5 changes: 4 additions & 1 deletion sdk/core/azure-core/tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def verify_authorization_header(request):


def test_azure_key_credential_policy_raises():
"""Tests AzureKeyCredential and AzureKeyCredentialPolicy raises with non-string input parameters."""
"""Tests AzureKeyCredential and AzureKeyCredentialPolicy raises with non-compliant input parameters."""
api_key = 1234
key_header = 5678
with pytest.raises(TypeError):
Expand All @@ -310,6 +310,9 @@ def test_azure_key_credential_policy_raises():
with pytest.raises(TypeError):
credential_policy = AzureKeyCredentialPolicy(credential=credential, name=key_header)

with pytest.raises(TypeError):
credential_policy = AzureKeyCredentialPolicy(credential=str(api_key), name=key_header)


def test_azure_key_credential_updates():
"""Tests AzureKeyCredential updates"""
Expand Down

0 comments on commit 41748f3

Please sign in to comment.