using Microsoft.AspNetCore.Http;
using Moq;
using NUnit.Framework;

namespace eSuite.API.UnitTests.SingleSignOn.CookieManager;

[TestFixture]
public class GetSsoIdFromSsoIdCookieUnitTests : CookieManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public async Task GetSsoIdFromSsoId_WhenCookieNotPresent_ReturnsNull()
    {
        //Arrange
        var fakeRequestCookieCollection = new FakeRequestCookieCollection();

        var httpRequestMock = new Mock<HttpRequest>();
        httpRequestMock.Setup(x => x.Cookies).Returns(fakeRequestCookieCollection);

        //Act
        var result = await _cookieManager.GetSsoIdFromSsoIdCookie(httpRequestMock.Object);

        //Assert
        Assert.That(result, Is.Null);
    }

    [Test]
    public async Task GetSsoIdFromSsoId_WhenCookieValueIsNull_ReturnsNull()
    {
        //Arrange
        var fakeRequestCookieCollection = new FakeRequestCookieCollection
        {
            { "eSuiteSsoProvider", null! }
        };

        var httpRequestMock = new Mock<HttpRequest>();
        httpRequestMock.Setup(x => x.Cookies).Returns(fakeRequestCookieCollection);

        //Act
        var result = await _cookieManager.GetSsoIdFromSsoIdCookie(httpRequestMock.Object);

        //Assert
        Assert.That(result, Is.Null);
    }

    [Test]
    public async Task GetSsoIdFromSsoId_WhenCookieValueIsNotANumber_ReturnsNull()
    {
        //Arrange
        var fakeRequestCookieCollection = new FakeRequestCookieCollection
        {
            { "eSuiteSsoProvider", "Not a number" }
        };

        var httpRequestMock = new Mock<HttpRequest>();
        httpRequestMock.Setup(x => x.Cookies).Returns(fakeRequestCookieCollection);

        //Act
        var result = await _cookieManager.GetSsoIdFromSsoIdCookie(httpRequestMock.Object);

        //Assert
        Assert.That(result, Is.Null);
    }

    [Test]
    public async Task GetSsoIdFromSsoId_WhenCookieValueIsANumber_ReturnsLong()
    {
        //Arrange
        var expectedResult = 12345;

        var fakeRequestCookieCollection = new FakeRequestCookieCollection
        {
            { "eSuiteSsoProvider", expectedResult.ToString() }
        };

        var httpRequestMock = new Mock<HttpRequest>();
        httpRequestMock.Setup(x => x.Cookies).Returns(fakeRequestCookieCollection);

        //Act
        var result = await _cookieManager.GetSsoIdFromSsoIdCookie(httpRequestMock.Object);

        //Assert
        Assert.That(result, Is.EqualTo(expectedResult));
    }
}