2023-04-14 19:25:56 +02:00
|
|
|
|
using Bit.Core.Auth.Models.Api.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
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
SqliteConnection.Dispose();
|
|
|
|
|
}
|
2022-08-29 16:24:52 +02:00
|
|
|
|
}
|