feat(notifications): SMTP delivery policy settable via management/CLI/UI (#149)
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.
This commit is contained in:
@@ -153,4 +153,55 @@ public class SmtpUpdateCommandTests
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ public class SmtpConfigurationPageTests : BunitContext
|
||||
SelectWithOption(cut, "Ews").Change("Ews");
|
||||
|
||||
// Port input, TLS select and Auth Type select are all gone under EWS.
|
||||
Assert.Empty(cut.FindAll("input[type=number]"));
|
||||
Assert.Empty(cut.FindAll("input[type=number][max=\"65535\"]"));
|
||||
Assert.False(HasSelectWithOption(cut, "StartTLS"));
|
||||
Assert.False(HasSelectWithOption(cut, "OAuth2"));
|
||||
Assert.Contains("EWS URL", cut.Markup);
|
||||
@@ -212,7 +212,7 @@ public class SmtpConfigurationPageTests : BunitContext
|
||||
SelectWithOption(cut, "Ews").Change("Smtp");
|
||||
|
||||
// Port and TLS controls are back...
|
||||
Assert.Single(cut.FindAll("input[type=number]"));
|
||||
Assert.Single(cut.FindAll("input[type=number][max=\"65535\"]"));
|
||||
Assert.True(HasSelectWithOption(cut, "StartTLS"));
|
||||
Assert.True(HasSelectWithOption(cut, "OAuth2"));
|
||||
|
||||
|
||||
@@ -3356,6 +3356,107 @@ public class ManagementActorTests : TestKit, IDisposable
|
||||
Assert.Contains("username:password", response.Error);
|
||||
}
|
||||
|
||||
private (INotificationRepository Repo, Commons.Entities.Notifications.SmtpConfiguration Existing) SmtpPolicyFixture()
|
||||
{
|
||||
var notifRepo = Substitute.For<INotificationRepository>();
|
||||
var existing = new Commons.Entities.Notifications.SmtpConfiguration(
|
||||
"smtp.example.com", "Basic", "ops@example.com")
|
||||
{
|
||||
Id = 1,
|
||||
Port = 587,
|
||||
ConnectionTimeoutSeconds = 15,
|
||||
MaxConcurrentConnections = 3,
|
||||
MaxRetries = 4,
|
||||
RetryDelay = TimeSpan.FromSeconds(30),
|
||||
};
|
||||
notifRepo.GetSmtpConfigurationByIdAsync(1, Arg.Any<CancellationToken>()).Returns(existing);
|
||||
_services.AddScoped(_ => notifRepo);
|
||||
return (notifRepo, existing);
|
||||
}
|
||||
|
||||
/// <summary>#149: the delivery-policy fields on an update are applied to the stored row.</summary>
|
||||
[Fact]
|
||||
public void UpdateSmtpConfig_AppliesDeliveryPolicyFields()
|
||||
{
|
||||
var (_, existing) = SmtpPolicyFixture();
|
||||
var actor = CreateActor();
|
||||
|
||||
actor.Tell(Envelope(
|
||||
new UpdateSmtpConfigCommand(1, "smtp.example.com", 587, "Basic", "ops@example.com",
|
||||
ConnectionTimeoutSeconds: 20, MaxConcurrentConnections: 0, MaxRetries: 10, RetryDelay: TimeSpan.FromMinutes(1)),
|
||||
"Administrator"));
|
||||
|
||||
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(5));
|
||||
Assert.Equal(20, existing.ConnectionTimeoutSeconds);
|
||||
Assert.Equal(0, existing.MaxConcurrentConnections);
|
||||
Assert.Equal(10, existing.MaxRetries);
|
||||
Assert.Equal(TimeSpan.FromMinutes(1), existing.RetryDelay);
|
||||
}
|
||||
|
||||
/// <summary>#149: an update that omits the delivery-policy fields preserves the stored values.</summary>
|
||||
[Fact]
|
||||
public void UpdateSmtpConfig_WithoutDeliveryPolicyFields_PreservesExistingValues()
|
||||
{
|
||||
var (_, existing) = SmtpPolicyFixture();
|
||||
var actor = CreateActor();
|
||||
|
||||
actor.Tell(Envelope(
|
||||
new UpdateSmtpConfigCommand(1, "smtp.example.com", 587, "Basic", "ops@example.com"),
|
||||
"Administrator"));
|
||||
|
||||
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(5));
|
||||
Assert.Equal(15, existing.ConnectionTimeoutSeconds);
|
||||
Assert.Equal(3, existing.MaxConcurrentConnections);
|
||||
Assert.Equal(4, existing.MaxRetries);
|
||||
Assert.Equal(TimeSpan.FromSeconds(30), existing.RetryDelay);
|
||||
}
|
||||
|
||||
/// <summary>#149: a negative delivery-policy value is refused and nothing is saved.</summary>
|
||||
[Theory]
|
||||
[InlineData(-1, null, null, null, "ConnectionTimeoutSeconds")]
|
||||
[InlineData(null, -1, null, null, "MaxConcurrentConnections")]
|
||||
[InlineData(null, null, -1, null, "MaxRetries")]
|
||||
[InlineData(null, null, null, -1, "RetryDelay")]
|
||||
public void UpdateSmtpConfig_NegativeDeliveryPolicyValue_ReturnsManagementError(
|
||||
int? timeout, int? concurrency, int? retries, int? delaySeconds, string field)
|
||||
{
|
||||
var (repo, _) = SmtpPolicyFixture();
|
||||
var actor = CreateActor();
|
||||
|
||||
actor.Tell(Envelope(
|
||||
new UpdateSmtpConfigCommand(1, "smtp.example.com", 587, "Basic", "ops@example.com",
|
||||
ConnectionTimeoutSeconds: timeout, MaxConcurrentConnections: concurrency, MaxRetries: retries,
|
||||
RetryDelay: delaySeconds is { } d ? TimeSpan.FromSeconds(d) : null),
|
||||
"Administrator"));
|
||||
|
||||
var response = ExpectMsg<ManagementError>(TimeSpan.FromSeconds(5));
|
||||
Assert.Contains(field, response.Error);
|
||||
repo.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
/// <summary>#149: a create carries the delivery-policy fields onto the new row.</summary>
|
||||
[Fact]
|
||||
public void CreateSmtpConfig_CarriesDeliveryPolicyFields()
|
||||
{
|
||||
var notifRepo = Substitute.For<INotificationRepository>();
|
||||
Commons.Entities.Notifications.SmtpConfiguration? added = null;
|
||||
notifRepo.AddSmtpConfigurationAsync(Arg.Do<Commons.Entities.Notifications.SmtpConfiguration>(c => added = c), Arg.Any<CancellationToken>())
|
||||
.Returns(Task.CompletedTask);
|
||||
_services.AddScoped(_ => notifRepo);
|
||||
var actor = CreateActor();
|
||||
|
||||
actor.Tell(Envelope(
|
||||
new CreateSmtpConfigCommand("smtp.example.com", 587, "Basic", "ops@example.com",
|
||||
MaxRetries: 10, RetryDelay: TimeSpan.FromMinutes(1)),
|
||||
"Administrator"));
|
||||
|
||||
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(5));
|
||||
Assert.NotNull(added);
|
||||
Assert.Equal(10, added!.MaxRetries);
|
||||
Assert.Equal(TimeSpan.FromMinutes(1), added.RetryDelay);
|
||||
Assert.Equal(0, added.ConnectionTimeoutSeconds);
|
||||
}
|
||||
|
||||
/// <summary>Verifies that update smtp config, when with null transport, preserves existing value.</summary>
|
||||
[Fact]
|
||||
public void UpdateSmtpConfig_WithNullTransport_PreservesExistingValue()
|
||||
|
||||
Reference in New Issue
Block a user