12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
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
+
15
28
using Microsoft . Azure . Commands . Common . Authentication ;
29
+ using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
16
30
using Microsoft . Azure . Commands . Common . Authentication . Models ;
31
+ using Microsoft . Azure . Commands . ResourceManager . Common ;
32
+ using Microsoft . Azure . Commands . ScenarioTest ;
17
33
using Microsoft . Azure . ServiceManagement . Common . Models ;
18
34
using Microsoft . WindowsAzure . Commands . Common . Test . Mocks ;
19
35
using Microsoft . WindowsAzure . Commands . ScenarioTest ;
20
36
using Microsoft . WindowsAzure . Commands . Utilities . Common ;
21
- using System . Management . Automation ;
22
- using System . Reflection ;
37
+
38
+ using Moq ;
39
+
23
40
using Xunit ;
24
41
using 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 ;
34
42
35
43
namespace Microsoft . Azure . Commands . Profile . Test
36
44
{
37
45
public class LoginCmdletTests
38
46
{
39
- private MemoryDataStore dataStore ;
40
47
private MockCommandRuntime commandRuntimeMock ;
41
48
42
49
public LoginCmdletTests ( ITestOutputHelper output )
43
50
{
44
51
TestExecutionHelpers . SetUpSessionAndProfile ( ) ;
45
52
XunitTracingInterceptor . AddToContext ( new XunitTracingInterceptor ( output ) ) ;
46
- dataStore = new MemoryDataStore ( ) ;
47
- AzureSession . Instance . DataStore = dataStore ;
48
53
commandRuntimeMock = new MockCommandRuntime ( ) ;
49
- AzureRmProfileProvider . Instance . Profile = new AzureRmProfile ( ) ;
54
+
55
+ AzureSessionTestInitializer . Initialize ( ) ;
50
56
}
51
57
52
58
[ Fact ]
@@ -113,10 +119,13 @@ public void LoginWithInvalidSubscriptionAndTenantThrowsCloudException()
113
119
// Setup
114
120
cmdlt . CommandRuntime = commandRuntimeMock ;
115
121
cmdlt . Subscription = "2c224e7e-3ef5-431d-a57b-e71f4662e3a5" ;
122
+ cmdlt . MyInvocation . BoundParameters . Add ( nameof ( cmdlt . Subscription ) , "2c224e7e-3ef5-431d-a57b-e71f4662e3a5" ) ;
116
123
cmdlt . Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47" ;
124
+ cmdlt . MyInvocation . BoundParameters . Add ( nameof ( cmdlt . Tenant ) , "72f988bf-86f1-41af-91ab-2d7cd011db47" ) ;
117
125
cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
118
126
119
127
// Act
128
+
120
129
cmdlt . InvokeBeginProcessing ( ) ;
121
130
Assert . Throws < PSInvalidOperationException > ( ( ) => cmdlt . ExecuteCmdlet ( ) ) ;
122
131
cmdlt . InvokeEndProcessing ( ) ;
@@ -129,7 +138,9 @@ public void LoginWithSubscriptionAndNoTenant()
129
138
var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
130
139
// Setup
131
140
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 ) ;
133
144
cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
134
145
135
146
// Act
@@ -140,6 +151,45 @@ public void LoginWithSubscriptionAndNoTenant()
140
151
Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext ) ;
141
152
}
142
153
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
+
143
193
[ Fact ]
144
194
[ Trait ( Category . RunType , Category . LiveOnly ) ]
145
195
public void LoginWithNoSubscriptionAndNoTenant ( )
@@ -157,14 +207,45 @@ public void LoginWithNoSubscriptionAndNoTenant()
157
207
Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext ) ;
158
208
}
159
209
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
+
160
239
[ Fact ]
161
240
[ Trait ( Category . RunType , Category . LiveOnly ) ]
162
241
public void LoginWithNoSubscriptionAndTenant ( )
163
242
{
164
243
var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
165
244
// Setup
166
245
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 ) ;
168
249
cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
169
250
170
251
// Act
@@ -182,7 +263,9 @@ public void LoginWithNoSubscriptionAndTenantDomain()
182
263
var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
183
264
// Setup
184
265
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 ) ;
186
269
cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
187
270
188
271
// Act
@@ -238,14 +321,16 @@ public void LoginWithRbacTenantOnly()
238
321
[ Trait ( Category . RunType , Category . LiveOnly ) ]
239
322
public void LoginWithRbacSPNAndCertificateOnly ( )
240
323
{
324
+ AzureSession . Instance . DataStore = new DiskDataStore ( ) ;
241
325
var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
242
326
// Setup
243
327
// NOTE: Use rbac SPN credentials for this test case
244
328
cmdlt . CommandRuntime = commandRuntimeMock ;
245
329
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" ;
249
334
cmdlt . SetParameterSet ( "ServicePrincipalCertificateWithSubscriptionId" ) ;
250
335
251
336
// Act
@@ -282,7 +367,7 @@ public void GetMultipleTenantsOnLogin()
282
367
Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext . Account ) ;
283
368
var tenants = AzureRmProfileProvider . Instance . Profile . DefaultContext . Account . GetTenants ( ) ;
284
369
Assert . NotNull ( tenants ) ;
285
- Assert . Equal ( 2 , tenants . Length ) ;
370
+ Assert . True ( tenants . Length >= 2 ) ;
286
371
}
287
372
288
373
[ Fact ]
@@ -292,7 +377,9 @@ public void LoginWithEnvironementName()
292
377
var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
293
378
// Setup
294
379
cmdlt . CommandRuntime = commandRuntimeMock ;
295
- cmdlt . Environment = "AzureUSGovernment" ;
380
+ var environmentName = "AzureUSGovernment" ;
381
+ cmdlt . Environment = environmentName ;
382
+ cmdlt . MyInvocation . BoundParameters . Add ( nameof ( cmdlt . Environment ) , environmentName ) ;
296
383
cmdlt . SetParameterSet ( "UserWithSubscriptionId" ) ;
297
384
298
385
// Act
@@ -302,7 +389,7 @@ public void LoginWithEnvironementName()
302
389
303
390
Assert . NotNull ( AzureRmProfileProvider . Instance . Profile . DefaultContext ) ;
304
391
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 ) ;
306
393
}
307
394
308
395
[ Fact ]
@@ -333,13 +420,10 @@ public void LoginWithCredentialParameterAndMSA()
333
420
cmdlt . ExecuteCmdlet ( ) ;
334
421
cmdlt . InvokeEndProcessing ( ) ;
335
422
}
336
- catch ( AadAuthenticationFailedException ex )
423
+ catch ( AuthenticationFailedException ex )
337
424
{
338
425
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 ) ;
343
427
}
344
428
}
345
429
@@ -371,8 +455,8 @@ public void LoginWithAccessToken()
371
455
public void LoginPopulatesContextList ( )
372
456
{
373
457
// 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
376
460
// This will clear all existing contexts on your machine so that this test can re-populate the list with a context for each subscription
377
461
378
462
var cmdlt = new ConnectAzureRmAccountCommand ( ) ;
@@ -394,7 +478,7 @@ public void LoginPopulatesContextList()
394
478
395
479
foreach ( var sub in profile . Subscriptions )
396
480
{
397
- var contextName = string . Format ( "{0} - {1}" , sub . Name , sub . Id ) ;
481
+ var contextName = $ " { sub . Name } ( { sub . Id } ) - { sub . ExtendedProperties [ "HomeTenant" ] } - { sub . ExtendedProperties [ "Account" ] } " ;
398
482
Assert . True ( profile . Contexts . ContainsKey ( contextName ) ) ;
399
483
var context = profile . Contexts [ contextName ] ;
400
484
Assert . NotNull ( context ) ;
0 commit comments