-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(#59): Added decorator to allow global BCC address in all emails
- Loading branch information
1 parent
0c458f2
commit c17f430
Showing
4 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Fusonic GmbH. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE file in the project root for license information. | ||
|
||
using Fusonic.Extensions.Mediator; | ||
|
||
namespace Fusonic.Extensions.Email; | ||
|
||
public class SendEmailBccDecorator(IRequestHandler<SendEmail, Unit> innerHandler, SendEmailBccDecorator.Options options) : IRequestHandler<SendEmail, Unit> | ||
{ | ||
public record Options(string BccAddress); | ||
|
||
public Task<Unit> Handle(SendEmail request, CancellationToken cancellationToken) | ||
{ | ||
if (string.IsNullOrWhiteSpace(request.BccRecipient)) | ||
{ | ||
request = request with | ||
{ | ||
BccRecipient = options.BccAddress | ||
}; | ||
} | ||
|
||
return innerHandler.Handle(request, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) Fusonic GmbH. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE file in the project root for license information. | ||
|
||
using System.Globalization; | ||
using FluentAssertions; | ||
using Fusonic.Extensions.Email.Tests.Models; | ||
using netDumbster.smtp; | ||
using SimpleInjector; | ||
using Xunit; | ||
|
||
namespace Fusonic.Extensions.Email.Tests; | ||
|
||
public class SendEmailBccDecoratorTests(SendEmailBccDecoratorTests.SendEmailBccDecoratorTestsFixture fixture) : TestBase<SendEmailBccDecoratorTests.SendEmailBccDecoratorTestsFixture>(fixture) | ||
{ | ||
[Fact] | ||
public async Task SendEmail_NoBccDefined_SetsConfiguredBccAddress() | ||
{ | ||
Fixture.SmtpServer!.ClearReceivedEmail(); | ||
|
||
var model = new SendEmailTestEmailViewModel { SomeField = "Some field." }; | ||
await SendAsync(new SendEmail("[email protected]", "The Recipient", new CultureInfo("de-AT"), model)); | ||
|
||
Fixture.SmtpServer.ReceivedEmailCount.Should().Be(1); | ||
var email = Fixture.SmtpServer.ReceivedEmail.Single(); | ||
email.ToAddresses | ||
.Should() | ||
.HaveCount(2) | ||
.And.Contain(a => a.Address == "[email protected]") | ||
.And.Contain(a => a.Address == "[email protected]"); | ||
} | ||
|
||
[Fact] | ||
public async Task SendEmail_BccDefined_ConfiguredBccAddressDoesNotOverwrite() | ||
{ | ||
Fixture.SmtpServer!.ClearReceivedEmail(); | ||
|
||
var model = new SendEmailTestEmailViewModel { SomeField = "Some field." }; | ||
await SendAsync(new SendEmail("[email protected]", "The Recipient", new CultureInfo("de-AT"), model, BccRecipient: "[email protected]")); | ||
|
||
Fixture.SmtpServer.ReceivedEmailCount.Should().Be(1); | ||
var email = Fixture.SmtpServer.ReceivedEmail.Single(); | ||
email.ToAddresses | ||
.Should() | ||
.HaveCount(2) | ||
.And.Contain(a => a.Address == "[email protected]") | ||
.And.Contain(a => a.Address == "[email protected]"); | ||
} | ||
|
||
public class SendEmailBccDecoratorTestsFixture : TestFixture | ||
{ | ||
public SimpleSmtpServer? SmtpServer { get; private set; } | ||
|
||
protected override void RegisterDependencies(Container container) | ||
{ | ||
base.RegisterDependencies(container); | ||
|
||
SmtpServer = SimpleSmtpServer.Start(); | ||
|
||
container.RegisterEmail(c => | ||
{ | ||
c.CssPath = "email.css"; | ||
c.SmtpServer = "localhost"; | ||
c.SmtpPort = SmtpServer.Configuration.Port; | ||
c.SenderAddress = "[email protected]"; | ||
c.SenderName = "Test Fusonic"; | ||
c.BccAddress = "[email protected]"; | ||
}); | ||
} | ||
|
||
public override async ValueTask DisposeAsync() | ||
{ | ||
SmtpServer?.Dispose(); | ||
await base.DisposeAsync(); | ||
} | ||
} | ||
} |