public void CreatesNewPSResourceGroup()
        {
            Mock <ICommandRuntime>   commandRuntimeMock = new Mock <ICommandRuntime>();
            AddAzureASAccountCommand cmdlet             = new AddAzureASAccountCommand()
            {
                CommandRuntime = commandRuntimeMock.Object
            };

            // Setup
            cmdlet.RolloutEnvironment = testAsAzureEnvironment;
            var password = new SecureString();
            var testpwd  = testPassword;

            testpwd.All(c => {
                password.AppendChar(c);
                return(true);
            });
            cmdlet.Credential = new PSCredential(testUser, password);

            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            // Act
            Assert.NotEmpty(AsAzureClientSession.Instance.Profile.Environments);
            Assert.NotNull(AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment]);
            var environment = (AsAzureEnvironment)AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment];

            Assert.Equal("", environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.AdAuthorityBaseUrl]);
            Assert.NotNull(environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.RestartEndpointFormat]);
        }
示例#2
0
        private void DoLogin(AddAzureASAccountCommand addCmdlet)
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            addCmdlet.RolloutEnvironment = testAsAzureEnvironment;
            var password = new SecureString();
            var testpwd  = testPassword;

            testpwd.All(c => {
                password.AppendChar(c);
                return(true);
            });
            addCmdlet.Credential = new PSCredential(testUser, password);
            commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true);

            // Act
            addCmdlet.InvokeBeginProcessing();
            addCmdlet.ExecuteCmdlet();
            AsAzureClientSession.TokenCache.Deserialize(Encoding.ASCII.GetBytes(testToken));
            addCmdlet.InvokeEndProcessing();
        }
示例#3
0
        public void TestAddAzureASAccountCommand()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();
            var addAmdlet = new AddAzureASAccountCommand()
            {
                CommandRuntime = commandRuntimeMock.Object
            };
            var expectedProfile = new AsAzureProfile
            {
                Context = new AsAzureContext(
                    new AsAzureAccount()
                {
                    Id     = testUser,
                    Tenant = null
                },
                    new AsAzureEnvironment(testAsAzureEnvironment))
            };

            expectedProfile.Context.Environment.Endpoints.Add(AsAzureEnvironment.AsRolloutEndpoints.AdAuthorityBaseUrl, AsAzureClientSession.GetAuthorityUrlForEnvironment(expectedProfile.Context.Environment));
            expectedProfile.Context.Environment.Endpoints.Add(AsAzureEnvironment.AsRolloutEndpoints.RestartEndpointFormat, AsAzureClientSession.RestartEndpointPathFormat);
            expectedProfile.Environments.Add(testAsAzureEnvironment, expectedProfile.Context.Environment);
            expectedProfile.Context.TokenCache = Encoding.ASCII.GetBytes(testToken);

            // Setup
            // Clear the the current profile
            AsAzureClientSession.Instance.Profile.Environments.Clear();

            var mockAuthenticationProvider = new Mock <IAsAzureAuthenticationProvider>();

            mockAuthenticationProvider.Setup(
                authProvider => authProvider.GetAadAuthenticatedToken(
                    It.IsAny <AsAzureContext>(),
                    It.IsAny <SecureString>(),
#if NETSTANDARD
                    It.IsAny <Action <string> >(),
#else
                    It.IsAny <PromptBehavior>(),
#endif
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <Uri>())).Returns(testToken);
            AsAzureClientSession.Instance.SetAsAzureAuthenticationProvider(mockAuthenticationProvider.Object);

            addAmdlet.RolloutEnvironment = testAsAzureEnvironment;
            var password = new SecureString();
            var testpwd  = testPassword;
            testpwd.All(c => {
                password.AppendChar(c);
                return(true);
            });
            addAmdlet.Credential = new PSCredential(testUser, password);
            commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true);
            commandRuntimeMock.Setup(f => f.WriteObject(expectedProfile));

            // Act
            addAmdlet.InvokeBeginProcessing();
            Assert.Empty(AsAzureClientSession.Instance.Profile.Environments);
            addAmdlet.ExecuteCmdlet();
            Assert.True(AsAzureClientSession.Instance.Profile.Environments.Count == 1);
            Assert.NotNull(AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment]);

            // Call InvokeBeginProcessing again to get coverage. It should use the existing environment in memory not create a new one.
            addAmdlet.InvokeBeginProcessing();
            Assert.True(AsAzureClientSession.Instance.Profile.Environments.Count == 1);
            Assert.NotNull(AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment]);

            addAmdlet.InvokeEndProcessing();

            var environment = (AsAzureEnvironment)AsAzureClientSession.Instance.Profile.Environments[testAsAzureEnvironment];
            Assert.Equal(environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.AdAuthorityBaseUrl], AsAzureClientSession.GetAuthorityUrlForEnvironment(environment));
            Assert.NotNull(environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.RestartEndpointFormat]);

            commandRuntimeMock.Verify(f => f.WriteObject(AsAzureClientSession.Instance.Profile));
            mockAuthenticationProvider.Verify(authProvider => authProvider.GetAadAuthenticatedToken(It.IsAny <AsAzureContext>(),
                                                                                                    It.IsAny <SecureString>(),
#if NETSTANDARD
                                                                                                    It.IsAny <Action <string> >(),
#else
                                                                                                    It.IsAny <PromptBehavior>(),
#endif
                                                                                                    It.IsAny <string>(),
                                                                                                    It.IsAny <string>(),
                                                                                                    It.IsAny <Uri>()), Times.Once);
        }