From 79648b311ef6d503286905ef27d0f3aac5b78446 Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Thu, 12 Oct 2023 05:15:02 -0400 Subject: [PATCH] [PM-3555] Remove `ClearTracker()` (#3213) * Remove ClearTracker * Remove from CipherRepositoryTests --- .../AuthRequestRepositoryTests.cs | 5 +-- .../EmergencyAccessRepositoryTests.cs | 5 +-- .../DatabaseDataAttribute.cs | 5 +-- .../OrganizationUserRepositoryTests.cs | 10 +---- .../TestDatabaseHelpers.cs | 42 ------------------- .../Repositories/CipherRepositoryTests.cs | 17 ++------ 6 files changed, 9 insertions(+), 75 deletions(-) delete mode 100644 test/Infrastructure.IntegrationTest/TestDatabaseHelpers.cs diff --git a/test/Infrastructure.IntegrationTest/Auth/Repositories/AuthRequestRepositoryTests.cs b/test/Infrastructure.IntegrationTest/Auth/Repositories/AuthRequestRepositoryTests.cs index b23b8ce4b..6c5bf135e 100644 --- a/test/Infrastructure.IntegrationTest/Auth/Repositories/AuthRequestRepositoryTests.cs +++ b/test/Infrastructure.IntegrationTest/Auth/Repositories/AuthRequestRepositoryTests.cs @@ -15,8 +15,7 @@ public class AuthRequestRepositoryTests [DatabaseTheory, DatabaseData] public async Task DeleteExpiredAsync_Works( IAuthRequestRepository authRequestRepository, - IUserRepository userRepository, - ITestDatabaseHelper helper) + IUserRepository userRepository) { var user = await userRepository.CreateAsync(new User { @@ -54,8 +53,6 @@ public class AuthRequestRepositoryTests var notExpiredApprovedAdminApprovalRequest = await authRequestRepository.CreateAsync( CreateAuthRequest(user.Id, AuthRequestType.AdminApproval, DateTime.UtcNow.AddDays(7), true, DateTime.UtcNow.AddHours(11))); - helper.ClearTracker(); - var numberOfDeleted = await authRequestRepository.DeleteExpiredAsync(_userRequestExpiration, _adminRequestExpiration, _afterAdminApprovalExpiration); // Ensure all the AuthRequests that should have been deleted, have been deleted. diff --git a/test/Infrastructure.IntegrationTest/Auth/Repositories/EmergencyAccessRepositoryTests.cs b/test/Infrastructure.IntegrationTest/Auth/Repositories/EmergencyAccessRepositoryTests.cs index 6454c01ea..def3dcb1e 100644 --- a/test/Infrastructure.IntegrationTest/Auth/Repositories/EmergencyAccessRepositoryTests.cs +++ b/test/Infrastructure.IntegrationTest/Auth/Repositories/EmergencyAccessRepositoryTests.cs @@ -10,8 +10,7 @@ public class EmergencyAccessRepositoriesTests { [DatabaseTheory, DatabaseData] public async Task DeleteAsync_UpdatesRevisionDate(IUserRepository userRepository, - IEmergencyAccessRepository emergencyAccessRepository, - ITestDatabaseHelper helper) + IEmergencyAccessRepository emergencyAccessRepository) { var grantorUser = await userRepository.CreateAsync(new User { @@ -36,8 +35,6 @@ public class EmergencyAccessRepositoriesTests Status = EmergencyAccessStatusType.Confirmed, }); - helper.ClearTracker(); - await emergencyAccessRepository.DeleteAsync(emergencyAccess); var updatedGrantee = await userRepository.GetByIdAsync(granteeUser.Id); diff --git a/test/Infrastructure.IntegrationTest/DatabaseDataAttribute.cs b/test/Infrastructure.IntegrationTest/DatabaseDataAttribute.cs index 3bf221102..6433fcb20 100644 --- a/test/Infrastructure.IntegrationTest/DatabaseDataAttribute.cs +++ b/test/Infrastructure.IntegrationTest/DatabaseDataAttribute.cs @@ -3,7 +3,6 @@ using Bit.Core.Enums; using Bit.Core.Settings; using Bit.Infrastructure.Dapper; using Bit.Infrastructure.EntityFramework; -using Bit.Infrastructure.EntityFramework.Repositories; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -65,7 +64,7 @@ public class DatabaseDataAttribute : DataAttribute }; dapperSqlServerCollection.AddSingleton(globalSettings); dapperSqlServerCollection.AddSingleton(globalSettings); - dapperSqlServerCollection.AddSingleton(_ => new DapperSqlServerTestDatabaseHelper(database)); + dapperSqlServerCollection.AddSingleton(database); dapperSqlServerCollection.AddDataProtection(); yield return dapperSqlServerCollection.BuildServiceProvider(); } @@ -75,7 +74,7 @@ public class DatabaseDataAttribute : DataAttribute efCollection.AddLogging(configureLogging); efCollection.SetupEntityFramework(database.ConnectionString, database.Type); efCollection.AddPasswordManagerEFRepositories(SelfHosted); - efCollection.AddTransient(sp => new EfTestDatabaseHelper(sp.GetRequiredService(), database)); + efCollection.AddSingleton(database); efCollection.AddDataProtection(); yield return efCollection.BuildServiceProvider(); } diff --git a/test/Infrastructure.IntegrationTest/Repositories/OrganizationUserRepositoryTests.cs b/test/Infrastructure.IntegrationTest/Repositories/OrganizationUserRepositoryTests.cs index 1b780f114..e07b3ba72 100644 --- a/test/Infrastructure.IntegrationTest/Repositories/OrganizationUserRepositoryTests.cs +++ b/test/Infrastructure.IntegrationTest/Repositories/OrganizationUserRepositoryTests.cs @@ -10,8 +10,7 @@ public class OrganizationUserRepositoryTests [DatabaseTheory, DatabaseData] public async Task DeleteAsync_Works(IUserRepository userRepository, IOrganizationRepository organizationRepository, - IOrganizationUserRepository organizationUserRepository, - ITestDatabaseHelper helper) + IOrganizationUserRepository organizationUserRepository) { var user = await userRepository.CreateAsync(new User { @@ -35,8 +34,6 @@ public class OrganizationUserRepositoryTests Status = OrganizationUserStatusType.Confirmed, }); - helper.ClearTracker(); - await organizationUserRepository.DeleteAsync(orgUser); var newUser = await userRepository.GetByIdAsync(user.Id); @@ -46,8 +43,7 @@ public class OrganizationUserRepositoryTests [DatabaseTheory, DatabaseData] public async Task DeleteManyAsync_Works(IUserRepository userRepository, IOrganizationRepository organizationRepository, - IOrganizationUserRepository organizationUserRepository, - ITestDatabaseHelper helper) + IOrganizationUserRepository organizationUserRepository) { var user1 = await userRepository.CreateAsync(new User { @@ -86,8 +82,6 @@ public class OrganizationUserRepositoryTests Status = OrganizationUserStatusType.Confirmed, }); - helper.ClearTracker(); - await organizationUserRepository.DeleteManyAsync(new List { orgUser1.Id, diff --git a/test/Infrastructure.IntegrationTest/TestDatabaseHelpers.cs b/test/Infrastructure.IntegrationTest/TestDatabaseHelpers.cs deleted file mode 100644 index 5cf8c73a8..000000000 --- a/test/Infrastructure.IntegrationTest/TestDatabaseHelpers.cs +++ /dev/null @@ -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 - } -} diff --git a/test/Infrastructure.IntegrationTest/Vault/Repositories/CipherRepositoryTests.cs b/test/Infrastructure.IntegrationTest/Vault/Repositories/CipherRepositoryTests.cs index d9cc228c2..1712313d3 100644 --- a/test/Infrastructure.IntegrationTest/Vault/Repositories/CipherRepositoryTests.cs +++ b/test/Infrastructure.IntegrationTest/Vault/Repositories/CipherRepositoryTests.cs @@ -16,8 +16,7 @@ public class CipherRepositoryTests [DatabaseTheory, DatabaseData] public async Task DeleteAsync_UpdatesUserRevisionDate( IUserRepository userRepository, - ICipherRepository cipherRepository, - ITestDatabaseHelper helper) + ICipherRepository cipherRepository) { var user = await userRepository.CreateAsync(new User { @@ -34,8 +33,6 @@ public class CipherRepositoryTests Data = "", // TODO: EF does not enforce this as NOT NULL }); - helper.ClearTracker(); - await cipherRepository.DeleteAsync(cipher); var deletedCipher = await cipherRepository.GetByIdAsync(cipher.Id); @@ -52,8 +49,7 @@ public class CipherRepositoryTests IOrganizationUserRepository organizationUserRepository, ICollectionRepository collectionRepository, ICipherRepository cipherRepository, - ICollectionCipherRepository collectionCipherRepository, - ITestDatabaseHelper helper) + ICollectionCipherRepository collectionCipherRepository) { var user = await userRepository.CreateAsync(new User { @@ -63,8 +59,6 @@ public class CipherRepositoryTests SecurityStamp = "stamp", }); - helper.ClearTracker(); - user = await userRepository.GetByIdAsync(user.Id); var organization = await organizationRepository.CreateAsync(new Organization @@ -100,8 +94,6 @@ public class CipherRepositoryTests }, }); - helper.ClearTracker(); - await Task.Delay(100); await cipherRepository.CreateAsync(new CipherDetails @@ -128,8 +120,7 @@ public class CipherRepositoryTests ICipherRepository cipherRepository, IOrganizationRepository organizationRepository, IOrganizationUserRepository organizationUserRepository, - IFolderRepository folderRepository, - ITestDatabaseHelper helper) + IFolderRepository folderRepository) { // This tests what happens when a cipher is moved into an organizations var user = await userRepository.CreateAsync(new User @@ -171,8 +162,6 @@ public class CipherRepositoryTests UserId = user.Id, }); - helper.ClearTracker(); - // Move cipher to organization vault await cipherRepository.ReplaceAsync(new CipherDetails {