1616using System . Collections . Generic ;
1717using System . IO ;
1818using System . Linq ;
19+ using Microsoft . Azure . Subscriptions . Models ;
1920using Microsoft . WindowsAzure . Commands . Common . Models ;
2021using Microsoft . WindowsAzure . Commands . Common . Test . Mocks ;
2122using Microsoft . WindowsAzure . Commands . Profile ;
@@ -46,6 +47,10 @@ public class ProfileClientTests
4647 private AzureSubscription azureSubscription3withoutUser ;
4748 private AzureEnvironment azureEnvironment ;
4849 private AzureAccount azureAccount ;
50+ private TenantIdDescription commonTenant ;
51+ private TenantIdDescription guestTenant ;
52+ private Subscriptions . Models . SubscriptionListOperationResponse . Subscription guestRdfeSubscription ;
53+ private Subscription guestCsmSubscription ;
4954
5055 public ProfileClientTests ( )
5156 {
@@ -285,6 +290,80 @@ public void AddAzureAccountReturnsAccountWithAllSubscriptionsInCsmMode()
285290 Assert . True ( account . GetSubscriptions ( client . Profile ) . Any ( s => s . Id == new Guid ( csmSubscription1 . SubscriptionId ) ) ) ;
286291 }
287292
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+
288367 [ Fact ]
289368 public void GetAzureAccountReturnsAccountWithSubscriptions ( )
290369 {
@@ -1139,7 +1218,7 @@ public void SelectAzureSubscriptionByIdWorks()
11391218 cmdlt . InvokeBeginProcessing ( ) ;
11401219 cmdlt . ExecuteCmdlet ( ) ;
11411220 cmdlt . InvokeEndProcessing ( ) ;
1142-
1221+
11431222 Assert . Equal ( tempSubscriptions [ 2 ] . Id , AzureSession . CurrentContext . Subscription . Id ) ;
11441223 }
11451224
@@ -1184,21 +1263,40 @@ public void ImportPublishSettingsAddsSecondCertificate()
11841263 }
11851264
11861265 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 )
11881269 {
11891270 ClientMocks clientMocks = new ClientMocks ( new Guid ( defaultSubscription ) ) ;
11901271
11911272 clientMocks . LoadRdfeSubscriptions ( rdfeSubscriptions ) ;
11921273 clientMocks . LoadCsmSubscriptions ( csmSubscriptions ) ;
1274+ clientMocks . LoadTenants ( tenants ) ;
11931275
11941276 AzureSession . ClientFactory = new MockClientFactory ( new object [ ] { clientMocks . RdfeSubscriptionClientMock . Object ,
11951277 clientMocks . CsmSubscriptionClientMock . Object } ) ;
11961278
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 ;
11981286 }
11991287
12001288 private void SetMockData ( )
12011289 {
1290+ commonTenant = new TenantIdDescription
1291+ {
1292+ Id = "Common" ,
1293+ TenantId = "Common"
1294+ } ;
1295+ guestTenant = new TenantIdDescription
1296+ {
1297+ Id = "Guest" ,
1298+ TenantId = "Guest"
1299+ } ;
12021300 rdfeSubscription1 = new Subscriptions . Models . SubscriptionListOperationResponse . Subscription
12031301 {
12041302 SubscriptionId = "16E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E" ,
@@ -1213,6 +1311,13 @@ private void SetMockData()
12131311 SubscriptionStatus = Subscriptions . Models . SubscriptionStatus . Active ,
12141312 ActiveDirectoryTenantId = "Common"
12151313 } ;
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+ } ;
12161321 csmSubscription1 = new Azure . Subscriptions . Models . Subscription
12171322 {
12181323 Id = "Subscriptions/36E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E" ,
@@ -1234,6 +1339,13 @@ private void SetMockData()
12341339 State = "Active" ,
12351340 SubscriptionId = "46E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E"
12361341 } ;
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+ } ;
12371349 azureSubscription1 = new AzureSubscription
12381350 {
12391351 Id = new Guid ( "56E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E" ) ,
0 commit comments