Пример #1
0
        public void GetPsVersionFromUserAgent()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            int preProcessingUserAgentCount = AzureSession.ClientFactory.UserAgents.Count;
            Debug.WriteLine("UserAgents count prior to cmdLet processing = {0}", preProcessingUserAgentCount.ToString());
            foreach (ProductInfoHeaderValue hv in AzureSession.ClientFactory.UserAgents)
            {
                Debug.WriteLine("Product:{0} - Version:{1}", hv.Product.Name, hv.Product.Version);
            }

            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
            cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";

            cmdlt.InvokeBeginProcessing();
            int postProcessingUserAgentCount = AzureSession.ClientFactory.UserAgents.Count;
            Debug.WriteLine("UserAgents count prior to cmdLet post processing = {0}", postProcessingUserAgentCount.ToString());
            Assert.True(AzureSession.ClientFactory.UserAgents.Count >= preProcessingUserAgentCount);
            HashSet<ProductInfoHeaderValue> piHv = AzureSession.ClientFactory.UserAgents;
            string psUserAgentString = string.Empty;

            foreach(ProductInfoHeaderValue hv in piHv)
            {
                if(hv.Product.Name.Equals("PSVersion") && (!string.IsNullOrEmpty(hv.Product.Version)))
                {
                    psUserAgentString = string.Format("{0}-{1}", hv.Product.Name, hv.Product.Version);
                }
            }

            Assert.NotEmpty(psUserAgentString);
            Assert.Contains("PSVersion", psUserAgentString);
        }
Пример #2
0
        public void LoginWithInvalidSubscriptionAndTenantThrowsCloudException()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a5";
            cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";

            // Act
            cmdlt.InvokeBeginProcessing();
            Assert.Throws<PSInvalidOperationException>(() => cmdlt.ExecuteCmdlet());
            cmdlt.InvokeEndProcessing();
        }
Пример #3
0
        public void LoginWithSubscriptionAndNoTenant()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.Context.Tenant.Domain);
        }
Пример #4
0
        public void LoginWithSubscriptionname()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            cmdlt.CommandRuntime   = commandRuntimeMock;
            cmdlt.TenantId         = "72f988bf-86f1-41af-91ab-2d7cd011db47";
            cmdlt.SubscriptionName = "Node CLI Test";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.Context.Tenant.Domain);
        }
Пример #5
0
        public void LoginWithSubscriptionAndNoTenant()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.Subscription   = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
            cmdlt.SetParameterSet("UserWithSubscriptionId");

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
            Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Directory);
        }
Пример #6
0
        public void LoginWithEnvironementName()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            cmdlt.CommandRuntime  = commandRuntimeMock;
            cmdlt.EnvironmentName = "AzureUSGovernment";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Environment);
            Assert.Equal("AzureUSGovernment", AzureRmProfileProvider.Instance.Profile.Context.Environment.Name);
        }
Пример #7
0
        public void LoginWithNoSubscriptionAndTenant()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.TenantId       = "72f988bf-86f1-41af-91ab-2d7cd011db47";
            cmdlt.SetParameterSet("UserWithSubscriptionId");

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
            Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Directory);
        }
Пример #8
0
        public void LoginWithSubscriptionAndTenant()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
            cmdlt.Tenant         = "72f988bf-86f1-41af-91ab-2d7cd011db47";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.Context.Tenant.Domain);
        }
Пример #9
0
        public void LoginWithRbacTenantOnly()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            // NOTE: Use [email protected] credentials for this test case
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.TenantId       = "1449d5b7-8a83-47db-ae4c-9b03e888bad0";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.Equal("rbactest.onmicrosoft.com", AzureRmProfileProvider.Instance.Profile.Context.Tenant.Domain);
            Assert.Equal(cmdlt.TenantId, AzureRmProfileProvider.Instance.Profile.Context.Tenant.Id.ToString());
            Assert.Null(AzureRmProfileProvider.Instance.Profile.Context.Subscription);
        }
Пример #10
0
        public void GetMultipleTenantsOnLogin()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            // NOTE: Use account that has at exactly two tenants
            cmdlt.CommandRuntime = commandRuntimeMock;

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Account);
            var tenants = AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants);

            Assert.NotNull(tenants);
            Assert.Equal(2, tenants.Length);
        }
Пример #11
0
        public void GetMultipleTenantsOnLogin()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            // NOTE: Use [email protected] credentials for this test case
            cmdlt.CommandRuntime = commandRuntimeMock;

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Account);
            var tenants = AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants);

            Assert.NotNull(tenants);
            Assert.Equal(3, tenants.Length);
        }
