public void SetUp()
        {
            ClientId     = Guid.NewGuid();
            EmailService = Substitute.For <IEmailService>();


            UserService = Substitute.For <IWebUserService>();
            UserService.GeneratePasswordResetToken(Arg.Any <LoginUser>()).Returns("Token");

            LoginConfig = Substitute.For <ILoginConfig>();
            LoginConfig.BaseUrl.Returns("https://baseurl");
            LoginConfig.PasswordResetExpiryInHours = 1;


            var dbContextOptions = new DbContextOptionsBuilder <LoginContext>()
                                   .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                                   .Options;

            LoginContext = new LoginContext(dbContextOptions);

            Handler = new RequestPasswordResetHandler(EmailService, LoginConfig, LoginContext, UserService);
        }
        public async Task <Unit> Handle(RequestPasswordResetRequest request, CancellationToken cancellationToken)
        {
            var client = await _loginContext.Clients.SingleOrDefaultAsync(c => c.Id == request.ClientId, cancellationToken);

            var loginUser = await _userService.FindByEmail(request.Email);

            if (loginUser == null)
            {
                _loginContext.UserLogs.Add(new UserLog()
                {
                    Id       = GuidGenerator.NewGuid(),
                    Action   = "Request reset password link",
                    Email    = request.Email,
                    DateTime = SystemTime.UtcNow(),
                    Result   = "Sent no account email"
                });
                await _loginContext.SaveChangesAsync(cancellationToken);

                await _emailService.SendResetNoAccountPassword(new PasswordResetNoAccountEmailViewModel()
                {
                    EmailAddress = request.Email,
                    LoginLink    = client.ServiceDetails.PostPasswordResetReturnUrl,
                    Subject      = "Password request received",
                    TemplateId   = client.ServiceDetails.EmailTemplates.Single(t => t.Name == "PasswordResetNoAccount").TemplateId,
                    ServiceName  = client.ServiceDetails.ServiceName,
                    ServiceTeam  = client.ServiceDetails.ServiceTeam
                });

                return(Unit.Value);
            }

            await ClearOutAnyPreviousStillValidRequests(request.Email);

            var identityToken = await _userService.GeneratePasswordResetToken(loginUser);

            var resetPasswordRequest = await SavePasswordRequest(request, cancellationToken, identityToken);

            var resetUri = new Uri(new Uri(_loginConfig.BaseUrl), $"NewPassword/{request.ClientId}/{resetPasswordRequest.Id}");

            await _emailService.SendResetPassword(new ResetPasswordEmailViewModel()
            {
                Contact      = loginUser.GivenName,
                EmailAddress = request.Email,
                LoginLink    = resetUri.ToString(),
                ServiceName  = client.ServiceDetails.ServiceName,
                ServiceTeam  = client.ServiceDetails.ServiceTeam,
                Subject      = "Password reset",
                TemplateId   = client.ServiceDetails.EmailTemplates.Single(t => t.Name == "PasswordReset").TemplateId
            });

            _loginContext.UserLogs.Add(new UserLog()
            {
                Id       = GuidGenerator.NewGuid(),
                Action   = "Request reset password link",
                Email    = request.Email,
                DateTime = SystemTime.UtcNow(),
                Result   = "Sent reset password email"
            });
            await _loginContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }