public void ConnectionInformationDialog_CreateConnectionInformation_ValidModel_ReturnsConnectionInformation()
        {
            // Setup
            var serverUrl = "https://localhost";
            var username  = "******";
            var inputPlaintextPassword = "******";
            var securePassword         = inputPlaintextPassword.ConvertToSecureString();

            ConnectionInfoDialogViewModel viewModel = ConnectionInformationDialog.CreateViewModel(null);

            viewModel.ServerUrlRaw = serverUrl;
            viewModel.Username     = username;
            viewModel.ValidateCredentials(securePassword);

            // Act
            ConnectionInformation connInfo = ConnectionInformationDialog.CreateConnectionInformation(viewModel, securePassword);

            // Verify
            Assert.IsNotNull(connInfo, "ConnectionInformation should be returned");
            Assert.AreEqual(new Uri(serverUrl), connInfo.ServerUri, "Server URI returned was different");
            Assert.AreEqual(username, connInfo.UserName, "Username returned was different");

            string outputPlaintextPassword = connInfo.Password.ConvertToUnsecureString();

            Assert.AreEqual(inputPlaintextPassword, outputPlaintextPassword, "Password returned was different");
        }
        public void TestSetup()
        {
            Model  = new ConnectionInfoDialogViewModel();
            Events = new List <PropertyChangedEventArgs>();

            Model.PropertyChanged += (sender, e) => Events.Add(e);
        }
Exemplo n.º 3
0
        public void ConnectionInformationDialog_CreateConnectionInformation_ValidModel_ReturnsConnectionInformation()
        {
            // Arrange
            var serverUrl = "https://localhost";
            var username  = "******";
            var inputPlaintextPassword = "******";
            var securePassword         = inputPlaintextPassword.ToSecureString();

            ConnectionInfoDialogViewModel viewModel = ConnectionInformationDialog.CreateViewModel(null);

            viewModel.ServerUrlRaw = serverUrl;
            viewModel.Username     = username;
            viewModel.ValidateCredentials(securePassword);

            // Act
            ConnectionInformation connInfo = ConnectionInformationDialog.CreateConnectionInformation(viewModel, securePassword);

            // Assert
            connInfo.Should().NotBeNull("ConnectionInformation should be returned");
            connInfo.ServerUri.Should().Be(new Uri(serverUrl), "Server URI returned was different");
            connInfo.UserName.Should().Be(username, "Username returned was different");

            string outputPlaintextPassword = connInfo.Password.ToUnsecureString();

            outputPlaintextPassword.Should().Be(inputPlaintextPassword, "Password returned was different");
        }
        public void ConnectDialogViewModel_ShowSecurityWarning_InsecureUriScheme_IsTrue()
        {
            var model = new ConnectionInfoDialogViewModel();

            model.ServerUrlRaw = "http://hostname";
            model.ShowSecurityWarning.Should().BeTrue("Security warning should be visible");

            model.ServerUrlRaw = "https://hostname";
            model.ShowSecurityWarning.Should().BeFalse("Security warning should not be visible");
        }
        public void ConnectDialogViewModel_GetErrorForProperty_ServerUrlRaw_NoValidationErrorWhenPristine()
        {
            // Test case 1: new model has pristine URL
            // Arrange
            var testSubject1 = new ConnectionInfoDialogViewModel();

            // Act
            string validationError1 = GetErrorForProperty(testSubject1, nameof(testSubject1.ServerUrlRaw));

            // Assert
            testSubject1.IsUrlPristine.Should().BeTrue("Server URL should be pristine on initial view model construction");
            validationError1.Should().BeNull("Validation error should be null on initial view model construction");

            // Test case 2a: setting raw server url makes it 'dirty' valid URL
            // Arrange
            var testSubject2a = new ConnectionInfoDialogViewModel
            {
                // Act
                ServerUrlRaw = "http://localhost"
            };
            string validationError2a = GetErrorForProperty(testSubject2a, nameof(testSubject2a.ServerUrlRaw));

            // Assert
            testSubject2a.IsUrlPristine.Should().BeFalse("Server URL should no longer be pristine once set to a valid URL");
            validationError2a.Should().BeNull("Validation error should be null for valid URL");

            // Test case 2b: setting raw server url makes it 'dirty' invalid URL
            // Arrange
            var testSubject2b = new ConnectionInfoDialogViewModel
            {
                // Act
                ServerUrlRaw = "not-a-valid-url"
            };
            string validationError2b = GetErrorForProperty(testSubject2b, nameof(testSubject2b.ServerUrlRaw));

            // Assert
            testSubject2b.IsUrlPristine.Should().BeFalse("Server URL should no longer be pristine once set to an invalid URL");
            validationError2b.Should().NotBeNull("Validation error should not be null for invalid URL");

            // Test case 3: clearing a non-pristine view model should still be non-pristine
            // Arrange
            var testSubject3 = new ConnectionInfoDialogViewModel
            {
                ServerUrlRaw = "blah" // Makes url field dirty
            };

            testSubject3.IsUrlPristine.Should().BeFalse("URL should be made dirty before clearing the field"); // Sanity check

            // Act
            testSubject3.ServerUrlRaw = null; // Clear field

            // Assert
            testSubject3.IsUrlPristine.Should().BeFalse("Server URL should still be non-pristine even after clearing the field");
        }
        public void ConnectDialogViewModel_ShowSecurityWarning_InsecureUriScheme_IsTrue()
        {
#pragma warning disable IDE0017 // Simplify object initialization
            var model = new ConnectionInfoDialogViewModel();
#pragma warning restore IDE0017 // Simplify object initialization

            model.ServerUrlRaw = "http://hostname";
            model.ShowSecurityWarning.Should().BeTrue("Security warning should be visible");

            model.ServerUrlRaw = "https://hostname";
            model.ShowSecurityWarning.Should().BeFalse("Security warning should not be visible");
        }
        public void ConnectionInformationDialog_CreateConnectionInformation_WithExistingConnection()
        {
            // Setup
            var connectionInformation = new ConnectionInformation(new Uri("http://blablabla"), "admin", "P@ssword1".ConvertToSecureString());

            // Act
            ConnectionInfoDialogViewModel viewModel = ConnectionInformationDialog.CreateViewModel(connectionInformation);

            // Verify
            Assert.AreEqual(connectionInformation.ServerUri, viewModel.ServerUrl, "Unexpected ServerUrl");
            Assert.AreEqual(connectionInformation.UserName, viewModel.Username, "Unexpected UserName");
        }
