using e_suite.API.Common.exceptions;
using e_suite.API.Common.models;
using NUnit.Framework;
using SSOManager.UnitTests.Helpers;

namespace SSOManager.UnitTests.SsoManager;

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

    [Test]
    public void AddSsoProviderAsync_WhenGuidExists_ThrowsException()
    {
        //Arrange
        var ssoProvider = new CreateSsoProvider
        {
            Guid = new Guid("14703B56-8F70-4D9D-9061-E62EC0D446BC")
        };

        //Assert
        var result = Assert.ThrowsAsync<ExistsException>(async () =>
        {
            //Act
            await SsoManager.AddSsoProviderAsync(AuditUserDetails, ssoProvider, CancellationToken.None);
        });

        Assert.That(result, Is.Not.Null);
        Assert.That(result!.Message, Is.EqualTo("An SsoProvider with this guid exists"));
    }

    [Test]
    public async Task AddSsoProviderAsync_WhenDoesNotExist_CreatesSsoProvider()
    {
        //Arrange
        var ssoProvider = new CreateSsoProvider
        {
            AuthorizationEndpoint = "authEndpoint",
            ClientId = "ClientId",
            ClientSecret = "It's A Secret",
            Guid = new Guid("af1e8e6d-1312-41ea-99d8-20284cb7d6c6"),
            IsPublic = true,
            Name = "my test provider",
            TokenEndpoint = "tokenEndpoint",
            ValidIssuer = "myTestProvider"
        };

        //Act
        await SsoManager.AddSsoProviderAsync(AuditUserDetails, ssoProvider, CancellationToken.None);

        //Assert
        Assert.That(SsoManagerRepository.SsoProviders.Count, Is.EqualTo(2));

        var newlyAddedSsoProvider = SsoManagerRepository.SsoProviders[1];

        Assert.That(newlyAddedSsoProvider.Name, Is.EqualTo(ssoProvider.Name));
        Assert.That(newlyAddedSsoProvider.Guid, Is.EqualTo(ssoProvider.Guid));
    }
}