1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-12 15:26:48 +01:00

fix and add tests

This commit is contained in:
Brandon 2024-11-21 16:25:26 -05:00
parent 08ae01c4a9
commit 20aad3bc8b
No known key found for this signature in database
GPG Key ID: A0E0EF0B207BA40D
4 changed files with 161 additions and 63 deletions

View File

@ -32,8 +32,6 @@ using Microsoft.Extensions.Options;
using File = System.IO.File; using File = System.IO.File;
using JsonSerializer = System.Text.Json.JsonSerializer; using JsonSerializer = System.Text.Json.JsonSerializer;
#nullable enable
namespace Bit.Core.Services; namespace Bit.Core.Services;
public class UserService : UserManager<User>, IUserService, IDisposable public class UserService : UserManager<User>, IUserService, IDisposable

View File

@ -349,6 +349,70 @@ public class UserServiceTests
Assert.False(result); Assert.False(result);
} }
[Theory, BitAutoData]
public async Task DeleteUserAsync_WithAccountDeprovisioningEnabled_Succeeds(
SutProvider<UserService> sutProvider, User user
)
{
// Arrange
sutProvider.GetDependency<IFeatureService>().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<UserService> sutProvider, User user
)
{
// Arrange
sutProvider.GetDependency<IFeatureService>().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<UserService> sutProvider, User user1, User user2, User user3
)
{
// Arrange
var userList = new List<User> { user1, user2 };
sutProvider.GetDependency<IFeatureService>().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<UserService> sutProvider, User user1, User user2, User user3
)
{
// Arrange
var userList = new List<User> { user1, user2 };
sutProvider.GetDependency<IFeatureService>().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, private static void SetupUserAndDevice(User user,
bool shouldHavePassword) bool shouldHavePassword)
{ {

View File

@ -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<User>
{
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);
}
}

View File

@ -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<User>
{
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);
}
}