Exemplo n.º 8
0
        public void ConnectionInformationDialog_CreateConnectionInformation_WithExistingConnection()
        {
            // Arrange
            var connectionInformation = new ConnectionInformation(new Uri("http://blablabla"), "admin", "P@ssword1".ToSecureString());

            // Act
            ConnectionInfoDialogViewModel viewModel = ConnectionInformationDialog.CreateViewModel(connectionInformation);

            // Assert
            viewModel.ServerUrl.Should().Be(connectionInformation.ServerUri, "Unexpected ServerUrl");
            viewModel.Username.Should().Be(null, "Not expecting the user name to be populated");
        }
        internal /*for testing purposes*/ static ConnectionInfoDialogViewModel CreateViewModel(ConnectionInformation currentConnection)
        {
            var vm = new ConnectionInfoDialogViewModel();

            if (currentConnection != null)
            {
                vm.ServerUrlRaw = currentConnection.ServerUri.AbsoluteUri;
                vm.Username     = currentConnection.UserName;
            }

            return(vm);
        }
        internal /*for testing purposes*/ static ConnectionInfoDialogViewModel CreateViewModel(ConnectionInformation currentConnection)
        {
            var vm = new ConnectionInfoDialogViewModel();

            if (currentConnection != null)
            {
                vm.ServerUrlRaw = currentConnection.ServerUri.AbsoluteUri;
                // Security: don't populate the user name field, as this might be a token
                // See https://github.com/SonarSource/sonarlint-visualstudio/issues/1081
            }

            return(vm);
        }
        public void ConnectDialogViewModel_ServerUrl_SetOnlyWhenServerUrlRawIsValid()
        {
            // Arrange
            var model = new ConnectionInfoDialogViewModel();

            // Test:
            //   Invalid entry does not set ServerUrl
            model.ServerUrlRaw = "http:/localhost/";
            model.ServerUrl.Should().BeNull();

            //   Valid entry updates ServerUrl
            model.ServerUrlRaw = "http://localhost/";
            model.ServerUrl.Should().NotBeNull();
            model.ServerUrl.Should().Be(new Uri(model.ServerUrlRaw), "Uri property should match raw string property");
        }
        public void ConnectDialogViewModel_ServerUrl_SetOnlyWhenServerUrlRawIsValid()
        {
            // Setup
            var model = new ConnectionInfoDialogViewModel();

            // Test:
            //   Invalid entry does not set ServerUrl
            model.ServerUrlRaw = "http:/localhost/";
            Assert.IsTrue(model.ServerUrl == null, "ServerUrl should not be set");

            //   Valid entry updates ServerUrl
            model.ServerUrlRaw = "http://localhost/";
            Assert.IsFalse(model.ServerUrl == null, "ServerUrl should set");
            Assert.AreEqual(new Uri(model.ServerUrlRaw), model.ServerUrl, "Uri property should match raw string property");
        }
        public void ConnectDialogViewModel_ServerUrl_SetOnlyWhenServerUrlRawIsValid()
        {
            // Arrange
#pragma warning disable IDE0017 // Simplify object initialization
            var model = new ConnectionInfoDialogViewModel();
#pragma warning restore IDE0017 // Simplify object initialization

            // Test:
            //   Invalid entry does not set ServerUrl
            model.ServerUrlRaw = "http:/localhost/";
            model.ServerUrl.Should().BeNull();

            //   Valid entry updates ServerUrl
            model.ServerUrlRaw = "http://localhost/";
            model.ServerUrl.Should().NotBeNull();
            model.ServerUrl.Should().Be(new Uri(model.ServerUrlRaw), "Uri property should match raw string property");
        }
