public void TestUnsuccessfulLogin() { // Arrange var userStore = new Mock<IUserStore<ApplicationUser>>(); var userManager = new Mock<UserManager<ApplicationUser>>(userStore.Object); var loginModel = new LoginViewModel { UserName = "******", Password = "******", RememberMe = false }; var returnUrl = "/foo"; var user = new ApplicationUser { UserName = loginModel.UserName }; var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id)); identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName)); userManager.Setup(um => um.FindAsync(loginModel.UserName, loginModel.Password)).Returns(Task.FromResult<ApplicationUser>(null)); var controller = new AccountController(userManager.Object); var helper = new MvcMockHelper(controller); // Act var actionResult = controller.Login(loginModel, returnUrl).Result; // Assert Assert.IsTrue(actionResult is ViewResult); var errors = controller.ModelState.Values.First().Errors; Assert.AreEqual(1, errors.Count()); }
public void TestSuccessfulLogin() { // Arrange var userStore = new Mock<IUserStore<ApplicationUser>>(); var userManager = new Mock<UserManager<ApplicationUser>>(userStore.Object); var loginModel = new LoginViewModel { UserName = "******", Password = "******", RememberMe = false }; var returnUrl = "/foo"; var user = new ApplicationUser { UserName = loginModel.UserName }; var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id)); identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName)); userManager.Setup(um => um.FindAsync(loginModel.UserName, loginModel.Password)).Returns(Task.FromResult(user)); userManager.Setup(um => um.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)).Returns(Task.FromResult(identity)); var controller = new AccountController(userManager.Object); var helper = new MvcMockHelper(controller); // Act var actionResult = controller.Login(loginModel, returnUrl).Result; // Assert var redirectResult = actionResult as RedirectResult; Assert.IsNotNull(redirectResult); Assert.AreEqual(returnUrl, redirectResult.Url); Assert.AreEqual(loginModel.UserName, helper.OwinContext.Authentication.AuthenticationResponseGrant.Identity.Name); Assert.AreEqual(DefaultAuthenticationTypes.ExternalCookie, helper.OwinContext.Authentication.AuthenticationResponseRevoke.AuthenticationTypes.First()); }