Ejemplo n.º 1
0
        public void HasErrorMessageShouldTriggerWhenProviderBreaks()
        {
            _provider.HostAuth("10.10.10.10", 5000, "hello", "world").Returns(x => throw new Exception("example"));

            var model = BuildHostAuthViewModel();

            model.HasErrorMessage.Should().BeFalse();

            model.Port     = "5000";
            model.Username = "******";
            model.Password = "******";
            model.Address  = "10.10.10.10";
            model.Login.Execute().Subscribe(ok => { }, error => { });

            model.HasErrorMessage.Should().BeTrue();
            model.ErrorMessage.Should().Be("example");
        }
Ejemplo n.º 2
0
        public HostAuthViewModel(HostAuthState state, ICloud provider)
        {
            this.ValidationRule(
                x => x.Username,
                name => !string.IsNullOrWhiteSpace(name),
                "User name shouldn't be null or white space.");

            this.ValidationRule(
                x => x.Password,
                pass => !string.IsNullOrWhiteSpace(pass),
                "Password shouldn't be null or white space.");

            this.ValidationRule(
                x => x.Address,
                host => !string.IsNullOrWhiteSpace(host),
                "Host address shouldn't be null or white space.");

            this.ValidationRule(
                x => x.Port,
                port => int.TryParse(port, out _),
                "Port should be a valid integer.");

            Login = ReactiveCommand.CreateFromTask(
                () => provider.HostAuth(Address, int.Parse(Port), Username, Password),
                this.IsValid());

            _isBusy = Login
                      .IsExecuting
                      .ToProperty(this, x => x.IsBusy);

            _errorMessage = Login
                            .ThrownExceptions
                            .Select(exception => exception.Message)
                            .Log(this, $"Host auth error occured in {provider.Name}")
                            .ToProperty(this, x => x.ErrorMessage);

            _hasErrorMessage = Login
                               .ThrownExceptions
                               .Select(exception => true)
                               .Merge(Login.Select(unit => false))
                               .ToProperty(this, x => x.HasErrorMessage);

            Username = state.Username;
            Password = state.Password;
            Address  = state.Address;
            Port     = state.Port;

            this.WhenAnyValue(x => x.Username)
            .Subscribe(name => state.Username = name);
            this.WhenAnyValue(x => x.Password)
            .Subscribe(name => state.Password = name);
            this.WhenAnyValue(x => x.Address)
            .Subscribe(name => state.Address = name);
            this.WhenAnyValue(x => x.Port)
            .Subscribe(name => state.Port = name);
        }