public async void new_should_redirect_if_model_is_valid() { // Arrange const string actionName = "New"; var validModel = new UserModel { Id = 1, FullName = "test", Password = "******", Email = "*****@*****.**", Language = Thread.CurrentThread.CurrentUICulture.Name }; var userService = new Mock<IUserService>(); userService.Setup(x => x.Create(validModel.FullName, validModel.Email, validModel.Password, SetRole.User.Value, validModel.Language)) .Returns(Task.FromResult<long?>(1)); var formsAuthenticationService = new Mock<IFormsAuthenticationService>(); formsAuthenticationService.Setup(x => x.SignIn(validModel.Id, validModel.FullName, validModel.Email, SetRole.User.Value, true)); // Act var sut = new UserControllerBuilder().WithUserService(userService.Object) .WithFormsAuthenticationService(formsAuthenticationService.Object) .Build(); var view = await sut.New(validModel) as RedirectResult; // Assert Assert.NotNull(view); Assert.AreEqual(view.Url, "/user/detail"); Assert.IsInstanceOf<BaseController>(sut); sut.AssertPostAttribute(actionName, new[] { typeof(UserModel) }); sut.AssertAllowAnonymousAttribute(actionName, new[] { typeof(UserModel) }); userService.Verify(x => x.Create(validModel.FullName, validModel.Email, validModel.Password, SetRole.User.Value, validModel.Language), Times.Once); formsAuthenticationService.Verify(x => x.SignIn(validModel.Id, validModel.FullName, validModel.Email, SetRole.User.Value, true), Times.Once); }
public async void login_should_redirect_if_model_is_valid() { // Arrange const string actionName = "Login"; const int id = 1; const string email = "*****@*****.**"; const string fullName = "test"; const string password = "******"; var validModel = new LoginModel { Password = password, Email = email }; var userService = new Mock<IUserService>(); userService.Setup(x => x.Authenticate(email, password)) .Returns(() => Task.FromResult(true)); userService.Setup(x => x.GetByEmail(email)) .Returns(() => Task.FromResult(new UserDto { Id = id, FullName = fullName, Email = email, RoleId = SetRole.User.Value })); var formsAuthenticationService = new Mock<IFormsAuthenticationService>(); formsAuthenticationService.Setup(x => x.SignIn(id, fullName, email, SetRole.User.Value, true)); // Act var sut = new UserControllerBuilder().WithUserService(userService.Object) .WithFormsAuthenticationService(formsAuthenticationService.Object) .Build(); var view = await sut.Login(validModel) as RedirectResult; // Assert Assert.NotNull(view); Assert.AreEqual(view.Url, "/"); Assert.IsInstanceOf<BaseController>(sut); userService.Verify(x => x.Authenticate(email, password), Times.Once); formsAuthenticationService.Verify(x => x.SignIn(id, fullName, email, SetRole.User.Value, true), Times.Once); sut.AssertPostAttribute(actionName, new[] { typeof(LoginModel) }); sut.AssertAllowAnonymousAttribute(actionName, new[] { typeof(LoginModel) }); }
public async void password_change_should_return_with_password_change_model_if_model_is_valid() { // Arrange const string actionName = "PasswordChange"; const string email = "*****@*****.**"; const string token = "token"; const string password = "******"; var validModel = new PasswordChangeModel { Email = email, Password = password, Token = token }; var userService = new Mock<IUserService>(); userService.Setup(x => x.IsPasswordResetRequestValid(email, token)) .Returns(() => Task.FromResult(true)); userService.Setup(x => x.ChangePassword(email, token, password)) .Returns(() => Task.FromResult(true)); var formsAuthenticationService = new Mock<IFormsAuthenticationService>(); formsAuthenticationService.Setup(x => x.SignOut()); // Act var sut = new UserControllerBuilder().WithUserService(userService.Object) .WithFormsAuthenticationService(formsAuthenticationService.Object) .BuildWithMockControllerContext(); var view = await sut.PasswordChange(validModel) as ViewResult; // Assert Assert.NotNull(view); Assert.NotNull(view.Model); Assert.IsInstanceOf<BaseController>(sut); Assert.IsAssignableFrom<PasswordChangeModel>(view.Model); userService.Verify(x => x.IsPasswordResetRequestValid(email, token), Times.Once); userService.Verify(x => x.ChangePassword(email, token, password), Times.Once); formsAuthenticationService.Verify(x => x.SignOut(), Times.Once); sut.AssertPostAttribute(actionName, new[] { typeof(PasswordChangeModel) }); sut.AssertAllowAnonymousAttribute(actionName, new[] { typeof(PasswordChangeModel) }); }