public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
        {
            SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock.Object;
            cmdlt.SubscriptionId = Guid.NewGuid().ToString();
            cmdlt.SubscriptionName = "NewSubscriptionName";
            cmdlt.CurrentStorageAccountName = "NewCloudStorage";
            cmdlt.Certificate = SampleCertificate;

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

            // Verify
            ProfileClient client = new ProfileClient();
            var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
            var newAccount = client.Profile.Accounts[SampleCertificate.Thumbprint];
            Assert.Equal(cmdlt.SubscriptionName, newSubscription.Name);
            Assert.Equal(EnvironmentName.AzureCloud, newSubscription.Environment);
            Assert.Equal(cmdlt.CurrentStorageAccountName, newSubscription.GetProperty(AzureSubscription.Property.StorageAccount));
            
            Assert.Equal(newAccount.Id, newSubscription.Account);
            Assert.Equal(AzureAccount.AccountType.Certificate, newAccount.Type);
            Assert.Equal(SampleCertificate.Thumbprint, newAccount.Id);
            Assert.Equal(cmdlt.SubscriptionId, newAccount.GetProperty(AzureAccount.Property.Subscriptions));
        }
        public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnAdd()
        {
            SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
            // Setup
            ProfileClient client = new ProfileClient();
            client.AddOrSetEnvironment(azureEnvironment);
            client.Profile.Save();

            cmdlt.CommandRuntime = commandRuntimeMock.Object;
            cmdlt.SubscriptionId = Guid.NewGuid().ToString();
            cmdlt.SubscriptionName = "NewSubscriptionName";
            cmdlt.CurrentStorageAccountName = "NewCloudStorage";
            cmdlt.Environment = azureEnvironment.Name;
            cmdlt.Certificate = SampleCertificate;

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

            // Verify
            client = new ProfileClient();
            var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
            Assert.Equal(cmdlt.SubscriptionName, newSubscription.Name);
            Assert.Equal(cmdlt.Environment, newSubscription.Environment);
            Assert.Equal(cmdlt.CurrentStorageAccountName, newSubscription.GetProperty(AzureSubscription.Property.StorageAccount));
        }
        public void SetAzureSubscriptionDerivesEnvironmentFromBothEndpointParameters()
        {
            SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
            // Setup
            ProfileClient client = new ProfileClient();
            client.AddOrSetAccount(azureAccount);
            client.AddOrSetEnvironment(azureEnvironment);
            client.AddOrSetSubscription(azureSubscription1);
            client.Profile.Save();

            cmdlt.CommandRuntime = commandRuntimeMock.Object;
            cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
            cmdlt.CurrentStorageAccountName = "NewCloudStorage";
            cmdlt.ServiceEndpoint = azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement);
            cmdlt.ResourceManagerEndpoint = azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager);

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

            // Verify
            client = new ProfileClient();
            var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
            Assert.Equal(cmdlt.Environment, newSubscription.Environment);
            Assert.Equal(cmdlt.CurrentStorageAccountName, newSubscription.GetProperty(AzureSubscription.Property.StorageAccount));
        }
        public void SetAzureSubscriptionUpdatesSubscriptionWithCertificate()
        {
            SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
            
            // Setup
            ProfileClient client = new ProfileClient();
            client.AddOrSetAccount(azureAccount);
            client.AddOrSetEnvironment(azureEnvironment);
            client.AddOrSetSubscription(azureSubscription1);
            client.Profile.Save();

            cmdlt.CommandRuntime = commandRuntimeMock.Object;
            cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
            cmdlt.CurrentStorageAccountName = "NewCloudStorage";
            cmdlt.Certificate = SampleCertificate;

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

            // Verify
            client = new ProfileClient();
            var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
            var newAccount = client.Profile.Accounts[SampleCertificate.Thumbprint];
            var existingAccount = client.Profile.Accounts[azureAccount.Id];
            Assert.Equal(azureEnvironment.Name, newSubscription.Environment);
            Assert.Equal(cmdlt.CurrentStorageAccountName, newSubscription.GetProperty(AzureSubscription.Property.StorageAccount));

            Assert.Equal(newAccount.Id, newSubscription.Account);
            Assert.Equal(AzureAccount.AccountType.Certificate, newAccount.Type);
            Assert.Equal(SampleCertificate.Thumbprint, newAccount.Id);
            Assert.Equal(cmdlt.SubscriptionId, newAccount.GetProperty(AzureAccount.Property.Subscriptions));

            Assert.Equal(azureAccount.Id, existingAccount.Id);
            Assert.Equal(AzureAccount.AccountType.User, existingAccount.Type);
            Assert.True(existingAccount.GetPropertyAsArray(AzureAccount.Property.Subscriptions).Contains(cmdlt.SubscriptionId));
        }
        public void SetAzureSubscriptionThrowsExceptionWithoutCertificateOnAdd()
        {
            SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
            // Setup
            ProfileClient client = new ProfileClient();
            client.AddOrSetEnvironment(azureEnvironment);
            client.Profile.Save();

            cmdlt.CommandRuntime = commandRuntimeMock.Object;
            cmdlt.SubscriptionId = Guid.NewGuid().ToString();
            cmdlt.SubscriptionName = "NewSubscriptionName";
            cmdlt.CurrentStorageAccountName = "NewCloudStorage";
            cmdlt.Environment = azureEnvironment.Name;

            // Verify
            cmdlt.InvokeBeginProcessing();
            Assert.Throws<ArgumentException>(()=> cmdlt.ExecuteCmdlet());
        }
        public void SetAzureSubscriptionDerivesEnvironmentFromResourcesEndpointParameterOnSet()
        {
            // Setup
            var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
            ProfileClient client = new ProfileClient(profile);
            AzurePSCmdlet.CurrentProfile = profile;
            client.AddOrSetAccount(azureAccount);
            client.AddOrSetEnvironment(azureEnvironment);
            client.AddOrSetSubscription(azureSubscription1);
            client.Profile.Save();
            SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();

            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
            cmdlt.CurrentStorageAccountName = "NewCloudStorage";
            cmdlt.ResourceManagerEndpoint = azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager);

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

            // Verify
            client = new ProfileClient(profile);
            var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
            Assert.Equal(cmdlt.Environment, newSubscription.Environment);
            Assert.Equal(cmdlt.CurrentStorageAccountName, newSubscription.GetProperty(AzureSubscription.Property.StorageAccount));
        }
        public void SetAzureSubscriptionUpdatesSubscriptionWithCertificate()
        {
            RunMockedCmdletTest(() =>
            {

                // Setup
                var profile =
                    new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
                AzureSMCmdlet.CurrentProfile = profile;
                ProfileClient client = new ProfileClient(profile);
                client.AddOrSetAccount(azureAccount);
                client.AddOrSetEnvironment(azureEnvironment);
                client.AddOrSetSubscription(azureSubscription1);
                client.Profile.Save();

                SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();

                cmdlt.CommandRuntime = commandRuntimeMock;
                cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
                cmdlt.CurrentStorageAccountName = "NewCloudStorage";
                cmdlt.Certificate = SampleCertificate;

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

                // Verify
                client =
                    new ProfileClient(
                        new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
                            AzureSession.ProfileFile)));
                var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
                var newAccount = client.Profile.Accounts[SampleCertificate.Thumbprint];
                var existingAccount = client.Profile.Accounts[azureAccount.Id];
                Assert.Equal(azureEnvironment.Name, newSubscription.Environment);
                Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
                    newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));

                Assert.Equal(newAccount.Id, newSubscription.Account);
                Assert.Equal(AzureAccount.AccountType.Certificate, newAccount.Type);
                Assert.Equal(SampleCertificate.Thumbprint, newAccount.Id);
                Assert.Equal(cmdlt.SubscriptionId,
                    newAccount.GetProperty(AzureAccount.Property.Subscriptions));

                Assert.Equal(azureAccount.Id, existingAccount.Id);
                Assert.Equal(AzureAccount.AccountType.User, existingAccount.Type);
                Assert.True(
                    existingAccount.GetPropertyAsArray(AzureAccount.Property.Subscriptions)
                        .Contains(cmdlt.SubscriptionId));
            });
        }
        public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnAdd()
        {
            RunMockedCmdletTest(() =>
            {

                // Setup
                var profile =
                    new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
                AzureSMCmdlet.CurrentProfile = profile;
                ProfileClient client = new ProfileClient(profile);
                client.AddOrSetEnvironment(azureEnvironment);
                client.Profile.Save();
                SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();

                cmdlt.CommandRuntime = commandRuntimeMock;
                cmdlt.SubscriptionId = Guid.NewGuid().ToString();
                cmdlt.SubscriptionName = "NewSubscriptionName";
                cmdlt.CurrentStorageAccountName = "NewCloudStorage";
                cmdlt.Environment = azureEnvironment.Name;
                cmdlt.Certificate = SampleCertificate;

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

                // Verify
                client =
                    new ProfileClient(
                        new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
                            AzureSession.ProfileFile)));
                var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
                Assert.Equal(cmdlt.SubscriptionName, newSubscription.Name);
                Assert.Equal(cmdlt.Environment, newSubscription.Environment);
                Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
                    newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
            });
        }
        public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
        {
            RunMockedCmdletTest(() =>
            {

                var profile =
                    new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
                ProfileClient client = new ProfileClient(profile);
                AzureSMProfileProvider.Instance.Profile = profile;
                SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
                // Setup
                cmdlt.CommandRuntime = commandRuntimeMock;
                cmdlt.SubscriptionId = Guid.NewGuid().ToString();
                cmdlt.SubscriptionName = "NewSubscriptionName";
                cmdlt.CurrentStorageAccountName = "NewCloudStorage";
                cmdlt.Certificate = SampleCertificate;

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

                // Verify
                var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
                var newAccount = client.Profile.Accounts[SampleCertificate.Thumbprint];
                Assert.Equal(cmdlt.SubscriptionName, newSubscription.Name);
                Assert.Equal(EnvironmentName.AzureCloud, newSubscription.Environment);
                Assert.True(
                    newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)
                        .Contains(string.Format("AccountName={0}", cmdlt.CurrentStorageAccountName)));

                Assert.Equal(newAccount.Id, newSubscription.Account);
                Assert.Equal(AzureAccount.AccountType.Certificate, newAccount.Type);
                Assert.Equal(SampleCertificate.Thumbprint, newAccount.Id);
                Assert.Equal(cmdlt.SubscriptionId,
                    newAccount.GetProperty(AzureAccount.Property.Subscriptions));
            });
        }