|
13 | 13 | from azure.identity import ManagedIdentityCredential |
14 | 14 | from azure.identity._constants import Endpoints, EnvironmentVariables |
15 | 15 | from azure.identity._internal.user_agent import USER_AGENT |
| 16 | +import pytest |
16 | 17 |
|
17 | 18 | from helpers import build_aad_response, validating_transport, mock_response, Request |
18 | 19 |
|
@@ -92,8 +93,8 @@ def test_cloud_shell_user_assigned_identity(): |
92 | 93 | assert token == expected_token |
93 | 94 |
|
94 | 95 |
|
95 | | -def test_app_service_2019_08_01(): |
96 | | - """App Service 2019-08-01: IDENTITY_ENDPOINT, IDENTITY_HEADER set""" |
| 96 | +def test_prefers_app_service_2019_08_01(): |
| 97 | + """When the environment is configured for both App Service versions, the credential should prefer the most recent""" |
97 | 98 |
|
98 | 99 | access_token = "****" |
99 | 100 | expires_on = 42 |
@@ -121,13 +122,60 @@ def test_app_service_2019_08_01(): |
121 | 122 | ], |
122 | 123 | ) |
123 | 124 |
|
124 | | - environ = {EnvironmentVariables.IDENTITY_ENDPOINT: endpoint, EnvironmentVariables.IDENTITY_HEADER: secret} |
| 125 | + environ = { |
| 126 | + EnvironmentVariables.IDENTITY_ENDPOINT: endpoint, |
| 127 | + EnvironmentVariables.IDENTITY_HEADER: secret, |
| 128 | + EnvironmentVariables.MSI_ENDPOINT: endpoint, |
| 129 | + EnvironmentVariables.MSI_SECRET: secret, |
| 130 | + } |
125 | 131 | with mock.patch.dict("os.environ", environ, clear=True): |
126 | 132 | token = ManagedIdentityCredential(transport=transport).get_token(scope) |
127 | 133 | assert token.token == access_token |
128 | 134 | assert token.expires_on == expires_on |
129 | 135 |
|
130 | 136 |
|
| 137 | +def test_app_service_2019_08_01(): |
| 138 | + """App Service 2019-08-01: IDENTITY_ENDPOINT, IDENTITY_HEADER set""" |
| 139 | + |
| 140 | + access_token = "****" |
| 141 | + expires_on = 42 |
| 142 | + endpoint = "http://localhost:42/token" |
| 143 | + secret = "expected-secret" |
| 144 | + scope = "scope" |
| 145 | + |
| 146 | + def send(request, **_): |
| 147 | + assert request.url.startswith(endpoint) |
| 148 | + assert request.method == "GET" |
| 149 | + assert request.headers["X-IDENTITY-HEADER"] == secret |
| 150 | + assert request.headers["User-Agent"] == USER_AGENT |
| 151 | + assert request.query["api-version"] == "2019-08-01" |
| 152 | + assert request.query["resource"] == scope |
| 153 | + |
| 154 | + return mock_response( |
| 155 | + json_payload={ |
| 156 | + "access_token": access_token, |
| 157 | + "expires_on": str(expires_on), |
| 158 | + "resource": scope, |
| 159 | + "token_type": "Bearer", |
| 160 | + } |
| 161 | + ) |
| 162 | + |
| 163 | + # when configuration for both API versions is present, the credential should prefer the most recent |
| 164 | + for environment in [ |
| 165 | + {EnvironmentVariables.IDENTITY_ENDPOINT: endpoint, EnvironmentVariables.IDENTITY_HEADER: secret}, |
| 166 | + { |
| 167 | + EnvironmentVariables.IDENTITY_ENDPOINT: endpoint, |
| 168 | + EnvironmentVariables.IDENTITY_HEADER: secret, |
| 169 | + EnvironmentVariables.MSI_ENDPOINT: endpoint, |
| 170 | + EnvironmentVariables.MSI_SECRET: secret, |
| 171 | + }, |
| 172 | + ]: |
| 173 | + with mock.patch.dict("os.environ", environment, clear=True): |
| 174 | + token = ManagedIdentityCredential(transport=mock.Mock(send=send)).get_token(scope) |
| 175 | + assert token.token == access_token |
| 176 | + assert token.expires_on == expires_on |
| 177 | + |
| 178 | + |
131 | 179 | def test_app_service_2017_09_01(): |
132 | 180 | """test parsing of App Service MSI 2017-09-01's eccentric platform-dependent expires_on strings""" |
133 | 181 |
|
|
0 commit comments