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

namespace eSuite.API.UnitTests.SingleSignOn.CookieManager;

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

    [Test]
    public async Task DeleteSessionCookie_WhenCalledAndCookiePresent_RemovesSessionCookieFromResponse()
    {
        //Arrange
        var cookies = new FakeResponseCookies();

        cookies.Append("eSuiteSession", "JSON Web Token", new CookieOptions
        {
            HttpOnly = false, //Set False as Javascript (React) needs to read the cookie.
            SameSite = SameSiteMode.Strict,
            Secure = true,
            IsEssential = true,
            Expires = null, //Session Cookie
            Path = "/"
        });

        var httpResponseMock = new Mock<HttpResponse>();
        httpResponseMock.SetupGet(x => x.Cookies).Returns(cookies);

        //Act
        await _cookieManager.DeleteSessionCookie(httpResponseMock.Object);

        //Assert
        Assert.That(cookies.CookieDictionary.ContainsKey("eSuiteSession"), Is.False);
    }

    [Test]
    public async Task DeleteSessionCookie_WhenCalledAndCookieNotPresent_DoesNothingGraceFully()
    {
        //Arrange
        var cookies = new FakeResponseCookies();

        var httpResponseMock = new Mock<HttpResponse>();
        httpResponseMock.SetupGet(x => x.Cookies).Returns(cookies);

        //Act
        await _cookieManager.DeleteSessionCookie(httpResponseMock.Object);

        //Assert
        Assert.That(cookies.CookieDictionary.ContainsKey("eSuiteSession"), Is.False);
    }
}