1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 13:08:17 -05:00

[PM-3555] Remove ClearTracker() (#3213)

* Remove ClearTracker

* Remove from CipherRepositoryTests
This commit is contained in:
Justin Baur 2023-10-12 05:15:02 -04:00 committed by GitHub
parent b2af73f00f
commit 79648b311e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 9 additions and 75 deletions

View File

@ -15,8 +15,7 @@ public class AuthRequestRepositoryTests
[DatabaseTheory, DatabaseData] [DatabaseTheory, DatabaseData]
public async Task DeleteExpiredAsync_Works( public async Task DeleteExpiredAsync_Works(
IAuthRequestRepository authRequestRepository, IAuthRequestRepository authRequestRepository,
IUserRepository userRepository, IUserRepository userRepository)
ITestDatabaseHelper helper)
{ {
var user = await userRepository.CreateAsync(new User var user = await userRepository.CreateAsync(new User
{ {
@ -54,8 +53,6 @@ public class AuthRequestRepositoryTests
var notExpiredApprovedAdminApprovalRequest = await authRequestRepository.CreateAsync( var notExpiredApprovedAdminApprovalRequest = await authRequestRepository.CreateAsync(
CreateAuthRequest(user.Id, AuthRequestType.AdminApproval, DateTime.UtcNow.AddDays(7), true, DateTime.UtcNow.AddHours(11))); CreateAuthRequest(user.Id, AuthRequestType.AdminApproval, DateTime.UtcNow.AddDays(7), true, DateTime.UtcNow.AddHours(11)));
helper.ClearTracker();
var numberOfDeleted = await authRequestRepository.DeleteExpiredAsync(_userRequestExpiration, _adminRequestExpiration, _afterAdminApprovalExpiration); var numberOfDeleted = await authRequestRepository.DeleteExpiredAsync(_userRequestExpiration, _adminRequestExpiration, _afterAdminApprovalExpiration);
// Ensure all the AuthRequests that should have been deleted, have been deleted. // Ensure all the AuthRequests that should have been deleted, have been deleted.

View File

@ -10,8 +10,7 @@ public class EmergencyAccessRepositoriesTests
{ {
[DatabaseTheory, DatabaseData] [DatabaseTheory, DatabaseData]
public async Task DeleteAsync_UpdatesRevisionDate(IUserRepository userRepository, public async Task DeleteAsync_UpdatesRevisionDate(IUserRepository userRepository,
IEmergencyAccessRepository emergencyAccessRepository, IEmergencyAccessRepository emergencyAccessRepository)
ITestDatabaseHelper helper)
{ {
var grantorUser = await userRepository.CreateAsync(new User var grantorUser = await userRepository.CreateAsync(new User
{ {
@ -36,8 +35,6 @@ public class EmergencyAccessRepositoriesTests
Status = EmergencyAccessStatusType.Confirmed, Status = EmergencyAccessStatusType.Confirmed,
}); });
helper.ClearTracker();
await emergencyAccessRepository.DeleteAsync(emergencyAccess); await emergencyAccessRepository.DeleteAsync(emergencyAccess);
var updatedGrantee = await userRepository.GetByIdAsync(granteeUser.Id); var updatedGrantee = await userRepository.GetByIdAsync(granteeUser.Id);

View File

@ -3,7 +3,6 @@ using Bit.Core.Enums;
using Bit.Core.Settings; using Bit.Core.Settings;
using Bit.Infrastructure.Dapper; using Bit.Infrastructure.Dapper;
using Bit.Infrastructure.EntityFramework; using Bit.Infrastructure.EntityFramework;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -65,7 +64,7 @@ public class DatabaseDataAttribute : DataAttribute
}; };
dapperSqlServerCollection.AddSingleton(globalSettings); dapperSqlServerCollection.AddSingleton(globalSettings);
dapperSqlServerCollection.AddSingleton<IGlobalSettings>(globalSettings); dapperSqlServerCollection.AddSingleton<IGlobalSettings>(globalSettings);
dapperSqlServerCollection.AddSingleton<ITestDatabaseHelper>(_ => new DapperSqlServerTestDatabaseHelper(database)); dapperSqlServerCollection.AddSingleton(database);
dapperSqlServerCollection.AddDataProtection(); dapperSqlServerCollection.AddDataProtection();
yield return dapperSqlServerCollection.BuildServiceProvider(); yield return dapperSqlServerCollection.BuildServiceProvider();
} }
@ -75,7 +74,7 @@ public class DatabaseDataAttribute : DataAttribute
efCollection.AddLogging(configureLogging); efCollection.AddLogging(configureLogging);
efCollection.SetupEntityFramework(database.ConnectionString, database.Type); efCollection.SetupEntityFramework(database.ConnectionString, database.Type);
efCollection.AddPasswordManagerEFRepositories(SelfHosted); efCollection.AddPasswordManagerEFRepositories(SelfHosted);
efCollection.AddTransient<ITestDatabaseHelper>(sp => new EfTestDatabaseHelper(sp.GetRequiredService<DatabaseContext>(), database)); efCollection.AddSingleton(database);
efCollection.AddDataProtection(); efCollection.AddDataProtection();
yield return efCollection.BuildServiceProvider(); yield return efCollection.BuildServiceProvider();
} }

View File

@ -10,8 +10,7 @@ public class OrganizationUserRepositoryTests
[DatabaseTheory, DatabaseData] [DatabaseTheory, DatabaseData]
public async Task DeleteAsync_Works(IUserRepository userRepository, public async Task DeleteAsync_Works(IUserRepository userRepository,
IOrganizationRepository organizationRepository, IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository, IOrganizationUserRepository organizationUserRepository)
ITestDatabaseHelper helper)
{ {
var user = await userRepository.CreateAsync(new User var user = await userRepository.CreateAsync(new User
{ {
@ -35,8 +34,6 @@ public class OrganizationUserRepositoryTests
Status = OrganizationUserStatusType.Confirmed, Status = OrganizationUserStatusType.Confirmed,
}); });
helper.ClearTracker();
await organizationUserRepository.DeleteAsync(orgUser); await organizationUserRepository.DeleteAsync(orgUser);
var newUser = await userRepository.GetByIdAsync(user.Id); var newUser = await userRepository.GetByIdAsync(user.Id);
@ -46,8 +43,7 @@ public class OrganizationUserRepositoryTests
[DatabaseTheory, DatabaseData] [DatabaseTheory, DatabaseData]
public async Task DeleteManyAsync_Works(IUserRepository userRepository, public async Task DeleteManyAsync_Works(IUserRepository userRepository,
IOrganizationRepository organizationRepository, IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository, IOrganizationUserRepository organizationUserRepository)
ITestDatabaseHelper helper)
{ {
var user1 = await userRepository.CreateAsync(new User var user1 = await userRepository.CreateAsync(new User
{ {
@ -86,8 +82,6 @@ public class OrganizationUserRepositoryTests
Status = OrganizationUserStatusType.Confirmed, Status = OrganizationUserStatusType.Confirmed,
}); });
helper.ClearTracker();
await organizationUserRepository.DeleteManyAsync(new List<Guid> await organizationUserRepository.DeleteManyAsync(new List<Guid>
{ {
orgUser1.Id, orgUser1.Id,

View File

@ -1,42 +0,0 @@
using Bit.Infrastructure.EntityFramework.Repositories;
namespace Bit.Infrastructure.IntegrationTest;
public interface ITestDatabaseHelper
{
Database Info { get; }
void ClearTracker();
}
public class EfTestDatabaseHelper : ITestDatabaseHelper
{
private readonly DatabaseContext _databaseContext;
public EfTestDatabaseHelper(DatabaseContext databaseContext, Database database)
{
_databaseContext = databaseContext;
Info = database;
}
public Database Info { get; }
public void ClearTracker()
{
_databaseContext.ChangeTracker.Clear();
}
}
public class DapperSqlServerTestDatabaseHelper : ITestDatabaseHelper
{
public DapperSqlServerTestDatabaseHelper(Database database)
{
Info = database;
}
public Database Info { get; }
public void ClearTracker()
{
// There are no tracked entities in Dapper SQL Server
}
}

View File

@ -16,8 +16,7 @@ public class CipherRepositoryTests
[DatabaseTheory, DatabaseData] [DatabaseTheory, DatabaseData]
public async Task DeleteAsync_UpdatesUserRevisionDate( public async Task DeleteAsync_UpdatesUserRevisionDate(
IUserRepository userRepository, IUserRepository userRepository,
ICipherRepository cipherRepository, ICipherRepository cipherRepository)
ITestDatabaseHelper helper)
{ {
var user = await userRepository.CreateAsync(new User var user = await userRepository.CreateAsync(new User
{ {
@ -34,8 +33,6 @@ public class CipherRepositoryTests
Data = "", // TODO: EF does not enforce this as NOT NULL Data = "", // TODO: EF does not enforce this as NOT NULL
}); });
helper.ClearTracker();
await cipherRepository.DeleteAsync(cipher); await cipherRepository.DeleteAsync(cipher);
var deletedCipher = await cipherRepository.GetByIdAsync(cipher.Id); var deletedCipher = await cipherRepository.GetByIdAsync(cipher.Id);
@ -52,8 +49,7 @@ public class CipherRepositoryTests
IOrganizationUserRepository organizationUserRepository, IOrganizationUserRepository organizationUserRepository,
ICollectionRepository collectionRepository, ICollectionRepository collectionRepository,
ICipherRepository cipherRepository, ICipherRepository cipherRepository,
ICollectionCipherRepository collectionCipherRepository, ICollectionCipherRepository collectionCipherRepository)
ITestDatabaseHelper helper)
{ {
var user = await userRepository.CreateAsync(new User var user = await userRepository.CreateAsync(new User
{ {
@ -63,8 +59,6 @@ public class CipherRepositoryTests
SecurityStamp = "stamp", SecurityStamp = "stamp",
}); });
helper.ClearTracker();
user = await userRepository.GetByIdAsync(user.Id); user = await userRepository.GetByIdAsync(user.Id);
var organization = await organizationRepository.CreateAsync(new Organization var organization = await organizationRepository.CreateAsync(new Organization
@ -100,8 +94,6 @@ public class CipherRepositoryTests
}, },
}); });
helper.ClearTracker();
await Task.Delay(100); await Task.Delay(100);
await cipherRepository.CreateAsync(new CipherDetails await cipherRepository.CreateAsync(new CipherDetails
@ -128,8 +120,7 @@ public class CipherRepositoryTests
ICipherRepository cipherRepository, ICipherRepository cipherRepository,
IOrganizationRepository organizationRepository, IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository, IOrganizationUserRepository organizationUserRepository,
IFolderRepository folderRepository, IFolderRepository folderRepository)
ITestDatabaseHelper helper)
{ {
// This tests what happens when a cipher is moved into an organizations // This tests what happens when a cipher is moved into an organizations
var user = await userRepository.CreateAsync(new User var user = await userRepository.CreateAsync(new User
@ -171,8 +162,6 @@ public class CipherRepositoryTests
UserId = user.Id, UserId = user.Id,
}); });
helper.ClearTracker();
// Move cipher to organization vault // Move cipher to organization vault
await cipherRepository.ReplaceAsync(new CipherDetails await cipherRepository.ReplaceAsync(new CipherDetails
{ {