public void SingleTenantAndSubscriptionSucceeds()
        {
            var tenants = new List <string> {
                DefaultTenant.ToString()
            };
            var firstList = new List <string> {
                DefaultSubscription.ToString()
            };
            var secondList = firstList;
            var thirdList  = firstList;
            var fourthList = firstList;
            var client     = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList, firstList, firstList);
            var subResults = new List <IAzureSubscription>(client.ListSubscriptions());

            Assert.Equal(2, subResults.Count);
            var tenantResults = client.ListTenants();

            Assert.Equal(1, tenantResults.Count());
            tenantResults = client.ListTenants(DefaultTenant.ToString());
            Assert.Equal(1, tenantResults.Count());
            IAzureSubscription subValue;

            Assert.True(client.TryGetSubscriptionById(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
            Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
            Assert.True(client.TryGetSubscriptionByName(DefaultTenant.ToString(),
                                                        MockSubscriptionClientFactory.GetSubscriptionNameFromId(DefaultSubscription.ToString()),
                                                        out subValue));
            Assert.Equal(DefaultSubscription.ToString(), subValue.Id.ToString());
        }
        public void SetContextBySubscriptionTenantNotExist()
        {
            string tenantA = Guid.NewGuid().ToString(), tenantB = Guid.NewGuid().ToString();
            string subscriptionA = Guid.NewGuid().ToString()
            , subscriptionB      = Guid.NewGuid().ToString()
            , subscriptionC      = Guid.NewGuid().ToString()
            , subscriptionD      = Guid.NewGuid().ToString();
            var clientFactory    = new MockSubscriptionClientFactory();

            Dictionary <string, string> subscriptionListB = GetSecondTenantSubscriptionsJson(tenantB, subscriptionA, subscriptionB, subscriptionC, subscriptionD, tenantA);

            subscriptionClients.Clear();
            subscriptionClients.Enqueue(clientFactory.GetSubscriptionClientVer2019(
                                            null
                                            , null
                                            , MockSubscriptionClientFactory.CreateSubscriptionListsFromJson(subscriptionListB.Values.ToList())
                                            ));

            var mock = new AccountMockClientFactory(() =>
            {
                return(subscriptionClients.Peek());
            }, true);

            mock.MoqClients = true;
            AzureSession.Instance.ClientFactory = mock;

            var client = GetProfileClient();

            Assert.Throws <ArgumentException>(() => client.SetCurrentContext(subscriptionB, tenantB));
        }
        public void SetContextBySubscriptionInManagedByTenants()
        {
            string tenantA = Guid.NewGuid().ToString(), tenantB = Guid.NewGuid().ToString();
            string subscriptionA = Guid.NewGuid().ToString()
            , subscriptionB      = Guid.NewGuid().ToString()
            , subscriptionC      = Guid.NewGuid().ToString()
            , subscriptionD      = Guid.NewGuid().ToString();
            var clientFactory    = new MockSubscriptionClientFactory();

            Dictionary <string, string> subscriptionListA = GetFirstTenantSubscriptionsJson(tenantA, subscriptionA, subscriptionB, subscriptionC, tenantB);
            Dictionary <string, string> subscriptionListB = GetSecondTenantSubscriptionsJson(tenantB, subscriptionA, subscriptionB, subscriptionC, subscriptionD, tenantA);

            subscriptionClients.Clear();
            subscriptionClients.Enqueue(clientFactory.GetSubscriptionClientVer2019(
                                            MockSubscriptionClientFactory.CreateTenantListFromJson(GetTenantsJson(tenantA, tenantB).Values.ToArray())
                                            , null
                                            , MockSubscriptionClientFactory.CreateSubscriptionListsFromJson(subscriptionListA.Values.ToList(), subscriptionListB.Values.ToList())
                                            ));

            var mock = new AccountMockClientFactory(() =>
            {
                return(subscriptionClients.Peek());
            }, true);

            mock.MoqClients = true;
            AzureSession.Instance.ClientFactory = mock;

            var client  = GetProfileClient();
            var context = client.SetCurrentContext(subscriptionC.ToString(), null);

            Assert.Equal("2019-06-01", client.SubscriptionAndTenantClient.ApiVersion);
            Assert.Equal(tenantA, context.Tenant.Id.ToString());
            Assert.Equal(subscriptionC, context.Subscription.Id.ToString());
        }
        public void SetContextBySubscriptionTenant()
        {
            string tenantA = Guid.NewGuid().ToString(), tenantB = Guid.NewGuid().ToString();
            string subscriptionA = Guid.NewGuid().ToString()
            , subscriptionB      = Guid.NewGuid().ToString()
            , subscriptionC      = Guid.NewGuid().ToString()
            , subscriptionD      = Guid.NewGuid().ToString();
            var clientFactory    = new MockSubscriptionClientFactory();

            Dictionary <string, string> subscriptionListA = GetFirstTenantSubscriptionsJson(tenantA, subscriptionA, subscriptionB, subscriptionC, tenantB);

            subscriptionClients.Clear();
            subscriptionClients.Enqueue(clientFactory.GetSubscriptionClientVer2019(
                                            null
                                            , null
                                            , MockSubscriptionClientFactory.CreateSubscriptionListsFromJson(subscriptionListA.Values.ToList())
                                            ));

            var mock = new AccountMockClientFactory(() =>
            {
                return(subscriptionClients.Peek());
            }, true);

            mock.MoqClients = true;
            AzureSession.Instance.ClientFactory = mock;


            var client           = GetProfileClient();
            var subscriptionName = (JObject.Parse(subscriptionListA[subscriptionB]))["displayName"];
            var context          = client.SetCurrentContext(subscriptionName.ToString(), tenantA);

            Assert.Equal("2019-06-01", client.SubscriptionAndTenantClient.ApiVersion);
            Assert.Equal(tenantA, context.Tenant.Id.ToString());
            Assert.Equal(subscriptionB, context.Subscription.Id.ToString());
        }
        public void SubscriptionNameNotInFirstTenant()
        {
            var tenants = new List <string> {
                DefaultTenant.ToString(), Guid.NewGuid().ToString()
            };
            var subscriptionInSecondTenant = Guid.NewGuid().ToString();
            var firstList = new List <string> {
                DefaultSubscription.ToString()
            };
            var secondList = new List <string> {
                Guid.NewGuid().ToString(), subscriptionInSecondTenant
            };
            var client = SetupTestEnvironment(tenants, firstList, secondList);

            ((MockTokenAuthenticationFactory)AzureSession.Instance.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
                                                                                                          new MockAccessToken
            {
                UserId      = "*****@*****.**",
                LoginType   = LoginType.OrgId,
                AccessToken = "bbb",
                TenantId    = DefaultTenant.ToString()
            };

            var listAsyncResponses = new Queue <Func <AzureOperationResponse <IPage <Subscription> > > >();

            listAsyncResponses.Enqueue(() =>
            {
                var sub1 = new Subscription(
                    id: DefaultSubscription.ToString(),
                    subscriptionId: DefaultSubscription.ToString(),
                    tenantId: null,
                    displayName: DefaultSubscriptionName,
                    state: SubscriptionState.Enabled,
                    subscriptionPolicies: null,
                    authorizationSource: null);
                var sub2 = new Subscription(
                    id: subscriptionInSecondTenant,
                    subscriptionId: subscriptionInSecondTenant,
                    tenantId: null,
                    displayName: MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
                    state: SubscriptionState.Enabled,
                    subscriptionPolicies: null,
                    authorizationSource: null);
                return(new AzureOperationResponse <IPage <Subscription> >
                {
                    Body = new MockPage <Subscription>(new List <Subscription> {
                        sub1, sub2
                    })
                });
            });
            MockSubscriptionClientFactory.SetListAsyncResponses(listAsyncResponses);

            var azureRmProfile = client.Login(
                Context.Account,
                Context.Environment,
                null,
                null,
                MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
                null);
        }
        public void SubscriptionIdNotExist()
        {
            var tenants = new List <string> {
                DefaultTenant.ToString()
            };
            var firstList = new List <string> {
                Guid.NewGuid().ToString()
            };
            var client = SetupTestEnvironment(tenants, firstList);

            ((MockTokenAuthenticationFactory)AzureSession.Instance.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
                                                                                                          new MockAccessToken
            {
                UserId      = "*****@*****.**",
                LoginType   = LoginType.OrgId,
                AccessToken = "bbb",
                TenantId    = DefaultTenant.ToString()
            };

            var getAsyncResponses = new Queue <Func <AzureOperationResponse <Subscription> > >();

            getAsyncResponses.Enqueue(() =>
            {
                throw new CloudException("InvalidAuthenticationTokenTenant: The access token is from the wrong issuer");
            });
            MockSubscriptionClientFactory.SetGetAsyncResponses(getAsyncResponses);

            Assert.Throws <PSInvalidOperationException>(() => client.Login(
                                                            Context.Account,
                                                            Context.Environment,
                                                            null,
                                                            DefaultSubscription.ToString(),
                                                            null,
                                                            null));
        }
        private static RMProfileClient SetupTestEnvironment(List <string> tenants, params List <string>[] subscriptionLists)
        {
            AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory(DefaultAccount,
                                                                                    Guid.NewGuid().ToString(), DefaultTenant.ToString());
            var subscriptionList = new Queue <List <string> >(subscriptionLists);
            var clientFactory    = new MockSubscriptionClientFactory(tenants, subscriptionList);
            var mock             = new MockClientFactory(new List <object>
            {
                clientFactory.GetSubscriptionClient()
            }, true);

            mock.MoqClients            = true;
            AzureSession.ClientFactory = mock;
            Context = new AzureContext(new AzureSubscription()
            {
                Account     = DefaultAccount,
                Environment = EnvironmentName.AzureCloud,
                Id          = DefaultSubscription,
                Name        = DefaultSubscriptionName
            },
                                       new AzureAccount()
            {
                Id = DefaultAccount, Type = AzureAccount.AccountType.User
            },
                                       AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
                                       new AzureTenant()
            {
                Domain = DefaultDomain, Id = DefaultTenant
            });
            var profile = new AzureRMProfile();

            profile.Context = Context;
            return(new RMProfileClient(profile));
        }
 private static RMProfileClient SetupTestEnvironment(List<string> tenants, params List<string>[] subscriptionLists)
 {
     AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory(DefaultAccount,
         Guid.NewGuid().ToString(), DefaultTenant.ToString());
     var subscriptionList = new Queue<List<string>>(subscriptionLists);
     var clientFactory = new MockSubscriptionClientFactory(tenants, subscriptionList);
     var mock = new MockClientFactory(new List<object>
     {
         clientFactory.GetSubscriptionClient()
     }, true);
     mock.MoqClients = true;
     AzureSession.ClientFactory = mock;
     Context = new AzureContext(new AzureSubscription()
         {
             Account = DefaultAccount,
             Environment = EnvironmentName.AzureCloud,
             Id = DefaultSubscription,
             Name = DefaultSubscriptionName
         },
         new AzureAccount() { Id = DefaultAccount, Type = AzureAccount.AccountType.User },
         AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
         new AzureTenant() { Domain = DefaultDomain, Id = DefaultTenant });
     var profile = new AzureRMProfile();
     profile.Context = Context;
     return new RMProfileClient(profile);
 }
예제 #9
0
        public void SubscriptionNameNotInFirstTenant()
        {
            var tenants = new List <string> {
                DefaultTenant.ToString(), Guid.NewGuid().ToString()
            };
            var subscriptionInSecondTenant = Guid.NewGuid().ToString();
            var firstList = new List <string> {
                DefaultSubscription.ToString()
            };
            var secondList = new List <string> {
                Guid.NewGuid().ToString(), subscriptionInSecondTenant
            };
            var client = SetupTestEnvironment(tenants, firstList, secondList);

            ((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
                                                                                                 new MockAccessToken
            {
                UserId      = "*****@*****.**",
                LoginType   = LoginType.OrgId,
                AccessToken = "bbb",
                TenantId    = DefaultTenant.ToString()
            };

            var listAsyncResponses = new Queue <Func <SubscriptionListResult> >();

            listAsyncResponses.Enqueue(() =>
            {
                var sub1 = new Subscription
                {
                    Id             = DefaultSubscription.ToString(),
                    SubscriptionId = DefaultSubscription.ToString(),
                    DisplayName    = DefaultSubscriptionName,
                    State          = "enabled"
                };
                var sub2 = new Subscription
                {
                    Id             = subscriptionInSecondTenant,
                    SubscriptionId = subscriptionInSecondTenant,
                    DisplayName    = MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
                    State          = "enabled"
                };
                return(new SubscriptionListResult
                {
                    Subscriptions = new List <Subscription> {
                        sub1, sub2
                    }
                });
            });
            MockSubscriptionClientFactory.SetListAsyncResponses(listAsyncResponses);

            var azureRmProfile = client.Login(
                Context.Account,
                Context.Environment,
                null,
                null,
                MockSubscriptionClientFactory.GetSubscriptionNameFromId(subscriptionInSecondTenant),
                null);
        }
        public void GetAzureRmSubscriptionPaginatedResult()
        {
            var tenants = new List <string> {
                Guid.NewGuid().ToString(), DefaultTenant.ToString()
            };
            var secondsubscriptionInTheFirstTenant = Guid.NewGuid().ToString();
            var firstList = new List <string> {
                DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant
            };
            var secondList = new List <string> {
                Guid.NewGuid().ToString()
            };
            var thirdList = new List <string> {
                DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant
            };
            var fourthList = new List <string> {
                DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant
            };
            var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);

            var dataStore = new MemoryDataStore();

            AzureSession.Instance.DataStore = dataStore;
            var commandRuntimeMock = new MockCommandRuntime();

            AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
            var profile = new AzureRmProfile();

            profile.EnvironmentTable.Add("foo", new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault()));
            profile.DefaultContext = Context;
            var cmdlt = new GetAzureRMSubscriptionCommand();

            // Setup
            cmdlt.DefaultProfile = profile;
            cmdlt.CommandRuntime = commandRuntimeMock;

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            var subscriptionName = MockSubscriptionClientFactory.GetSubscriptionNameFromId(secondList[0]);

            Assert.True(commandRuntimeMock.OutputPipeline.Count == 7);
            Assert.Equal("Disabled", ((PSAzureSubscription)commandRuntimeMock.OutputPipeline[2]).State);
            Assert.Equal(subscriptionName, ((PSAzureSubscription)commandRuntimeMock.OutputPipeline[2]).Name);
        }
        public void LoginBySubscriptionInMultitenantsHomeSecond()
        {
            string tenantA = Guid.NewGuid().ToString(), tenantB = Guid.NewGuid().ToString();
            string subscriptionA = Guid.NewGuid().ToString()
            , subscriptionB      = Guid.NewGuid().ToString()
            , subscriptionC      = Guid.NewGuid().ToString()
            , subscriptionD      = Guid.NewGuid().ToString();
            var clientFactory    = new MockSubscriptionClientFactory();

            var tenantList        = GetTenantsJson(tenantA, tenantB);
            var subscriptionListA = GetFirstTenantSubscriptionsJson(tenantA, subscriptionA, subscriptionB, subscriptionC, tenantB);
            var subscriptionListB = GetSecondTenantSubscriptionsJson(tenantB, subscriptionA, subscriptionB, subscriptionC, subscriptionD, tenantA);

            subscriptionClients.Clear();
            subscriptionClients.Enqueue(clientFactory.GetSubscriptionClientVer2019(
                                            MockSubscriptionClientFactory.CreateTenantListFromJson(tenantList[tenantB], tenantList[tenantA])
                                            , null
                                            , MockSubscriptionClientFactory.CreateSubscriptionListsFromJson(subscriptionListB.Values.ToList(), subscriptionListA.Values.ToList())
                                            ));

            var mock = new AccountMockClientFactory(() =>
            {
                return(subscriptionClients.Peek());
            }, true);

            mock.MoqClients = true;
            AzureSession.Instance.ClientFactory = mock;

            var subscriptionName = (JObject.Parse(subscriptionListA[subscriptionA]))["displayName"];

            var client         = GetProfileClient();
            var azureRmProfile = client.Login(
                Context.Account,
                Context.Environment,
                null,
                null,
                subscriptionName.ToString(),
                null,
                false,
                null);

            Assert.Equal("2019-06-01", client.SubscriptionAndTenantClient.ApiVersion);
            Assert.Equal(tenantA, azureRmProfile.DefaultContext.Tenant.Id.ToString());
            Assert.Equal(subscriptionA, azureRmProfile.DefaultContext.Subscription.Id.ToString());
        }
        public void LoginBySubscriptionTenant()
        {
            string tenantA = Guid.NewGuid().ToString(), tenantB = Guid.NewGuid().ToString();
            string subscriptionA = Guid.NewGuid().ToString()
            , subscriptionB      = Guid.NewGuid().ToString()
            , subscriptionC      = Guid.NewGuid().ToString()
            , subscriptionD      = Guid.NewGuid().ToString();
            var clientFactory    = new MockSubscriptionClientFactory();

            Dictionary <string, string> subscriptionListA = GetFirstTenantSubscriptionsJson(tenantA, subscriptionA, subscriptionB, subscriptionC, tenantB);

            subscriptionClients.Clear();
            subscriptionClients.Enqueue(clientFactory.GetSubscriptionClientVer2019(
                                            null
                                            , MockSubscriptionClientFactory.CreateSubscripitonsFromJson(subscriptionListA[subscriptionB])
                                            , null
                                            ));

            var mock = new AccountMockClientFactory(() =>
            {
                return(subscriptionClients.Peek());
            }, true);

            mock.MoqClients = true;
            AzureSession.Instance.ClientFactory = mock;


            var client         = GetProfileClient();
            var azureRmProfile = client.Login(
                Context.Account,
                Context.Environment,
                tenantA,
                subscriptionB,
                null,
                null,
                false,
                null);

            Assert.Equal("2019-06-01", client.SubscriptionAndTenantClient.ApiVersion);
            Assert.Equal(tenantA, azureRmProfile.DefaultContext.Tenant.Id.ToString());
            Assert.Equal(subscriptionB, azureRmProfile.DefaultContext.Subscription.Id.ToString());
        }
        public void LoginByTenantSubscriptionNotExist()
        {
            string tenantA = Guid.NewGuid().ToString(), tenantB = Guid.NewGuid().ToString();
            string subscriptionA = Guid.NewGuid().ToString()
            , subscriptionB      = Guid.NewGuid().ToString()
            , subscriptionC      = Guid.NewGuid().ToString()
            , subscriptionD      = Guid.NewGuid().ToString();
            var clientFactory    = new MockSubscriptionClientFactory();

            Dictionary <string, string> subscriptionListA = GetFirstTenantSubscriptionsJson(tenantA, subscriptionA, subscriptionB, subscriptionC, tenantB);
            Dictionary <string, string> subscriptionListB = GetSecondTenantSubscriptionsJson(tenantB, subscriptionA, subscriptionB, subscriptionC, subscriptionD, tenantA);

            subscriptionClients.Clear();
            subscriptionClients.Enqueue(clientFactory.GetSubscriptionClientVer2019(
                                            null
                                            , MockSubscriptionClientFactory.CreateSubscripitonsFromJson(null)
                                            , null
                                            ));

            var mock = new AccountMockClientFactory(() =>
            {
                return(subscriptionClients.Peek());
            }, true);

            mock.MoqClients = true;
            AzureSession.Instance.ClientFactory = mock;


            var client = GetProfileClient();

            Assert.Throws <PSInvalidOperationException>(() =>
                                                        client.Login(
                                                            Context.Account,
                                                            Context.Environment,
                                                            tenantB,
                                                            subscriptionB,
                                                            null,
                                                            null,
                                                            false,
                                                            null));
        }
예제 #14
0
        public void SubscriptionIdNotInFirstTenant()
        {
            var tenants = new List <string> {
                DefaultTenant.ToString(), Guid.NewGuid().ToString()
            };
            var subscriptionInSecondTenant = Guid.NewGuid().ToString();
            var firstList = new List <string> {
                DefaultSubscription.ToString()
            };
            var secondList = new List <string> {
                Guid.NewGuid().ToString(), subscriptionInSecondTenant
            };
            var client = SetupTestEnvironment(tenants, firstList, secondList);

            ((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
                                                                                                 new MockAccessToken
            {
                UserId      = "*****@*****.**",
                LoginType   = LoginType.OrgId,
                AccessToken = "bbb",
                TenantId    = DefaultTenant.ToString()
            };

            var getAsyncResponses = new Queue <Func <GetSubscriptionResult> >();

            getAsyncResponses.Enqueue(() =>
            {
                throw new CloudException("InvalidAuthenticationTokenTenant: The access token is from the wrong issuer");
            });
            MockSubscriptionClientFactory.SetGetAsyncResponses(getAsyncResponses);

            var azureRmProfile = client.Login(
                Context.Account,
                Context.Environment,
                null,
                subscriptionInSecondTenant,
                null,
                null);
        }
        public void GetAzureRmSubscriptionByNameMultiplePages()
        {
            var tenants = new List <string> {
                Guid.NewGuid().ToString(), DefaultTenant.ToString()
            };
            var firstTenantSubscriptions = new List <string> {
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString()
            };
            var secondTenantSubscriptions = new List <string> {
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString()
            };

            var firstList = new List <string> {
                firstTenantSubscriptions[0], firstTenantSubscriptions[1]
            };
            var secondList = new List <string> {
                firstTenantSubscriptions[2], firstTenantSubscriptions[3]
            };

            var thirdList = new List <string> {
                secondTenantSubscriptions[0], secondTenantSubscriptions[1]
            };
            var fourthList = new List <string> {
                secondTenantSubscriptions[2], secondTenantSubscriptions[3]
            };

            var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);

            var subscriptionName = MockSubscriptionClientFactory.GetSubscriptionNameFromId(secondTenantSubscriptions[2]);

            var dataStore = new MemoryDataStore();

            AzureSession.Instance.DataStore = dataStore;
            var commandRuntimeMock = new MockCommandRuntime();

            AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
            var profile = new AzureRmProfile();

            profile.EnvironmentTable.Add("foo", new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault()));
            profile.DefaultContext = Context;
            var cmdlt = new GetAzureRMSubscriptionCommand();

            // Setup
            cmdlt.DefaultProfile   = profile;
            cmdlt.CommandRuntime   = commandRuntimeMock;
            cmdlt.SubscriptionName = subscriptionName;

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);

            // Make sure we can get a subscription from the second page of the second tenant by subscription name
            var resultSubscription = (PSAzureSubscription)commandRuntimeMock.OutputPipeline[0];

            Assert.Equal(subscriptionName, resultSubscription.Name);
            Assert.Equal(tenants[1], resultSubscription.TenantId);
        }
예제 #16
0
        public void SubscriptionClientListTenantFallback()
        {
            var tenants = new List <string> {
                DefaultTenant.ToString()
            };
            var firstList = new List <string> {
                Guid.NewGuid().ToString()
            };
            var subscriptionList = new Queue <List <string> >();

            subscriptionList.Enqueue(firstList);
            var clientFactory = new MockSubscriptionClientFactory(tenants, subscriptionList);

            MockSubscriptionClientFactory.TenantListQueueVerLatest = new Queue <Func <AzureOperationResponse <IPage <TenantIdDescription> > > >();
            MockSubscriptionClientFactory.TenantListQueueVerLatest.Enqueue(() =>
            {
                var e       = new CloudException("The api-version is invalid. The supported versions are '2018-09-01,2018-08-01,2018-07-01,2018-06-01,2018-05-01,2018-02-01,2018-01-01,2017-12-01,2017-08-01,2017-06-01,2017-05-10,2017-05-01,2017-03-01,2016-09-01,2016-07-01,2016-06-01,2016-02-01,2015-11-01,2015-01-01,2014-04-01-preview,2014-04-01,2014-01-01,2013-03-01,2014-02-26,2014-04'.");
                e.Body      = new CloudError();
                e.Body.Code = "InvalidApiVersionParameter";
                throw e;
            });

            clientFactory.ListTenantQueueDequeueVerLatest = () =>
            {
                AzureOperationResponse <IPage <TenantIdDescription> > result = null;
                try
                {
                    result = MockSubscriptionClientFactory.TenantListQueueVerLatest.Dequeue().Invoke();
                }
                catch (CloudException e)
                {
                    if (e.Body != null && !string.IsNullOrEmpty(e.Body.Code) && e.Body.Code.Equals("InvalidApiVersionParameter"))
                    {
                        subscriptionClients.Dequeue();
                    }
                    throw e;
                }
                return(Task.FromResult(result));
            };

            subscriptionClients.Clear();
            subscriptionClients.Enqueue(clientFactory.GetSubscriptionClientVerLatest());
            subscriptionClients.Enqueue(clientFactory.GetSubscriptionClientVer2016());

            var mock = new AccountMockClientFactory(() =>
            {
                return(subscriptionClients.Peek());
            }, true);

            mock.MoqClients = true;
            AzureSession.Instance.ClientFactory = mock;

            var client         = GetProfileClient();
            var azureRmProfile = client.Login(
                Context.Account,
                Context.Environment,
                null,
                firstList.First().ToString(),
                null,
                null,
                false,
                null);

            Assert.Equal("2016-06-01", client.SubscriptionAndTenantClient.ApiVersion);
            Assert.Equal(DefaultTenant.ToString(), azureRmProfile.DefaultContext.Tenant.Id.ToString());
            Assert.Equal(firstList.First().ToString(), azureRmProfile.DefaultContext.Subscription.Id.ToString());
        }