a4523e090e
SmtpConfiguration.MaxRetries/RetryDelay (and ConnectionTimeoutSeconds/ MaxConcurrentConnections) drive the outbox Email policy but had no write path, so every row sat at 0/0 and was clamped with a Warning; DEV vd03 had to be fixed by direct SQL. - Update/CreateSmtpConfigCommand: four trailing additive nullable fields (null = preserve on update / entity default on create; 0 = fallback). - ManagementActor: preserve-if-null, negatives refused. - CLI notification smtp create|update: --max-retries, --retry-delay-seconds, --connection-timeout-seconds, --max-concurrent-connections (>= 0). - Central UI /notifications/smtp: fields on the form + read-only panel; new rows pre-fill 10 retries / 60 s. - README + Component-NotificationService; tests for apply/preserve/reject.
208 lines
8.8 KiB
C#
208 lines
8.8 KiB
C#
using System.CommandLine;
|
|
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests.Commands;
|
|
|
|
/// <summary>
|
|
/// Tests for the <c>scadabridge notification smtp update</c> subcommand. The command
|
|
/// gained optional flags — <c>--tls-mode</c>, <c>--credentials</c> and <c>--transport</c> —
|
|
/// that plumb through to <see cref="UpdateSmtpConfigCommand"/>. These tests pin that the
|
|
/// flags parse, are genuinely optional (non-breaking), that <c>--tls-mode</c> rejects
|
|
/// values outside the canonical {None, StartTLS, SSL} set, and that <c>--transport</c>
|
|
/// rejects values outside {Smtp, Ews}.
|
|
/// </summary>
|
|
public class SmtpUpdateCommandTests
|
|
{
|
|
private static readonly Option<string> Url = new("--url") { Recursive = true };
|
|
private static readonly Option<string> Username = new("--username") { Recursive = true };
|
|
private static readonly Option<string> Password = new("--password") { Recursive = true };
|
|
private static readonly Option<string> Format = CliOptions.CreateFormatOption();
|
|
|
|
private static Command SmtpUpdateCommand()
|
|
{
|
|
var notification = NotificationCommands.Build(Url, Format, Username, Password);
|
|
var smtp = notification.Subcommands.Single(c => c.Name == "smtp");
|
|
return smtp.Subcommands.Single(c => c.Name == "update");
|
|
}
|
|
|
|
private static ParseResult ParseUpdate(params string[] args)
|
|
=> SmtpUpdateCommand().Parse(args);
|
|
|
|
/// <summary>Verifies that an update with a TLS mode and credentials produces a command carrying both.</summary>
|
|
[Fact]
|
|
public void Update_WithTlsModeAndCredentials_ProducesCommandCarryingThem()
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "1", "--server", "smtp.example.com", "--port", "587",
|
|
"--auth-mode", "Basic", "--from-address", "noreply@example.com",
|
|
"--tls-mode", "None", "--credentials", "user:pass");
|
|
|
|
Assert.Empty(parse.Errors);
|
|
var cmd = NotificationCommands.BuildUpdateSmtpConfigCommand(parse);
|
|
|
|
Assert.Equal(1, cmd.SmtpConfigId);
|
|
Assert.Equal("smtp.example.com", cmd.Server);
|
|
Assert.Equal(587, cmd.Port);
|
|
Assert.Equal("Basic", cmd.AuthMode);
|
|
Assert.Equal("noreply@example.com", cmd.FromAddress);
|
|
Assert.Equal("None", cmd.TlsMode);
|
|
Assert.Equal("user:pass", cmd.Credentials);
|
|
}
|
|
|
|
/// <summary>Verifies that an update without a TLS mode or credentials produces a command with null values for them.</summary>
|
|
[Fact]
|
|
public void Update_WithoutTlsModeAndCredentials_ProducesCommandWithNulls()
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "2", "--server", "smtp.example.com", "--port", "25",
|
|
"--auth-mode", "OAuth2", "--from-address", "noreply@example.com");
|
|
|
|
Assert.Empty(parse.Errors);
|
|
var cmd = NotificationCommands.BuildUpdateSmtpConfigCommand(parse);
|
|
|
|
Assert.Equal(2, cmd.SmtpConfigId);
|
|
Assert.Null(cmd.TlsMode);
|
|
Assert.Null(cmd.Credentials);
|
|
}
|
|
|
|
/// <summary>Verifies that the TLS mode option accepts each canonical value.</summary>
|
|
/// <param name="value">The TLS mode value supplied on the command line.</param>
|
|
[Theory]
|
|
[InlineData("None")]
|
|
[InlineData("StartTLS")]
|
|
[InlineData("SSL")]
|
|
public void Update_TlsModeOption_AcceptsCanonicalValues(string value)
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "1", "--server", "smtp.example.com", "--port", "587",
|
|
"--auth-mode", "Basic", "--from-address", "noreply@example.com",
|
|
"--tls-mode", value);
|
|
|
|
Assert.Empty(parse.Errors);
|
|
}
|
|
|
|
/// <summary>Verifies that the TLS mode option rejects values outside the canonical set.</summary>
|
|
/// <param name="value">The non-canonical TLS mode value supplied on the command line.</param>
|
|
[Theory]
|
|
[InlineData("Bogus")]
|
|
[InlineData("tls")]
|
|
[InlineData("none")] // AcceptOnlyFromAmong is case-sensitive: constrain to canonical spelling
|
|
public void Update_TlsModeOption_RejectsValuesOutsideCanonicalSet(string value)
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "1", "--server", "smtp.example.com", "--port", "587",
|
|
"--auth-mode", "Basic", "--from-address", "noreply@example.com",
|
|
"--tls-mode", value);
|
|
|
|
Assert.NotEmpty(parse.Errors);
|
|
}
|
|
|
|
/// <summary>Verifies that an update with the EWS transport produces a command carrying that transport.</summary>
|
|
[Fact]
|
|
public void Update_WithTransportEws_ProducesCommandCarryingIt()
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "1", "--server", "https://mail.example.com/ews/exchange.asmx", "--port", "443",
|
|
"--auth-mode", "Basic", "--from-address", "noreply@example.com",
|
|
"--transport", "Ews");
|
|
|
|
Assert.Empty(parse.Errors);
|
|
var cmd = NotificationCommands.BuildUpdateSmtpConfigCommand(parse);
|
|
|
|
Assert.Equal("Ews", cmd.Transport);
|
|
}
|
|
|
|
/// <summary>Verifies that an update without a transport produces a command with a null transport.</summary>
|
|
[Fact]
|
|
public void Update_WithoutTransport_ProducesCommandWithNullTransport()
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "2", "--server", "smtp.example.com", "--port", "25",
|
|
"--auth-mode", "Basic", "--from-address", "noreply@example.com");
|
|
|
|
Assert.Empty(parse.Errors);
|
|
var cmd = NotificationCommands.BuildUpdateSmtpConfigCommand(parse);
|
|
|
|
Assert.Null(cmd.Transport);
|
|
}
|
|
|
|
/// <summary>Verifies that the transport option rejects values outside the canonical set.</summary>
|
|
/// <param name="value">The non-canonical transport value supplied on the command line.</param>
|
|
[Theory]
|
|
[InlineData("Graph")]
|
|
[InlineData("ews")] // AcceptOnlyFromAmong is case-sensitive: constrain to canonical spelling
|
|
public void Update_TransportOption_RejectsValuesOutsideCanonicalSet(string value)
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "1", "--server", "smtp.example.com", "--port", "587",
|
|
"--auth-mode", "Basic", "--from-address", "noreply@example.com",
|
|
"--transport", value);
|
|
|
|
Assert.NotEmpty(parse.Errors);
|
|
}
|
|
|
|
/// <summary>Verifies that the TLS mode and credentials options are not required.</summary>
|
|
[Fact]
|
|
public void Update_TlsModeAndCredentials_AreNotRequired()
|
|
{
|
|
var update = SmtpUpdateCommand();
|
|
var tls = update.Options.Single(o => o.Name == "--tls-mode");
|
|
var creds = update.Options.Single(o => o.Name == "--credentials");
|
|
|
|
Assert.False(tls.Required, "--tls-mode must be optional (preserve-if-omitted).");
|
|
Assert.False(creds.Required, "--credentials must be optional (preserve-if-omitted).");
|
|
}
|
|
/// <summary>#149: the delivery-policy flags parse onto the command (retry delay in seconds).</summary>
|
|
[Fact]
|
|
public void Update_WithDeliveryPolicyFlags_ProducesCommandCarryingThem()
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "1", "--server", "https://mail.example.com/ews/exchange.asmx", "--port", "587",
|
|
"--auth-mode", "Basic", "--from-address", "noreply@example.com",
|
|
"--max-retries", "10", "--retry-delay-seconds", "60",
|
|
"--connection-timeout-seconds", "20", "--max-concurrent-connections", "0");
|
|
|
|
Assert.Empty(parse.Errors);
|
|
var cmd = NotificationCommands.BuildUpdateSmtpConfigCommand(parse);
|
|
|
|
Assert.Equal(10, cmd.MaxRetries);
|
|
Assert.Equal(TimeSpan.FromSeconds(60), cmd.RetryDelay);
|
|
Assert.Equal(20, cmd.ConnectionTimeoutSeconds);
|
|
Assert.Equal(0, cmd.MaxConcurrentConnections);
|
|
}
|
|
|
|
/// <summary>#149: omitted delivery-policy flags map to null so the server preserves the stored values.</summary>
|
|
[Fact]
|
|
public void Update_WithoutDeliveryPolicyFlags_ProducesNulls()
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "1", "--server", "smtp.example.com", "--port", "587",
|
|
"--auth-mode", "Basic", "--from-address", "noreply@example.com");
|
|
|
|
Assert.Empty(parse.Errors);
|
|
var cmd = NotificationCommands.BuildUpdateSmtpConfigCommand(parse);
|
|
|
|
Assert.Null(cmd.MaxRetries);
|
|
Assert.Null(cmd.RetryDelay);
|
|
Assert.Null(cmd.ConnectionTimeoutSeconds);
|
|
Assert.Null(cmd.MaxConcurrentConnections);
|
|
}
|
|
|
|
/// <summary>#149: a negative delivery-policy flag is a parse error.</summary>
|
|
[Theory]
|
|
[InlineData("--max-retries")]
|
|
[InlineData("--retry-delay-seconds")]
|
|
[InlineData("--connection-timeout-seconds")]
|
|
[InlineData("--max-concurrent-connections")]
|
|
public void Update_NegativeDeliveryPolicyFlag_IsRejected(string flag)
|
|
{
|
|
var parse = ParseUpdate(
|
|
"--id", "1", "--server", "smtp.example.com", "--port", "587",
|
|
"--auth-mode", "Basic", "--from-address", "noreply@example.com",
|
|
flag, "-1");
|
|
|
|
Assert.NotEmpty(parse.Errors);
|
|
}
|
|
}
|