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 static UserModel Map(User user) { var model = new UserModel { Id = user.Id, Email = user.Email, FullName = user.FullName, RoleName = user.RoleName, Language = user.Language, IsActive = user.IsActive, }; return model; }
public async Task<ActionResult> New(UserModel model) { if (!model.IsValid()) { SetPleaseTryAgain(model); return View(model); } model.Language = Thread.CurrentThread.CurrentUICulture.Name; var userId = await _userService.Create(model.FullName, model.Email, model.Password, SetRole.User.Value, model.Language); if (userId == null) { SetPleaseTryAgain(model); return View(model); } _formsAuthenticationService.SignIn(userId.Value, model.FullName, model.Email, SetRole.User.Value, true); return Redirect("/user/detail"); }
public ViewResult New() { var model = new UserModel(); return View(model); }