mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-29 08:28:39 +00:00
Implement dismiss expired cases repeating task (#891)
* Implement dismiss expired cases repeating task I have the time temporarily set to every 1 minute for testing purposes * Rename cases list variable to be a bit more self documenting * Switch repeat interval to four hours * Make no operation log a debug * Add case expiration unit tests I called the file ModerationTests in case we want to add more general moderation unit tests in the future * I blame zaprit * Fix tests * Tests nitpicks and use UtcNow instead of Now * Use Assert.Null and Assert.NotNull for better readability
This commit is contained in:
parent
d0686b5f5f
commit
22c220c432
2 changed files with 96 additions and 0 deletions
57
ProjectLighthouse.Tests/Unit/ModerationTests.cs
Normal file
57
ProjectLighthouse.Tests/Unit/ModerationTests.cs
Normal file
|
@ -0,0 +1,57 @@
|
|||
using System;
|
||||
using LBPUnion.ProjectLighthouse.Administration.Maintenance.RepeatingTasks;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Moderation;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Tests.Unit;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
public class ModerationTests
|
||||
{
|
||||
[Fact]
|
||||
public async void DismissExpiredCases_ShouldDismissExpiredCase()
|
||||
{
|
||||
await using DatabaseContext database = await MockHelper.GetTestDatabase();
|
||||
|
||||
ModerationCaseEntity @case = new()
|
||||
{
|
||||
CaseId = 1,
|
||||
ExpiresAt = DateTime.UnixEpoch,
|
||||
CreatorUsername = "unitTestUser",
|
||||
};
|
||||
|
||||
database.Cases.Add(@case);
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
DismissExpiredCasesTask task = new();
|
||||
await task.Run(database);
|
||||
|
||||
Assert.Null(await database.Cases.FirstOrDefaultAsync(c => c.CaseId == 1 && c.DismissedAt == null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void DismissExpiredCases_ShouldNotDismissActiveCase()
|
||||
{
|
||||
await using DatabaseContext database = await MockHelper.GetTestDatabase();
|
||||
|
||||
ModerationCaseEntity @case = new()
|
||||
{
|
||||
CaseId = 2,
|
||||
ExpiresAt = DateTime.UtcNow.AddHours(1),
|
||||
CreatorUsername = "unitTestUser",
|
||||
};
|
||||
|
||||
database.Cases.Add(@case);
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
DismissExpiredCasesTask task = new();
|
||||
await task.Run(database);
|
||||
|
||||
Assert.NotNull(await database.Cases.FirstOrDefaultAsync(c => c.CaseId == 2 && c.DismissedAt == null));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Moderation;
|
||||
using LBPUnion.ProjectLighthouse.Types.Logging;
|
||||
using LBPUnion.ProjectLighthouse.Types.Maintenance;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Administration.Maintenance.RepeatingTasks;
|
||||
|
||||
public class DismissExpiredCasesTask : IRepeatingTask
|
||||
{
|
||||
public string Name => "Dismiss Expired Cases";
|
||||
public TimeSpan RepeatInterval => TimeSpan.FromHours(4);
|
||||
public DateTime LastRan { get; set; }
|
||||
|
||||
public async Task Run(DatabaseContext database)
|
||||
{
|
||||
List<ModerationCaseEntity> expiredCases =
|
||||
await database.Cases.Where(c => c.ExpiresAt != null && c.ExpiresAt < DateTime.UtcNow).ToListAsync();
|
||||
|
||||
if (expiredCases.Count == 0)
|
||||
{
|
||||
Logger.Debug("There are no expired cases to dismiss", LogArea.Maintenance);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (ModerationCaseEntity @case in expiredCases)
|
||||
{
|
||||
@case.DismissedAt = DateTime.Now;
|
||||
Logger.Info($"Dismissed expired case {@case.CaseId}", LogArea.Maintenance);
|
||||
}
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue