1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-24 12:35:25 +01:00
bitwarden-server/test/Core.Test/Services/SendGridMailDeliveryServiceTests.cs

85 lines
2.8 KiB
C#
Raw Normal View History

using Bit.Core.Models.Mail;
using Bit.Core.Services;
using Bit.Core.Settings;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using NSubstitute;
using SendGrid;
using SendGrid.Helpers.Mail;
using Xunit;
2022-08-29 22:06:55 +02:00
namespace Bit.Core.Test.Services;
public class SendGridMailDeliveryServiceTests : IDisposable
{
2022-08-29 22:06:55 +02:00
private readonly SendGridMailDeliveryService _sut;
2022-08-29 22:06:55 +02:00
private readonly GlobalSettings _globalSettings;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly ILogger<SendGridMailDeliveryService> _logger;
private readonly ISendGridClient _sendGridClient;
2022-08-29 22:06:55 +02:00
public SendGridMailDeliveryServiceTests()
{
_globalSettings = new GlobalSettings
{
2022-08-29 22:06:55 +02:00
Mail =
{
2022-08-29 22:06:55 +02:00
SendGridApiKey = "SendGridApiKey"
}
};
2022-08-29 22:06:55 +02:00
_hostingEnvironment = Substitute.For<IWebHostEnvironment>();
_logger = Substitute.For<ILogger<SendGridMailDeliveryService>>();
_sendGridClient = Substitute.For<ISendGridClient>();
2022-08-29 22:06:55 +02:00
_sut = new SendGridMailDeliveryService(
_sendGridClient,
_globalSettings,
_hostingEnvironment,
_logger
);
}
2022-08-29 22:06:55 +02:00
public void Dispose()
{
_sut?.Dispose();
}
2022-08-29 22:06:55 +02:00
[Fact]
public async Task SendEmailAsync_CallsSendEmailAsync_WhenMessageIsValid()
{
var mailMessage = new MailMessage
{
2022-08-29 22:06:55 +02:00
ToEmails = new List<string> { "ToEmails" },
BccEmails = new List<string> { "BccEmails" },
Subject = "Subject",
HtmlContent = "HtmlContent",
TextContent = "TextContent",
Category = "Category"
};
2022-08-29 22:06:55 +02:00
_sendGridClient.SendEmailAsync(Arg.Any<SendGridMessage>()).Returns(
new Response(System.Net.HttpStatusCode.OK, null, null));
await _sut.SendEmailAsync(mailMessage);
2022-08-29 22:06:55 +02:00
await _sendGridClient.Received(1).SendEmailAsync(
Arg.Do<SendGridMessage>(msg =>
{
msg.Received(1).AddTos(new List<EmailAddress> { new EmailAddress(mailMessage.ToEmails.First()) });
msg.Received(1).AddBccs(new List<EmailAddress> { new EmailAddress(mailMessage.ToEmails.First()) });
2022-08-29 22:06:55 +02:00
Assert.Equal(mailMessage.Subject, msg.Subject);
Assert.Equal(mailMessage.HtmlContent, msg.HtmlContent);
Assert.Equal(mailMessage.TextContent, msg.PlainTextContent);
2022-08-29 22:06:55 +02:00
Assert.Contains("type:Cateogry", msg.Categories);
Assert.Contains(msg.Categories, x => x.StartsWith("env:"));
Assert.Contains(msg.Categories, x => x.StartsWith("sender:"));
2022-08-29 22:06:55 +02:00
msg.Received(1).SetClickTracking(false, false);
msg.Received(1).SetOpenTracking(false);
}));
}
}