22// Licensed under the MIT License.
33
44using System ;
5+ using System . Collections . Generic ;
56using System . ComponentModel ;
67using System . Diagnostics ;
78using System . Globalization ;
@@ -61,16 +62,27 @@ public async Task AuthenticateWithAzurePowerShellCredential(
6162 }
6263 }
6364
65+ private static IEnumerable < object [ ] > ErrorScenarios ( )
66+ {
67+ yield return new object [ ] { "'pwsh' is not recognized" , AzurePowerShellCredential . PowerShellNotInstalledError } ;
68+ yield return new object [ ] { "pwsh: command not found" , AzurePowerShellCredential . PowerShellNotInstalledError } ;
69+ yield return new object [ ] { "pwsh: not found" , AzurePowerShellCredential . PowerShellNotInstalledError } ;
70+ yield return new object [ ] { "Run Connect-AzAccount to login" , AzurePowerShellCredential . AzurePowerShellNotLogInError } ;
71+ yield return new object [ ] { "NoAzAccountModule" , AzurePowerShellCredential . AzurePowerShellModuleNotInstalledError } ;
72+ yield return new object [ ] { "Get-AzAccessToken: Run Connect-AzAccount to login." , AzurePowerShellCredential . AzurePowerShellNotLogInError } ;
73+ yield return new object [ ] { "No accounts were found in the cache" , AzurePowerShellCredential . AzurePowerShellNotLogInError } ;
74+ yield return new object [ ] { "cannot retrieve access token" , AzurePowerShellCredential . AzurePowerShellNotLogInError } ;
75+ }
76+
6477 [ Test ]
65- public void AuthenticateWithAzurePowerShellCredential_PwshNotInstalled (
66- [ Values ( "'pwsh' is not recognized" , "pwsh: command not found" , "pwsh: not found" ) ]
67- string errorMessage )
78+ [ TestCaseSource ( nameof ( ErrorScenarios ) ) ]
79+ public void AuthenticateWithAzurePowerShellCredential_ErrorScenarios ( string errorMessage , string expectedError )
6880 {
6981 var testProcess = new TestProcess { Error = errorMessage } ;
7082 AzurePowerShellCredential credential = InstrumentClient (
7183 new AzurePowerShellCredential ( new AzurePowerShellCredentialOptions ( ) , CredentialPipeline . GetInstance ( null ) , new TestProcessService ( testProcess ) ) ) ;
7284 var ex = Assert . ThrowsAsync < CredentialUnavailableException > ( async ( ) => await credential . GetTokenAsync ( new TokenRequestContext ( MockScopes . Default ) ) ) ;
73- Assert . AreEqual ( AzurePowerShellCredential . PowerShellNotInstalledError , ex . Message ) ;
85+ Assert . AreEqual ( expectedError , ex . Message ) ;
7486 }
7587
7688 [ Test ]
@@ -82,10 +94,7 @@ public async Task HandlesAlternateDateTimeFormats([Values("en-US", "nl-NL")] str
8294 {
8395 var ( expectedToken , expectedExpiresOn , processOutput ) = CredentialTestHelpers . CreateTokenForAzurePowerShell ( TimeSpan . FromSeconds ( 30 ) ) ;
8496 TestContext . WriteLine ( processOutput ) ;
85- var testProcess = new TestProcess
86- {
87- Output = processOutput ,
88- } ;
97+ var testProcess = new TestProcess { Output = processOutput , } ;
8998 AzurePowerShellCredential credential = InstrumentClient (
9099 new AzurePowerShellCredential (
91100 new AzurePowerShellCredentialOptions ( ) ,
@@ -138,28 +147,6 @@ public void AuthenticateWithAzurePowerShellCredential_PowerShellNotInstalled(
138147 Assert . AreEqual ( AzurePowerShellCredential . PowerShellNotInstalledError , ex . Message ) ;
139148 }
140149
141- [ Test ]
142- public void AuthenticateWithAzurePowerShellCredential_RunConnectAzAccount (
143- [ Values ( "Run Connect-AzAccount to login" ) ]
144- string errorMessage )
145- {
146- var testProcess = new TestProcess { Error = errorMessage } ;
147- AzurePowerShellCredential credential = InstrumentClient (
148- new AzurePowerShellCredential ( new AzurePowerShellCredentialOptions ( ) , CredentialPipeline . GetInstance ( null ) , new TestProcessService ( testProcess ) ) ) ;
149- var ex = Assert . ThrowsAsync < CredentialUnavailableException > ( async ( ) => await credential . GetTokenAsync ( new TokenRequestContext ( MockScopes . Default ) ) ) ;
150- Assert . AreEqual ( AzurePowerShellCredential . AzurePowerShellNotLogInError , ex . Message ) ;
151- }
152-
153- [ Test ]
154- public void AuthenticateWithAzurePowerShellCredential_AzurePowerShellModuleNotInstalled ( [ Values ( "NoAzAccountModule" ) ] string message )
155- {
156- var testProcess = new TestProcess { Output = message } ;
157- AzurePowerShellCredential credential = InstrumentClient (
158- new AzurePowerShellCredential ( new AzurePowerShellCredentialOptions ( ) , CredentialPipeline . GetInstance ( null ) , new TestProcessService ( testProcess ) ) ) ;
159- var ex = Assert . ThrowsAsync < CredentialUnavailableException > ( async ( ) => await credential . GetTokenAsync ( new TokenRequestContext ( MockScopes . Default ) ) ) ;
160- Assert . AreEqual ( AzurePowerShellCredential . AzurePowerShellModuleNotInstalledError , ex . Message ) ;
161- }
162-
163150 [ Test ]
164151 public void AuthenticateWithAzurePowerShellCredential_AzurePowerShellUnknownError ( )
165152 {
0 commit comments