2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Text.Json;
|
2021-03-30 23:21:46 +02:00
|
|
|
|
using AutoFixture.Xunit2;
|
2023-04-18 14:05:17 +02:00
|
|
|
|
using Bit.Api.Tools.Controllers;
|
|
|
|
|
using Bit.Api.Tools.Models.Request;
|
|
|
|
|
using Bit.Api.Tools.Models.Response;
|
2021-06-08 23:34:36 +02:00
|
|
|
|
using Bit.Core.Context;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2022-12-13 20:30:01 +01:00
|
|
|
|
using Bit.Core.Exceptions;
|
2021-03-30 23:21:46 +02:00
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using Bit.Core.Settings;
|
2023-04-18 14:05:17 +02:00
|
|
|
|
using Bit.Core.Tools.Entities;
|
|
|
|
|
using Bit.Core.Tools.Enums;
|
|
|
|
|
using Bit.Core.Tools.Repositories;
|
|
|
|
|
using Bit.Core.Tools.Services;
|
2021-03-30 23:21:46 +02:00
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2023-04-18 14:05:17 +02:00
|
|
|
|
namespace Bit.Api.Test.Tools.Controllers;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
|
|
|
|
public class SendsControllerTests : IDisposable
|
2021-03-30 23:21:46 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
private readonly SendsController _sut;
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
private readonly ISendRepository _sendRepository;
|
|
|
|
|
private readonly ISendService _sendService;
|
|
|
|
|
private readonly ISendFileStorageService _sendFileStorageService;
|
|
|
|
|
private readonly ILogger<SendsController> _logger;
|
|
|
|
|
private readonly ICurrentContext _currentContext;
|
2021-03-30 23:21:46 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
public SendsControllerTests()
|
|
|
|
|
{
|
|
|
|
|
_userService = Substitute.For<IUserService>();
|
|
|
|
|
_sendRepository = Substitute.For<ISendRepository>();
|
|
|
|
|
_sendService = Substitute.For<ISendService>();
|
|
|
|
|
_sendFileStorageService = Substitute.For<ISendFileStorageService>();
|
|
|
|
|
_globalSettings = new GlobalSettings();
|
|
|
|
|
_logger = Substitute.For<ILogger<SendsController>>();
|
|
|
|
|
_currentContext = Substitute.For<ICurrentContext>();
|
2021-03-30 23:21:46 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
_sut = new SendsController(
|
|
|
|
|
_sendRepository,
|
|
|
|
|
_userService,
|
|
|
|
|
_sendService,
|
|
|
|
|
_sendFileStorageService,
|
|
|
|
|
_logger,
|
|
|
|
|
_globalSettings,
|
|
|
|
|
_currentContext
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-03-30 23:21:46 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_sut?.Dispose();
|
|
|
|
|
}
|
2021-03-30 23:21:46 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task SendsController_WhenSendHidesEmail_CreatorIdentifierShouldBeNull(
|
|
|
|
|
Guid id, Send send, User user)
|
|
|
|
|
{
|
|
|
|
|
var accessId = CoreHelpers.Base64UrlEncode(id.ToByteArray());
|
2021-03-30 23:21:46 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
send.Id = default;
|
|
|
|
|
send.Type = SendType.Text;
|
|
|
|
|
send.Data = JsonSerializer.Serialize(new Dictionary<string, string>());
|
|
|
|
|
send.HideEmail = true;
|
2021-03-30 23:21:46 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
_sendService.AccessAsync(id, null).Returns((send, false, false));
|
|
|
|
|
_userService.GetUserByIdAsync(Arg.Any<Guid>()).Returns(user);
|
2021-03-30 23:21:46 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
var request = new SendAccessRequestModel();
|
|
|
|
|
var actionResult = await _sut.Access(accessId, request);
|
|
|
|
|
var response = (actionResult as ObjectResult)?.Value as SendAccessResponseModel;
|
2021-03-30 23:21:46 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Assert.NotNull(response);
|
|
|
|
|
Assert.Null(response.CreatorIdentifier);
|
2021-03-30 23:21:46 +02:00
|
|
|
|
}
|
2022-12-13 20:30:01 +01:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Post_DeletionDateIsMoreThan31DaysFromNow_ThrowsBadRequest()
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var expected = "You cannot have a Send with a deletion date that far " +
|
|
|
|
|
"into the future. Adjust the Deletion Date to a value less than 31 days from now " +
|
|
|
|
|
"and try again.";
|
|
|
|
|
var request = new SendRequestModel() { DeletionDate = now.AddDays(32) };
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() => _sut.Post(request));
|
|
|
|
|
Assert.Equal(expected, exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PostFile_DeletionDateIsMoreThan31DaysFromNow_ThrowsBadRequest()
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var expected = "You cannot have a Send with a deletion date that far " +
|
|
|
|
|
"into the future. Adjust the Deletion Date to a value less than 31 days from now " +
|
|
|
|
|
"and try again.";
|
|
|
|
|
var request = new SendRequestModel() { Type = SendType.File, FileLength = 1024L, DeletionDate = now.AddDays(32) };
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() => _sut.PostFile(request));
|
|
|
|
|
Assert.Equal(expected, exception.Message);
|
|
|
|
|
}
|
2021-03-30 23:21:46 +02:00
|
|
|
|
}
|