mirror of
https://github.com/bitwarden/server.git
synced 2025-12-10 00:42:07 -06:00
This implements a new Mailer service which supersedes the `HandlebarsMailService`. It allows teams to create emails without having to extend a generic service. The `IMailer` only contains a single method, `SendEmail`, which sends an instance of `BaseMail`.
38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
using Bit.Core.Models.Mail;
|
|
using Bit.Core.Platform.Mailer;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Test.Platform.Mailer.TestMail;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace Bit.Core.Test.Platform.Mailer;
|
|
|
|
public class MailerTest
|
|
{
|
|
[Fact]
|
|
public async Task SendEmailAsync()
|
|
{
|
|
var deliveryService = Substitute.For<IMailDeliveryService>();
|
|
var mailer = new Core.Platform.Mailer.Mailer(new HandlebarMailRenderer(), deliveryService);
|
|
|
|
var mail = new TestMail.TestMail()
|
|
{
|
|
ToEmails = ["test@bw.com"],
|
|
View = new TestMailView() { Name = "John Smith" }
|
|
};
|
|
|
|
MailMessage? sentMessage = null;
|
|
await deliveryService.SendEmailAsync(Arg.Do<MailMessage>(message =>
|
|
sentMessage = message
|
|
));
|
|
|
|
await mailer.SendEmail(mail);
|
|
|
|
Assert.NotNull(sentMessage);
|
|
Assert.Contains("test@bw.com", sentMessage.ToEmails);
|
|
Assert.Equal("Test Email", sentMessage.Subject);
|
|
Assert.Equivalent("Hello John Smith", sentMessage.TextContent.Trim());
|
|
Assert.Equivalent("Hello <b>John Smith</b>", sentMessage.HtmlContent.Trim());
|
|
}
|
|
}
|