public async Task LoginPostSendsApplicationUserQueryWithTheCorrectEmail()
        {
            var model = new LoginViewModel { Email = "email" };
            var mediator = new Mock<IMediator>();

            var signInManager = MockHelper.CreateSignInManagerMock(MockHelper.CreateUserManagerMock());
            signInManager.Setup(x => x.PasswordSignInAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.Failed);

            var sut = new AccountController(null, signInManager.Object, null, mediator.Object, null, null);
            await sut.Login(model);

            mediator.Verify(x => x.SendAsync(It.Is<ApplicationUserQuery>(y => y.UserName == model.Email)), Times.Once);
        }
        public async Task LoginPostReturnsLockoutView_WhenUserIsNull_AndResultIsLockedOutIsTrue()
        {
            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<ApplicationUserQuery>())).ReturnsAsync(new ApplicationUser());

            var signInManager = MockHelper.CreateSignInManagerMock(MockHelper.CreateUserManagerMock());
            signInManager.Setup(x => x.PasswordSignInAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.LockedOut);

            var sut = new AccountController(null, signInManager.Object, null, mediator.Object, null, null);

            var result = await sut.Login(new LoginViewModel()) as ViewResult;

            Assert.Equal(result.ViewName, "Lockout");
        }
        public async Task LoginPostRedirectsToCorrectActionWithCorrectRouteValues_WhenUserIsNull_AndResultRequiresTwoFactorIsTrue()
        {
            const string returnUrl = "returnUrl";

            var model = new LoginViewModel { RememberMe = true };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<ApplicationUserQuery>())).ReturnsAsync(new ApplicationUser());

            var signInManager = MockHelper.CreateSignInManagerMock(MockHelper.CreateUserManagerMock());
            signInManager.Setup(x => x.PasswordSignInAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.TwoFactorRequired);

            var routeValueDictionary = new RouteValueDictionary
            {
                ["ReturnUrl"] = returnUrl,
                ["RememberMe"] = model.RememberMe
            };

            var sut = new AccountController(null, signInManager.Object, null, mediator.Object, null, null);

            var result = await sut.Login(model, returnUrl) as RedirectToActionResult;

            Assert.Equal(result.ActionName, nameof(AdminController.SendCode));
            Assert.Equal(result.ControllerName, "Admin");
            Assert.Equal(result.RouteValues, routeValueDictionary);
        }
        public async Task LoginPostInvokesRedirectToLocalWithCorrectParameters_WhenUserIsNull_AndResultSucceeded()
        {
            const string returnUrl = "returnUrl";
            var applicationUser = new ApplicationUser();

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<ApplicationUserQuery>())).ReturnsAsync(applicationUser);

            var signInManager = MockHelper.CreateSignInManagerMock(MockHelper.CreateUserManagerMock());
            signInManager.Setup(x => x.PasswordSignInAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.Success);

            var redirectAccountControllerRequests = new Mock<IRedirectAccountControllerRequests>();
            var urlHelper = new Mock<IUrlHelper>();

            var sut = new AccountController(null, signInManager.Object, null, mediator.Object, null, redirectAccountControllerRequests.Object)
            {
                Url = urlHelper.Object
            };
            await sut.Login(new LoginViewModel(), returnUrl);

            redirectAccountControllerRequests.Verify(x => x.RedirectToLocal(returnUrl, applicationUser, urlHelper.Object));
        }
        public async Task LoginPostInvokesPasswordSignInAsyncWithCorrectParameters_WhenUserIsNull()
        {
            var model = new LoginViewModel { Email = "email", Password = "******", RememberMe = true };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<ApplicationUserQuery>())).ReturnsAsync(new ApplicationUser());

            var signInManager = MockHelper.CreateSignInManagerMock(MockHelper.CreateUserManagerMock());
            signInManager.Setup(x => x.PasswordSignInAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.Failed);

            var sut = new AccountController(null, signInManager.Object, null, mediator.Object, null, null);
            await sut.Login(model);

            signInManager.Verify(x => x.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false), Times.Once);
        }
        public async Task LoginPostReturnsErrorViewWithCorrectTextInViewData_WhenUserIsNotNull_AndUserIsASiteAdmin_AndUsersEmailIsNotConfirmed()
        {
            var applicationUser = new ApplicationUser();
            applicationUser.MakeSiteAdmin();

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<ApplicationUserQuery>())).ReturnsAsync(applicationUser);

            var userManager = MockHelper.CreateUserManagerMock();
            userManager.Setup(x => x.IsEmailConfirmedAsync(applicationUser)).ReturnsAsync(false).Verifiable();

            var signInManager = MockHelper.CreateSignInManagerMock(userManager);
            signInManager.Setup(x => x.PasswordSignInAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>())).ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.Failed);

            var sut = new AccountController(userManager.Object, signInManager.Object, null, mediator.Object, null, null);
            var result = await sut.Login(new LoginViewModel()) as ViewResult;

            Assert.Equal(result.ViewData["Message"], "You must have a confirmed email to log on.");
            Assert.Equal(result.ViewName, "Error");
        }