2024-08-15 23:14:22 +02:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Bit.Api.IntegrationTest.Factories;
|
2024-05-31 01:23:31 +02:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
2024-06-14 21:34:47 +02:00
|
|
|
|
using Bit.Core.Billing.Enums;
|
2023-11-29 00:18:08 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Models.Business;
|
2024-05-31 01:23:31 +02:00
|
|
|
|
using Bit.Core.Models.Data;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using Bit.IntegrationTestCommon.Factories;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.IntegrationTest.Helpers;
|
|
|
|
|
|
|
|
|
|
public static class OrganizationTestHelpers
|
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
public static async Task<Tuple<Organization, OrganizationUser>> SignUpAsync<T>(WebApplicationFactoryBase<T> factory,
|
2023-01-13 15:02:53 +01:00
|
|
|
|
PlanType plan = PlanType.Free,
|
|
|
|
|
string ownerEmail = "integration-test@bitwarden.com",
|
|
|
|
|
string name = "Integration Test Org",
|
|
|
|
|
string billingEmail = "integration-test@bitwarden.com",
|
2024-05-31 01:23:31 +02:00
|
|
|
|
string ownerKey = "test-key",
|
|
|
|
|
int passwordManagerSeats = 0,
|
|
|
|
|
PaymentMethodType paymentMethod = PaymentMethodType.None) where T : class
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
|
|
|
|
var userRepository = factory.GetService<IUserRepository>();
|
|
|
|
|
var organizationService = factory.GetService<IOrganizationService>();
|
|
|
|
|
|
|
|
|
|
var owner = await userRepository.GetByEmailAsync(ownerEmail);
|
|
|
|
|
|
2024-02-14 15:41:51 +01:00
|
|
|
|
var signUpResult = await organizationService.SignUpAsync(new OrganizationSignup
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
|
|
|
|
Name = name,
|
|
|
|
|
BillingEmail = billingEmail,
|
|
|
|
|
Plan = plan,
|
|
|
|
|
OwnerKey = ownerKey,
|
|
|
|
|
Owner = owner,
|
2024-05-31 01:23:31 +02:00
|
|
|
|
AdditionalSeats = passwordManagerSeats,
|
|
|
|
|
PaymentMethodType = paymentMethod
|
2023-01-13 15:02:53 +01:00
|
|
|
|
});
|
2024-02-14 15:41:51 +01:00
|
|
|
|
|
2024-08-15 23:14:22 +02:00
|
|
|
|
Debug.Assert(signUpResult.organizationUser is not null);
|
|
|
|
|
|
2024-02-14 15:41:51 +01:00
|
|
|
|
return new Tuple<Organization, OrganizationUser>(signUpResult.organization, signUpResult.organizationUser);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
2023-01-20 16:33:11 +01:00
|
|
|
|
|
2024-05-31 01:23:31 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an OrganizationUser. The user account must already be created.
|
|
|
|
|
/// </summary>
|
2023-01-20 16:33:11 +01:00
|
|
|
|
public static async Task<OrganizationUser> CreateUserAsync<T>(
|
|
|
|
|
WebApplicationFactoryBase<T> factory,
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
string userEmail,
|
2023-01-31 18:38:53 +01:00
|
|
|
|
OrganizationUserType type,
|
2024-05-31 01:23:31 +02:00
|
|
|
|
bool accessSecretsManager = false,
|
|
|
|
|
Permissions? permissions = null
|
2023-01-20 16:33:11 +01:00
|
|
|
|
) where T : class
|
|
|
|
|
{
|
|
|
|
|
var userRepository = factory.GetService<IUserRepository>();
|
|
|
|
|
var organizationUserRepository = factory.GetService<IOrganizationUserRepository>();
|
|
|
|
|
|
|
|
|
|
var user = await userRepository.GetByEmailAsync(userEmail);
|
2024-08-15 23:14:22 +02:00
|
|
|
|
Debug.Assert(user is not null);
|
2023-01-20 16:33:11 +01:00
|
|
|
|
|
|
|
|
|
var orgUser = new OrganizationUser
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = organizationId,
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
Key = null,
|
|
|
|
|
Type = type,
|
2023-01-31 18:38:53 +01:00
|
|
|
|
Status = OrganizationUserStatusType.Confirmed,
|
2023-01-20 16:33:11 +01:00
|
|
|
|
ExternalId = null,
|
2023-01-31 18:38:53 +01:00
|
|
|
|
AccessSecretsManager = accessSecretsManager,
|
2023-01-20 16:33:11 +01:00
|
|
|
|
};
|
|
|
|
|
|
2024-05-31 01:23:31 +02:00
|
|
|
|
if (permissions != null)
|
|
|
|
|
{
|
|
|
|
|
orgUser.SetPermissions(permissions);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 16:33:11 +01:00
|
|
|
|
await organizationUserRepository.CreateAsync(orgUser);
|
|
|
|
|
|
|
|
|
|
return orgUser;
|
|
|
|
|
}
|
2024-05-31 01:23:31 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new User account with a unique email address and a corresponding OrganizationUser for
|
|
|
|
|
/// the specified organization.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static async Task<(string, OrganizationUser)> CreateNewUserWithAccountAsync(
|
|
|
|
|
ApiApplicationFactory factory,
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
OrganizationUserType userType,
|
|
|
|
|
Permissions? permissions = null
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
var email = $"integration-test{Guid.NewGuid()}@bitwarden.com";
|
|
|
|
|
|
|
|
|
|
// Create user
|
|
|
|
|
await factory.LoginWithNewAccount(email);
|
|
|
|
|
|
|
|
|
|
// Create organizationUser
|
|
|
|
|
var organizationUser = await OrganizationTestHelpers.CreateUserAsync(factory, organizationId, email, userType,
|
|
|
|
|
permissions: permissions);
|
|
|
|
|
|
|
|
|
|
return (email, organizationUser);
|
|
|
|
|
}
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|