1212// limitations under the License.
1313// ----------------------------------------------------------------------------------
1414
15+ using System ;
16+ using System . Collections . Generic ;
17+ using System . Diagnostics ;
18+ using System . Linq ;
19+ using System . Management . Automation ;
20+ using System . Net ;
21+ using System . Net . Http . Headers ;
22+ using System . Net . Sockets ;
23+ using System . Reflection ;
24+ using System . Security ;
25+
26+ using Azure . Identity ;
27+
1528using Microsoft . Azure . Commands . Common . Authentication ;
29+ using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
1630using Microsoft . Azure . Commands . Common . Authentication . Models ;
31+ using Microsoft . Azure . Commands . ResourceManager . Common ;
32+ using Microsoft . Azure . Commands . ScenarioTest ;
1733using Microsoft . Azure . ServiceManagement . Common . Models ;
1834using Microsoft . WindowsAzure . Commands . Common . Test . Mocks ;
1935using Microsoft . WindowsAzure . Commands . ScenarioTest ;
2036using Microsoft . WindowsAzure . Commands . Utilities . Common ;
21- using System . Management . Automation ;
22- using System . Reflection ;
37+
38+ using Moq ;
39+
2340using Xunit ;
2441using Xunit . Abstractions ;
25- using System . Collections . Generic ;
26- using System . Net . Http . Headers ;
27- using System . Diagnostics ;
28- using System ;
29- using System . Security ;
30- using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
31- using Microsoft . Azure . Commands . ResourceManager . Common ;
32- using Microsoft . Azure . Commands . ScenarioTest ;
33- using System . Linq ;
3442
3543namespace Microsoft . Azure . Commands . Profile . Test
3644{
3745 public class LoginCmdletTests
3846 {
39- private MemoryDataStore dataStore ;
4047 private MockCommandRuntime commandRuntimeMock ;
4148
4249 public LoginCmdletTests ( ITestOutputHelper output )
4350 {
4451 TestExecutionHelpers . SetUpSessionAndProfile ( ) ;
4552 XunitTracingInterceptor . AddToContext ( new XunitTracingInterceptor ( output ) ) ;
46- dataStore = new MemoryDataStore ( ) ;
47- AzureSession . Instance . DataStore = dataStore ;
4853 commandRuntimeMock = new MockCommandRuntime ( ) ;
49- AzureRmProfileProvider . Instance . Profile = new AzureRmProfile ( ) ;
54+
55+ AzureSessionTestInitializer . Initialize ( ) ;
5056 }
5157
5258 [ Fact ]
@@ -113,10 +119,13 @@ public void LoginWithInvalidSubscriptionAndTenantThrowsCloudException()
113119 // Setup
114120 cmdlt . CommandRuntime = commandRuntimeMock ;
115121 cmdlt . Subscription = "2c224e7e-3ef5-431d-a57b-e71f4662e3a5" ;
122+ cmdlt . MyInvocation . BoundParameters . Add ( nameof ( cmdlt . Subscription ) , "2c224e7e-3ef5-431d-a57b-e71f4662e3a5" ) ;
116123 cmdlt . Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47" ;
124+ cmdlt . MyInvocation . BoundParameters . Add ( nameof ( cmdlt . Tenant ) , "72f988bf-86f1-41af-91ab-2d7cd011db47" ) ;
117125 cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
118126
119127 // Act
128+
120129 cmdlt . InvokeBeginProcessing ( ) ;
121130 Assert . Throws < PSInvalidOperationException > ( ( ) => cmdlt . ExecuteCmdlet ( ) ) ;
122131 cmdlt . InvokeEndProcessing ( ) ;
@@ -129,7 +138,9 @@ public void LoginWithSubscriptionAndNoTenant()
129138 var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
130139 // Setup
131140 cmdlt . CommandRuntime = commandRuntimeMock ;
132- cmdlt . Subscription = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6" ;
141+ var subscriptionId = "9e223dbe-3399-4e19-88eb-0975f02ac87f" ;
142+ cmdlt . Subscription = subscriptionId ;
143+ cmdlt . MyInvocation . BoundParameters . Add ( nameof ( cmdlt . Subscription ) , subscriptionId ) ;
133144 cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
134145
135146 // Act
@@ -140,6 +151,45 @@ public void LoginWithSubscriptionAndNoTenant()
140151 Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext ) ;
141152 }
142153
154+ [ Fact ]
155+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
156+ //Verify https://github.com/Azure/azure-powershell/issues/13340
157+ public void LoginUseInteractiveThruNonDesktop ( )
158+ {
159+ var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
160+
161+ // Setup
162+ CommonUtilities commonUtilities ;
163+ AzureSession . Instance . TryGetComponent ( nameof ( CommonUtilities ) , out commonUtilities ) ;
164+ try
165+ {
166+ var mockUtilities = new Mock < CommonUtilities > ( ) ;
167+ mockUtilities . Setup ( u => u . IsDesktopSession ( ) ) . Returns ( false ) ;
168+ AzureSession . Instance . RegisterComponent ( nameof ( CommonUtilities ) ,
169+ ( ) => mockUtilities . Object , true ) ;
170+ cmdlt . CommandRuntime = commandRuntimeMock ;
171+ cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
172+
173+ // Act
174+ cmdlt . InvokeBeginProcessing ( ) ;
175+ cmdlt . ExecuteCmdlet ( ) ;
176+ cmdlt . InvokeEndProcessing ( ) ;
177+
178+ //Verify
179+ Assert . Single ( commandRuntimeMock . WarningStream ) ;
180+ Assert . Equal ( "Interactive authentication is not supported in this session, please run cmdlet 'Connect-AzAccount -UseDeviceAuthentication'." , commandRuntimeMock . WarningStream [ 0 ] ) ;
181+ Assert . Null ( AzureRmProfileProvider . Instance . Profile . DefaultContext ) ;
182+ }
183+ finally
184+ {
185+ if ( commonUtilities != null )
186+ {
187+ AzureSession . Instance . RegisterComponent ( nameof ( CommonUtilities ) ,
188+ ( ) => commonUtilities , true ) ;
189+ }
190+ }
191+ }
192+
143193 [ Fact ]
144194 [ Trait ( Category . RunType , Category . LiveOnly ) ]
145195 public void LoginWithNoSubscriptionAndNoTenant ( )
@@ -157,14 +207,45 @@ public void LoginWithNoSubscriptionAndNoTenant()
157207 Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext ) ;
158208 }
159209
210+ [ Fact ]
211+ [ Trait ( Category . RunType , Category . LiveOnly ) ]
212+ //Verify https://github.com/Azure/azure-powershell/issues/13419
213+ public void LoginWithNoSubscriptionAndNoTenantAndFirstPortNotAvailable ( )
214+ {
215+ var listener = new TcpListener ( IPAddress . Loopback , 8400 ) ;
216+
217+ try
218+ {
219+ listener . Start ( ) ;
220+
221+ var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
222+ // Setup
223+ cmdlt . CommandRuntime = commandRuntimeMock ;
224+ cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
225+
226+ // Act
227+ cmdlt . InvokeBeginProcessing ( ) ;
228+ cmdlt . ExecuteCmdlet ( ) ;
229+ cmdlt . InvokeEndProcessing ( ) ;
230+
231+ Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext ) ;
232+ }
233+ finally
234+ {
235+ listener . Stop ( ) ;
236+ }
237+ }
238+
160239 [ Fact ]
161240 [ Trait ( Category . RunType , Category . LiveOnly ) ]
162241 public void LoginWithNoSubscriptionAndTenant ( )
163242 {
164243 var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
165244 // Setup
166245 cmdlt . CommandRuntime = commandRuntimeMock ;
167- cmdlt . Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47" ;
246+ var tenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47" ;
247+ cmdlt . Tenant = tenantId ;
248+ cmdlt . MyInvocation . BoundParameters . Add ( nameof ( cmdlt . Tenant ) , tenantId ) ;
168249 cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
169250
170251 // Act
@@ -182,7 +263,9 @@ public void LoginWithNoSubscriptionAndTenantDomain()
182263 var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
183264 // Setup
184265 cmdlt . CommandRuntime = commandRuntimeMock ;
185- cmdlt . Tenant = "microsoft.onmicrosoft.com" ;
266+ var tenantName = "microsoft.onmicrosoft.com" ;
267+ cmdlt . Tenant = tenantName ;
268+ cmdlt . MyInvocation . BoundParameters . Add ( nameof ( cmdlt . Tenant ) , tenantName ) ;
186269 cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
187270
188271 // Act
@@ -238,14 +321,16 @@ public void LoginWithRbacTenantOnly()
238321 [ Trait ( Category . RunType , Category . LiveOnly ) ]
239322 public void LoginWithRbacSPNAndCertificateOnly ( )
240323 {
324+ AzureSession . Instance . DataStore = new DiskDataStore ( ) ;
241325 var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
242326 // Setup
243327 // NOTE: Use rbac SPN credentials for this test case
244328 cmdlt . CommandRuntime = commandRuntimeMock ;
245329 cmdlt . ServicePrincipal = true ;
246- cmdlt . Tenant = "54826b22-38d6-4fb2-bad9-b7b93a3e9c5a" ;
247- cmdlt . ApplicationId = "99edf981-74c0-4284-bddf-3e9d092ba4e2" ;
248- cmdlt . CertificateThumbprint = "F064B7C7EACC942D10662A5115E047E94FA18498" ;
330+ cmdlt . Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47" ;
331+ cmdlt . ApplicationId = "343d1f33-e5bc-4857-9216-a50144e7da46" ;
332+ //You must have this cert installed on your machine
333+ cmdlt . CertificateThumbprint = "15385B6BF747423330CD8CA5B34022F7AC60B86C" ;
249334 cmdlt . SetParameterSet ( "ServicePrincipalCertificateWithSubscriptionId" ) ;
250335
251336 // Act
@@ -282,7 +367,7 @@ public void GetMultipleTenantsOnLogin()
282367 Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext . Account ) ;
283368 var tenants = AzureRmProfileProvider . Instance . Profile . DefaultContext . Account . GetTenants ( ) ;
284369 Assert . NotNull ( tenants ) ;
285- Assert . Equal ( 2 , tenants . Length ) ;
370+ Assert . True ( tenants . Length >= 2 ) ;
286371 }
287372
288373 [ Fact ]
@@ -292,7 +377,9 @@ public void LoginWithEnvironementName()
292377 var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
293378 // Setup
294379 cmdlt . CommandRuntime = commandRuntimeMock ;
295- cmdlt . Environment = "AzureUSGovernment" ;
380+ var environmentName = "AzureUSGovernment" ;
381+ cmdlt . Environment = environmentName ;
382+ cmdlt . MyInvocation . BoundParameters . Add ( nameof ( cmdlt . Environment ) , environmentName ) ;
296383 cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
297384
298385 // Act
@@ -302,7 +389,7 @@ public void LoginWithEnvironementName()
302389
303390 Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext ) ;
304391 Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext . Environment ) ;
305- Assert . Equal ( "AzureUSGovernment" , AzureRmProfileProvider . Instance . Profile . DefaultContext . Environment . Name ) ;
392+ Assert . Equal ( environmentName , AzureRmProfileProvider . Instance . Profile . DefaultContext . Environment . Name ) ;
306393 }
307394
308395 [ Fact ]
@@ -333,13 +420,10 @@ public void LoginWithCredentialParameterAndMSA()
333420 cmdlt . ExecuteCmdlet ( ) ;
334421 cmdlt . InvokeEndProcessing ( ) ;
335422 }
336- catch ( AadAuthenticationFailedException ex )
423+ catch ( AuthenticationFailedException ex )
337424 {
338425 Assert . NotNull ( ex ) ;
339- Assert . Equal ( "-Credential parameter can only be used with Organization ID credentials. " +
340- "For more information, please refer to http://go.microsoft.com/fwlink/?linkid=331007&clcid=0x409 " +
341- "for more information about the difference between an organizational account and a Microsoft account." ,
342- ex . Message ) ;
426+ Assert . Contains ( "UsernamePasswordCredential authentication failed" , ex . Message ) ;
343427 }
344428 }
345429
@@ -371,8 +455,8 @@ public void LoginWithAccessToken()
371455 public void LoginPopulatesContextList ( )
372456 {
373457 // Before running this test, make sure to clear the contexts on your machine by removing the following two files:
374- // - %APPDATA%/Windows Azure Powershell /AzureRmContext.json
375- // - %APPDATA%/Windows Azure Powershell /AzureRmContextSettings.json
458+ // - %USERPROFILE%/. Azure/AzureRmContext.json
459+ // - %USERPROFILE%/. Azure/AzureRmContextSettings.json
376460 // This will clear all existing contexts on your machine so that this test can re-populate the list with a context for each subscription
377461
378462 var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
@@ -394,7 +478,7 @@ public void LoginPopulatesContextList()
394478
395479 foreach ( var sub in profile . Subscriptions )
396480 {
397- var contextName = string . Format ( "{0} - {1}" , sub . Name , sub . Id ) ;
481+ var contextName = $ " { sub . Name } ( { sub . Id } ) - { sub . ExtendedProperties [ "HomeTenant" ] } - { sub . ExtendedProperties [ "Account" ] } " ;
398482 Assert . True ( profile . Contexts . ContainsKey ( contextName ) ) ;
399483 var context = profile . Contexts [ contextName ] ;
400484 Assert . NotNull ( context ) ;
0 commit comments