mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-06 20:32:10 +00:00
44 lines
1.7 KiB
C#
44 lines
1.7 KiB
C#
|
|
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");
|
||
|
|
}
|
||
|
|
}
|