1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-05 14:23:30 +01:00
bitwarden-server/test/Core.Test/Services/AmazonSesMailDeliveryServiceTests.cs

87 lines
3.0 KiB
C#
Raw Normal View History

using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
using Bit.Core.Models.Mail;
using Bit.Core.Services;
using Bit.Core.Settings;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
2022-08-29 22:06:55 +02:00
namespace Bit.Core.Test.Services;
public class AmazonSesMailDeliveryServiceTests : IDisposable
{
2022-08-29 22:06:55 +02:00
private readonly AmazonSesMailDeliveryService _sut;
2022-08-29 22:06:55 +02:00
private readonly GlobalSettings _globalSettings;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly ILogger<AmazonSesMailDeliveryService> _logger;
private readonly IAmazonSimpleEmailService _amazonSimpleEmailService;
2022-08-29 22:06:55 +02:00
public AmazonSesMailDeliveryServiceTests()
{
_globalSettings = new GlobalSettings
{
2022-08-29 22:06:55 +02:00
Amazon =
{
AccessKeyId = "AccessKeyId-AmazonSesMailDeliveryServiceTests",
AccessKeySecret = "AccessKeySecret-AmazonSesMailDeliveryServiceTests",
Region = "Region-AmazonSesMailDeliveryServiceTests"
}
};
2022-08-29 22:06:55 +02:00
_hostingEnvironment = Substitute.For<IWebHostEnvironment>();
_logger = Substitute.For<ILogger<AmazonSesMailDeliveryService>>();
_amazonSimpleEmailService = Substitute.For<IAmazonSimpleEmailService>();
2022-08-29 22:06:55 +02:00
_sut = new AmazonSesMailDeliveryService(
_globalSettings,
_hostingEnvironment,
_logger,
_amazonSimpleEmailService
);
}
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
await _sut.SendEmailAsync(mailMessage);
2022-08-29 22:06:55 +02:00
await _amazonSimpleEmailService.Received(1).SendEmailAsync(
Arg.Do<SendEmailRequest>(request =>
{
Assert.False(string.IsNullOrEmpty(request.Source));
2022-08-29 22:06:55 +02:00
Assert.Single(request.Destination.ToAddresses);
Assert.Equal(mailMessage.ToEmails.First(), request.Destination.ToAddresses.First());
2022-08-29 22:06:55 +02:00
Assert.Equal(mailMessage.Subject, request.Message.Subject.Data);
Assert.Equal(mailMessage.HtmlContent, request.Message.Body.Html.Data);
Assert.Equal(mailMessage.TextContent, request.Message.Body.Text.Data);
2022-08-29 22:06:55 +02:00
Assert.Single(request.Destination.BccAddresses);
Assert.Equal(mailMessage.BccEmails.First(), request.Destination.BccAddresses.First());
2022-08-29 22:06:55 +02:00
Assert.Contains(request.Tags, x => x.Name == "Environment");
Assert.Contains(request.Tags, x => x.Name == "Sender");
Assert.Contains(request.Tags, x => x.Name == "Category");
}));
}
}