2024-06-19 21:11:24 +02:00
|
|
|
|
using Bit.Identity.Models.Request.Accounts;
|
2022-08-29 16:24:52 +02:00
|
|
|
|
using Bit.IntegrationTestCommon.Factories;
|
2023-11-20 22:32:23 +01:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2022-08-29 16:24:52 +02:00
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
2023-10-27 18:13:52 +02:00
|
|
|
|
using Microsoft.Data.Sqlite;
|
2022-08-29 16:24:52 +02:00
|
|
|
|
|
2024-06-28 16:28:07 +02:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
2022-08-29 16:24:52 +02:00
|
|
|
|
namespace Bit.Api.IntegrationTest.Factories;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-08-29 16:24:52 +02:00
|
|
|
|
public class ApiApplicationFactory : WebApplicationFactoryBase<Startup>
|
|
|
|
|
{
|
|
|
|
|
private readonly IdentityApplicationFactory _identityApplicationFactory;
|
2023-10-27 18:13:52 +02:00
|
|
|
|
private const string _connectionString = "DataSource=:memory:";
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-08-29 16:24:52 +02:00
|
|
|
|
public ApiApplicationFactory()
|
|
|
|
|
{
|
2023-10-27 18:13:52 +02:00
|
|
|
|
SqliteConnection = new SqliteConnection(_connectionString);
|
|
|
|
|
SqliteConnection.Open();
|
|
|
|
|
|
2022-08-29 16:24:52 +02:00
|
|
|
|
_identityApplicationFactory = new IdentityApplicationFactory();
|
2023-10-27 18:13:52 +02:00
|
|
|
|
_identityApplicationFactory.SqliteConnection = SqliteConnection;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-08-29 16:24:52 +02:00
|
|
|
|
|
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-08-29 16:24:52 +02:00
|
|
|
|
base.ConfigureWebHost(builder);
|
|
|
|
|
|
|
|
|
|
builder.ConfigureTestServices(services =>
|
|
|
|
|
{
|
2023-01-13 15:02:53 +01:00
|
|
|
|
// Remove scheduled background jobs to prevent errors in parallel test execution
|
2023-11-20 22:32:23 +01:00
|
|
|
|
var jobService = services.First(sd => sd.ServiceType == typeof(IHostedService) && sd.ImplementationType == typeof(Jobs.JobsHostedService));
|
2023-01-13 15:02:53 +01:00
|
|
|
|
services.Remove(jobService);
|
|
|
|
|
|
2023-11-20 22:32:23 +01:00
|
|
|
|
services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
|
2022-08-29 16:24:52 +02:00
|
|
|
|
{
|
2023-11-20 22:32:23 +01:00
|
|
|
|
options.BackchannelHttpHandler = _identityApplicationFactory.Server.CreateHandler();
|
2022-08-29 16:24:52 +02:00
|
|
|
|
});
|
2022-08-29 22:06:55 +02:00
|
|
|
|
});
|
2022-08-29 16:24:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper for registering and logging in to a new account
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<(string Token, string RefreshToken)> LoginWithNewAccount(string email = "integration-test@bitwarden.com", string masterPasswordHash = "master_password_hash")
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-08-29 16:24:52 +02:00
|
|
|
|
await _identityApplicationFactory.RegisterAsync(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = email,
|
|
|
|
|
MasterPasswordHash = masterPasswordHash,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await _identityApplicationFactory.TokenFromPasswordAsync(email, masterPasswordHash);
|
|
|
|
|
}
|
2023-01-20 16:33:11 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper for logging in to an account
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<(string Token, string RefreshToken)> LoginAsync(string email = "integration-test@bitwarden.com", string masterPasswordHash = "master_password_hash")
|
|
|
|
|
{
|
|
|
|
|
return await _identityApplicationFactory.TokenFromPasswordAsync(email, masterPasswordHash);
|
|
|
|
|
}
|
2023-10-27 18:13:52 +02:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(disposing);
|
2024-06-28 16:28:07 +02:00
|
|
|
|
SqliteConnection!.Dispose();
|
2023-10-27 18:13:52 +02:00
|
|
|
|
}
|
2024-03-29 17:00:30 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper for logging in via client secret.
|
|
|
|
|
/// Currently used for Secrets Manager service accounts
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<string> LoginWithClientSecretAsync(Guid clientId, string clientSecret)
|
|
|
|
|
{
|
|
|
|
|
return await _identityApplicationFactory.TokenFromAccessTokenAsync(clientId, clientSecret);
|
|
|
|
|
}
|
2024-05-31 01:23:31 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper for logging in with an Organization api key.
|
|
|
|
|
/// Currently used for the Public Api
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<string> LoginWithOrganizationApiKeyAsync(string clientId, string clientSecret)
|
|
|
|
|
{
|
|
|
|
|
return await _identityApplicationFactory.TokenFromOrganizationApiKeyAsync(clientId, clientSecret);
|
|
|
|
|
}
|
2022-08-29 16:24:52 +02:00
|
|
|
|
}
|