Files
git.stella-ops.org/src/Notify/__Tests/StellaOps.Notify.Models.Tests/NotifyRuleTests.cs
StellaOps Bot 564df71bfb
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Concelier Attestation Tests / attestation-tests (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Notify Smoke Test / Notify Unit Tests (push) Has been cancelled
Notify Smoke Test / Notifier Service Tests (push) Has been cancelled
Notify Smoke Test / Notification Smoke Test (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Scanner Analyzers / Discover Analyzers (push) Has been cancelled
Scanner Analyzers / Build Analyzers (push) Has been cancelled
Scanner Analyzers / Test Language Analyzers (push) Has been cancelled
Scanner Analyzers / Validate Test Fixtures (push) Has been cancelled
Scanner Analyzers / Verify Deterministic Output (push) Has been cancelled
Signals CI & Image / signals-ci (push) Has been cancelled
Signals Reachability Scoring & Events / reachability-smoke (push) Has been cancelled
Signals Reachability Scoring & Events / sign-and-upload (push) Has been cancelled
up
2025-12-13 00:20:26 +02:00

64 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace StellaOps.Notify.Models.Tests;
public sealed class NotifyRuleTests
{
[Fact]
public void ConstructorThrowsWhenActionsMissing()
{
var match = NotifyRuleMatch.Create(eventKinds: new[] { NotifyEventKinds.ScannerReportReady });
var exception = Assert.Throws<ArgumentException>(() =>
NotifyRule.Create(
ruleId: "rule-1",
tenantId: "tenant-a",
name: "critical",
match: match,
actions: Array.Empty<NotifyRuleAction>()));
Assert.Contains("At least one action is required", exception.Message, StringComparison.Ordinal);
}
[Fact]
public void ConstructorNormalizesCollections()
{
var rule = NotifyRule.Create(
ruleId: "rule-1",
tenantId: "tenant-a",
name: "critical",
match: NotifyRuleMatch.Create(
eventKinds: new[] { "Zastava.Admission", NotifyEventKinds.ScannerReportReady }),
actions: new[]
{
NotifyRuleAction.Create(actionId: "b", channel: "slack:sec-alerts", throttle: TimeSpan.FromMinutes(5)),
NotifyRuleAction.Create(actionId: "a", channel: "email:soc", metadata: new Dictionary<string, string>
{
[" locale "] = " EN-us "
})
},
labels: new Dictionary<string, string>
{
[" team "] = " SecOps "
},
metadata: new Dictionary<string, string>
{
["source"] = "tests"
});
Assert.Equal(NotifySchemaVersions.Rule, rule.SchemaVersion);
Assert.Equal(new[] { "scanner.report.ready", "zastava.admission" }, rule.Match.EventKinds);
Assert.Equal(new[] { "a", "b" }, rule.Actions.Select(action => action.ActionId));
Assert.Equal(TimeSpan.FromMinutes(5), rule.Actions.Last().Throttle);
Assert.Equal("secops", rule.Labels.Single().Value.ToLowerInvariant());
Assert.Equal("en-us", rule.Actions.First().Metadata["locale"].ToLowerInvariant());
var json = NotifyCanonicalJsonSerializer.Serialize(rule);
Assert.Contains("\"schemaVersion\":\"notify.rule@1\"", json, StringComparison.Ordinal);
Assert.Contains("\"actions\":[{\"actionId\":\"a\"", json, StringComparison.Ordinal);
Assert.Contains("\"throttle\":\"PT5M\"", json, StringComparison.Ordinal);
}
}