1414namespace Microsoft . Azure . Services . AppAuthentication . Unit . Tests
1515{
1616 /// <summary>
17- /// Test cases for MsiAccessTokenProvider class. MsiAccessTokenProvider is an internal class.
17+ /// Test cases for MsiAccessTokenProvider class. MsiAccessTokenProvider is an internal class.
1818 /// </summary>
1919 public class MsiAccessTokenProviderTests : IDisposable
2020 {
@@ -48,26 +48,26 @@ public async Task GetTokenUsingManagedIdentityAzureVm(bool specifyUserAssignedMa
4848 expectedAppId = Constants . TestAppId ;
4949 }
5050
51- // MockMsi is being asked to act like response from Azure VM MSI succeeded.
51+ // MockMsi is being asked to act like response from Azure VM MSI succeeded.
5252 MockMsi mockMsi = new MockMsi ( msiTestType ) ;
5353 HttpClient httpClient = new HttpClient ( mockMsi ) ;
5454 MsiAccessTokenProvider msiAccessTokenProvider = new MsiAccessTokenProvider ( httpClient , managedIdentityClientId : managedIdentityArgument ) ;
5555
5656 // Get token.
5757 var authResult = await msiAccessTokenProvider . GetAuthResultAsync ( Constants . KeyVaultResourceId , Constants . TenantId ) . ConfigureAwait ( false ) ;
5858
59- // Check if the principalused and type are as expected.
59+ // Check if the principalused and type are as expected.
6060 Validator . ValidateToken ( authResult . AccessToken , msiAccessTokenProvider . PrincipalUsed , Constants . AppType , Constants . TenantId , expectedAppId , expiresOn : authResult . ExpiresOn ) ;
6161 }
6262
6363 /// <summary>
64- /// If json parse error when aquiring token, an exception should be thrown.
64+ /// If json parse error when aquiring token, an exception should be thrown.
6565 /// </summary>
6666 /// <returns></returns>
6767 [ Fact ]
6868 public async Task ParseErrorMsiGetTokenTest ( )
6969 {
70- // MockMsi is being asked to act like response from Azure VM MSI suceeded.
70+ // MockMsi is being asked to act like response from Azure VM MSI suceeded.
7171 MockMsi mockMsi = new MockMsi ( MockMsi . MsiTestType . MsiAppJsonParseFailure ) ;
7272 HttpClient httpClient = new HttpClient ( mockMsi ) ;
7373 MsiAccessTokenProvider msiAccessTokenProvider = new MsiAccessTokenProvider ( httpClient ) ;
@@ -80,13 +80,13 @@ public async Task ParseErrorMsiGetTokenTest()
8080 }
8181
8282 /// <summary>
83- /// If MSI response if missing the token, an exception should be thrown.
83+ /// If MSI response if missing the token, an exception should be thrown.
8484 /// </summary>
8585 /// <returns></returns>
8686 [ Fact ]
8787 public async Task MsiResponseMissingTokenTest ( )
8888 {
89- // MockMsi is being asked to act like response from Azure VM MSI failed.
89+ // MockMsi is being asked to act like response from Azure VM MSI failed.
9090 MockMsi mockMsi = new MockMsi ( MockMsi . MsiTestType . MsiMissingToken ) ;
9191 HttpClient httpClient = new HttpClient ( mockMsi ) ;
9292 MsiAccessTokenProvider msiAccessTokenProvider = new MsiAccessTokenProvider ( httpClient ) ;
@@ -103,7 +103,7 @@ public async Task MsiResponseMissingTokenTest()
103103 [ InlineData ( false ) ]
104104 public async Task GetTokenUsingManagedIdentityAppServices ( bool specifyUserAssignedManagedIdentity )
105105 {
106- // Setup the environment variables that App Service MSI would setup.
106+ // Setup the environment variables that App Service MSI would setup.
107107 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceEndpointEnv , Constants . MsiEndpoint ) ;
108108 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceHeaderEnv , Constants . ClientSecret ) ;
109109
@@ -125,19 +125,19 @@ public async Task GetTokenUsingManagedIdentityAppServices(bool specifyUserAssign
125125 expectedAppId = Constants . TestAppId ;
126126 }
127127
128- // MockMsi is being asked to act like response from App Service MSI suceeded.
128+ // MockMsi is being asked to act like response from App Service MSI suceeded.
129129 MockMsi mockMsi = new MockMsi ( msiTestType ) ;
130130 HttpClient httpClient = new HttpClient ( mockMsi ) ;
131131 MsiAccessTokenProvider msiAccessTokenProvider = new MsiAccessTokenProvider ( httpClient , managedIdentityClientId : managedIdentityArgument ) ;
132132
133- // Get token. This confirms that the environment variables are being read.
133+ // Get token. This confirms that the environment variables are being read.
134134 var authResult = await msiAccessTokenProvider . GetAuthResultAsync ( Constants . KeyVaultResourceId , Constants . TenantId ) . ConfigureAwait ( false ) ;
135135
136136 Validator . ValidateToken ( authResult . AccessToken , msiAccessTokenProvider . PrincipalUsed , Constants . AppType , Constants . TenantId , expectedAppId , expiresOn : authResult . ExpiresOn ) ;
137137 }
138138
139139 /// <summary>
140- /// Test response when IDENTITY_HEADER in AppServices MSI is invalid.
140+ /// Test response when IDENTITY_HEADER in AppServices MSI is invalid.
141141 /// </summary>
142142 /// <returns></returns>
143143 [ Fact ]
@@ -147,7 +147,7 @@ public async Task UnauthorizedTest()
147147 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceEndpointEnv , Constants . MsiEndpoint ) ;
148148 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceHeaderEnv , Constants . ClientSecret ) ;
149149
150- // MockMsi is being asked to act like response from App Service MSI failed (unauthorized).
150+ // MockMsi is being asked to act like response from App Service MSI failed (unauthorized).
151151 MockMsi mockMsi = new MockMsi ( MockMsi . MsiTestType . MsiAppServicesUnauthorized ) ;
152152 HttpClient httpClient = new HttpClient ( mockMsi ) ;
153153 MsiAccessTokenProvider msiAccessTokenProvider = new MsiAccessTokenProvider ( httpClient ) ;
@@ -159,7 +159,7 @@ public async Task UnauthorizedTest()
159159 }
160160
161161 /// <summary>
162- /// Test that response when MSI request is not valid is as expected.
162+ /// Test that response when MSI request is not valid is as expected.
163163 /// </summary>
164164 /// <returns></returns>
165165 [ Fact ]
@@ -180,7 +180,7 @@ public async Task IncorrectFormatTest()
180180 }
181181
182182 /// <summary>
183- /// If an unexpected http response has been received, ensure exception is thrown.
183+ /// If an unexpected http response has been received, ensure exception is thrown.
184184 /// </summary>
185185 /// <returns></returns>
186186 [ Fact ]
@@ -208,13 +208,13 @@ public async Task AzureVmImdsTimeoutTest()
208208 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceEndpointEnv , null ) ;
209209 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceHeaderEnv , null ) ;
210210
211- MockMsi mockMsi = new MockMsi ( MockMsi . MsiTestType . MsiAzureVmTimeout ) ;
211+ MockMsi mockMsi = new MockMsi ( MockMsi . MsiTestType . MsiAzureVmImdsTimeout ) ;
212212 HttpClient httpClient = new HttpClient ( mockMsi ) ;
213213 MsiAccessTokenProvider msiAccessTokenProvider = new MsiAccessTokenProvider ( httpClient ) ;
214214
215215 var exception = await Assert . ThrowsAsync < AzureServiceTokenProviderException > ( ( ) => Task . Run ( ( ) => msiAccessTokenProvider . GetAuthResultAsync ( Constants . KeyVaultResourceId , Constants . TenantId ) ) ) ;
216216
217- Assert . Contains ( AzureServiceTokenProviderException . MsiEndpointNotListening , exception . Message ) ;
217+ Assert . Contains ( AzureServiceTokenProviderException . MetadataEndpointNotListening , exception . Message ) ;
218218 Assert . DoesNotContain ( AzureServiceTokenProviderException . RetryFailure , exception . Message ) ;
219219 }
220220
@@ -224,7 +224,7 @@ public async Task AzureVmImdsTimeoutTest()
224224 [ InlineData ( MockMsi . MsiTestType . MsiTransientServerError ) ]
225225 internal async Task TransientErrorRetryTest ( MockMsi . MsiTestType testType )
226226 {
227- // To simplify tests, mock as MSI App Services to skip Azure VM IDMS probe request by
227+ // To simplify tests, mock as MSI App Services to skip Azure VM IDMS probe request by
228228 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceEndpointEnv , Constants . MsiEndpoint ) ;
229229 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceHeaderEnv , Constants . ClientSecret ) ;
230230
@@ -254,12 +254,17 @@ internal async Task TransientErrorRetryTest(MockMsi.MsiTestType testType)
254254 }
255255 }
256256
257- [ Fact ]
258- private async Task MsiRetryTimeoutTest ( )
257+ [ Theory ]
258+ [ InlineData ( false ) ]
259+ [ InlineData ( true ) ]
260+ internal async Task MsiRetryTimeoutTest ( bool isAppServices )
259261 {
260- // To simplify tests, mock as MSI App Services to skip Azure VM IDMS probe request
261- Environment . SetEnvironmentVariable ( Constants . MsiAppServiceEndpointEnv , Constants . MsiEndpoint ) ;
262- Environment . SetEnvironmentVariable ( Constants . MsiAppServiceHeaderEnv , Constants . ClientSecret ) ;
262+ if ( isAppServices )
263+ {
264+ // Mock as MSI App Services to skip Azure VM IDMS probe request
265+ Environment . SetEnvironmentVariable ( Constants . MsiAppServiceEndpointEnv , Constants . MsiEndpoint ) ;
266+ Environment . SetEnvironmentVariable ( Constants . MsiAppServiceHeaderEnv , Constants . ClientSecret ) ;
267+ }
263268
264269 int timeoutInSeconds = ( new Random ( ) ) . Next ( 1 , 4 ) ;
265270
@@ -291,11 +296,11 @@ private async Task AppServicesDifferentCultureTest()
291296 // ensure thread culture is NOT using en-US culture (App Services MSI endpoint always uses en-US DateTime format)
292297 Thread . CurrentThread . CurrentCulture = new CultureInfo ( "en-GB" ) ;
293298
294- // Setup the environment variables that App Service MSI would setup.
299+ // Setup the environment variables that App Service MSI would setup.
295300 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceEndpointEnv , Constants . MsiEndpoint ) ;
296301 Environment . SetEnvironmentVariable ( Constants . MsiAppServiceHeaderEnv , Constants . ClientSecret ) ;
297302
298- // MockMsi is being asked to act like response from App Service MSI suceeded.
303+ // MockMsi is being asked to act like response from App Service MSI suceeded.
299304 MockMsi mockMsi = new MockMsi ( MockMsi . MsiTestType . MsiAppServicesSuccess ) ;
300305 HttpClient httpClient = new HttpClient ( mockMsi ) ;
301306 MsiAccessTokenProvider msiAccessTokenProvider = new MsiAccessTokenProvider ( httpClient ) ;
0 commit comments