mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
[PM-3797 Part 4] Add Sends to new Key Rotation (#3442)
* add send validation * add send repo methods * add send rotation to delegate list * add success test
This commit is contained in:
@ -3,6 +3,7 @@ using Bit.Api.Auth.Controllers;
|
||||
using Bit.Api.Auth.Models.Request;
|
||||
using Bit.Api.Auth.Models.Request.Accounts;
|
||||
using Bit.Api.Auth.Validators;
|
||||
using Bit.Api.Tools.Models.Request;
|
||||
using Bit.Api.Vault.Models.Request;
|
||||
using Bit.Core;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
@ -20,6 +21,7 @@ using Bit.Core.Models.Data;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Tools.Entities;
|
||||
using Bit.Core.Tools.Repositories;
|
||||
using Bit.Core.Tools.Services;
|
||||
using Bit.Core.Vault.Entities;
|
||||
@ -53,9 +55,9 @@ public class AccountsControllerTests : IDisposable
|
||||
private readonly IFeatureService _featureService;
|
||||
private readonly ICurrentContext _currentContext;
|
||||
|
||||
|
||||
private readonly IRotationValidator<IEnumerable<CipherWithIdRequestModel>, IEnumerable<Cipher>> _cipherValidator;
|
||||
private readonly IRotationValidator<IEnumerable<FolderWithIdRequestModel>, IEnumerable<Folder>> _folderValidator;
|
||||
private readonly IRotationValidator<IEnumerable<SendWithIdRequestModel>, IReadOnlyList<Send>> _sendValidator;
|
||||
private readonly IRotationValidator<IEnumerable<EmergencyAccessWithIdRequestModel>, IEnumerable<EmergencyAccess>>
|
||||
_emergencyAccessValidator;
|
||||
|
||||
@ -83,6 +85,7 @@ public class AccountsControllerTests : IDisposable
|
||||
Substitute.For<IRotationValidator<IEnumerable<CipherWithIdRequestModel>, IEnumerable<Cipher>>>();
|
||||
_folderValidator =
|
||||
Substitute.For<IRotationValidator<IEnumerable<FolderWithIdRequestModel>, IEnumerable<Folder>>>();
|
||||
_sendValidator = Substitute.For<IRotationValidator<IEnumerable<SendWithIdRequestModel>, IReadOnlyList<Send>>>();
|
||||
_emergencyAccessValidator = Substitute.For<IRotationValidator<IEnumerable<EmergencyAccessWithIdRequestModel>,
|
||||
IEnumerable<EmergencyAccess>>>();
|
||||
|
||||
@ -106,6 +109,7 @@ public class AccountsControllerTests : IDisposable
|
||||
_currentContext,
|
||||
_cipherValidator,
|
||||
_folderValidator,
|
||||
_sendValidator,
|
||||
_emergencyAccessValidator
|
||||
);
|
||||
}
|
||||
|
184
test/Api.Test/Tools/Validators/SendRotationValidatorTests.cs
Normal file
184
test/Api.Test/Tools/Validators/SendRotationValidatorTests.cs
Normal file
@ -0,0 +1,184 @@
|
||||
using System.Text.Json;
|
||||
using Bit.Api.Tools.Models;
|
||||
using Bit.Api.Tools.Models.Request;
|
||||
using Bit.Api.Tools.Validators;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Tools.Entities;
|
||||
using Bit.Core.Tools.Enums;
|
||||
using Bit.Core.Tools.Models.Data;
|
||||
using Bit.Core.Tools.Repositories;
|
||||
using Bit.Core.Tools.Services;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Api.Test.Tools.Validators;
|
||||
|
||||
[SutProviderCustomize]
|
||||
public class SendRotationValidatorTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task ValidateAsync_Success()
|
||||
{
|
||||
// Arrange
|
||||
var sendService = Substitute.For<ISendService>();
|
||||
var sendRepository = Substitute.For<ISendRepository>();
|
||||
|
||||
var sut = new SendRotationValidator(
|
||||
sendService,
|
||||
sendRepository
|
||||
);
|
||||
|
||||
var user = new User { Id = new Guid() };
|
||||
var sends = CreateInputSendRequests();
|
||||
|
||||
sendRepository.GetManyByUserIdAsync(user.Id).Returns(MockUserSends(user));
|
||||
|
||||
// Act
|
||||
var result = await sut.ValidateAsync(user, sends);
|
||||
|
||||
// Assert
|
||||
var sendIds = new Guid[]
|
||||
{
|
||||
new("72e9ac6d-05f4-4227-ae0d-8a5207623a1a"), new("6b55836c-9280-4589-8762-01b0d8172c97"),
|
||||
new("9a65bbfb-8138-4aa5-a572-e5c0a41b540e"),
|
||||
};
|
||||
Assert.All(result, c => Assert.Contains(c.Id, sendIds));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_SendNotReturnedFromRepository_NotIncludedInOutput()
|
||||
{
|
||||
// Arrange
|
||||
var sendService = Substitute.For<ISendService>();
|
||||
var sendRepository = Substitute.For<ISendRepository>();
|
||||
|
||||
var sut = new SendRotationValidator(
|
||||
sendService,
|
||||
sendRepository
|
||||
);
|
||||
|
||||
var user = new User { Id = new Guid() };
|
||||
var sends = CreateInputSendRequests();
|
||||
|
||||
var userSends = MockUserSends(user);
|
||||
userSends.RemoveAll(c => c.Id == new Guid("72e9ac6d-05f4-4227-ae0d-8a5207623a1a"));
|
||||
sendRepository.GetManyByUserIdAsync(user.Id).Returns(userSends);
|
||||
|
||||
var result = await sut.ValidateAsync(user, sends);
|
||||
|
||||
Assert.DoesNotContain(result, c => c.Id == new Guid("72e9ac6d-05f4-4227-ae0d-8a5207623a1a"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_InputMissingUserSend_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var sendService = Substitute.For<ISendService>();
|
||||
var sendRepository = Substitute.For<ISendRepository>();
|
||||
|
||||
var sut = new SendRotationValidator(
|
||||
sendService,
|
||||
sendRepository
|
||||
);
|
||||
|
||||
var user = new User { Id = new Guid() };
|
||||
var sends = CreateInputSendRequests();
|
||||
|
||||
var userSends = MockUserSends(user);
|
||||
userSends.Add(new Send { Id = new Guid(), Data = "{}" });
|
||||
sendRepository.GetManyByUserIdAsync(user.Id).Returns(userSends);
|
||||
|
||||
// Act, Assert
|
||||
await Assert.ThrowsAsync<BadRequestException>(async () =>
|
||||
await sut.ValidateAsync(user, sends));
|
||||
}
|
||||
|
||||
private IEnumerable<SendWithIdRequestModel> CreateInputSendRequests()
|
||||
{
|
||||
return new[]
|
||||
{
|
||||
new SendWithIdRequestModel
|
||||
{
|
||||
DeletionDate = new DateTime(2080, 12, 31),
|
||||
Disabled = false,
|
||||
Id = new Guid("72e9ac6d-05f4-4227-ae0d-8a5207623a1a"),
|
||||
Key = "Send1Key",
|
||||
Name = "Send 1",
|
||||
Type = SendType.Text,
|
||||
Text = new SendTextModel(new SendTextData("Text name", "Notes", "Encrypted text for Send 1", false))
|
||||
},
|
||||
new SendWithIdRequestModel
|
||||
{
|
||||
DeletionDate = new DateTime(2080, 12, 31),
|
||||
Disabled = true,
|
||||
Id = new Guid("6b55836c-9280-4589-8762-01b0d8172c97"),
|
||||
Key = "Send2Key",
|
||||
Name = "Send 2",
|
||||
Type = SendType.Text,
|
||||
Text = new SendTextModel(new SendTextData("Text name", "Notes", "Encrypted text for Send 2",
|
||||
false)),
|
||||
},
|
||||
new SendWithIdRequestModel
|
||||
{
|
||||
DeletionDate = new DateTime(2080, 12, 31),
|
||||
Disabled = false,
|
||||
Id = new Guid("9a65bbfb-8138-4aa5-a572-e5c0a41b540e"),
|
||||
Key = "Send3Key",
|
||||
Name = "Send 3",
|
||||
Type = SendType.File,
|
||||
File = new SendFileModel(new SendFileData("File name", "Notes", "File name here")),
|
||||
HideEmail = true
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private List<Send> MockUserSends(User user)
|
||||
{
|
||||
return new List<Send>(new[]
|
||||
{
|
||||
new Send
|
||||
{
|
||||
DeletionDate = new DateTime(2080, 12, 31),
|
||||
Disabled = false,
|
||||
Id = new Guid("72e9ac6d-05f4-4227-ae0d-8a5207623a1a"),
|
||||
UserId = user.Id,
|
||||
Key = "Send1Key",
|
||||
Type = SendType.Text,
|
||||
Data = JsonSerializer.Serialize(
|
||||
new SendTextModel(new SendTextData("Text name", "Notes", "Encrypted text for Send 1", false)),
|
||||
JsonHelpers.IgnoreWritingNull),
|
||||
},
|
||||
new Send
|
||||
{
|
||||
DeletionDate = new DateTime(2080, 12, 31),
|
||||
Disabled = true,
|
||||
Id = new Guid("6b55836c-9280-4589-8762-01b0d8172c97"),
|
||||
UserId = user.Id,
|
||||
Key = "Send2Key",
|
||||
Type = SendType.Text,
|
||||
Data = JsonSerializer.Serialize(
|
||||
new SendTextModel(new SendTextData("Text name", "Notes", "Encrypted text for Send 2",
|
||||
false)),
|
||||
JsonHelpers.IgnoreWritingNull),
|
||||
},
|
||||
new Send
|
||||
{
|
||||
DeletionDate = new DateTime(2080, 12, 31),
|
||||
Disabled = false,
|
||||
Id = new Guid("9a65bbfb-8138-4aa5-a572-e5c0a41b540e"),
|
||||
UserId = user.Id,
|
||||
Key = "Send3Key",
|
||||
Type = SendType.File,
|
||||
Data = JsonSerializer.Serialize(
|
||||
new SendFileModel(new SendFileData("File name", "Notes", "File name here")),
|
||||
JsonHelpers.IgnoreWritingNull),
|
||||
HideEmail = true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user