shoc-backend/Api.SeaHavenIndustries.Tests/PasswordResetEmailChannelTests.cs

44 lines
1.7 KiB
C#
Raw Normal View History

using Api.SeaHavenIndustries.HostedServices;
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
namespace Api.SeaHavenIndustries.Tests;
public class PasswordResetEmailChannelTests
{
[Fact]
public void A_full_queue_refuses_the_email_instead_of_dropping_it_silently()
{
var channel = new PasswordResetEmailChannel(NullLogger<PasswordResetEmailChannel>.Instance);
for (var i = 0; i < PasswordResetEmailChannel.Capacity; i++)
channel.TryEnqueue($"user{i}@example.com", "subject", "body").Should().BeTrue();
var accepted = channel.TryEnqueue("late@example.com", "subject", "body");
accepted.Should().BeFalse();
channel.Pending.Should().Be(PasswordResetEmailChannel.Capacity);
}
[Fact]
public async Task A_refused_email_is_never_delivered_and_the_queue_accepts_again_once_drained()
{
var channel = new PasswordResetEmailChannel(NullLogger<PasswordResetEmailChannel>.Instance);
for (var i = 0; i < PasswordResetEmailChannel.Capacity; i++)
channel.TryEnqueue($"user{i}@example.com", "subject", "body");
channel.TryEnqueue("late@example.com", "subject", "body").Should().BeFalse();
var delivered = new List<string>();
while (channel.Reader.TryRead(out var email))
{
delivered.Add(email.EmailTo);
channel.MarkHandled();
}
delivered.Should().HaveCount(PasswordResetEmailChannel.Capacity).And.NotContain("late@example.com");
channel.Pending.Should().Be(0);
channel.TryEnqueue("retry@example.com", "subject", "body").Should().BeTrue();
(await channel.Reader.ReadAsync()).EmailTo.Should().Be("retry@example.com");
}
}