using Hotline.Api.Controllers;
using Hotline.Identity.Accounts;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shouldly;
using XF.Domain.Repository;

namespace Hotline.Tests.Controller;
public class UserControllerTest
{
    private readonly UserController _userController;
    private readonly IRepository<Account> _accountRepository;

    public UserControllerTest(UserController userController, IRepository<Account> accountRepository)
    {
        _userController = userController;
        _userController.ControllerContext = new ControllerContext
        {
            HttpContext = new DefaultHttpContext()
        };
        _accountRepository = accountRepository;
    }

    [Fact]
    public async Task UnlockUser_Test()
    {
        var user = await _accountRepository.Queryable()
            .Where(m => m.LockoutEnd == null).FirstAsync();
        user.ShouldNotBeNull();
        user.LockoutEnd = DateTime.Now.AddDays(1);
        await _accountRepository.UpdateAsync(user);
        user = await _accountRepository.GetAsync(user.Id);
        user.LockoutEnd.ShouldNotBeNull();

        await _userController.UnlockUserAsync(user.Id);
        user = await _accountRepository.GetAsync(user.Id);
        user.LockoutEnd.ShouldBeNull();
    }
}
