1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import itertools
import os
from unittest import mock
from azure.identity import CredentialUnavailableError
from azure.identity.aio import EnvironmentCredential
from azure.identity._constants import EnvironmentVariables
import pytest
from helpers import mock_response, Request
from helpers_async import async_validating_transport
from test_environment_credential import ALL_VARIABLES
@pytest.mark.asyncio
async def test_incomplete_configuration():
"""get_token should raise CredentialUnavailableError for incomplete configuration."""
with mock.patch.dict(os.environ, {}, clear=True):
with pytest.raises(CredentialUnavailableError) as ex:
await EnvironmentCredential().get_token("scope")
for a, b in itertools.combinations(ALL_VARIABLES, 2): # all credentials require at least 3 variables set
with mock.patch.dict(os.environ, {a: "a", b: "b"}, clear=True):
with pytest.raises(CredentialUnavailableError) as ex:
await EnvironmentCredential().get_token("scope")
@pytest.mark.parametrize(
"credential_name,environment_variables",
(
("ClientSecretCredential", EnvironmentVariables.CLIENT_SECRET_VARS),
("CertificateCredential", EnvironmentVariables.CERT_VARS),
),
)
def test_passes_authority_argument(credential_name, environment_variables):
"""the credential pass the 'authority' keyword argument to its inner credential"""
authority = "authority"
with mock.patch.dict("os.environ", {variable: "foo" for variable in environment_variables}, clear=True):
with mock.patch(EnvironmentCredential.__module__ + "." + credential_name) as mock_credential:
EnvironmentCredential(authority=authority)
assert mock_credential.call_count == 1
_, kwargs = mock_credential.call_args
assert kwargs["authority"] == authority
def test_client_secret_configuration():
"""the credential should pass expected values and any keyword arguments to its inner credential"""
client_id = "client-id"
client_secret = "..."
tenant_id = "tenant_id"
bar = "bar"
environment = {
EnvironmentVariables.AZURE_CLIENT_ID: client_id,
EnvironmentVariables.AZURE_CLIENT_SECRET: client_secret,
EnvironmentVariables.AZURE_TENANT_ID: tenant_id,
}
with mock.patch(EnvironmentCredential.__module__ + ".ClientSecretCredential") as mock_credential:
with mock.patch.dict("os.environ", environment, clear=True):
EnvironmentCredential(foo=bar)
assert mock_credential.call_count == 1
_, kwargs = mock_credential.call_args
assert kwargs["client_id"] == client_id
assert kwargs["client_secret"] == client_secret
assert kwargs["tenant_id"] == tenant_id
assert kwargs["foo"] == bar
def test_certificate_configuration():
"""the credential should pass expected values and any keyword arguments to its inner credential"""
client_id = "client-id"
certificate_path = "..."
tenant_id = "tenant_id"
bar = "bar"
environment = {
EnvironmentVariables.AZURE_CLIENT_ID: client_id,
EnvironmentVariables.AZURE_CLIENT_CERTIFICATE_PATH: certificate_path,
EnvironmentVariables.AZURE_TENANT_ID: tenant_id,
}
with mock.patch(EnvironmentCredential.__module__ + ".CertificateCredential") as mock_credential:
with mock.patch.dict("os.environ", environment, clear=True):
EnvironmentCredential(foo=bar)
assert mock_credential.call_count == 1
_, kwargs = mock_credential.call_args
assert kwargs["client_id"] == client_id
assert kwargs["certificate_path"] == certificate_path
assert kwargs["tenant_id"] == tenant_id
assert kwargs["foo"] == bar
@pytest.mark.asyncio
async def test_client_secret_environment_credential():
client_id = "fake-client-id"
secret = "fake-client-secret"
tenant_id = "fake-tenant-id"
access_token = "***"
transport = async_validating_transport(
requests=[Request(url_substring=tenant_id, required_data={"client_id": client_id, "client_secret": secret})],
responses=[
mock_response(
json_payload={
"token_type": "Bearer",
"expires_in": 42,
"ext_expires_in": 42,
"access_token": access_token,
}
)
],
)
environment = {
EnvironmentVariables.AZURE_CLIENT_ID: client_id,
EnvironmentVariables.AZURE_CLIENT_SECRET: secret,
EnvironmentVariables.AZURE_TENANT_ID: tenant_id,
}
with mock.patch.dict("os.environ", environment, clear=True):
token = await EnvironmentCredential(transport=transport).get_token("scope")
assert token.token == access_token
|