-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
308 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
sdk/identity/azure-identity/tests/test_context_manager.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
try: | ||
from unittest.mock import MagicMock, patch | ||
except ImportError: | ||
from mock import MagicMock, patch # type: ignore | ||
|
||
from azure.identity import ( | ||
AzureApplicationCredential, | ||
AzureCliCredential, | ||
AzurePowerShellCredential, | ||
AuthorizationCodeCredential, | ||
CertificateCredential, | ||
ClientSecretCredential, | ||
DeviceCodeCredential, | ||
EnvironmentCredential, | ||
InteractiveBrowserCredential, | ||
SharedTokenCacheCredential, | ||
UsernamePasswordCredential, | ||
VisualStudioCodeCredential, | ||
) | ||
from azure.identity._constants import EnvironmentVariables | ||
|
||
import pytest | ||
|
||
from test_certificate_credential import CERT_PATH | ||
from test_vscode_credential import GET_USER_SETTINGS | ||
|
||
|
||
class CredentialFixture: | ||
def __init__(self, cls, default_kwargs=None, ctor_patch=None, get_token_patch=None): | ||
self.cls = cls | ||
self.get_token_patch = get_token_patch or MagicMock() | ||
self._default_kwargs = default_kwargs or {} | ||
self._ctor_patch = ctor_patch or MagicMock() | ||
|
||
def __call__(self, **kwargs): | ||
with self._ctor_patch: | ||
return self.cls(**dict(self._default_kwargs, **kwargs)) | ||
|
||
|
||
FIXTURES = ( | ||
CredentialFixture( | ||
AuthorizationCodeCredential, | ||
{kwarg: "..." for kwarg in ("tenant_id", "client_id", "authorization_code", "redirect_uri")}, | ||
), | ||
CredentialFixture(CertificateCredential, {"tenant_id": "...", "client_id": "...", "certificate_path": CERT_PATH}), | ||
CredentialFixture(ClientSecretCredential, {kwarg: "..." for kwarg in ("tenant_id", "client_id", "client_secret")}), | ||
CredentialFixture(DeviceCodeCredential), | ||
CredentialFixture( | ||
EnvironmentCredential, | ||
ctor_patch=patch.dict("os.environ", {var: ".." for var in EnvironmentVariables.CLIENT_SECRET_VARS}, clear=True), | ||
), | ||
CredentialFixture(InteractiveBrowserCredential), | ||
CredentialFixture(UsernamePasswordCredential, {"client_id": "...", "username": "...", "password": "..."}), | ||
CredentialFixture(VisualStudioCodeCredential, ctor_patch=patch(GET_USER_SETTINGS, lambda: {})), | ||
) | ||
|
||
all_fixtures = pytest.mark.parametrize("fixture", FIXTURES, ids=lambda fixture: fixture.cls.__name__) | ||
|
||
|
||
@all_fixtures | ||
def test_close(fixture): | ||
transport = MagicMock() | ||
credential = fixture(transport=transport) | ||
assert not transport.__enter__.called | ||
assert not transport.__exit__.called | ||
|
||
credential.close() | ||
assert not transport.__enter__.called | ||
assert transport.__exit__.call_count == 1 | ||
|
||
|
||
@all_fixtures | ||
def test_context_manager(fixture): | ||
transport = MagicMock() | ||
credential = fixture(transport=transport) | ||
|
||
with credential: | ||
assert transport.__enter__.call_count == 1 | ||
assert not transport.__exit__.called | ||
|
||
assert transport.__enter__.call_count == 1 | ||
assert transport.__exit__.call_count == 1 | ||
|
||
|
||
@all_fixtures | ||
def test_exit_args(fixture): | ||
transport = MagicMock() | ||
credential = fixture(transport=transport) | ||
expected_args = ("type", "value", "traceback") | ||
credential.__exit__(*expected_args) | ||
transport.__exit__.assert_called_once_with(*expected_args) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"cls", (AzureCliCredential, AzureApplicationCredential, AzurePowerShellCredential, EnvironmentCredential, SharedTokenCacheCredential) | ||
) | ||
def test_no_op(cls): | ||
"""Credentials that don't allow custom transports, or require initialization or optional config, should have no-op methods""" | ||
credential = cls() | ||
with credential: | ||
pass | ||
credential.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.