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 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
# cspell:ignore oidcrequesturi
import os
from unittest.mock import AsyncMock, patch
import pytest
from azure.core.exceptions import ClientAuthenticationError
from azure.identity import CredentialUnavailableError
from azure.identity._credentials.azure_pipelines import SYSTEM_OIDCREQUESTURI
from azure.identity.aio import AzurePipelinesCredential, ChainedTokenCredential, ClientAssertionCredential
from helpers import GET_TOKEN_METHODS
def test_azure_pipelines_credential_initialize():
system_access_token = "token"
service_connection_id = "connection-id"
tenant_id = "tenant-id"
client_id = "client-id"
credential = AzurePipelinesCredential(
system_access_token=system_access_token,
tenant_id=tenant_id,
client_id=client_id,
service_connection_id=service_connection_id,
)
assert credential._service_connection_id == service_connection_id
assert credential._system_access_token == system_access_token
assert isinstance(credential._client_assertion_credential, ClientAssertionCredential)
@pytest.mark.asyncio
async def test_azure_pipelines_credential_initialize_empty_kwarg():
with patch.dict("os.environ", {}, clear=True):
with pytest.raises(ValueError):
AzurePipelinesCredential(
system_access_token="token", client_id="client-id", tenant_id="tenant-id", service_connection_id=""
)
@pytest.mark.asyncio
async def test_azure_pipelines_credential_context_manager():
transport = AsyncMock()
credential = AzurePipelinesCredential(
system_access_token="token",
client_id="client-id",
tenant_id="tenant-id",
service_connection_id="connection-id",
transport=transport,
)
async with credential:
assert transport.__enter__.called
assert not transport.__exit__.called
assert transport.__exit__.called
@pytest.mark.asyncio
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
async def test_azure_pipelines_credential_missing_system_env_var(get_token_method):
credential = AzurePipelinesCredential(
system_access_token="token",
client_id="client-id",
tenant_id="tenant-id",
service_connection_id="connection-id",
)
with patch.dict("os.environ", {}, clear=True):
with pytest.raises(CredentialUnavailableError) as ex:
await getattr(credential, get_token_method)("scope")
assert f"Missing value for the {SYSTEM_OIDCREQUESTURI} environment variable" in str(ex.value)
@pytest.mark.asyncio
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
async def test_azure_pipelines_credential_in_chain(get_token_method):
mock_credential = AsyncMock()
with patch.dict("os.environ", {}, clear=True):
chain_credential = ChainedTokenCredential(
AzurePipelinesCredential(
system_access_token="token",
tenant_id="tenant-id",
client_id="client-id",
service_connection_id="connection-id",
),
mock_credential,
)
await getattr(chain_credential, get_token_method)("scope")
assert getattr(mock_credential, get_token_method).called
@pytest.mark.asyncio
@pytest.mark.live_test_only("Requires Azure Pipelines environment with configured service connection")
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
async def test_azure_pipelines_credential_authentication(get_token_method):
system_access_token = os.environ.get("SYSTEM_ACCESSTOKEN", "")
service_connection_id = os.environ.get("AZURE_SERVICE_CONNECTION_ID", "")
tenant_id = os.environ.get("AZURE_SERVICE_CONNECTION_TENANT_ID", "")
client_id = os.environ.get("AZURE_SERVICE_CONNECTION_CLIENT_ID", "")
scope = "https://vault.azure.net/.default"
if not all([service_connection_id, tenant_id, client_id]):
pytest.skip("This test requires environment variables to be set")
credential = AzurePipelinesCredential(
system_access_token=system_access_token,
tenant_id=tenant_id,
client_id=client_id,
service_connection_id=service_connection_id,
)
token = await getattr(credential, get_token_method)(scope)
assert token.token
assert isinstance(token.expires_on, int)
@pytest.mark.asyncio
@pytest.mark.live_test_only("Requires Azure Pipelines environment with configured service connection")
@pytest.mark.parametrize("get_token_method", GET_TOKEN_METHODS)
async def test_azure_pipelines_credential_authentication_invalid_token(get_token_method):
system_access_token = "invalid"
service_connection_id = os.environ.get("AZURE_SERVICE_CONNECTION_ID", "")
tenant_id = os.environ.get("AZURE_SERVICE_CONNECTION_TENANT_ID", "")
client_id = os.environ.get("AZURE_SERVICE_CONNECTION_CLIENT_ID", "")
scope = "https://vault.azure.net/.default"
if not all([service_connection_id, tenant_id, client_id]):
pytest.skip("This test requires environment variables to be set")
credential = AzurePipelinesCredential(
system_access_token=system_access_token,
tenant_id=tenant_id,
client_id=client_id,
service_connection_id=service_connection_id,
)
with pytest.raises(ClientAuthenticationError) as ex:
await getattr(credential, get_token_method)(scope)
assert ex.value.status_code == 401
|