Skip to content

[PM-21079] Add support to integration tests for using sqlserver #5823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace Bit.Api.IntegrationTest.AdminConsole.Controllers;

public class OrganizationUsersControllerPerformanceTest(ITestOutputHelper testOutputHelper)
{
[Theory(Skip = "Performance test")]
[Theory]
[InlineData(100)]
[InlineData(60000)]
public async Task GetAsync(int seats)
{
await using var factory = new ApiApplicationFactory();
await using var factory = new SqlServerApiApplicationFactory();
var client = factory.CreateClient();

var db = factory.GetDatabaseContext();
Expand Down
23 changes: 10 additions & 13 deletions test/Api.IntegrationTest/Factories/ApiApplicationFactory.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
๏ปฟusing Bit.Core;
using Bit.Core.Auth.Models.Api.Request.Accounts;
using Bit.Core.Enums;
using Bit.IntegrationTestCommon;
using Bit.IntegrationTestCommon.Factories;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Data.Sqlite;

#nullable enable

namespace Bit.Api.IntegrationTest.Factories;

public class ApiApplicationFactory : WebApplicationFactoryBase<Startup>
{
private readonly IdentityApplicationFactory _identityApplicationFactory;
private const string _connectionString = "DataSource=:memory:";
protected IdentityApplicationFactory _identityApplicationFactory;

public ApiApplicationFactory()
public ApiApplicationFactory() : this(new SqlServerTestDatabase())
{
SqliteConnection = new SqliteConnection(_connectionString);
SqliteConnection.Open();
}

public ApiApplicationFactory(ITestDatabase db)
{
TestDatabase = db;
_handleDbDisposal = true;

Check failure on line 24 in test/Api.IntegrationTest/Factories/ApiApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / Run tests

The name '_handleDbDisposal' does not exist in the current context

Check failure on line 24 in test/Api.IntegrationTest/Factories/ApiApplicationFactory.cs

View workflow job for this annotation

GitHub Actions / Quality scan

The name '_handleDbDisposal' does not exist in the current context

_identityApplicationFactory = new IdentityApplicationFactory();
_identityApplicationFactory.SqliteConnection = SqliteConnection;
_identityApplicationFactory.TestDatabase = TestDatabase;
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
Expand Down Expand Up @@ -73,12 +76,6 @@
return await _identityApplicationFactory.TokenFromPasswordAsync(email, masterPasswordHash);
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
SqliteConnection!.Dispose();
}

/// <summary>
/// Helper for logging in via client secret.
/// Currently used for Secrets Manager service accounts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
๏ปฟusing Bit.IntegrationTestCommon;

#nullable enable

namespace Bit.Api.IntegrationTest.Factories;

public class SqlServerApiApplicationFactory() : ApiApplicationFactory(new SqlServerTestDatabase());
119 changes: 56 additions & 63 deletions test/IntegrationTestCommon/Factories/WebApplicationFactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -37,12 +36,12 @@ public abstract class WebApplicationFactoryBase<T> : WebApplicationFactory<T>
/// <remarks>
/// This will need to be set BEFORE using the <c>Server</c> property
/// </remarks>
public SqliteConnection? SqliteConnection { get; set; }
public ITestDatabase? TestDatabase { get; set; }

private readonly List<Action<IServiceCollection>> _configureTestServices = new();
private readonly List<Action<IConfigurationBuilder>> _configureAppConfiguration = new();

private bool _handleSqliteDisposal { get; set; }
private bool _handleDbDisposal { get; set; }


public void SubstituteService<TService>(Action<TService> mockService)
Expand Down Expand Up @@ -119,13 +118,48 @@ public void UpdateConfiguration(string key, string? value)
/// </summary>
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
if (SqliteConnection == null)
if (TestDatabase == null)
{
SqliteConnection = new SqliteConnection("DataSource=:memory:");
SqliteConnection.Open();
_handleSqliteDisposal = true;
TestDatabase = new SqliteTestDatabase();
_handleDbDisposal = true;
}

var config = new Dictionary<string, string?>
{
// Manually insert a EF provider so that ConfigureServices will add EF repositories but we will override
// DbContextOptions to use an in memory database
{ "globalSettings:databaseProvider", "postgres" },
{ "globalSettings:postgreSql:connectionString", "Host=localhost;Username=test;Password=test;Database=test" },

// Clear the redis connection string for distributed caching, forcing an in-memory implementation
{ "globalSettings:redis:connectionString", "" },

// Clear Storage
{ "globalSettings:attachment:connectionString", null },
{ "globalSettings:events:connectionString", null },
{ "globalSettings:send:connectionString", null },
{ "globalSettings:notifications:connectionString", null },
{ "globalSettings:storage:connectionString", null },

// This will force it to use an ephemeral key for IdentityServer
{ "globalSettings:developmentDirectory", null },

// Email Verification
{ "globalSettings:enableEmailVerification", "true" },
{ "globalSettings:disableUserRegistration", "false" },
{ "globalSettings:launchDarkly:flagValues:email-verification", "true" },

// New Device Verification
{ "globalSettings:disableEmailNewDevice", "false" },

// Web push notifications
{ "globalSettings:webPush:vapidPublicKey", "BGBtAM0bU3b5jsB14IjBYarvJZ6rWHilASLudTTYDDBi7a-3kebo24Yus_xYeOMZ863flAXhFAbkL6GVSrxgErg" },
{ "globalSettings:launchDarkly:flagValues:web-push", "true" },
};

// Some database drivers modify the connection string
TestDatabase.ModifyGlobalSettings(config);

builder.ConfigureAppConfiguration(c =>
{
c.SetBasePath(AppContext.BaseDirectory)
Expand All @@ -134,39 +168,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)

c.AddUserSecrets(typeof(Identity.Startup).Assembly, optional: true);

c.AddInMemoryCollection(new Dictionary<string, string?>
{
// Manually insert a EF provider so that ConfigureServices will add EF repositories but we will override
// DbContextOptions to use an in memory database
{ "globalSettings:databaseProvider", "postgres" },
{ "globalSettings:postgreSql:connectionString", "Host=localhost;Username=test;Password=test;Database=test" },

// Clear the redis connection string for distributed caching, forcing an in-memory implementation
{ "globalSettings:redis:connectionString", ""},

// Clear Storage
{ "globalSettings:attachment:connectionString", null},
{ "globalSettings:events:connectionString", null},
{ "globalSettings:send:connectionString", null},
{ "globalSettings:notifications:connectionString", null},
{ "globalSettings:storage:connectionString", null},

// This will force it to use an ephemeral key for IdentityServer
{ "globalSettings:developmentDirectory", null },


// Email Verification
{ "globalSettings:enableEmailVerification", "true" },
{ "globalSettings:disableUserRegistration", "false" },
{ "globalSettings:launchDarkly:flagValues:email-verification", "true" },

// New Device Verification
{ "globalSettings:disableEmailNewDevice", "false" },

// Web push notifications
{ "globalSettings:webPush:vapidPublicKey", "BGBtAM0bU3b5jsB14IjBYarvJZ6rWHilASLudTTYDDBi7a-3kebo24Yus_xYeOMZ863flAXhFAbkL6GVSrxgErg" },
{ "globalSettings:launchDarkly:flagValues:web-push", "true" },
});
c.AddInMemoryCollection(config);
});

// Run configured actions after defaults to allow them to take precedence
Expand All @@ -177,17 +179,16 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)

builder.ConfigureTestServices(services =>
{
var dbContextOptions = services.First(sd => sd.ServiceType == typeof(DbContextOptions<DatabaseContext>));
var dbContextOptions =
services.First(sd => sd.ServiceType == typeof(DbContextOptions<DatabaseContext>));
services.Remove(dbContextOptions);
services.AddScoped(services =>
{
return new DbContextOptionsBuilder<DatabaseContext>()
.UseSqlite(SqliteConnection)
.UseApplicationServiceProvider(services)
.Options;
});

MigrateDbContext<DatabaseContext>(services);
// Add database to the service collection
TestDatabase.AddDatabase(services);
if (_handleDbDisposal)
{
TestDatabase.Migrate(services);
}

// QUESTION: The normal licensing service should run fine on developer machines but not in CI
// should we have a fork here to leave the normal service for developers?
Expand Down Expand Up @@ -286,22 +287,14 @@ public TService GetService<TService>()
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (_handleSqliteDisposal)
if (_handleDbDisposal)
{
SqliteConnection!.Dispose();
}
}
_handleDbDisposal = false;

private void MigrateDbContext<TContext>(IServiceCollection serviceCollection) where TContext : DbContext
{
var serviceProvider = serviceCollection.BuildServiceProvider();
using var scope = serviceProvider.CreateScope();
var services = scope.ServiceProvider;
var context = services.GetRequiredService<TContext>();
if (_handleSqliteDisposal)
{
context.Database.EnsureDeleted();
if (TestDatabase != null)
{
TestDatabase!.Dispose();
}
}
context.Database.EnsureCreated();
}
}
19 changes: 19 additions & 0 deletions test/IntegrationTestCommon/ITestDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
๏ปฟusing Microsoft.Extensions.DependencyInjection;

namespace Bit.IntegrationTestCommon;

#nullable enable

public interface ITestDatabase
{
public void AddDatabase(IServiceCollection serviceCollection);

public void Migrate(IServiceCollection serviceCollection);

public void Dispose();

public void ModifyGlobalSettings(Dictionary<string, string?> config)
{
// Default implementation does nothing
}
}
1 change: 1 addition & 0 deletions test/IntegrationTestCommon/IntegrationTestCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Identity\Identity.csproj" />
<ProjectReference Include="..\..\util\Migrator\Migrator.csproj" />
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>
</Project>
70 changes: 70 additions & 0 deletions test/IntegrationTestCommon/SqlServerTestDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
๏ปฟusing Bit.Core.Settings;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Migrator;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Bit.IntegrationTestCommon;

public class SqlServerTestDatabase : ITestDatabase
{
public string SqlServerConnection { get; set; }

public SqlServerTestDatabase()
{
SqlServerConnection = "Server=localhost;Database=vault_test;User Id=SA;Password=SET_A_PASSWORD_HERE_123;Encrypt=True;TrustServerCertificate=True;";
}

public void ModifyGlobalSettings(Dictionary<string, string> config)
{
config["globalSettings:databaseProvider"] = "sqlserver";
config["globalSettings:sqlServer:connectionString"] = SqlServerConnection;
}

public void AddDatabase(IServiceCollection serviceCollection)
{
serviceCollection.AddScoped(s => new DbContextOptionsBuilder<DatabaseContext>()
.UseSqlServer(SqlServerConnection)
.UseApplicationServiceProvider(s)
.Options);
}

public void Migrate(IServiceCollection serviceCollection)
{
var serviceProvider = serviceCollection.BuildServiceProvider();
using var scope = serviceProvider.CreateScope();
var services = scope.ServiceProvider;
var globalSettings = services.GetRequiredService<GlobalSettings>();
var logger = services.GetRequiredService<ILogger<DbMigrator>>();

var migrator = new SqlServerDbMigrator(globalSettings, logger);
migrator.MigrateDatabase();
}

public void Dispose()
{
var masterConnectionString = new SqlConnectionStringBuilder(SqlServerConnection)
{
InitialCatalog = "master"
}.ConnectionString;

using var connection = new SqlConnection(masterConnectionString);
var databaseName = new SqlConnectionStringBuilder(SqlServerConnection).InitialCatalog;

connection.Open();

var databaseNameQuoted = new SqlCommandBuilder().QuoteIdentifier(databaseName);

using (var cmd = new SqlCommand($"ALTER DATABASE {databaseNameQuoted} SET single_user WITH rollback IMMEDIATE", connection))
{
cmd.ExecuteNonQuery();
}

using (var cmd = new SqlCommand($"DROP DATABASE {databaseNameQuoted}", connection))
{
cmd.ExecuteNonQuery();
}
}
}
41 changes: 41 additions & 0 deletions test/IntegrationTestCommon/SqliteTestDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
๏ปฟusing Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace Bit.IntegrationTestCommon;

public class SqliteTestDatabase : ITestDatabase
{
private SqliteConnection SqliteConnection { get; set; }

public SqliteTestDatabase()
{
SqliteConnection = new SqliteConnection("DataSource=:memory:");
SqliteConnection.Open();
}

public void AddDatabase(IServiceCollection serviceCollection)
{
serviceCollection.AddScoped(s => new DbContextOptionsBuilder<DatabaseContext>()
.UseSqlite(SqliteConnection)
.UseApplicationServiceProvider(s)
.Options);
}

public void Migrate(IServiceCollection serviceCollection)
{
var serviceProvider = serviceCollection.BuildServiceProvider();
using var scope = serviceProvider.CreateScope();
var services = scope.ServiceProvider;
var context = services.GetRequiredService<DatabaseContext>();

context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}

public void Dispose()
{
SqliteConnection.Dispose();
}
}
Loading