using e_suite.API.Common.exceptions;
using e_suite.Database.Core.Tables.UserManager;
using eSuite.Core.Miscellaneous;
using NUnit.Framework;
using SSOManager.UnitTests.Helpers;

namespace SSOManager.UnitTests.SsoManager;

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

    [Test]
    public void GetSsoProviderAsync_WhenIdNotFound_ThrowsNotFoundException()
    {
        //Arrange
        GeneralIdRef generalIdRef = new GeneralIdRef()
        {
            Guid = new Guid("24c70487-04e2-4684-bd77-55a51439d85a")
        };

        //Assert
        var result = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await SsoManager.GetSsoProviderAsync(generalIdRef, CancellationToken.None);
        });

        Assert.That(result, Is.Not.Null);
        Assert.That(result!.Message, Is.EqualTo("Unable to find SsoProvider"));
    }

    [Test]
    public async Task GetSsoProviderAsync_WhenIdFound_ReturnsSsoProvider()
    {
        //Arrange
        var ssoProvider = new SsoProvider
        {
            Id = 2,
            Guid = new Guid("22534016-701b-4bf5-bebd-f0d638ac778e"),
            Name = "Test provider",
            AuthorizationEndpoint = "AuthEndPoint",
            TokenEndpoint = "TokenEndPoint",
            ClientId = "ClientId",
            ClientSecret = "ClientSecret"
        };

        SsoManagerRepository.SsoProviders.Add(ssoProvider);

        GeneralIdRef generalIdRef = new GeneralIdRef
        {
            Guid = ssoProvider.Guid
        };


        //Act
        var result = await SsoManager.GetSsoProviderAsync(generalIdRef, CancellationToken.None);

        //Assert
        Assert.That(result, Is.Not.Null);
        Assert.That(result.Id, Is.EqualTo(ssoProvider.Id));
        Assert.That(result.Guid, Is.EqualTo(ssoProvider.Guid));
    }
}