16
16
using System . Collections . Generic ;
17
17
using System . IO ;
18
18
using System . Linq ;
19
+ using Microsoft . Azure . Subscriptions . Models ;
19
20
using Microsoft . WindowsAzure . Commands . Common . Models ;
20
21
using Microsoft . WindowsAzure . Commands . Common . Test . Mocks ;
21
22
using Microsoft . WindowsAzure . Commands . Profile ;
@@ -46,6 +47,10 @@ public class ProfileClientTests
46
47
private AzureSubscription azureSubscription3withoutUser ;
47
48
private AzureEnvironment azureEnvironment ;
48
49
private AzureAccount azureAccount ;
50
+ private TenantIdDescription commonTenant ;
51
+ private TenantIdDescription guestTenant ;
52
+ private Subscriptions . Models . SubscriptionListOperationResponse . Subscription guestRdfeSubscription ;
53
+ private Subscription guestCsmSubscription ;
49
54
50
55
public ProfileClientTests ( )
51
56
{
@@ -285,6 +290,80 @@ public void AddAzureAccountReturnsAccountWithAllSubscriptionsInCsmMode()
285
290
Assert . True ( account . GetSubscriptions ( client . Profile ) . Any ( s => s . Id == new Guid ( csmSubscription1 . SubscriptionId ) ) ) ;
286
291
}
287
292
293
+ [ Fact ]
294
+ public void AddAzureAccountWithImpersonatedGuestWithNoSubscriptions ( )
295
+ {
296
+ SetMocks ( new [ ] { rdfeSubscription1 } . ToList ( ) , new List < Azure . Subscriptions . Models . Subscription > ( ) ,
297
+ new [ ] { commonTenant , guestTenant } . ToList ( ) ,
298
+ ( userAccount , environment , tenant ) =>
299
+ {
300
+ var token = new MockAccessToken
301
+ {
302
+ UserId = tenant == commonTenant . TenantId ? userAccount . Id : "UserB" ,
303
+ AccessToken = "def" ,
304
+ LoginType = LoginType . OrgId
305
+ } ;
306
+ userAccount . Id = token . UserId ;
307
+ return token ;
308
+ } ) ;
309
+ MockDataStore dataStore = new MockDataStore ( ) ;
310
+ dataStore . VirtualStore [ oldProfileDataPath ] = oldProfileData ;
311
+ ProfileClient . DataStore = dataStore ;
312
+ ProfileClient client = new ProfileClient ( ) ;
313
+
314
+ var account = client . AddAccountAndLoadSubscriptions ( new AzureAccount { Id = "UserA" , Type = AzureAccount . AccountType . User } , AzureEnvironment . PublicEnvironments [ EnvironmentName . AzureCloud ] , null ) ;
315
+
316
+ Assert . Equal ( "UserA" , account . Id ) ;
317
+ Assert . Equal ( 1 , account . GetSubscriptions ( client . Profile ) . Count ) ;
318
+ var subrdfe1 = account . GetSubscriptions ( client . Profile ) . FirstOrDefault ( s => s . Id == new Guid ( rdfeSubscription1 . SubscriptionId ) ) ;
319
+ var userA = client . GetAccount ( "UserA" ) ;
320
+ var userB = client . GetAccount ( "UserB" ) ;
321
+ Assert . NotNull ( userA ) ;
322
+ Assert . NotNull ( userB ) ;
323
+ Assert . Contains < string > ( rdfeSubscription1 . SubscriptionId , userA . GetPropertyAsArray ( AzureAccount . Property . Subscriptions ) , StringComparer . OrdinalIgnoreCase ) ;
324
+ Assert . False ( userB . HasSubscription ( new Guid ( rdfeSubscription1 . SubscriptionId ) ) ) ;
325
+ Assert . NotNull ( subrdfe1 ) ;
326
+ Assert . Equal ( "UserA" , subrdfe1 . Account ) ;
327
+ }
328
+
329
+ [ Fact ]
330
+ public void AddAzureAccountWithImpersonatedGuestWithSubscriptions ( )
331
+ {
332
+ SetMocks ( new [ ] { rdfeSubscription1 , guestRdfeSubscription } . ToList ( ) , new List < Azure . Subscriptions . Models . Subscription > ( ) , new [ ] { commonTenant , guestTenant } . ToList ( ) ,
333
+ ( userAccount , environment , tenant ) =>
334
+ {
335
+ var token = new MockAccessToken
336
+ {
337
+ UserId = tenant == commonTenant . TenantId ? userAccount . Id : "UserB" ,
338
+ AccessToken = "def" ,
339
+ LoginType = LoginType . OrgId
340
+ } ;
341
+ userAccount . Id = token . UserId ;
342
+ return token ;
343
+ } ) ;
344
+ MockDataStore dataStore = new MockDataStore ( ) ;
345
+ dataStore . VirtualStore [ oldProfileDataPath ] = oldProfileData ;
346
+ ProfileClient . DataStore = dataStore ;
347
+ ProfileClient client = new ProfileClient ( ) ;
348
+
349
+ var account = client . AddAccountAndLoadSubscriptions ( new AzureAccount { Id = "UserA" , Type = AzureAccount . AccountType . User } , AzureEnvironment . PublicEnvironments [ EnvironmentName . AzureCloud ] , null ) ;
350
+
351
+ Assert . Equal ( "UserA" , account . Id ) ;
352
+ Assert . Equal ( 1 , account . GetSubscriptions ( client . Profile ) . Count ) ;
353
+ var subrdfe1 = account . GetSubscriptions ( client . Profile ) . FirstOrDefault ( s => s . Id == new Guid ( rdfeSubscription1 . SubscriptionId ) ) ;
354
+ var userA = client . GetAccount ( "UserA" ) ;
355
+ var userB = client . GetAccount ( "UserB" ) ;
356
+ var subGuest = userB . GetSubscriptions ( client . Profile ) . FirstOrDefault ( s => s . Id == new Guid ( guestRdfeSubscription . SubscriptionId ) ) ;
357
+ Assert . NotNull ( userA ) ;
358
+ Assert . NotNull ( userB ) ;
359
+ Assert . Contains < string > ( rdfeSubscription1 . SubscriptionId , userA . GetPropertyAsArray ( AzureAccount . Property . Subscriptions ) , StringComparer . OrdinalIgnoreCase ) ;
360
+ Assert . Contains < string > ( guestRdfeSubscription . SubscriptionId , userB . GetPropertyAsArray ( AzureAccount . Property . Subscriptions ) , StringComparer . OrdinalIgnoreCase ) ;
361
+ Assert . NotNull ( subrdfe1 ) ;
362
+ Assert . NotNull ( subGuest ) ;
363
+ Assert . Equal ( "UserA" , subrdfe1 . Account ) ;
364
+ Assert . Equal ( "UserB" , subGuest . Account ) ;
365
+ }
366
+
288
367
[ Fact ]
289
368
public void GetAzureAccountReturnsAccountWithSubscriptions ( )
290
369
{
@@ -1139,7 +1218,7 @@ public void SelectAzureSubscriptionByIdWorks()
1139
1218
cmdlt . InvokeBeginProcessing ( ) ;
1140
1219
cmdlt . ExecuteCmdlet ( ) ;
1141
1220
cmdlt . InvokeEndProcessing ( ) ;
1142
-
1221
+
1143
1222
Assert . Equal ( tempSubscriptions [ 2 ] . Id , AzureSession . CurrentContext . Subscription . Id ) ;
1144
1223
}
1145
1224
@@ -1184,21 +1263,40 @@ public void ImportPublishSettingsAddsSecondCertificate()
1184
1263
}
1185
1264
1186
1265
private void SetMocks ( List < WindowsAzure . Subscriptions . Models . SubscriptionListOperationResponse . Subscription > rdfeSubscriptions ,
1187
- List < Azure . Subscriptions . Models . Subscription > csmSubscriptions )
1266
+ List < Azure . Subscriptions . Models . Subscription > csmSubscriptions ,
1267
+ List < Azure . Subscriptions . Models . TenantIdDescription > tenants = null ,
1268
+ Func < AzureAccount , AzureEnvironment , string , IAccessToken > tokenProvider = null )
1188
1269
{
1189
1270
ClientMocks clientMocks = new ClientMocks ( new Guid ( defaultSubscription ) ) ;
1190
1271
1191
1272
clientMocks . LoadRdfeSubscriptions ( rdfeSubscriptions ) ;
1192
1273
clientMocks . LoadCsmSubscriptions ( csmSubscriptions ) ;
1274
+ clientMocks . LoadTenants ( tenants ) ;
1193
1275
1194
1276
AzureSession . ClientFactory = new MockClientFactory ( new object [ ] { clientMocks . RdfeSubscriptionClientMock . Object ,
1195
1277
clientMocks . CsmSubscriptionClientMock . Object } ) ;
1196
1278
1197
- AzureSession . AuthenticationFactory = new MockTokenAuthenticationFactory ( ) ;
1279
+ var mockFactory = new MockTokenAuthenticationFactory ( ) ;
1280
+ if ( tokenProvider != null )
1281
+ {
1282
+ mockFactory . TokenProvider = tokenProvider ;
1283
+ }
1284
+
1285
+ AzureSession . AuthenticationFactory = mockFactory ;
1198
1286
}
1199
1287
1200
1288
private void SetMockData ( )
1201
1289
{
1290
+ commonTenant = new TenantIdDescription
1291
+ {
1292
+ Id = "Common" ,
1293
+ TenantId = "Common"
1294
+ } ;
1295
+ guestTenant = new TenantIdDescription
1296
+ {
1297
+ Id = "Guest" ,
1298
+ TenantId = "Guest"
1299
+ } ;
1202
1300
rdfeSubscription1 = new Subscriptions . Models . SubscriptionListOperationResponse . Subscription
1203
1301
{
1204
1302
SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E" ,
@@ -1213,6 +1311,13 @@ private void SetMockData()
1213
1311
SubscriptionStatus = Subscriptions . Models . SubscriptionStatus . Active ,
1214
1312
ActiveDirectoryTenantId = "Common"
1215
1313
} ;
1314
+ guestRdfeSubscription = new Subscriptions . Models . SubscriptionListOperationResponse . Subscription
1315
+ {
1316
+ SubscriptionId = "26E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1C" ,
1317
+ SubscriptionName = "RdfeSub2" ,
1318
+ SubscriptionStatus = Subscriptions . Models . SubscriptionStatus . Active ,
1319
+ ActiveDirectoryTenantId = "Guest"
1320
+ } ;
1216
1321
csmSubscription1 = new Azure . Subscriptions . Models . Subscription
1217
1322
{
1218
1323
Id = "Subscriptions/36E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E" ,
@@ -1234,6 +1339,13 @@ private void SetMockData()
1234
1339
State = "Active" ,
1235
1340
SubscriptionId = "46E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E"
1236
1341
} ;
1342
+ guestCsmSubscription = new Azure . Subscriptions . Models . Subscription
1343
+ {
1344
+ Id = "Subscriptions/76E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1D" ,
1345
+ DisplayName = "CsmGuestSub" ,
1346
+ State = "Active" ,
1347
+ SubscriptionId = "76E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1D"
1348
+ } ;
1237
1349
azureSubscription1 = new AzureSubscription
1238
1350
{
1239
1351
Id = new Guid ( "56E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E" ) ,
0 commit comments