1
0
mirror of https://github.com/bitwarden/server synced 2026-01-09 03:53:42 +00:00

merge branch 'master' into 'encrypted-string-perf'

This commit is contained in:
Justin Baur
2022-10-07 09:53:33 -04:00
parent e0b1f9544b
commit a20e127c9c
149 changed files with 9934 additions and 394 deletions

View File

@@ -30,6 +30,7 @@ public class AccountsControllerTests : IDisposable
private readonly ISendRepository _sendRepository;
private readonly ISendService _sendService;
private readonly IProviderUserRepository _providerUserRepository;
private readonly ICaptchaValidationService _captchaValidationService;
public AccountsControllerTests()
{
@@ -44,6 +45,7 @@ public class AccountsControllerTests : IDisposable
_globalSettings = new GlobalSettings();
_sendRepository = Substitute.For<ISendRepository>();
_sendService = Substitute.For<ISendService>();
_captchaValidationService = Substitute.For<ICaptchaValidationService>();
_sut = new AccountsController(
_globalSettings,
_cipherRepository,
@@ -55,7 +57,8 @@ public class AccountsControllerTests : IDisposable
_userRepository,
_userService,
_sendRepository,
_sendService
_sendService,
_captchaValidationService
);
}

View File

@@ -897,7 +897,7 @@ public class OrganizationServiceTests
[BitAutoData(0, 100, 100, true, "")]
[BitAutoData(0, null, 100, true, "")]
[BitAutoData(1, 100, null, true, "")]
[BitAutoData(1, 100, 100, false, "Cannot invite new users. Seat limit has been reached")]
[BitAutoData(1, 100, 100, false, "Seat limit has been reached")]
public void CanScale(int seatsToAdd, int? currentSeats, int? maxAutoscaleSeats,
bool expectedResult, string expectedFailureMessage, Organization organization,
SutProvider<OrganizationService> sutProvider)

View File

