public async Task ResetPasswordSendsUserByUserIdQueryWithCorrectUserId()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();

            string userId = It.IsAny<string>();
            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))).Returns(new ApplicationUser());
            var controller = new SiteController(null, logger.Object, mediator.Object);

            await controller.ResetPassword(userId);
            mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId)), Times.Once);
        }
        public async Task ResetPasswordAddsCorrectErrorMessagetoViewBagWhenExceptionIsThrown()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();
            mediator.Setup(x => x.Send(It.IsAny<UserByUserIdQuery>()))
                .Throws<Exception>();

            var controller = new SiteController(null, logger.Object, mediator.Object);
            string userId = "1234";
            var result = (ViewResult)await controller.ResetPassword(userId);

            Assert.Equal($"Failed to reset password for {userId}. Exception thrown.", controller.ViewBag.ErrorMessage);
        }
        public async Task ResetPasswordReturnsAViewWhenExcpetionIsThrown()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();
            mediator.Setup(x => x.Send(It.IsAny<UserByUserIdQuery>()))
                .Throws<Exception>();

            var controller = new SiteController(null, logger.Object, mediator.Object);
            string userId = "1234";
            var result = (ViewResult)await controller.ResetPassword(userId);
            Assert.IsType<ViewResult>(result);
        }
        public async Task ResetPasswordReturnsAView()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var user = new ApplicationUser()
            {
                Id = "1234",
                Email = "*****@*****.**",
                UserName = "******"
            };

            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);
            string code = "passcode";
            userManager.Setup(u => u.GeneratePasswordResetTokenAsync(user)).ReturnsAsync(code);

            var controller = new SiteController(userManager.Object, null, mediator.Object);
            controller.SetDefaultHttpContext();
            controller.Url = GetMockUrlHelper("any");
            var result = (ViewResult)await controller.ResetPassword(user.Id);

            Assert.IsType<ViewResult>(result);
        }
        public async Task ResetPasswordInvokesLogErrorWhenExceptionIsThrown()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();
            mediator.Setup(x => x.Send(It.IsAny<UserByUserIdQuery>()))
                .Throws<Exception>();

            var controller = new SiteController(null, logger.Object, mediator.Object);
            var result = (ViewResult)await controller.ResetPassword("1234");

            logger.Verify(l => l.Log<object>(LogLevel.Error, 0,
                It.IsAny<Microsoft.Extensions.Logging.Internal.FormattedLogValues>(), null,
                It.IsAny<Func<object, Exception, string>>()));
        }
        public async Task ResetPasswordAddsCorrectSuccessMessagetoViewBagWhenUserIsNotNull()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var user = new ApplicationUser()
            {
                Id = "1234",
                Email = "*****@*****.**",
                UserName = "******"
            };

            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);
            string code = "passcode";
            userManager.Setup(u => u.GeneratePasswordResetTokenAsync(user)).ReturnsAsync(code);

            var controller = new SiteController(userManager.Object, null, mediator.Object);
            controller.SetDefaultHttpContext();
            controller.Url = GetMockUrlHelper("any");
            var result = await controller.ResetPassword(user.Id);
            Assert.Equal($"Sent password reset email for {user.UserName}.", controller.ViewBag.SuccessMessage);
        }
        public async Task ResetPasswordSendsSendResetPasswordEmailWithCorrectDataWhenUserIsNotNull()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var user = new ApplicationUser()
            {
                Id = "1234",
                Email = "*****@*****.**"
            };

            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);
            string code = "passcode";
            userManager.Setup(u => u.GeneratePasswordResetTokenAsync(user)).ReturnsAsync(code);
            string url = String.Format("Admin/ResetPassword?userId={0}&code={1}", user.Id, code);
            var controller = new SiteController(userManager.Object, null, mediator.Object);
            controller.SetDefaultHttpContext();
            controller.Url = GetMockUrlHelper(url);
            var result = await controller.ResetPassword(user.Id);

            mediator.Verify(x => x.SendAsync(It.Is<AllReady.Areas.Admin.Features.Site.SendResetPasswordEmail>(e => e.Email == user.Email && e.CallbackUrl == url)));
        }
        public async Task ResetPasswordInvokesUrlActionWithCorrectParametersWhenUserIsNotNull()
        {
            const string requestScheme = "requestScheme";
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var user = new ApplicationUser()
            {
                Id = "1234",
                Email = "*****@*****.**"
            };

            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);
            string code = "passcode";
            userManager.Setup(u => u.GeneratePasswordResetTokenAsync(user)).ReturnsAsync(code);
            var controller = new SiteController(userManager.Object, null, mediator.Object);
            var urlHelper = new Mock<IUrlHelper>();
            controller.Url = urlHelper.Object;
            controller.SetFakeHttpRequestSchemeTo(requestScheme);
            var result = await controller.ResetPassword(user.Id);

            urlHelper.Verify(mock => mock.Action(It.Is<UrlActionContext>(uac =>
                uac.Action == "ResetPassword" &&
                uac.Controller == "Admin" &&
                uac.Protocol == requestScheme)),
                Times.Once);
        }
        public async Task ResetPasswordInvokesGeneratePasswordResetTokenAsyncWithCorrectUserWhenUserIsNotNull()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var user = new ApplicationUser()
            {
                Id = "1234",
                Email = "*****@*****.**"
            };

            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);

            var controller = new SiteController(userManager.Object, null, mediator.Object);
            controller.SetDefaultHttpContext();
            controller.Url = GetMockUrlHelper("any");
            var result = await controller.ResetPassword(user.Id);

            userManager.Verify(u => u.GeneratePasswordResetTokenAsync(user));
        }
        public async Task ResetPasswordAddsCorrectErrorMessageToViewBagWhenUserIsNull()
        {
            var mediator = new Mock<IMediator>();
            string userId = "1234";
            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))).Returns<ApplicationUser>(null); 

            var controller = new SiteController(null, null, mediator.Object);
            var result = await controller.ResetPassword(userId);
            Assert.Equal("User not found.", controller.ViewBag.ErrorMessage);
        }