Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.RegularExpressions;
using FluentAssertions;
using CleanArchitecture.IntegrationTests.Infrastructure;
using Identity.Application.Services;
using Identity.Infrastructure.Services;
using Microsoft.AspNetCore.WebUtilities;

Expand Down Expand Up @@ -49,7 +50,7 @@ private static AuthPayload ExtractAuthPayload(string jsonContent)

private static (string userId, string token) ExtractUserIdAndTokenFromLastEmail(string recipient)
{
var message = FakeEmailService.GetLastMessage(recipient);
var message = WaitForLastMessage(recipient, timeoutMs: 3000, pollMs: 100);
message.Should().NotBeNull("Expected an email to be sent");

var link = ExtractFirstLink(message!.TextBody);
Expand All @@ -65,6 +66,23 @@ private static (string userId, string token) ExtractUserIdAndTokenFromLastEmail(
return (userId, token);
}

private static EmailMessage? WaitForLastMessage(string recipient, int timeoutMs, int pollMs)
{
var start = DateTime.UtcNow;
while ((DateTime.UtcNow - start).TotalMilliseconds < timeoutMs)
{
var message = FakeEmailService.GetLastMessage(recipient);
if (message != null)
{
return message;
}

Thread.Sleep(pollMs);
}

return null;
}

private static string ExtractFirstLink(string text)
{
var match = Regex.Match(text, @"https?://\S+");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.Json;
using System.Text.RegularExpressions;
using FluentAssertions;
using Identity.Application.Services;
using Identity.Infrastructure.Services;
using Microsoft.AspNetCore.WebUtilities;

Expand Down Expand Up @@ -69,7 +70,7 @@ public static async Task<string> AuthenticateAsync(HttpClient client)

private static (string userId, string token) ExtractUserIdAndTokenFromLastEmail(string recipient)
{
var message = FakeEmailService.GetLastMessage(recipient);
var message = WaitForLastMessage(recipient, timeoutMs: 3000, pollMs: 100);
message.Should().NotBeNull("Expected an email to be sent");

var link = ExtractFirstLink(message!.TextBody);
Expand All @@ -85,6 +86,23 @@ private static (string userId, string token) ExtractUserIdAndTokenFromLastEmail(
return (userId, token);
}

private static EmailMessage? WaitForLastMessage(string recipient, int timeoutMs, int pollMs)
{
var start = DateTime.UtcNow;
while ((DateTime.UtcNow - start).TotalMilliseconds < timeoutMs)
{
var message = FakeEmailService.GetLastMessage(recipient);
if (message != null)
{
return message;
}

Thread.Sleep(pollMs);
}

return null;
}

private static string ExtractFirstLink(string text)
{
var match = Regex.Match(text, @"https?://\S+");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ public PostgresWebApplicationFactory()

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
if (_useTestcontainers && TryEnsureContainer())
builder.ConfigureAppConfiguration((context, config) =>
{
_connectionString = BuildConnectionString(_container!);
var settings = new Dictionary<string, string?>
{
["EmailSettings:UseFakeEmail"] = "true"
};

builder.ConfigureAppConfiguration((context, config) =>
if (_useTestcontainers && TryEnsureContainer())
{
var settings = new Dictionary<string, string?>
{
["ConnectionStrings:DefaultConnection"] = _connectionString,
["EmailSettings:UseFakeEmail"] = "true"
};

config.AddInMemoryCollection(settings);
});
}
_connectionString = BuildConnectionString(_container!);
settings["ConnectionStrings:DefaultConnection"] = _connectionString;
}

config.AddInMemoryCollection(settings);
});

builder.ConfigureTestServices(services =>
{
Expand Down
Loading