1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-01 13:43:23 +01:00
bitwarden-server/test/Core.Test/Services/AmazonSesMailDeliveryServiceTests.cs
Matt Gibson 5537470703
Use sas token for attachment downloads (#1153)
* Get limited life attachment download URL

This change limits url download to a 1min lifetime.
This requires moving to a new container to allow for non-public blob
access.

Clients will have to call GetAttachmentData api function to receive the download
URL. For backwards compatibility, attachment URLs are still present, but will not
work for attachments stored in non-public access blobs.

* Make GlobalSettings interface for testing

* Test LocalAttachmentStorageService equivalence

* Remove comment

* Add missing globalSettings using

* Simplify default attachment container

* Default to attachments containe for existing methods

A new upload method will be made for uploading to attachments-v2.
For compatibility for clients which don't use these new methods, we need
to still use the old container. The new container will be used only for
new uploads

* Remove Default MetaData fixture.

* Keep attachments container blob-level security for all instances

* Close unclosed FileStream

* Favor default value for noop services
2021-02-22 15:35:16 -06:00

92 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
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;
namespace Bit.Core.Test.Services
{
public class AmazonSesMailDeliveryServiceTests : IDisposable
{
private readonly AmazonSesMailDeliveryService _sut;
private readonly GlobalSettings _globalSettings;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly ILogger<AmazonSesMailDeliveryService> _logger;
private readonly IAmazonSimpleEmailService _amazonSimpleEmailService;
public AmazonSesMailDeliveryServiceTests()
{
_globalSettings = new GlobalSettings
{
Amazon =
{
AccessKeyId = "AccessKeyId-AmazonSesMailDeliveryServiceTests",
AccessKeySecret = "AccessKeySecret-AmazonSesMailDeliveryServiceTests",
Region = "Region-AmazonSesMailDeliveryServiceTests"
}
};
_hostingEnvironment = Substitute.For<IWebHostEnvironment>();
_logger = Substitute.For<ILogger<AmazonSesMailDeliveryService>>();
_amazonSimpleEmailService = Substitute.For<IAmazonSimpleEmailService>();
_sut = new AmazonSesMailDeliveryService(
_globalSettings,
_hostingEnvironment,
_logger,
_amazonSimpleEmailService
);
}
public void Dispose()
{
_sut?.Dispose();
}
[Fact]
public async Task SendEmailAsync_CallsSendEmailAsync_WhenMessageIsValid()
{
var mailMessage = new MailMessage
{
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");
}));
}
}
}