2022-06-30 01:46:41 +02:00
|
|
|
|
using Amazon.SimpleEmail;
|
2020-06-18 15:55:46 +02:00
|
|
|
|
using Amazon.SimpleEmail.Model;
|
|
|
|
|
using Bit.Core.Models.Mail;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
using Bit.Core.Services;
|
2021-02-22 22:35:16 +01:00
|
|
|
|
using Bit.Core.Settings;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.Services;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2019-07-06 05:35:54 +02:00
|
|
|
|
public class AmazonSesMailDeliveryServiceTests : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly AmazonSesMailDeliveryService _sut;
|
|
|
|
|
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2020-01-10 14:47:58 +01:00
|
|
|
|
private readonly IWebHostEnvironment _hostingEnvironment;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
private readonly ILogger<AmazonSesMailDeliveryService> _logger;
|
2020-06-18 15:55:46 +02:00
|
|
|
|
private readonly IAmazonSimpleEmailService _amazonSimpleEmailService;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
|
|
|
|
|
public AmazonSesMailDeliveryServiceTests()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2020-06-18 15:55:46 +02:00
|
|
|
|
_globalSettings = new GlobalSettings
|
2019-07-06 05:35:54 +02:00
|
|
|
|
{
|
2020-06-18 15:57:24 +02:00
|
|
|
|
Amazon =
|
2020-06-18 15:55:46 +02:00
|
|
|
|
{
|
|
|
|
|
AccessKeyId = "AccessKeyId-AmazonSesMailDeliveryServiceTests",
|
|
|
|
|
AccessKeySecret = "AccessKeySecret-AmazonSesMailDeliveryServiceTests",
|
|
|
|
|
Region = "Region-AmazonSesMailDeliveryServiceTests"
|
|
|
|
|
}
|
2020-06-18 15:57:24 +02:00
|
|
|
|
};
|
2020-06-18 15:55:46 +02:00
|
|
|
|
|
2020-01-10 14:47:58 +01:00
|
|
|
|
_hostingEnvironment = Substitute.For<IWebHostEnvironment>();
|
2019-07-06 05:35:54 +02:00
|
|
|
|
_logger = Substitute.For<ILogger<AmazonSesMailDeliveryService>>();
|
2020-06-18 15:55:46 +02:00
|
|
|
|
_amazonSimpleEmailService = Substitute.For<IAmazonSimpleEmailService>();
|
|
|
|
|
|
2019-07-06 05:35:54 +02:00
|
|
|
|
_sut = new AmazonSesMailDeliveryService(
|
|
|
|
|
_globalSettings,
|
|
|
|
|
_hostingEnvironment,
|
2020-06-18 15:55:46 +02:00
|
|
|
|
_logger,
|
|
|
|
|
_amazonSimpleEmailService
|
2019-07-06 05:35:54 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_sut?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 15:55:46 +02:00
|
|
|
|
[Fact]
|
|
|
|
|
public async Task SendEmailAsync_CallsSendEmailAsync_WhenMessageIsValid()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2020-06-18 15:55:46 +02:00
|
|
|
|
var mailMessage = new MailMessage
|
2019-07-06 05:35:54 +02:00
|
|
|
|
{
|
2020-06-18 15:55:46 +02:00
|
|
|
|
ToEmails = new List<string> { "ToEmails" },
|
|
|
|
|
BccEmails = new List<string> { "BccEmails" },
|
|
|
|
|
Subject = "Subject",
|
|
|
|
|
HtmlContent = "HtmlContent",
|
|
|
|
|
TextContent = "TextContent",
|
|
|
|
|
Category = "Category"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await _sut.SendEmailAsync(mailMessage);
|
|
|
|
|
|
|
|
|
|
await _amazonSimpleEmailService.Received(1).SendEmailAsync(
|
|
|
|
|
Arg.Do<SendEmailRequest>(request =>
|
|
|
|
|
{
|
|
|
|
|
Assert.False(string.IsNullOrEmpty(request.Source));
|
|
|
|
|
|
|
|
|
|
Assert.Single(request.Destination.ToAddresses);
|
|
|
|
|
Assert.Equal(mailMessage.ToEmails.First(), request.Destination.ToAddresses.First());
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
Assert.Single(request.Destination.BccAddresses);
|
|
|
|
|
Assert.Equal(mailMessage.BccEmails.First(), request.Destination.BccAddresses.First());
|
|
|
|
|
|
|
|
|
|
Assert.Contains(request.Tags, x => x.Name == "Environment");
|
|
|
|
|
Assert.Contains(request.Tags, x => x.Name == "Sender");
|
|
|
|
|
Assert.Contains(request.Tags, x => x.Name == "Category");
|
|
|
|
|
}));
|
2019-07-06 05:35:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|