Пример #12
0
        public void LoginWithAccessToken()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;

            // Obtain an access token by using [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache.ReadItems() in powershell after logging in.
            // Ensure you are using the token with Resource: https://management.core.windows.net/
            string accessTokenEnvironmentVariable = Environment.GetEnvironmentVariable("AZURE_TEST_ACCESS_TOKEN");

            cmdlt.AccessToken = accessTokenEnvironmentVariable;
            cmdlt.AccountId   = "testAccount";
            cmdlt.SetParameterSet("AccessTokenWithSubscriptionId");

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
        }
Пример #13
0
        public void GetMultipleTenantsOnLogin()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            // NOTE: Use account that has at exactly two tenants
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.SetParameterSet("UserWithSubscriptionId");

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext.Account);
            var tenants = AzureRmProfileProvider.Instance.Profile.DefaultContext.Account.GetTenants();

            Assert.NotNull(tenants);
            Assert.Equal(2, tenants.Length);
        }
Пример #14
0
        public void LoginWithCredentialParameterAndMSA()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;

            // Example of environment variable: TEST_AZURE_CREDENTIALS=<subscription-id-value>;<*****@*****.**>;<email-password>"
            string credsEnvironmentVariable = Environment.GetEnvironmentVariable("TEST_AZURE_CREDENTIALS");

            string[] creds = credsEnvironmentVariable.Split(';');

            string userName = creds[1];
            string password = creds[2];

            var securePassword = new SecureString();

            Array.ForEach(password.ToCharArray(), securePassword.AppendChar);

            cmdlt.Credential = new PSCredential(userName, securePassword);
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            // Act
            try
            {
                cmdlt.InvokeBeginProcessing();
                cmdlt.ExecuteCmdlet();
                cmdlt.InvokeEndProcessing();
            }
            catch (AadAuthenticationFailedException ex)
            {
                Assert.NotNull(ex);
                Assert.Equal("-Credential parameter can only be used with Organization ID credentials. " +
                             "For more information, please refer to http://go.microsoft.com/fwlink/?linkid=331007&clcid=0x409 " +
                             "for more information about the difference between an organizational account and a Microsoft account.",
                             ex.Message);
            }
        }
Пример #15
0
        public void ThrowOnUnknownEnvironment()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            // Setup
            cmdlt.CommandRuntime  = commandRuntimeMock;
            cmdlt.EnvironmentName = "unknown";
            var testPassed = false;

            // Act
            try
            {
                cmdlt.InvokeBeginProcessing();
            }
            catch (TargetInvocationException ex)
            {
                Assert.NotNull(ex);
                Assert.NotNull(ex.InnerException);
                Assert.Equal("Unable to find environment with name 'unknown'", ex.InnerException.Message);
                testPassed = true;
            }

            Assert.True(testPassed);
        }
Пример #16
0
        public void GetPsVersionFromUserAgent()
        {
            var cmdlt = new AddAzureRMAccountCommand();

            int preProcessingUserAgentCount = AzureSession.Instance.ClientFactory.UserAgents.Length;

            Debug.WriteLine("UserAgents count prior to cmdLet processing = {0}", preProcessingUserAgentCount.ToString());
            foreach (ProductInfoHeaderValue hv in AzureSession.Instance.ClientFactory.UserAgents)
            {
                Debug.WriteLine("Product:{0} - Version:{1}", hv.Product.Name, hv.Product.Version);
            }

            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.Subscription   = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
            cmdlt.TenantId       = "72f988bf-86f1-41af-91ab-2d7cd011db47";
            cmdlt.SetParameterSet("UserWithSubscriptionId");

            cmdlt.InvokeBeginProcessing();
            int postProcessingUserAgentCount = AzureSession.Instance.ClientFactory.UserAgents.Length;

            Debug.WriteLine("UserAgents count prior to cmdLet post processing = {0}", postProcessingUserAgentCount.ToString());
            Assert.True(AzureSession.Instance.ClientFactory.UserAgents.Length >= preProcessingUserAgentCount);
            IEnumerable <ProductInfoHeaderValue> piHv = AzureSession.Instance.ClientFactory.UserAgents;
            string psUserAgentString = string.Empty;

            foreach (ProductInfoHeaderValue hv in piHv)
            {
                if (hv.Product.Name.Equals("PSVersion") && (!string.IsNullOrEmpty(hv.Product.Version)))
                {
                    psUserAgentString = string.Format("{0}-{1}", hv.Product.Name, hv.Product.Version);
                }
            }

            Assert.NotEmpty(psUserAgentString);
            Assert.Contains("PSVersion", psUserAgentString);
        }
