test(notification-outbox): cover Email adapter permanent/cancellation arms, align error casing
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user