Path: redb-identity/tests/redb.Identity.Tests/FullStack/FullStackMfaEmailSmtpTests.cs Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/FullStack/FullStackMfaEmailSmtpTests.cs ---- using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using AwesomeAssertions; using MailKit.Net.Imap; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using redb.Identity.Contracts.Routes; using redb.Identity.Core.Configuration; using redb.Identity.Core.Services; using redb.Route.Components; using redb.Route.Core; using redb.Route.Mail; using Xunit; using Xunit.Abstractions; namespace redb.Identity.Tests.FullStack; /// /// The one-time code of e-mail MFA over a real SMTP server: on top of /// delivers the message to the /// GreenMail container, and the code is in the HTML and the text part. The enrollment itself over HTTP, with the /// in-memory e-mail channel of the test host, is covered by . /// [Trait("Category", "Integration")] [Trait("Requires", "Greenmail")] public sealed class FullStackMfaEmailSmtpTests { private const string GreenmailHost = "127.0.0.1"; private const int GreenmailSmtpPort = 3025; private const int GreenmailImapPort = 3143; private const string ApiBase = "http://127.0.0.1:8080"; private readonly ITestOutputHelper _output; public FullStackMfaEmailSmtpTests(ITestOutputHelper output) => _output = output; [Fact] public async Task The_mfa_code_lands_in_the_greenmail_inbox() { SkipIfGreenmailUnavailable(); var recipientUser = $"e2e-mfa-{Guid.NewGuid():N}"; var recipient = $"{recipientUser}@localhost"; await ProvisionGreenmailUserAsync(recipientUser, "secret"); var sc = new ServiceCollection(); sc.AddSingleton(); await using var sp = sc.BuildServiceProvider(); await using var ctx = new RouteContext(sp); ctx.AddComponent(new SmtpComponent()); ctx.AddRoutes(r => { r.From(IdentityEndpoints.EmailSend) .RouteId(IdentityEndpoints.RouteIds.EmailSend) .To(Smtp.Send(GreenmailHost) .Port(GreenmailSmtpPort) .Security("None") .From("noreply@redb.local") .ContentType("text/html") .AlternativeBody()); }); await ctx.Start(); var options = Options.Create(new RedbIdentityOptions { Smtp = new SmtpOptions { Enabled = true, Host = GreenmailHost, Port = GreenmailSmtpPort, Security = "None", FromAddress = "noreply@redb.local", } }); using var mail = new SmtpEmailNotificationChannel(ctx, new InlineEmailTemplateRegistry(), options, NullLoggerFactory.Instance); var channel = new EmailMfaDeliveryChannel(mail, TimeSpan.FromMinutes(5)); await channel.SendCodeAsync(recipient, "482913"); var (subject, htmlBody, textBody) = await ReadFirstMessageAsync(recipientUser, "secret"); _output.WriteLine($"Subject: {subject}"); subject.Should().Be("Your verification code"); htmlBody.Should().Contain("482913"); textBody.Should().Contain("482913").And.Contain("5 minutes"); } // ── Helpers (verbatim from FullStackPasswordRecoverySmtpTests; kept inline so each // integration test file is self-contained and can be removed independently) ── private static void SkipIfGreenmailUnavailable() { var reachable = false; try { using var probe = new TcpClient(); var task = probe.ConnectAsync(GreenmailHost, GreenmailSmtpPort); reachable = task.Wait(TimeSpan.FromMilliseconds(500)) && probe.Connected; } catch { /* fall through — reachable stays false */ } Assert.True(reachable, $"Greenmail not reachable on {GreenmailHost}:{GreenmailSmtpPort}. " + "Start with: docker compose -f redb.Route/docker-compose.tests.yml up greenmail -d, " + "or filter this test out via `--filter Category!=Integration`."); } private static async Task ProvisionGreenmailUserAsync(string login, string password) { using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(3) }; var json = $"{{\"email\":\"{login}@localhost\",\"login\":\"{login}\",\"password\":\"{password}\"}}"; using var content = new StringContent(json, Encoding.UTF8, "application/json"); try { using var _ = await http.PostAsync($"{ApiBase}/api/user", content); } catch { /* greenmail REST is best-effort here — SMTP listener creates the account on delivery anyway. */ } } private static async Task<(string? Subject, string? HtmlBody, string? TextBody)> ReadFirstMessageAsync( string login, string password) { Exception? lastError = null; for (var attempt = 0; attempt < 15; attempt++) { try { using var imap = new ImapClient(); await imap.ConnectAsync(GreenmailHost, GreenmailImapPort, MailKit.Security.SecureSocketOptions.None); await imap.AuthenticateAsync(login, password); var inbox = imap.Inbox; await inbox.OpenAsync(MailKit.FolderAccess.ReadOnly); if (inbox.Count > 0) { var msg = await inbox.GetMessageAsync(0); await imap.DisconnectAsync(true); return (msg.Subject, msg.HtmlBody, msg.TextBody); } await imap.DisconnectAsync(true); } catch (Exception ex) { lastError = ex; } await Task.Delay(200); } throw new Xunit.Sdk.XunitException( $"No message arrived in {login}@localhost INBOX within 3 s. Last error: {lastError?.Message}"); } }