Replaced the `Email` record with the new `EmailContext` record to introduce additional context and functionality in email handling. Updated property definitions to distinguish between .NET Framework (`set`) and other frameworks (`init`). Modified `SendingEmailEvent` to use `EmailContext` for the `Mail` property. Updated mappings in `EmailMappingProfile` to map `SendEmailCommand` to `EmailContext`. Adjusted `SendEmailCommandHandler` to use `EmailContext` when mapping requests. Refactored `EmailSender` to use `EmailContext` in its `Send` method, including updates to method signatures and documentation. Updated all related test classes (`EmailSenderTests`, `EmailSenderUrlOverloadTests`, `SendingEmailPublisherTests`) to validate the behavior of `EmailContext`, ensuring consistency and thorough testing of the transition. These changes ensure compatibility across frameworks and improve the maintainability of the email handling process.
109 lines
3.7 KiB
C#
109 lines
3.7 KiB
C#
using DigitalData.MessagingService.Client;
|
|
using DigitalData.MessagingService.Abstraction;
|
|
|
|
namespace DigitalData.MessagingService.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// xUnit collection that serializes all <see cref="EmailSenderTests"/> so they share
|
|
/// the same process-wide static state of <see cref="EmailSender.LazyProvider"/>.
|
|
/// </summary>
|
|
[CollectionDefinition(Name)]
|
|
public sealed class EmailSenderCollection : ICollectionFixture<EmailSenderFixture>
|
|
{
|
|
public const string Name = "EmailSender";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fixture that connects <see cref="EmailSender"/> once before all tests in the collection run.
|
|
/// </summary>
|
|
public sealed class EmailSenderFixture
|
|
{
|
|
public EmailSenderFixture()
|
|
{
|
|
// EmailSender is a static class with a Lazy<IServiceProvider>.
|
|
// ConnectRabbitMq can only be called successfully once per process.
|
|
if (!EmailSender.IsConnected)
|
|
{
|
|
EmailSender.ConnectRabbitMq(cfg =>
|
|
{
|
|
cfg.HostName = RabbitMqTestConfig.HostName;
|
|
cfg.Port = RabbitMqTestConfig.Port;
|
|
cfg.UserName = RabbitMqTestConfig.UserName;
|
|
cfg.Password = RabbitMqTestConfig.Password;
|
|
cfg.VirtualHost = RabbitMqTestConfig.VirtualHost;
|
|
cfg.QueueName = RabbitMqTestConfig.QueueName;
|
|
cfg.ExchangeName = RabbitMqTestConfig.ExchangeName;
|
|
cfg.RoutingKey = RabbitMqTestConfig.RoutingKey;
|
|
cfg.DlqQueueName = RabbitMqTestConfig.DlqQueueName;
|
|
cfg.DlqExchangeName = RabbitMqTestConfig.DlqExchangeName;
|
|
cfg.DlqRoutingKey = RabbitMqTestConfig.DlqRoutingKey;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Integration tests for the <see cref="EmailSender"/> static client.
|
|
/// All tests run inside <see cref="EmailSenderCollection"/> to share the single
|
|
/// static connection established by <see cref="EmailSenderFixture"/>.
|
|
/// </summary>
|
|
[Collection(EmailSenderCollection.Name)]
|
|
public sealed class EmailSenderTests
|
|
{
|
|
[Fact]
|
|
public void IsConnected_AfterConnectRabbitMq_ReturnsTrue()
|
|
{
|
|
Assert.True(EmailSender.IsConnected);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConnectRabbitMq_WhenAlreadyConnected_WithThrowException_ThrowsInvalidOperationException()
|
|
{
|
|
Assert.Throws<InvalidOperationException>(() =>
|
|
EmailSender.ConnectRabbitMq(cfg => { }, OnReconnect.ThrowException));
|
|
}
|
|
|
|
[Fact]
|
|
public void ConnectRabbitMq_WhenAlreadyConnected_WithIgnore_DoesNotThrow()
|
|
{
|
|
var exception = Record.Exception(() =>
|
|
EmailSender.ConnectRabbitMq(cfg => { }, OnReconnect.Ignore));
|
|
|
|
Assert.Null(exception);
|
|
}
|
|
|
|
[Fact]
|
|
public void Send_WithValidEmail_DoesNotThrow()
|
|
{
|
|
var email = new EmailContext
|
|
{
|
|
Sender = new EmailAccountDto { Username = "test@example.com", Password = "password", SmtpServer = "smtp.example.com" },
|
|
Recipients = ["hakanttek@gmail.com"],
|
|
Subject = "EmailSender.Send Integration Test",
|
|
Body = "<p>Sent via EmailSender static client.</p>",
|
|
IsHtml = true,
|
|
};
|
|
|
|
var exception = Record.Exception(() => EmailSender.Send(email));
|
|
|
|
Assert.Null(exception);
|
|
}
|
|
|
|
[Fact]
|
|
public void Send_WithPlainTextBody_DoesNotThrow()
|
|
{
|
|
var email = new EmailContext
|
|
{
|
|
Sender = new EmailAccountDto { Username = "test@example.com", Password = "password", SmtpServer = "smtp.example.com" },
|
|
Recipients = ["hakanttek@gmail.com"],
|
|
Subject = "Plain Text Test",
|
|
Body = "This is a plain text email.",
|
|
IsHtml = false
|
|
};
|
|
|
|
var exception = Record.Exception(() => EmailSender.Send(email));
|
|
|
|
Assert.Null(exception);
|
|
}
|
|
}
|