@@ -70,31 +70,6 @@ public class CoreHelpersTests
Assert.Equal(expectedComb, comb);
}
[Theory]
[InlineData(2, 5, new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 })]
[InlineData(2, 3, new[] { 1, 2, 3, 4, 5 })]
[InlineData(2, 1, new[] { 1, 2 })]
[InlineData(1, 1, new[] { 1 })]
[InlineData(2, 2, new[] { 1, 2, 3 })]
public void Batch_Success(int batchSize, int totalBatches, int[] collection)
{
// Arrange
var remainder = collection.Length % batchSize;
// Act
var batches = collection.Batch(batchSize);
// Assert
Assert.Equal(totalBatches, batches.Count());
foreach (var batch in batches.Take(totalBatches - 1))
{
Assert.Equal(batchSize, batch.Count());
}
Assert.Equal(batches.Last().Count(), remainder == 0 ? batchSize : remainder);
}
/*
[Fact]
public void ToGuidIdArrayTVP_Success()

View File

@@ -3,6 +3,7 @@ using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Models.Api.Request.Accounts;
using Bit.Core.Repositories;
using Bit.Identity.IdentityServer;
using Bit.IntegrationTestCommon.Factories;
using Bit.Test.Common.AutoFixture.Attributes;
using Bit.Test.Common.Helpers;
@@ -279,7 +280,7 @@ public class IdentityServerTests : IClassFixture<IdentityApplicationFactory>
/// <summary>
/// This test currently does not test any code that is not covered by other tests but
/// it shows that we probably have some dead code in <see cref="Core.IdentityServer.ClientStore"/>
/// it shows that we probably have some dead code in <see cref="ClientStore"/>
/// for installation, organization, and user they split on a <c>'.'</c> but have already checked that at least one
/// <c>'.'</c> exists in the <c>client_id</c> by checking it with <see cref="string.StartsWith(string)"/>
/// I believe that idParts.Length > 1 will ALWAYS return true

View File

@@ -20,16 +20,19 @@ public class AccountsControllerTests : IDisposable
private readonly ILogger<AccountsController> _logger;
private readonly IUserRepository _userRepository;
private readonly IUserService _userService;
private readonly ICaptchaValidationService _captchaValidationService;
public AccountsControllerTests()
{
_logger = Substitute.For<ILogger<AccountsController>>();
_userRepository = Substitute.For<IUserRepository>();
_userService = Substitute.For<IUserService>();
_captchaValidationService = Substitute.For<ICaptchaValidationService>();
_sut = new AccountsController(
_logger,
_userRepository,
_userService
_userService,
_captchaValidationService
);
}

View File

@@ -0,0 +1,61 @@
using AutoFixture;
using AutoFixture.Kernel;
using Bit.Core.Entities;
using Bit.Core.Test.AutoFixture.UserFixtures;
using Bit.Infrastructure.EFIntegration.Test.AutoFixture.Relays;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
namespace Bit.Infrastructure.EFIntegration.Test.AutoFixture;
internal class AuthRequestBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var type = request as Type;
if (type == null || type != typeof(AuthRequest))
{
return new NoSpecimen();
}
var fixture = new Fixture();
fixture.Customizations.Insert(0, new MaxLengthStringRelay());
var obj = fixture.WithAutoNSubstitutions().Create<AuthRequest>();
return obj;
}
}
internal class EfAuthRequest : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new IgnoreVirtualMembersCustomization());
fixture.Customizations.Add(new GlobalSettingsBuilder());
fixture.Customizations.Add(new AuthRequestBuilder());
fixture.Customizations.Add(new DeviceBuilder());
fixture.Customizations.Add(new UserBuilder());
fixture.Customizations.Add(new EfRepositoryListBuilder<AuthRequestRepository>());
fixture.Customizations.Add(new EfRepositoryListBuilder<DeviceRepository>());
fixture.Customizations.Add(new EfRepositoryListBuilder<UserRepository>());
}
}
internal class EfAuthRequestAutoDataAttribute : CustomAutoDataAttribute
{
public EfAuthRequestAutoDataAttribute() : base(new SutProviderCustomization(), new EfAuthRequest())
{ }
}
internal class InlineEfAuthRequestAutoDataAttribute : InlineCustomAutoDataAttribute
{
public InlineEfAuthRequestAutoDataAttribute(params object[] values) : base(new[] { typeof(SutProviderCustomization),
typeof(EfAuthRequest) }, values)
{ }
}

View File

@@ -63,6 +63,7 @@ public class EfRepositoryListBuilder<T> : ISpecimenBuilder where T : BaseEntityF
fixture.Customize<IMapper>(x => x.FromFactory(() =>
new MapperConfiguration(cfg =>
{
cfg.AddProfile<AuthRequestMapperProfile>();
cfg.AddProfile<CipherMapperProfile>();
cfg.AddProfile<CollectionCipherMapperProfile>();
cfg.AddProfile<CollectionMapperProfile>();

View File

@@ -0,0 +1,50 @@
using Bit.Core.Entities;
using Bit.Core.Test.AutoFixture.Attributes;
using Bit.Infrastructure.EFIntegration.Test.AutoFixture;
using Bit.Infrastructure.EFIntegration.Test.Repositories.EqualityComparers;
using Xunit;
using EfRepo = Bit.Infrastructure.EntityFramework.Repositories;
using SqlRepo = Bit.Infrastructure.Dapper.Repositories;
namespace Bit.Infrastructure.EFIntegration.Test.Repositories;
public class AuthRequestRepositoryTests
{
[CiSkippedTheory, EfAuthRequestAutoData]
public async void CreateAsync_Works_DataMatches(
AuthRequest authRequest,
AuthRequestCompare equalityComparer,
List<EfRepo.AuthRequestRepository> suts,
SqlRepo.AuthRequestRepository sqlAuthRequestRepo,
User user,
List<EfRepo.UserRepository> efUserRepos,
SqlRepo.UserRepository sqlUserRepo
)
{
authRequest.ResponseDeviceId = null;
var savedAuthRequests = new List<AuthRequest>();
foreach (var sut in suts)
{
var i = suts.IndexOf(sut);
var efUser = await efUserRepos[i].CreateAsync(user);
sut.ClearChangeTracking();
authRequest.UserId = efUser.Id;
var postEfAuthRequest = await sut.CreateAsync(authRequest);
sut.ClearChangeTracking();
var savedAuthRequest = await sut.GetByIdAsync(postEfAuthRequest.Id);
savedAuthRequests.Add(savedAuthRequest);
}
var sqlUser = await sqlUserRepo.CreateAsync(user);
authRequest.UserId = sqlUser.Id;
var sqlAuthRequest = await sqlAuthRequestRepo.CreateAsync(authRequest);
var savedSqlAuthRequest = await sqlAuthRequestRepo.GetByIdAsync(sqlAuthRequest.Id);
savedAuthRequests.Add(savedSqlAuthRequest);
var distinctItems = savedAuthRequests.Distinct(equalityComparer);
Assert.True(!distinctItems.Skip(1).Any());
}
}

View File

@@ -0,0 +1,23 @@
using System.Diagnostics.CodeAnalysis;
using Bit.Core.Entities;
namespace Bit.Infrastructure.EFIntegration.Test.Repositories.EqualityComparers;
public class AuthRequestCompare : IEqualityComparer<AuthRequest>
{
public bool Equals(AuthRequest x, AuthRequest y)
{
return x.AccessCode == y.AccessCode &&
x.MasterPasswordHash == y.MasterPasswordHash &&
x.PublicKey == y.PublicKey &&
x.RequestDeviceIdentifier == y.RequestDeviceIdentifier &&
x.RequestDeviceType == y.RequestDeviceType &&
x.RequestIpAddress == y.RequestIpAddress &&
x.RequestFingerprint == y.RequestFingerprint;
}
public int GetHashCode([DisallowNull] AuthRequest obj)
{
return base.GetHashCode();
}
}