Files
server/test/Core.Test/Platform/Mail/DomainClaimedEmailRenderTest.cs
Jared 7c216366a7 [PM-31153] email updates for domain claim pt 2 (#6965)
* [PM-31361] Enhance domain claimed email notifications

* Updated the email template to include the claimed domain name and user email.
* Modified the `ClaimedUserDomainClaimedEmails` model to include the domain name.
* Adjusted the `SendClaimedDomainUserEmailAsync` method to pass the domain name to the email message.
* Added a new test for rendering the domain claimed email to ensure proper content delivery.

* Update email templates for domain claimed notifications

* Adjusted styles and formatting in the DomainClaimedByOrganization email template for improved readability.
* Modified the TitleContactUs layout to ensure proper rendering of titles.
* Updated the HandlebarsMailService to include HTML line breaks in the email title for better presentation.

* Update TitleContactUs email template to center-align title text for improved presentation

* Refine TitleContactUs email template by removing unnecessary text-align property for improved consistency in styling

* Fix PR comments

* Update test/Core.Test/Platform/Mail/DomainClaimedEmailRenderTest.cs

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>

* Update test/Core.Test/Platform/Mail/DomainClaimedEmailRenderTest.cs

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>

* Update test/Core.Test/Platform/Mail/DomainClaimedEmailRenderTest.cs

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>

* Remove unnecessary comments

---------

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
2026-02-09 14:38:50 -05:00

196 lines
6.0 KiB
C#

using Bit.Core.AdminConsole.Entities;
using Bit.Core.Models.Data.Organizations;
using Bit.Core.Platform.Mail.Delivery;
using Bit.Core.Platform.Mail.Enqueuing;
using Bit.Core.Services.Mail;
using Bit.Core.Settings;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace Bit.Core.Test.Platform.Mail;
public class DomainClaimedEmailRenderTest
{
[Fact]
public async Task RenderDomainClaimedEmail_ToVerifyTemplate()
{
var globalSettings = new GlobalSettings
{
Mail = new GlobalSettings.MailSettings
{
ReplyToEmail = "no-reply@bitwarden.com",
Smtp = new GlobalSettings.MailSettings.SmtpSettings
{
Host = "localhost",
Port = 1025,
StartTls = false,
Ssl = false
}
},
SiteName = "Bitwarden"
};
var mailDeliveryService = Substitute.For<IMailDeliveryService>();
var mailEnqueuingService = new BlockingMailEnqueuingService();
var distributedCache = Substitute.For<IDistributedCache>();
var logger = Substitute.For<ILogger<HandlebarsMailService>>();
var mailService = new HandlebarsMailService(
globalSettings,
mailDeliveryService,
mailEnqueuingService,
distributedCache,
logger
);
var organization = new Organization
{
Id = Guid.NewGuid(),
Name = "Acme Corporation"
};
var testEmails = new List<string>
{
"alice@acme.com",
"bob@acme.com",
"charlie@acme.com"
};
var emailList = new ClaimedUserDomainClaimedEmails(
testEmails,
organization,
"acme.com"
);
await mailService.SendClaimedDomainUserEmailAsync(emailList);
await mailDeliveryService.Received(3).SendEmailAsync(Arg.Any<Bit.Core.Models.Mail.MailMessage>());
var calls = mailDeliveryService.ReceivedCalls()
.Where(call => call.GetMethodInfo().Name == "SendEmailAsync")
.ToList();
Assert.Equal(3, calls.Count);
foreach (var call in calls)
{
var mailMessage = call.GetArguments()[0] as Bit.Core.Models.Mail.MailMessage;
Assert.NotNull(mailMessage);
var recipient = mailMessage.ToEmails.First();
Assert.Contains("@acme.com", mailMessage.HtmlContent);
Assert.Contains(recipient, mailMessage.HtmlContent);
Assert.DoesNotContain("[at]", mailMessage.HtmlContent);
Assert.DoesNotContain("[dot]", mailMessage.HtmlContent);
}
}
[Fact(Skip = "For local development - requires MailCatcher at localhost:10250")]
public async Task SendDomainClaimedEmail_ToMailCatcher()
{
var globalSettings = new GlobalSettings
{
Mail = new GlobalSettings.MailSettings
{
ReplyToEmail = "no-reply@bitwarden.com",
Smtp = new GlobalSettings.MailSettings.SmtpSettings
{
Host = "localhost",
Port = 10250,
StartTls = false,
Ssl = false
}
},
SiteName = "Bitwarden"
};
var mailDeliveryLogger = Substitute.For<ILogger<MailKitSmtpMailDeliveryService>>();
var mailDeliveryService = new MailKitSmtpMailDeliveryService(globalSettings, mailDeliveryLogger);
var mailEnqueuingService = new BlockingMailEnqueuingService();
var distributedCache = Substitute.For<IDistributedCache>();
var logger = Substitute.For<ILogger<HandlebarsMailService>>();
var mailService = new HandlebarsMailService(
globalSettings,
mailDeliveryService,
mailEnqueuingService,
distributedCache,
logger
);
var organization = new Organization
{
Id = Guid.NewGuid(),
Name = "Acme Corporation"
};
var testEmails = new List<string>
{
"alice@acme.com",
"bob@acme.com"
};
var emailList = new ClaimedUserDomainClaimedEmails(
testEmails,
organization,
"acme.com"
);
await mailService.SendClaimedDomainUserEmailAsync(emailList);
}
[Fact(Skip = "This test sends actual emails and is for manual template verification only")]
public async Task RenderDomainClaimedEmail_WithSpecialCharacters()
{
var globalSettings = new GlobalSettings
{
Mail = new GlobalSettings.MailSettings
{
Smtp = new GlobalSettings.MailSettings.SmtpSettings
{
Host = "localhost",
Port = 1025,
StartTls = false,
Ssl = false
}
},
SiteName = "Bitwarden"
};
var mailDeliveryService = Substitute.For<IMailDeliveryService>();
var mailEnqueuingService = new BlockingMailEnqueuingService();
var distributedCache = Substitute.For<IDistributedCache>();
var logger = Substitute.For<ILogger<HandlebarsMailService>>();
var mailService = new HandlebarsMailService(
globalSettings,
mailDeliveryService,
mailEnqueuingService,
distributedCache,
logger
);
var organization = new Organization
{
Id = Guid.NewGuid(),
Name = "Test Corp & Co."
};
var testEmails = new List<string>
{
"test.user+tag@example.com"
};
var emailList = new ClaimedUserDomainClaimedEmails(
testEmails,
organization,
"example.com"
);
await mailService.SendClaimedDomainUserEmailAsync(emailList);
}
}