mirror of
https://github.com/bitwarden/server
synced 2026-01-14 22:43:19 +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>
101 lines
3.0 KiB
C#
101 lines
3.0 KiB
C#
using Bit.SeederApi.Commands.Interfaces;
|
|
using Bit.SeederApi.Execution;
|
|
using Bit.SeederApi.Models.Request;
|
|
using Bit.SeederApi.Queries.Interfaces;
|
|
using Bit.SeederApi.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Bit.SeederApi.Controllers;
|
|
|
|
[Route("seed")]
|
|
public class SeedController(
|
|
ILogger<SeedController> logger,
|
|
ISceneExecutor sceneExecutor,
|
|
IDestroySceneCommand destroySceneCommand,
|
|
IDestroyBatchScenesCommand destroyBatchScenesCommand,
|
|
IGetAllPlayIdsQuery getAllPlayIdsQuery) : Controller
|
|
{
|
|
[HttpPost]
|
|
public async Task<IActionResult> SeedAsync([FromBody] SeedRequestModel request)
|
|
{
|
|
logger.LogInformation("Received seed request with template: {Template}", request.Template);
|
|
|
|
try
|
|
{
|
|
var response = await sceneExecutor.ExecuteAsync(request.Template, request.Arguments);
|
|
|
|
return Json(response);
|
|
}
|
|
catch (SceneNotFoundException ex)
|
|
{
|
|
return NotFound(new { Error = ex.Message });
|
|
}
|
|
catch (SceneExecutionException ex)
|
|
{
|
|
logger.LogError(ex, "Error executing scene: {Template}", request.Template);
|
|
return BadRequest(new { Error = ex.Message, Details = ex.InnerException?.Message });
|
|
}
|
|
}
|
|
|
|
[HttpDelete("batch")]
|
|
public async Task<IActionResult> DeleteBatchAsync([FromBody] List<string> playIds)
|
|
{
|
|
logger.LogInformation("Deleting batch of seeded data with IDs: {PlayIds}", string.Join(", ", playIds));
|
|
|
|
try
|
|
{
|
|
await destroyBatchScenesCommand.DestroyAsync(playIds);
|
|
return Ok(new { Message = "Batch delete completed successfully" });
|
|
}
|
|
catch (AggregateException ex)
|
|
{
|
|
return BadRequest(new
|
|
{
|
|
Error = ex.Message,
|
|
Details = ex.InnerExceptions.Select(e => e.Message).ToList()
|
|
});
|
|
}
|
|
}
|
|
|
|
[HttpDelete("{playId}")]
|
|
public async Task<IActionResult> DeleteAsync([FromRoute] string playId)
|
|
{
|
|
logger.LogInformation("Deleting seeded data with ID: {PlayId}", playId);
|
|
|
|
try
|
|
{
|
|
var result = await destroySceneCommand.DestroyAsync(playId);
|
|
|
|
return Json(result);
|
|
}
|
|
catch (SceneExecutionException ex)
|
|
{
|
|
logger.LogError(ex, "Error deleting seeded data: {PlayId}", playId);
|
|
return BadRequest(new { Error = ex.Message, Details = ex.InnerException?.Message });
|
|
}
|
|
}
|
|
|
|
|
|
[HttpDelete]
|
|
public async Task<IActionResult> DeleteAllAsync()
|
|
{
|
|
logger.LogInformation("Deleting all seeded data");
|
|
|
|
var playIds = getAllPlayIdsQuery.GetAllPlayIds();
|
|
|
|
try
|
|
{
|
|
await destroyBatchScenesCommand.DestroyAsync(playIds);
|
|
return NoContent();
|
|
}
|
|
catch (AggregateException ex)
|
|
{
|
|
return BadRequest(new
|
|
{
|
|
Error = ex.Message,
|
|
Details = ex.InnerExceptions.Select(e => e.Message).ToList()
|
|
});
|
|
}
|
|
}
|
|
}
|