Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Created March 24, 2012 07:12
Show Gist options
  • Select an option

  • Save anaisbetts/2179351 to your computer and use it in GitHub Desktop.

Select an option

Save anaisbetts/2179351 to your computer and use it in GitHub Desktop.
public interface ILoginViewModel : IRoutableViewModel
{
string User { get; set; }
string Password { get; set; }
ReactiveAsyncCommand Confirm { get; }
}
public class LoginViewModel : ReactiveObject, ILoginViewModel
{
string _User;
public string User
{
get { return _User; }
set { this.RaiseAndSetIfChanged(x => x.User, value); }
}
string _Password;
public string Password
{
get { return _Password; }
set { this.RaiseAndSetIfChanged(x => x.Password, value); }
}
ObservableAsPropertyHelper<string> _ErrorMessage;
public string ErrorMessage {
get { return _ErrorMessage.Value; }
}
public string UrlPathSegment
{
get { return "login"; }
}
public IScreen HostScreen { get; protected set; }
public ReactiveAsyncCommand Confirm { get; protected set; }
public LoginViewModel(ReactiveAsyncCommand confirm = null)
{
var canConfirm = this.WhenAny(x => x.User, x => x.Password,
(u, p) => !String.IsNullOrWhiteSpace(u.Value) && !String.IsNullOrWhiteSpace(p.Value));
Confirm = confirm ?? new ReactiveAsyncCommand(canConfirm);
var result = Confirm.RegisterAsyncObservable(_ => {
var client = new RestClient("https://api.github.com");
client.Authenticator = new HttpBasicAuthenticator(User, Password);
return client.RequestAsync<GitHubUser>(new RestRequest("user"))
.Do(res => this.Log().Info("User's URL: {0}", res.Data.url))
.Select(res => new Tuple<string, string>(User, Password));
});
MessageBus.Current.RegisterMessageSource(result, "login");
Confirm.ThrownExceptions
.Select(x => x.Message)
.ToProperty(this, x => x.ErrorMessage);
}
}
public class LoginViewModelTests : IEnableLogger
{
[Fact]
public void SuccessfulLoginShouldActuallyLogIn()
{
var fixture = new LoginViewModel();
fixture.User = "xpaulbettsx";
fixture.Password = "[REDACTED]";
fixture.Confirm.CanExecute(null).Should().BeTrue();
fixture.Confirm.Execute(null);
var result = MessageBus.Current.Listen<Tuple<string, string>>("login")
.Timeout(TimeSpan.FromSeconds(10), RxApp.TaskpoolScheduler)
.First();
result.Should().NotBeNull();
result.Item1.Should().Be(fixture.User);
result.Item2.Should().Be(fixture.Password);
}
[Fact]
public void BlankFieldsMeansNoLogin()
{
var fixture = new LoginViewModel();
fixture.Confirm.CanExecute(null).Should().BeFalse();
fixture.User = "Foo";
fixture.Confirm.CanExecute(null).Should().BeFalse();
fixture.User = "";
fixture.Password = "Bar";
fixture.Confirm.CanExecute(null).Should().BeFalse();
fixture.User = "Foo";
fixture.Confirm.CanExecute(null).Should().BeTrue();
}
[Fact]
public void BadPasswordMeansErrorMessage()
{
var fixture = new LoginViewModel();
fixture.User = "herpderp";
fixture.Password = "woefawoeifjwoefijwe";
fixture.Confirm.CanExecute(null).Should().BeTrue();
fixture.Confirm.Execute(null);
var result = fixture.ObservableForProperty(x => x.ErrorMessage)
.Timeout(TimeSpan.FromSeconds(10), RxApp.TaskpoolScheduler)
.First();
this.Log().Info("Error Message: {0}", result.Value);
result.Value.Should().NotBeNullOrEmpty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment