mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[PM-16812] Shortcut duplicate group patch requests (#5354)
* Copy PatchGroupCommand to vNext and refactor * Detect duplicate add requests and return early * Update read repository method to use HA replica * Add new write repository method
This commit is contained in:
@ -0,0 +1,57 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Infrastructure.IntegrationTest.AdminConsole;
|
||||
|
||||
/// <summary>
|
||||
/// A set of extension methods used to arrange simple test data.
|
||||
/// This should only be used for basic, repetitive data arrangement, not for anything complex or for
|
||||
/// the repository method under test.
|
||||
/// </summary>
|
||||
public static class OrganizationTestHelpers
|
||||
{
|
||||
public static Task<User> CreateTestUserAsync(this IUserRepository userRepository, string identifier = "test")
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
return userRepository.CreateAsync(new User
|
||||
{
|
||||
Id = id,
|
||||
Name = $"{identifier}-{id}",
|
||||
Email = $"{id}@example.com",
|
||||
ApiKey = "TEST",
|
||||
SecurityStamp = "stamp",
|
||||
});
|
||||
}
|
||||
|
||||
public static Task<Organization> CreateTestOrganizationAsync(this IOrganizationRepository organizationRepository,
|
||||
string identifier = "test")
|
||||
=> organizationRepository.CreateAsync(new Organization
|
||||
{
|
||||
Name = $"{identifier}-{Guid.NewGuid()}",
|
||||
BillingEmail = "billing@example.com", // TODO: EF does not enforce this being NOT NULL
|
||||
Plan = "Test", // TODO: EF does not enforce this being NOT NULl
|
||||
});
|
||||
|
||||
public static Task<OrganizationUser> CreateTestOrganizationUserAsync(
|
||||
this IOrganizationUserRepository organizationUserRepository,
|
||||
Organization organization,
|
||||
User user)
|
||||
=> organizationUserRepository.CreateAsync(new OrganizationUser
|
||||
{
|
||||
OrganizationId = organization.Id,
|
||||
UserId = user.Id,
|
||||
Status = OrganizationUserStatusType.Confirmed,
|
||||
Type = OrganizationUserType.Owner
|
||||
});
|
||||
|
||||
public static Task<Group> CreateTestGroupAsync(
|
||||
this IGroupRepository groupRepository,
|
||||
Organization organization,
|
||||
string identifier = "test")
|
||||
=> groupRepository.CreateAsync(
|
||||
new Group { OrganizationId = organization.Id, Name = $"{identifier} {Guid.NewGuid()}" }
|
||||
);
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Repositories;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Infrastructure.IntegrationTest.AdminConsole.Repositories;
|
||||
|
||||
public class GroupRepositoryTests
|
||||
{
|
||||
[DatabaseTheory, DatabaseData]
|
||||
public async Task AddGroupUsersByIdAsync_CreatesGroupUsers(
|
||||
IGroupRepository groupRepository,
|
||||
IUserRepository userRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IOrganizationRepository organizationRepository)
|
||||
{
|
||||
// Arrange
|
||||
var user1 = await userRepository.CreateTestUserAsync("user1");
|
||||
var user2 = await userRepository.CreateTestUserAsync("user2");
|
||||
var user3 = await userRepository.CreateTestUserAsync("user3");
|
||||
|
||||
var org = await organizationRepository.CreateTestOrganizationAsync();
|
||||
var orgUser1 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user1);
|
||||
var orgUser2 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user2);
|
||||
var orgUser3 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user3);
|
||||
var orgUserIds = new List<Guid>([orgUser1.Id, orgUser2.Id, orgUser3.Id]);
|
||||
var group = await groupRepository.CreateTestGroupAsync(org);
|
||||
|
||||
// Act
|
||||
await groupRepository.AddGroupUsersByIdAsync(group.Id, orgUserIds);
|
||||
|
||||
// Assert
|
||||
var actual = await groupRepository.GetManyUserIdsByIdAsync(group.Id);
|
||||
Assert.Equal(orgUserIds!.Order(), actual.Order());
|
||||
}
|
||||
|
||||
[DatabaseTheory, DatabaseData]
|
||||
public async Task AddGroupUsersByIdAsync_IgnoresExistingGroupUsers(
|
||||
IGroupRepository groupRepository,
|
||||
IUserRepository userRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IOrganizationRepository organizationRepository)
|
||||
{
|
||||
// Arrange
|
||||
var user1 = await userRepository.CreateTestUserAsync("user1");
|
||||
var user2 = await userRepository.CreateTestUserAsync("user2");
|
||||
var user3 = await userRepository.CreateTestUserAsync("user3");
|
||||
|
||||
var org = await organizationRepository.CreateTestOrganizationAsync();
|
||||
var orgUser1 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user1);
|
||||
var orgUser2 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user2);
|
||||
var orgUser3 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user3);
|
||||
var orgUserIds = new List<Guid>([orgUser1.Id, orgUser2.Id, orgUser3.Id]);
|
||||
var group = await groupRepository.CreateTestGroupAsync(org);
|
||||
|
||||
// Add user 2 to the group already, make sure this is executed correctly before proceeding
|
||||
await groupRepository.UpdateUsersAsync(group.Id, [orgUser2.Id]);
|
||||
var existingUsers = await groupRepository.GetManyUserIdsByIdAsync(group.Id);
|
||||
Assert.Equal([orgUser2.Id], existingUsers);
|
||||
|
||||
// Act
|
||||
await groupRepository.AddGroupUsersByIdAsync(group.Id, orgUserIds);
|
||||
|
||||
// Assert - group should contain all users
|
||||
var actual = await groupRepository.GetManyUserIdsByIdAsync(group.Id);
|
||||
Assert.Equal(orgUserIds!.Order(), actual.Order());
|
||||
}
|
||||
|
||||
[DatabaseTheory, DatabaseData]
|
||||
public async Task AddGroupUsersByIdAsync_IgnoresUsersNotInOrganization(
|
||||
IGroupRepository groupRepository,
|
||||
IUserRepository userRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IOrganizationRepository organizationRepository)
|
||||
{
|
||||
// Arrange
|
||||
var user1 = await userRepository.CreateTestUserAsync("user1");
|
||||
var user2 = await userRepository.CreateTestUserAsync("user2");
|
||||
var user3 = await userRepository.CreateTestUserAsync("user3");
|
||||
|
||||
var org = await organizationRepository.CreateTestOrganizationAsync();
|
||||
var orgUser1 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user1);
|
||||
var orgUser2 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user2);
|
||||
|
||||
// User3 belongs to a different org
|
||||
var otherOrg = await organizationRepository.CreateTestOrganizationAsync();
|
||||
var orgUser3 = await organizationUserRepository.CreateTestOrganizationUserAsync(otherOrg, user3);
|
||||
|
||||
var orgUserIds = new List<Guid>([orgUser1.Id, orgUser2.Id, orgUser3.Id]);
|
||||
var group = await groupRepository.CreateTestGroupAsync(org);
|
||||
|
||||
// Act
|
||||
await groupRepository.AddGroupUsersByIdAsync(group.Id, orgUserIds);
|
||||
|
||||
// Assert
|
||||
var actual = await groupRepository.GetManyUserIdsByIdAsync(group.Id);
|
||||
Assert.Equal(2, actual.Count);
|
||||
Assert.Contains(orgUser1.Id, actual);
|
||||
Assert.Contains(orgUser2.Id, actual);
|
||||
Assert.DoesNotContain(orgUser3.Id, actual);
|
||||
}
|
||||
|
||||
[DatabaseTheory, DatabaseData]
|
||||
public async Task AddGroupUsersByIdAsync_IgnoresDuplicateUsers(
|
||||
IGroupRepository groupRepository,
|
||||
IUserRepository userRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IOrganizationRepository organizationRepository)
|
||||
{
|
||||
// Arrange
|
||||
var user1 = await userRepository.CreateTestUserAsync("user1");
|
||||
var user2 = await userRepository.CreateTestUserAsync("user2");
|
||||
|
||||
var org = await organizationRepository.CreateTestOrganizationAsync();
|
||||
var orgUser1 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user1);
|
||||
var orgUser2 = await organizationUserRepository.CreateTestOrganizationUserAsync(org, user2);
|
||||
|
||||
var orgUserIds = new List<Guid>([orgUser1.Id, orgUser2.Id, orgUser2.Id]); // duplicate orgUser2
|
||||
var group = await groupRepository.CreateTestGroupAsync(org);
|
||||
|
||||
// Act
|
||||
await groupRepository.AddGroupUsersByIdAsync(group.Id, orgUserIds);
|
||||
|
||||
// Assert
|
||||
var actual = await groupRepository.GetManyUserIdsByIdAsync(group.Id);
|
||||
Assert.Equal(2, actual.Count);
|
||||
Assert.Contains(orgUser1.Id, actual);
|
||||
Assert.Contains(orgUser2.Id, actual);
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ using Bit.Core.Entities;
|
||||
using Bit.Core.Repositories;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Infrastructure.IntegrationTest.Repositories;
|
||||
namespace Bit.Infrastructure.IntegrationTest.AdminConsole.Repositories;
|
||||
|
||||
public class OrganizationDomainRepositoryTests
|
||||
{
|
||||
|
@ -4,7 +4,7 @@ using Bit.Core.Enums;
|
||||
using Bit.Core.Repositories;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Infrastructure.IntegrationTest.Repositories;
|
||||
namespace Bit.Infrastructure.IntegrationTest.AdminConsole.Repositories;
|
||||
|
||||
public class OrganizationRepositoryTests
|
||||
{
|
||||
|
@ -4,7 +4,7 @@ using Bit.Core.Enums;
|
||||
using Bit.Core.Repositories;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Infrastructure.IntegrationTest.Repositories;
|
||||
namespace Bit.Infrastructure.IntegrationTest.AdminConsole.Repositories;
|
||||
|
||||
public class OrganizationUserRepositoryTests
|
||||
{
|
||||
|
Reference in New Issue
Block a user