test(notification-outbox): cover Email adapter permanent/cancellation arms, align error casing

This commit is contained in:
Joseph Doherty
2026-05-19 01:32:54 -04:00
parent b8dece0e70
commit 04e00d56c6
2 changed files with 82 additions and 6 deletions

View File

@@ -188,4 +188,80 @@ public class EmailNotificationDeliveryAdapterTests
Assert.Equal(DeliveryResult.TransientFailure, outcome.Result);
}
[Fact]
public async Task Deliver_UnclassifiedException_ReturnsPermanent()
{
SetupHappyPath();
_smtpClient
.SendAsync(Arg.Any<string>(), Arg.Any<IEnumerable<string>>(),
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new InvalidOperationException("token endpoint unreachable"));
var adapter = CreateAdapter();
var outcome = await adapter.DeliverAsync(MakeNotification());
Assert.Equal(DeliveryResult.PermanentFailure, outcome.Result);
Assert.NotNull(outcome.Error);
}
[Fact]
public async Task Deliver_MalformedRecipientAddress_ReturnsPermanent()
{
const string malformed = "bad-recipient@";
var list = new NotificationList("ops-team") { Id = 1 };
_repository.GetListByNameAsync("ops-team").Returns(list);
_repository.GetRecipientsByListIdAsync(1).Returns(new List<NotificationRecipient>
{
new("Mallory", malformed) { Id = 1, NotificationListId = 1 }
});
_repository.GetAllSmtpConfigurationsAsync().Returns(new List<SmtpConfiguration>
{
new("smtp.example.com", "basic", "noreply@example.com")
{
Id = 1, Port = 587, Credentials = "user:pass", TlsMode = "starttls"
}
});
var adapter = CreateAdapter();
var outcome = await adapter.DeliverAsync(MakeNotification());
Assert.Equal(DeliveryResult.PermanentFailure, outcome.Result);
Assert.Contains(malformed, outcome.Error);
}
[Fact]
public async Task Deliver_CancelledToken_ThrowsOperationCanceledException()
{
using var cts = new CancellationTokenSource();
cts.Cancel();
// List/recipient/SMTP-config resolution must succeed for any token so that
// delivery proceeds far enough to observe the cancellation at the SMTP call.
var list = new NotificationList("ops-team") { Id = 1 };
_repository.GetListByNameAsync("ops-team", Arg.Any<CancellationToken>()).Returns(list);
_repository.GetRecipientsByListIdAsync(1, Arg.Any<CancellationToken>())
.Returns(new List<NotificationRecipient>
{
new("Alice", "alice@example.com") { Id = 1, NotificationListId = 1 }
});
_repository.GetAllSmtpConfigurationsAsync(Arg.Any<CancellationToken>())
.Returns(new List<SmtpConfiguration>
{
new("smtp.example.com", "basic", "noreply@example.com")
{
Id = 1, Port = 587, Credentials = "user:pass", TlsMode = "starttls"
}
});
// The connect call honours the cancelled token, mirroring a real SMTP client.
_smtpClient
.ConnectAsync(Arg.Any<string>(), Arg.Any<int>(), Arg.Any<SmtpTlsMode>(),
Arg.Any<int>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new OperationCanceledException(cts.Token));
var adapter = CreateAdapter();
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => adapter.DeliverAsync(MakeNotification(), cts.Token));
}
}