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

namespace UserManager.UnitTests.UserManager;

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

    [Test]
    public async Task TurnOfSsoForUser_UserExists_TurnsOffSsoForUser()
    {
        //Arrange
        var actualSsoProvider = UserManagerRepository.SsoProviders.First();
        
        var user = new User
        {
            Email = "testuser@sun-strategy.com",
            SsoProvider = actualSsoProvider,
            SsoProviderId = actualSsoProvider.Id,
            SsoSubject = "SecretString123456"
        };

        await UserManagerRepository.AddUser(AuditUserDetails, user, default);

        //Act
        await UserManager.TurnOfSsoForUser(AuditUserDetails, user.ToGeneralIdRef()!, CancellationToken.None);

        //Assert
        Assert.That(user.SsoProviderId, Is.Null);
        Assert.That(user.SsoProvider, Is.Null);
        Assert.That(user.SsoSubject, Is.EqualTo(string.Empty));
    }

    [Test]
    public void TurnOfSsoForUser_UserNotFound_ThrowsNotFoundException()
    {
        //Arrange
        var actualSsoProvider = UserManagerRepository.SsoProviders.First();

        var user = new User
        {
            Email = "testuser@sun-strategy.com",
            SsoProvider = actualSsoProvider,
            SsoProviderId = actualSsoProvider.Id,
            SsoSubject = "SecretString123456"
        };

        //Act & Assert
        Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            await UserManager.TurnOfSsoForUser(AuditUserDetails, user.ToGeneralIdRef()!, CancellationToken.None);
        });
    }
}