- Added new DELETE endpoints for managing audit logs and disabled apps in the admin maintenance section. - Updated existing endpoint for closed tickets to use a unified response type, `MaintenanceCleanupResponseDto`. - Enhanced API documentation to reflect the new operations and their expected request/response formats. - Improved frontend integration with new functions for deleting old audit logs and disabled apps, including user confirmation prompts. - Added localization support for new maintenance actions in both Russian and English.
73 lines
2.8 KiB
C#
73 lines
2.8 KiB
C#
using PnvPanel.Application.Admin.Maintenance;
|
|
using PnvPanel.Application.Tests.TestSupport;
|
|
using PnvPanel.Domain.Audit;
|
|
using Xunit;
|
|
|
|
namespace PnvPanel.Application.Tests.Admin.Maintenance;
|
|
|
|
public class DeleteOldAuditLogsCommandHandlerTests
|
|
{
|
|
[Fact]
|
|
public async Task Handle_DeletesOldLogs_KeepsItsOwnCleanupEntry()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var oldLog = AuditLog.Create(
|
|
Guid.NewGuid(),
|
|
"OldAction",
|
|
"Test",
|
|
"1",
|
|
null,
|
|
AuditSource.Web
|
|
);
|
|
dbContext.AuditLogs.Add(oldLog);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
// Гарантируем, что порог (UtcNow внутри хендлера) окажется позже CreatedAt старой записи.
|
|
await Task.Delay(5, CancellationToken.None);
|
|
|
|
var currentUser = FakeCurrentUser.Authenticated(Guid.NewGuid(), "admin");
|
|
var handler = new DeleteOldAuditLogsCommandHandler(dbContext, currentUser);
|
|
|
|
var result = await handler.Handle(
|
|
new DeleteOldAuditLogsCommand(OlderThanDays: 0),
|
|
CancellationToken.None
|
|
);
|
|
// Хендлер не коммитит сам (в проде это делает UnitOfWorkBehavior после диспетчера) — коммитим явно.
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(1, result.Value);
|
|
Assert.False(dbContext.AuditLogs.Any(l => l.Id == oldLog.Id));
|
|
// Собственная запись об очистке создаётся уже после выборки порога и не попадает под удаление.
|
|
Assert.True(dbContext.AuditLogs.Any(l => l.Action == "AuditLogsCleanedUp"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenNothingOlderThanThreshold_ReturnsZero()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var recentLog = AuditLog.Create(
|
|
Guid.NewGuid(),
|
|
"RecentAction",
|
|
"Test",
|
|
"1",
|
|
null,
|
|
AuditSource.Web
|
|
);
|
|
dbContext.AuditLogs.Add(recentLog);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
var currentUser = FakeCurrentUser.Authenticated(Guid.NewGuid(), "admin");
|
|
var handler = new DeleteOldAuditLogsCommandHandler(dbContext, currentUser);
|
|
|
|
var result = await handler.Handle(
|
|
new DeleteOldAuditLogsCommand(OlderThanDays: 36500),
|
|
CancellationToken.None
|
|
);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(0, result.Value);
|
|
Assert.True(dbContext.AuditLogs.Any(l => l.Id == recentLog.Id));
|
|
}
|
|
}
|