using e_suite.API.Common.models;
using e_suite.Database.Core.Tables.UserManager;
using NUnit.Framework;
using UserManager.UnitTests.Helpers;

namespace UserManager.UnitTests.UserManager;

[TestFixture]
public class RefreshTokenByEmailUnitTests : UserManagerTestBase<object>
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public async Task RefreshToken_UserNotFound_ReturnsFailed()
    {
        //Arrange
        var email = "fred@bloggs.com"; //User does not exist in fake database.

        //Act
        var result = await UserManager.RefreshToken(email);

        //Assert
        Assert.That(result.Result, Is.EqualTo(LoginResult.Failed));
    }

    [Test]
    public async Task RefreshToken_User_ReturnsSuccess()
    {
        //Arrange
        var existingUser = new User
        {
            Email = "testuser@sun-strategy.com",
            EmailConfirmed = true
        };
        await UserManagerRepository.AddUser(AuditUserDetails, existingUser, default);

        //Act
        var result = await UserManager.RefreshToken(existingUser.Email);

        //Assert
        Assert.That(result.Result, Is.EqualTo(LoginResult.Success));
        Assert.That(result.Token, Is.Not.Empty);
    }

    [Test]
    public async Task RefreshToken_DeactivatedUser_ReturnsFailed()
    {
        //Arrange
        var existingUser = new User
        {
            Email = "testuser@sun-strategy.com",
            Active = false

        };
        await UserManagerRepository.AddUser(AuditUserDetails, existingUser, default);

        //Act
        var result = await UserManager.RefreshToken(existingUser.Email);

        //Assert
        Assert.That(result.Result, Is.EqualTo(LoginResult.Failed));
        Assert.That(result.Token, Is.Empty);
    }
}