Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.NotificationService.Tests/SmtpErrorClassifierTests.cs
T
Joseph Doherty fd618cf1dc fix(review): full code-review remediation — 5 High + Medium/Low across 16 modules
Remediation from the full per-module code review at 4307c381 (findings recorded
separately in code-reviews/).

Highs fixed:
- DeploymentManager-025/SiteRuntime-031: stop broadcasting notification lists + SMTP
  configs (incl. credentials) to sites; site purges already-persisted rows on apply
  (enforces the central-only delivery design; clears plaintext SMTP creds at rest).
- DataConnectionLayer-023: guard the native-alarm subscribe path against the
  mid-flight-unsubscribe adapter-feed leak (mirrors the DCL-021 tag-path fix).
- SiteEventLogging-024: normalize From/To query bounds to UTC (the -016 fix the
  audit trail claimed but never committed).
- KpiHistory-001: add an in-flight guard to the recorder sample tick.
- ScriptAnalysis-001: harden the trust analyzer's TPA-absent fallback (resolve
  forbidden anchors in the minimal reference set; warn on degraded mode) — anchors
  added to validation references only, never the compile gate.
(InboundAPI-026 left to the feat/ipsen-movein effort per owner decision.)

Medium/Low: DM-026 deterministic deploy-status tiebreaker; SR-027/028/029/030
native-alarm leak/phantom-active/delete-during-redeploy fixes; AL-013/014/016;
TE-024 (folder-mutation audit rows now persisted)/025; SF-025 gauge-provider
clear-on-stop; ESG-025/026; SEC-023/024/025; SCA-007/008/009; plus doc/test
accuracy COM-023/024, HOST-025/026, HM-024/025, NS-027/028.

Full-solution build 0 warnings; ~3560 tests across 18 touched suites green.
2026-06-20 17:55:12 -04:00

133 lines
4.4 KiB
C#

using System.Net.Sockets;
using MailKit;
using MailKit.Net.Smtp;
namespace ZB.MOM.WW.ScadaBridge.NotificationService.Tests;
/// <summary>
/// NS-002/NS-003: Tests for the shared SMTP error classification policy. This
/// policy is correctness-relevant — it decides whether a delivery failure is
/// retried (transient) or returned to the caller (permanent) — and is shared
/// by the central outbox's <c>EmailNotificationDeliveryAdapter</c>, so it
/// deserves direct coverage.
/// </summary>
public class SmtpErrorClassifierTests
{
[Theory]
[InlineData(421)] // service not available
[InlineData(450)] // mailbox unavailable (busy)
[InlineData(451)] // local error in processing
[InlineData(452)] // insufficient system storage
public void Classify_Smtp4xxCommand_IsTransient(int statusCode)
{
var ex = new SmtpCommandException(
SmtpErrorCode.MessageNotAccepted, (SmtpStatusCode)statusCode, "rejected");
Assert.Equal(SmtpErrorClass.Transient, SmtpErrorClassifier.Classify(ex, CancellationToken.None));
}
[Theory]
[InlineData(500)] // syntax error
[InlineData(550)] // mailbox unavailable (rejected)
[InlineData(553)] // mailbox name not allowed
[InlineData(554)] // transaction failed
public void Classify_Smtp5xxCommand_IsPermanent(int statusCode)
{
var ex = new SmtpCommandException(
SmtpErrorCode.MessageNotAccepted, (SmtpStatusCode)statusCode, "rejected");
Assert.Equal(SmtpErrorClass.Permanent, SmtpErrorClassifier.Classify(ex, CancellationToken.None));
}
[Fact]
public void Classify_SmtpCommandWithUnusualCode_IsUnknown()
{
// A status code outside the 4xx/5xx bands is not classifiable.
var ex = new SmtpCommandException(
SmtpErrorCode.UnexpectedStatusCode, (SmtpStatusCode)250, "ok-ish");
Assert.Equal(SmtpErrorClass.Unknown, SmtpErrorClassifier.Classify(ex, CancellationToken.None));
}
[Fact]
public void Classify_SmtpProtocolException_IsTransient()
{
Assert.Equal(
SmtpErrorClass.Transient,
SmtpErrorClassifier.Classify(new SmtpProtocolException("protocol error"), CancellationToken.None));
}
[Fact]
public void Classify_ServiceNotConnectedException_IsTransient()
{
Assert.Equal(
SmtpErrorClass.Transient,
SmtpErrorClassifier.Classify(new ServiceNotConnectedException(), CancellationToken.None));
}
[Fact]
public void Classify_SocketException_IsTransient()
{
Assert.Equal(
SmtpErrorClass.Transient,
SmtpErrorClassifier.Classify(new SocketException(), CancellationToken.None));
}
[Fact]
public void Classify_TimeoutException_IsTransient()
{
Assert.Equal(
SmtpErrorClass.Transient,
SmtpErrorClassifier.Classify(new TimeoutException(), CancellationToken.None));
}
[Fact]
public void Classify_RequestedCancellation_IsUnknown()
{
using var cts = new CancellationTokenSource();
cts.Cancel();
Assert.Equal(
SmtpErrorClass.Unknown,
SmtpErrorClassifier.Classify(new OperationCanceledException(), cts.Token));
}
[Fact]
public void Classify_OperationCanceledWithoutRequestedCancellation_IsUnknown()
{
// Not a recognised SMTP error, and cancellation was not requested.
Assert.Equal(
SmtpErrorClass.Unknown,
SmtpErrorClassifier.Classify(new OperationCanceledException(), CancellationToken.None));
}
[Fact]
public void Classify_UnrecognisedException_IsUnknown()
{
Assert.Equal(
SmtpErrorClass.Unknown,
SmtpErrorClassifier.Classify(new InvalidOperationException("bad credential triple"), CancellationToken.None));
}
[Theory]
[InlineData(450, true)]
[InlineData(550, false)]
[InlineData(250, false)]
public void IsTransient_MatchesClassification(int statusCode, bool expectedTransient)
{
var ex = new SmtpCommandException(
SmtpErrorCode.MessageNotAccepted, (SmtpStatusCode)statusCode, "x");
Assert.Equal(expectedTransient, SmtpErrorClassifier.IsTransient(ex, CancellationToken.None));
}
[Fact]
public void IsTransient_RequestedCancellation_IsFalse()
{
using var cts = new CancellationTokenSource();
cts.Cancel();
Assert.False(SmtpErrorClassifier.IsTransient(new OperationCanceledException(), cts.Token));
}
}