From 20aad3bc8bd80f3a99825a7d991a39d3b445b7fb Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 21 Nov 2024 16:25:26 -0500 Subject: [PATCH] fix and add tests --- .../Services/Implementations/UserService.cs | 2 - test/Core.Test/Services/UserServiceTests.cs | 64 ++++++++++++ .../Auth/Repositories/UserRepoistoryTests.cs | 61 ------------ .../Auth/Repositories/UserRepositoryTests.cs | 97 +++++++++++++++++++ 4 files changed, 161 insertions(+), 63 deletions(-) delete mode 100644 test/Infrastructure.IntegrationTest/Auth/Repositories/UserRepoistoryTests.cs create mode 100644 test/Infrastructure.IntegrationTest/Auth/Repositories/UserRepositoryTests.cs diff --git a/src/Core/Services/Implementations/UserService.cs b/src/Core/Services/Implementations/UserService.cs index f0867856e1..3e3476906b 100644 --- a/src/Core/Services/Implementations/UserService.cs +++ b/src/Core/Services/Implementations/UserService.cs @@ -32,8 +32,6 @@ using Microsoft.Extensions.Options; using File = System.IO.File; using JsonSerializer = System.Text.Json.JsonSerializer; -#nullable enable - namespace Bit.Core.Services; public class UserService : UserManager, IUserService, IDisposable diff --git a/test/Core.Test/Services/UserServiceTests.cs b/test/Core.Test/Services/UserServiceTests.cs index aa2c0a5cc9..0c6791eb90 100644 --- a/test/Core.Test/Services/UserServiceTests.cs +++ b/test/Core.Test/Services/UserServiceTests.cs @@ -349,6 +349,70 @@ public class UserServiceTests Assert.False(result); } + [Theory, BitAutoData] + public async Task DeleteUserAsync_WithAccountDeprovisioningEnabled_Succeeds( + SutProvider sutProvider, User user + ) + { + // Arrange + sutProvider.GetDependency().IsEnabled(FeatureFlagKeys.AccountDeprovisioning).Returns(true); + + // Act + var result = await sutProvider.Sut.DeleteAsync(user); + + // Assert + Assert.True(result == IdentityResult.Success); + } + + [Theory, BitAutoData] + public async Task DeleteUserAsync_WithAccountDeprovisioningDisabled_Succeeds( + SutProvider sutProvider, User user + ) + { + // Arrange + sutProvider.GetDependency().IsEnabled(FeatureFlagKeys.AccountDeprovisioning).Returns(false); + + // Act + var result = await sutProvider.Sut.DeleteAsync(user); + + // Assert + Assert.True(result == IdentityResult.Success); + } + + [Theory, BitAutoData] + public async Task DeleteManyAsync_WithAccountDeprovisioningEnabled_Succeeds( + SutProvider sutProvider, User user1, User user2, User user3 + ) + { + // Arrange + var userList = new List { user1, user2 }; + sutProvider.GetDependency().IsEnabled(FeatureFlagKeys.AccountDeprovisioning).Returns(true); + + // Act + var results = await sutProvider.Sut.DeleteManyAsync(userList); + + // Assert + Assert.True(results.Where(result => result.UserId == user1.Id || result.UserId == user2.Id).Count() == 2); + Assert.True(results.Where(result => result.UserId == user3.Id).Count() == 0); + } + + [Theory, BitAutoData] + public async Task DeleteManyAsync_WithAccountDeprovisioningDisabled_Succeeds( + SutProvider sutProvider, User user1, User user2, User user3 + ) + { + // Arrange + var userList = new List { user1, user2 }; + sutProvider.GetDependency().IsEnabled(FeatureFlagKeys.AccountDeprovisioning).Returns(false); + + // Act + var results = await sutProvider.Sut.DeleteManyAsync(userList); + + // Assert + Assert.True(results.Where(result => result.UserId == user1.Id || result.UserId == user2.Id).Count() == 2); + Assert.True(results.Where(result => result.UserId == user3.Id).Count() == 0); + } + private static void SetupUserAndDevice(User user, bool shouldHavePassword) { diff --git a/test/Infrastructure.IntegrationTest/Auth/Repositories/UserRepoistoryTests.cs b/test/Infrastructure.IntegrationTest/Auth/Repositories/UserRepoistoryTests.cs deleted file mode 100644 index c1e946e6fa..0000000000 --- a/test/Infrastructure.IntegrationTest/Auth/Repositories/UserRepoistoryTests.cs +++ /dev/null @@ -1,61 +0,0 @@ -using Bit.Core.Entities; -using Bit.Core.Repositories; -using Xunit; - -namespace Bit.Infrastructure.IntegrationTest.Repositories; - -public class UserRepositoryTests -{ - [DatabaseTheory, DatabaseData] - public async Task DeleteAsync_Works(IUserRepository userRepository) - { - var user = await userRepository.CreateAsync(new User - { - Name = "Test User", - Email = $"test+{Guid.NewGuid()}@example.com", - ApiKey = "TEST", - SecurityStamp = "stamp", - }); - - await userRepository.DeleteAsync(user); - - var newUser = await userRepository.GetByIdAsync(user.Id); - Assert.NotNull(newUser); - Assert.NotEqual(newUser.AccountRevisionDate, user.AccountRevisionDate); - } - - [DatabaseTheory, DatabaseData] - public async Task DeleteManyAsync_Works(IUserRepository userRepository) - { - var user1 = await userRepository.CreateAsync(new User - { - Name = "Test User 1", - Email = $"test+{Guid.NewGuid()}@email.com", - ApiKey = "TEST", - SecurityStamp = "stamp", - }); - - var user2 = await userRepository.CreateAsync(new User - { - Name = "Test User 2", - Email = $"test+{Guid.NewGuid()}@email.com", - ApiKey = "TEST", - SecurityStamp = "stamp", - }); - - await userRepository.DeleteManyAsync(new List - { - user1, - user2 - }); - - var updatedUser1 = await userRepository.GetByIdAsync(user1.Id); - Assert.NotNull(updatedUser1); - var updatedUser2 = await userRepository.GetByIdAsync(user2.Id); - Assert.NotNull(updatedUser2); - - Assert.NotEqual(updatedUser1.AccountRevisionDate, user1.AccountRevisionDate); - Assert.NotEqual(updatedUser2.AccountRevisionDate, user2.AccountRevisionDate); - } - -} diff --git a/test/Infrastructure.IntegrationTest/Auth/Repositories/UserRepositoryTests.cs b/test/Infrastructure.IntegrationTest/Auth/Repositories/UserRepositoryTests.cs new file mode 100644 index 0000000000..b79a40b599 --- /dev/null +++ b/test/Infrastructure.IntegrationTest/Auth/Repositories/UserRepositoryTests.cs @@ -0,0 +1,97 @@ +using Bit.Core.AdminConsole.Entities; +using Bit.Core.Entities; +using Bit.Core.Enums; +using Bit.Core.Repositories; +using Xunit; + +namespace Bit.Infrastructure.IntegrationTest.Repositories; + +public class UserRepositoryTests +{ + [DatabaseTheory, DatabaseData] + public async Task DeleteAsync_Works(IUserRepository userRepository) + { + var user = await userRepository.CreateAsync(new User + { + Name = "Test User", + Email = $"test+{Guid.NewGuid()}@example.com", + ApiKey = "TEST", + SecurityStamp = "stamp", + }); + + await userRepository.DeleteAsync(user); + + var deletedUser = await userRepository.GetByIdAsync(user.Id); + Assert.Null(deletedUser); + } + + [DatabaseTheory, DatabaseData] + public async Task DeleteManyAsync_Works(IUserRepository userRepository, IOrganizationUserRepository organizationUserRepository, IOrganizationRepository organizationRepository) + { + var user1 = await userRepository.CreateAsync(new User + { + Name = "Test User 1", + Email = $"test+{Guid.NewGuid()}@email.com", + ApiKey = "TEST", + SecurityStamp = "stamp", + }); + + var user2 = await userRepository.CreateAsync(new User + { + Name = "Test User 2", + Email = $"test+{Guid.NewGuid()}@email.com", + ApiKey = "TEST", + SecurityStamp = "stamp", + }); + + var user3 = await userRepository.CreateAsync(new User + { + Name = "Test User 3", + Email = $"test+{Guid.NewGuid()}@email.com", + ApiKey = "TEST", + SecurityStamp = "stamp", + }); + + var organization = await organizationRepository.CreateAsync(new Organization + { + Name = "Test Org", + BillingEmail = user3.Email, // TODO: EF does not enfore this being NOT NULL + Plan = "Test", // TODO: EF does not enforce this being NOT NULl + }); + + await organizationUserRepository.CreateAsync(new OrganizationUser + { + OrganizationId = organization.Id, + UserId = user1.Id, + Status = OrganizationUserStatusType.Confirmed, + }); + + await organizationUserRepository.CreateAsync(new OrganizationUser + { + OrganizationId = organization.Id, + UserId = user3.Id, + Status = OrganizationUserStatusType.Confirmed, + }); + + await userRepository.DeleteManyAsync(new List + { + user1, + user2 + }); + + var deletedUser1 = await userRepository.GetByIdAsync(user1.Id); + var deletedUser2 = await userRepository.GetByIdAsync(user2.Id); + var notDeletedUser3 = await userRepository.GetByIdAsync(user3.Id); + + var orgUser1Deleted = await organizationUserRepository.GetByIdAsync(user1.Id); + var notDeletedOrgUser3 = await organizationUserRepository.GetByIdAsync(user3.Id); + + Assert.Null(deletedUser1); + Assert.Null(deletedUser2); + Assert.NotNull(notDeletedUser3); + + Assert.Null(orgUser1Deleted); + Assert.NotNull(notDeletedOrgUser3); + } + +}