using e_suite.Database.Core.Tables.Sentinel;
using NUnit.Framework;
using Sentinel.UnitTests.Helpers;

namespace Sentinel.UnitTests.SentinelMaintenance;

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

    [Test]
    public void ClearOldSentinelData_WhenCalled_DeletesOldData()
    {
        //Arrange
        _fakeClock.DateTime = new DateTimeOffset(2022, 9, 23, 12, 06, 45, TimeSpan.Zero);

        SentinelRepository.FailedAccessAttempts.Add( new FailedAccessAttempt
        {
            AttemptedTime = _fakeClock.DateTime.AddMinutes(-120),
            IPAddress = "127.0.0.1"
        });

        //Act
        SentinelMaintenance.ClearOldSentinelData();

        //Assert
        Assert.That(SentinelRepository.FailedAccessAttempts.Count, Is.EqualTo(0));
    }

    [Test]
    public void ClearOldSentinelData_WhenCalled_DoesNotAlterCurrentData()
    {
        //Arrange
        _fakeClock.DateTime = new DateTimeOffset(2022, 9, 23, 12, 06, 45, TimeSpan.Zero);

        SentinelRepository.FailedAccessAttempts.Add(new FailedAccessAttempt
        {
            AttemptedTime = _fakeClock.DateTime.AddMinutes(-15),
            IPAddress = "127.0.0.1"
        });

        //Act
        SentinelMaintenance.ClearOldSentinelData();

        //Assert
        Assert.That(SentinelRepository.FailedAccessAttempts.Count, Is.EqualTo(1));
    }
}