2023-11-02 12:15:14 +00:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System.Data;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Events;
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
|
|
|
|
using Umbraco.Cms.Core.Scoping;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
2025-10-07 18:49:21 +02:00
|
|
|
using Umbraco.Cms.Infrastructure.BackgroundJobs.Jobs.DistributedJobs;
|
2023-11-02 12:15:14 +00:00
|
|
|
using Umbraco.Cms.Tests.Common;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.BackgroundJobs.Jobs;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class LogScrubberJobTests
|
|
|
|
|
{
|
|
|
|
|
private Mock<IAuditService> _mockAuditService;
|
|
|
|
|
|
|
|
|
|
private const int MaxLogAgeInMinutes = 60;
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Executes_And_Scrubs_Logs()
|
|
|
|
|
{
|
|
|
|
|
var sut = CreateLogScrubber();
|
2025-10-07 18:49:21 +02:00
|
|
|
await sut.ExecuteAsync();
|
2023-11-02 12:15:14 +00:00
|
|
|
VerifyLogsScrubbed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LogScrubberJob CreateLogScrubber()
|
|
|
|
|
{
|
|
|
|
|
var settings = new LoggingSettings { MaxLogAge = TimeSpan.FromMinutes(MaxLogAgeInMinutes) };
|
|
|
|
|
|
|
|
|
|
var mockScope = new Mock<IScope>();
|
|
|
|
|
var mockScopeProvider = new Mock<ICoreScopeProvider>();
|
|
|
|
|
mockScopeProvider
|
|
|
|
|
.Setup(x => x.CreateCoreScope(
|
|
|
|
|
It.IsAny<IsolationLevel>(),
|
|
|
|
|
It.IsAny<RepositoryCacheMode>(),
|
|
|
|
|
It.IsAny<IEventDispatcher>(),
|
|
|
|
|
It.IsAny<IScopedNotificationPublisher>(),
|
|
|
|
|
It.IsAny<bool?>(),
|
|
|
|
|
It.IsAny<bool>(),
|
|
|
|
|
It.IsAny<bool>()))
|
|
|
|
|
.Returns(mockScope.Object);
|
|
|
|
|
var mockProfilingLogger = new Mock<IProfilingLogger>();
|
|
|
|
|
|
|
|
|
|
_mockAuditService = new Mock<IAuditService>();
|
|
|
|
|
|
|
|
|
|
return new LogScrubberJob(
|
|
|
|
|
_mockAuditService.Object,
|
|
|
|
|
new TestOptionsMonitor<LoggingSettings>(settings),
|
|
|
|
|
mockScopeProvider.Object,
|
|
|
|
|
mockProfilingLogger.Object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyLogsNotScrubbed() => VerifyLogsScrubbed(Times.Never());
|
|
|
|
|
|
|
|
|
|
private void VerifyLogsScrubbed() => VerifyLogsScrubbed(Times.Once());
|
|
|
|
|
|
|
|
|
|
private void VerifyLogsScrubbed(Times times) =>
|
2025-07-01 09:12:37 +02:00
|
|
|
_mockAuditService.Verify(x => x.CleanLogsAsync(It.Is<int>(y => y == MaxLogAgeInMinutes)), times);
|
2023-11-02 12:15:14 +00:00
|
|
|
}
|