Exemplo n.º 14
0
        public void ConnectionInformationDialog_CreateConnectionInformation_NullArgumentChecks()
        {
            // Arrange
            ConnectionInfoDialogViewModel viewModel = ConnectionInformationDialog.CreateViewModel(null);

            // Test 1: null viewModel
            Exceptions.Expect <ArgumentNullException>(() =>
            {
                ConnectionInformationDialog.CreateConnectionInformation(null, new SecureString());
            });

            // Test 2: null password
            Exceptions.Expect <ArgumentNullException>(() =>
            {
                ConnectionInformationDialog.CreateConnectionInformation(viewModel, null);
            });
        }
        /// <summary>
        /// Opens a window and returns only when the dialog is closed.
        /// </summary>
        /// <param name="currentConnection">Optional, the current connection information to show in the dialog</param>
        /// <returns>Captured connection information if closed successfully, null otherwise.</returns>
        public ConnectionInformation ShowDialog(ConnectionInformation currentConnection)
        {
            ConnectionInfoDialogViewModel vm     = CreateViewModel(currentConnection);
            ConnectionInfoDialogView      dialog = CreateView();

            dialog.ViewModel = vm;
            dialog.Owner     = Application.Current.MainWindow;

            bool?result = dialog.ShowDialog();

            if (result.GetValueOrDefault())
            {
                return(CreateConnectionInformation(vm, dialog.Password));
            }

            return(null);
        }
Exemplo n.º 16
0
        public void ConnectionInformationDialog_CreateConnectionInformation_InvalidModel_ReturnsNull()
        {
            // Arrange
            ConnectionInfoDialogViewModel viewModel = ConnectionInformationDialog.CreateViewModel(null);

            viewModel.IsValid.Should().BeFalse("Empty view model should be invalid");
            var emptyPassword = new SecureString();

            // Act
            ConnectionInformation connInfo;

            using (var assertIgnoreScope = new AssertIgnoreScope())
            {
                connInfo = ConnectionInformationDialog.CreateConnectionInformation(viewModel, emptyPassword);
            }

            // Assert
            connInfo.Should().BeNull("No ConnectionInformation should be returned with an invalid model");
        }
Exemplo n.º 17
0
        /// <summary>
        /// Shows a (modal) dialog which requests connection details from the user
        /// </summary>
        /// <param name="currentConnection">Connection information used to pre-populate fields</param>
        /// <returns>Connection details inputted by user or null if the dialog is dismissed</returns>
        public static ConnectionInformation ShowDialog(ConnectionInformation currentConnection = null)
        {
            var viewModel = new ConnectionInfoDialogViewModel();
            var dialog    = new ConnectionInfoDialogView
            {
                DataContext = viewModel,
                Owner       = Application.Current.MainWindow
            };

            if (null != currentConnection)
            {
                viewModel.ServerUrl = currentConnection.ServerUrl;
                viewModel.Login     = currentConnection.Login;
            }

            bool?result = dialog.ShowDialog();

            if (result.GetValueOrDefault())
            {
                return(new ConnectionInformation(viewModel.ServerUrl, viewModel.Login, viewModel.Password));
            }

            return(null);
        }
        private static string GetErrorForProperty(ConnectionInfoDialogViewModel viewModel, string propertyName)
        {
            var errors = ((INotifyDataErrorInfo)viewModel).GetErrors(propertyName) as IEnumerable <string>;

            return(errors?.FirstOrDefault());
        }
        internal /* testing purposes */ static ConnectionInformation CreateConnectionInformation(ConnectionInfoDialogViewModel viewModel, SecureString password)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException(nameof(viewModel));
            }

            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            ConnectionInformation info = null;

            Debug.Assert(viewModel.IsValid, "View model should be valid when creating connection information");
            if (viewModel.IsValid)
            {
                Uri    serverUri = viewModel.ServerUrl;
                string username  = viewModel.Username;

                info = new ConnectionInformation(serverUri, username, password);
            }

            return(info);
        }