Пример #17
0
        public void ThrowOnUnknownEnvironment()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.EnvironmentName = "unknown";
            var testPassed = false;

            // Act
            try
            {
                cmdlt.InvokeBeginProcessing();
            }
            catch(TargetInvocationException ex)
            {
                Assert.NotNull(ex);
                Assert.NotNull(ex.InnerException);
                Assert.Equal("Unable to find environment with name 'unknown'", ex.InnerException.Message);
                testPassed = true;
            }

            Assert.True(testPassed);
        }
Пример #18
0
        public void LoginWithEnvironementName()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.EnvironmentName = "AzureUSGovernment";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Environment);
            Assert.Equal("AzureUSGovernment", AzureRmProfileProvider.Instance.Profile.Context.Environment.Name);
        }
Пример #19
0
        public void GetMultipleTenantsOnLogin()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            // NOTE: Use [email protected] credentials for this test case
            cmdlt.CommandRuntime = commandRuntimeMock;

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Account);
            var tenants = AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants);
            Assert.NotNull(tenants);
            Assert.Equal(3, tenants.Length);
        }
Пример #20
0
        public void LoginWithRbacSPNAndCertificateOnly()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            // NOTE: Use rbac SPN credentials for this test case
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.ServicePrincipal = true;
            cmdlt.TenantId = "1449d5b7-8a83-47db-ae4c-9b03e888bad0";
            cmdlt.ApplicationId = "20c58db7-4501-44e8-8e76-6febdb400c6b";
            cmdlt.CertificateThumbprint = "F064B7C7EACC942D10662A5115E047E94FA18498";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.Equal(cmdlt.TenantId, AzureRmProfileProvider.Instance.Profile.Context.Tenant.Id.ToString());
            Assert.Equal(cmdlt.ApplicationId, AzureRmProfileProvider.Instance.Profile.Context.Account.Id.ToString());
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Subscription);
            Assert.Equal(
                cmdlt.CertificateThumbprint,
                AzureRmProfileProvider.Instance.Profile.Context.Account.GetProperty(AzureAccount.Property.CertificateThumbprint));
            
        }
Пример #21
0
        public void LoginWithRbacTenantOnly()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            // NOTE: Use [email protected] credentials for this test case
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.TenantId = "1449d5b7-8a83-47db-ae4c-9b03e888bad0";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.Equal("rbactest.onmicrosoft.com", AzureRmProfileProvider.Instance.Profile.Context.Tenant.Domain);
            Assert.Equal(cmdlt.TenantId, AzureRmProfileProvider.Instance.Profile.Context.Tenant.Id.ToString());
            Assert.Null(AzureRmProfileProvider.Instance.Profile.Context.Subscription);
        }
Пример #22
0
        public void LoginWithSubscriptionname()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
            cmdlt.SubscriptionName = "Node CLI Test";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.Context.Tenant.Domain);
        }
Пример #23
0
        public void LoginWithRbacSPNAndCertificateOnly()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            // NOTE: Use rbac SPN credentials for this test case
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.ServicePrincipal = true;
            cmdlt.TenantId = "54826b22-38d6-4fb2-bad9-b7b93a3e9c5a";
            cmdlt.ApplicationId = "99edf981-74c0-4284-bddf-3e9d092ba4e2";
            cmdlt.CertificateThumbprint = "F064B7C7EACC942D10662A5115E047E94FA18498";

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.Equal(cmdlt.TenantId, AzureRmProfileProvider.Instance.Profile.Context.Tenant.Id.ToString());
            Assert.Equal(cmdlt.ApplicationId, AzureRmProfileProvider.Instance.Profile.Context.Account.Id.ToString());
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Subscription);
            Assert.Equal(
                cmdlt.CertificateThumbprint,
                AzureRmProfileProvider.Instance.Profile.Context.Account.GetProperty(AzureAccount.Property.CertificateThumbprint));

        }
Пример #24
0
        public void GetMultipleTenantsOnLogin()
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            // NOTE: Use account that has at exactly two tenants
            cmdlt.CommandRuntime = commandRuntimeMock;

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context.Account);
            var tenants = AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants);
            Assert.NotNull(tenants);
            Assert.Equal(2, tenants.Length);
        }
Пример #25
0
        private void Login(string subscriptionId, string tenantId)
        {
            var cmdlt = new AddAzureRMAccountCommand();
            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.SubscriptionId = subscriptionId;
            cmdlt.TenantId = tenantId;

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

            Assert.NotNull(AzureRmProfileProvider.Instance.Profile.Context);
        }