mirror of
https://github.com/bitwarden/server
synced 2026-01-15 06:53:26 +00:00
We want to reduce the amount of business critical test data in the company. One way of doing that is to generate test data on demand prior to client side testing. Clients will request a scene to be set up with a JSON body set of options, specific to a given scene. Successful seed requests will be responded to with a mangleMap which maps magic strings present in the request to the mangled, non-colliding versions inserted into the database. This way, the server is solely responsible for understanding uniqueness requirements in the database. scenes also are able to return custom data, depending on the scene. For example, user creation would benefit from a return value of the userId for further test setup on the client side. Clients will indicate they are running tests by including a unique header, x-play-id which specifies a unique testing context. The server uses this PlayId as the seed for any mangling that occurs. This allows the client to decide it will reuse a given PlayId if the test context builds on top of previously executed tests. When a given context is no longer needed, the API user will delete all test data associated with the PlayId by calling a delete endpoint. --------- Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
79 lines
2.7 KiB
C#
79 lines
2.7 KiB
C#
using System.Text.Json;
|
|
using Bit.Seeder;
|
|
using Bit.SeederApi.Models.Response;
|
|
using Bit.SeederApi.Services;
|
|
|
|
namespace Bit.SeederApi.Execution;
|
|
|
|
public class SceneExecutor(
|
|
ILogger<SceneExecutor> logger,
|
|
IServiceProvider serviceProvider) : ISceneExecutor
|
|
{
|
|
|
|
public async Task<SceneResponseModel> ExecuteAsync(string templateName, JsonElement? arguments)
|
|
{
|
|
try
|
|
{
|
|
var scene = serviceProvider.GetKeyedService<IScene>(templateName)
|
|
?? throw new SceneNotFoundException(templateName);
|
|
|
|
var requestType = scene.GetRequestType();
|
|
var requestModel = DeserializeRequestModel(templateName, requestType, arguments);
|
|
var result = await scene.SeedAsync(requestModel);
|
|
|
|
logger.LogInformation("Successfully executed scene: {TemplateName}", templateName);
|
|
return SceneResponseModel.FromSceneResult(result);
|
|
}
|
|
catch (Exception ex) when (ex is not SceneNotFoundException and not SceneExecutionException)
|
|
{
|
|
logger.LogError(ex, "Unexpected error executing scene: {TemplateName}", templateName);
|
|
throw new SceneExecutionException(
|
|
$"An unexpected error occurred while executing scene '{templateName}'",
|
|
ex.InnerException ?? ex);
|
|
}
|
|
}
|
|
|
|
private object DeserializeRequestModel(string templateName, Type requestType, JsonElement? arguments)
|
|
{
|
|
if (arguments == null)
|
|
{
|
|
return CreateDefaultRequestModel(templateName, requestType);
|
|
}
|
|
|
|
try
|
|
{
|
|
var requestModel = JsonSerializer.Deserialize(arguments.Value.GetRawText(), requestType, JsonConfiguration.Options);
|
|
if (requestModel == null)
|
|
{
|
|
throw new SceneExecutionException(
|
|
$"Failed to deserialize request model for scene '{templateName}'");
|
|
}
|
|
return requestModel;
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
throw new SceneExecutionException(
|
|
$"Failed to deserialize request model for scene '{templateName}': {ex.Message}", ex);
|
|
}
|
|
}
|
|
|
|
private object CreateDefaultRequestModel(string templateName, Type requestType)
|
|
{
|
|
try
|
|
{
|
|
var requestModel = Activator.CreateInstance(requestType);
|
|
if (requestModel == null)
|
|
{
|
|
throw new SceneExecutionException(
|
|
$"Arguments are required for scene '{templateName}'");
|
|
}
|
|
return requestModel;
|
|
}
|
|
catch
|
|
{
|
|
throw new SceneExecutionException(
|
|
$"Arguments are required for scene '{templateName}'");
|
|
}
|
|
}
|
|
}
|