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 LoginSsoUnitTests : UserManagerTestBase<object>
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public async Task LoginSso_UserNotFound_ReturnsFailed()
    {
        //Arrange
        //Act
        var result = await UserManager.LoginSso(1,"1",CancellationToken.None);

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

    [Test]
    public async Task LoginSso_EmailNotConfirmed_ReturnsError()
    {
        //Arrange
        var existingUser = new User
        {
            Id = 16,
            Email = "testuser2@sun-strategy.com",
            Password = "testing",
            EmailConfirmed = false,
            SsoSubject = "10",
            SsoProviderId = 1
        };
        await UserManagerRepository.AddUser(AuditUserDetails, existingUser, default);

        
        //Act
        var result = await UserManager.LoginSso(existingUser.SsoProviderId.Value, existingUser.SsoSubject, CancellationToken.None);

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


    [Test]
    public async Task LoginSso_AccountIsNotActive_ReturnsFailed()
    {
        //Arrange
        var existingUser = new User
        {
            Id = 15,
            Email = "testuser@sun-strategy.com",
            Password = "testing",
            EmailConfirmed = true,
            Active = false,
            SsoSubject = "10",
            SsoProviderId = 1
        };
        await UserManagerRepository.AddUser(AuditUserDetails, existingUser, default);

        //Act
        var result = await UserManager.LoginSso(existingUser.SsoProviderId.Value, existingUser.SsoSubject, CancellationToken.None);

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