-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
test/Infrastructure.IntegrationTest/AdminConsole/OrganizationTestHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = "[email protected]", // 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()}" } | ||
); | ||
} |
67 changes: 67 additions & 0 deletions
67
test/Infrastructure.IntegrationTest/AdminConsole/Repositories/GroupRepositoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters