1631786ce2
Add push contracts, provider delivery and channel diagnostics. Link email channels to services and document migration and validation. Ref: IT-1074
92 lines
3.8 KiB
C#
92 lines
3.8 KiB
C#
namespace HrynCo.NotificationService.Services.Tests;
|
|
|
|
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
|
using HrynCo.NotificationService.DAL.Abstract.Repositories;
|
|
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
|
using HrynCo.NotificationService.Migrator;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
|
|
public sealed class DevelopmentDataSeederTests
|
|
{
|
|
private readonly IEmailChannelRepository _channels = Substitute.For<IEmailChannelRepository>();
|
|
private readonly IEmailTemplateRepository _templates = Substitute.For<IEmailTemplateRepository>();
|
|
private readonly INotificationServiceRepository _services = Substitute.For<INotificationServiceRepository>();
|
|
|
|
public DevelopmentDataSeederTests()
|
|
{
|
|
_services.SaveAsync(Arg.Any<NotificationServiceInfo>(), true, Arg.Any<CancellationToken>())
|
|
.Returns(call => { call.Arg<NotificationServiceInfo>().Id = Guid.NewGuid(); return Task.CompletedTask; });
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SeedAsync_WhenConfigurationIsMissing_CreatesMailpitChannelAndTestTemplate()
|
|
{
|
|
_channels.GetByServiceAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
|
.Returns(Array.Empty<EmailChannel>());
|
|
_templates.GetAsync("TestService", "TestEmail", "en", Arg.Any<CancellationToken>())
|
|
.Returns((EmailTemplate?)null);
|
|
|
|
DevelopmentDataSeeder seeder = CreateSeeder();
|
|
|
|
await seeder.SeedAsync("TestService", CancellationToken.None);
|
|
|
|
await _channels.Received(1).AddAsync(
|
|
Arg.Is<EmailChannel>(channel => IsMailpitChannel(channel)),
|
|
Arg.Any<CancellationToken>());
|
|
await _templates.Received(1).AddAsync(
|
|
Arg.Is<EmailTemplate>(template =>
|
|
template.ServiceName == "TestService" &&
|
|
template.Key == "TestEmail" &&
|
|
template.Variables.Any(variable => variable.Name == "Message" && variable.Required)),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SeedAsync_WhenConfigurationExists_DoesNotDuplicateOrOverwriteIt()
|
|
{
|
|
_channels.GetByServiceAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
|
.Returns([new EmailChannel
|
|
{
|
|
ServiceId = Guid.NewGuid(),
|
|
EmailChannelType = EmailChannelType.Smtp,
|
|
Settings = new SmtpChannelSettings()
|
|
}]);
|
|
_templates.GetAsync("TestService", "TestEmail", "en", Arg.Any<CancellationToken>())
|
|
.Returns(new EmailTemplate
|
|
{
|
|
ServiceName = "TestService",
|
|
Key = "TestEmail",
|
|
LanguageCode = "en",
|
|
Subject = "Existing",
|
|
HtmlBody = "Existing",
|
|
TextBody = "Existing"
|
|
});
|
|
|
|
DevelopmentDataSeeder seeder = CreateSeeder();
|
|
|
|
await seeder.SeedAsync("TestService", CancellationToken.None);
|
|
|
|
await _channels.DidNotReceive().AddAsync(Arg.Any<EmailChannel>(), Arg.Any<CancellationToken>());
|
|
await _templates.DidNotReceive().AddAsync(Arg.Any<EmailTemplate>(), Arg.Any<CancellationToken>());
|
|
await _channels.DidNotReceive().UpdateAsync(Arg.Any<EmailChannel>(), Arg.Any<CancellationToken>());
|
|
await _templates.DidNotReceive().UpdateAsync(Arg.Any<EmailTemplate>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
private DevelopmentDataSeeder CreateSeeder() => new(
|
|
_channels,
|
|
_templates,
|
|
_services,
|
|
NullLogger<DevelopmentDataSeeder>.Instance);
|
|
|
|
private static bool IsMailpitChannel(EmailChannel channel)
|
|
{
|
|
return channel.ServiceId != Guid.Empty &&
|
|
channel.IsActive &&
|
|
channel.Settings is SmtpChannelSettings smtp &&
|
|
smtp.Host == "mailpit" &&
|
|
smtp.Port == 1025 &&
|
|
!smtp.UseSsl;
|
|
}
|
|
}
|