Files
Khaemhat/tests/Khaemhat.Core.Tests/Common/CronExpressionTests.cs
T
Alby96andClaude Opus 5.5 032c602b49
CI / Build and test (SQLite) (pull_request) Successful in 55s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 5s
CI / Docker image (pull_request) Failing after 2m4s
feat: system and security - start-up, accounts, sign-in, licences, log, jobs, companies (M2)
The instance now starts on its own from KHAEMHAT_* variables: it migrates the system
and company databases, creates the super user and the first company, and only then
reports ready. Users sign in with password and TOTP through revocable database
sessions, and licences decide which modules and how many companies are available.

- Configuration: every wrong variable reported at once (exit code 2), _FILE variants
- Accounts, password policy and lockout, TOTP with recovery codes, session states
  with token rotation, security audit with a hash chain
- Sign-in pages (mockups 001-003), authorization attributes, security headers and a
  CSP without inline scripts, login rate limit
- Signed licences, activation codes, test mode, licence tool
- DataProtection keys in the system database, encrypted with the instance key
- Memory or Redis cache (in-house RESP client) that falls back to memory
- Database log by section with trace id; cron scheduler with locks and system jobs;
  record history (audit_log)
- Company creation, states, company context and X-Company, 503 in maintenance
- In-memory SQLite databases stay alive for the process (tests, demo)
- Tests for all of the above, ADR 0019, wiki, SECURITY, CHANGELOG, STATUS, DATA-MODEL

Refs #15 #16 #17 #18 #19 #20 #21 #22 #23 #24

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:29:24 +01:00

78 lines
3.5 KiB
C#

using Khaemhat.Core.Logging;
using Khaemhat.Core.Scheduling;
namespace Khaemhat.Core.Tests.Common;
/// <summary>The five-field cron of the scheduled tasks (SPEC §9.6) and the log sections (SPEC §9.5).</summary>
[TestClass]
public sealed class CronExpressionTests
{
private static readonly TimeZoneInfo Rome = TimeZoneInfo.FindSystemTimeZoneById("Europe/Rome");
[TestMethod]
[DataRow("15 * * * *", "2026-11-11T10:20:00Z", "2026-11-11T11:15:00Z")]
[DataRow("*/10 * * * *", "2026-11-11T10:20:00Z", "2026-11-11T10:30:00Z")]
[DataRow("0 9-17/4 * * *", "2026-11-11T10:00:00Z", "2026-11-11T13:00:00Z")]
[DataRow("30 3 1,15 * *", "2026-11-11T00:00:00Z", "2026-11-15T03:30:00Z")]
[DataRow("0 0 * * 7", "2026-11-11T00:00:00Z", "2026-11-15T00:00:00Z")]
[DataRow("0 0 29 2 *", "2026-03-01T00:00:00Z", "2028-02-29T00:00:00Z")]
public void NextAfter_InUtc(string cron, string after, string expected)
{
var next = CronExpression.Parse(cron).NextAfter(DateTime.Parse(after, null, System.Globalization.DateTimeStyles.AdjustToUniversal), TimeZoneInfo.Utc);
Assert.AreEqual(DateTime.Parse(expected, null, System.Globalization.DateTimeStyles.AdjustToUniversal), next);
}
[TestMethod]
public void DayOfMonthAndDayOfWeek_MatchEither()
{
// The 13th or any Friday: after Wednesday 11 November 2026 comes Friday the 13th, then Friday the 20th.
var cron = CronExpression.Parse("0 12 13 * 5");
var first = cron.NextAfter(new DateTime(2026, 11, 11, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Utc);
var second = cron.NextAfter(first!.Value, TimeZoneInfo.Utc);
Assert.AreEqual(new DateTime(2026, 11, 13, 12, 0, 0, DateTimeKind.Utc), first);
Assert.AreEqual(new DateTime(2026, 11, 20, 12, 0, 0, DateTimeKind.Utc), second);
}
[TestMethod]
public void LocalTimes_FollowTheTimeZone_AndSkipTheMissingHour()
{
var cron = CronExpression.Parse("30 2 * * *");
// 29 March 2026: in Rome clocks jump from 02:00 to 03:00, so 02:30 does not exist that night.
var spring = cron.NextAfter(new DateTime(2026, 3, 28, 12, 0, 0, DateTimeKind.Utc), Rome);
Assert.AreEqual(new DateTime(2026, 3, 30, 0, 30, 0, DateTimeKind.Utc), spring);
// In winter Rome is UTC+1.
var winter = cron.NextAfter(new DateTime(2026, 11, 11, 12, 0, 0, DateTimeKind.Utc), Rome);
Assert.AreEqual(new DateTime(2026, 11, 12, 1, 30, 0, DateTimeKind.Utc), winter);
}
[TestMethod]
[DataRow("")]
[DataRow("* * * *")]
[DataRow("60 * * * *")]
[DataRow("* 24 * * *")]
[DataRow("* * 0 * *")]
[DataRow("* * * 13 *")]
[DataRow("* * * * 8")]
[DataRow("*/0 * * * *")]
[DataRow("a * * * *")]
public void InvalidExpressions_AreRefused(string text)
{
Assert.IsFalse(CronExpression.TryParse(text, out _, out var error));
Assert.IsFalse(string.IsNullOrEmpty(error));
}
[TestMethod]
public void LogSections_ComeFromTheCategory()
{
Assert.AreEqual(LogSection.Migrations, LogSections.FromCategory("Khaemhat.Data.Migrations.MigrationRunner"));
Assert.AreEqual("Sistema", LogSections.Label(LogSection.System));
Assert.AreEqual(LogSection.Authentication, LogSections.FromCategory("Khaemhat.Application.Security.SignInService"));
Assert.AreEqual(LogSection.ScheduledJobs, LogSections.FromCategory("Khaemhat.Application.Jobs.JobScheduler"));
Assert.AreEqual("Performance", LogSections.Scope(LogSection.Performance).Single().Value);
}
}