Files
hrynco-notification-service/HrynCo.NotificationService.Web.IntegrationTests/DefinitionApiTestContext.cs
agrynco 8e81572386 feat(notification): add admin rest management and deletion
Add revision-safe service and template APIs, channel mapping, and validated admin controls.

Ref: IT-1601
2026-10-17 22:51:01 +03:00

48 lines
2.1 KiB
C#

using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace HrynCo.NotificationService.Web.IntegrationTests;
public sealed class DefinitionApiTestContext : IAsyncDisposable
{
public required TemplateWebApplicationFactory App { get; init; }
public required HttpClient Client { get; init; }
public required Guid ServiceId { get; init; }
public string ServiceRoot => $"/api/v1/services/{ServiceId}";
public static async Task<DefinitionApiTestContext> CreateAsync(TemplateDatabaseFixture fixture)
{
var app = new TemplateWebApplicationFactory(await fixture.CreateDatabaseAsync());
var client = app.CreateClient();
var response = await client.GetFromJsonAsync<JsonElement>("/api/v1/services?name=TestService");
return new DefinitionApiTestContext { App = app, Client = client, ServiceId = response.GetProperty("data").GetProperty("id").GetGuid() };
}
public string DefinitionUrl(string key = "Welcome", bool upsert = false) =>
ServiceRoot + "/email-template-definitions" + (upsert ? "/upsert" : "") + "?key=" + Uri.EscapeDataString(key);
public Task<HttpResponseMessage> UpsertAsync(JsonNode body, string key = "Welcome") =>
Client.PostAsJsonAsync(DefinitionUrl(key, upsert: true), body);
public async Task<JsonElement> ReadAsync(string key = "Welcome") =>
(await Client.GetFromJsonAsync<JsonElement>(DefinitionUrl(key))).GetProperty("data");
public static JsonObject Body(Guid? revision, params string[] codes) => new()
{
["revision"] = revision?.ToString(),
["variables"] = new JsonArray(new JsonObject { ["name"] = "Name", ["required"] = true }),
["translations"] = new JsonArray(codes.Select(code => (JsonNode)new JsonObject
{
["languageCode"] = code, ["subject"] = "Subject " + code,
["htmlBody"] = "<p>Hello {{Name}}</p>", ["textBody"] = "Hello {{Name}}"
}).ToArray())
};
public async ValueTask DisposeAsync()
{
Client.Dispose();
await App.DisposeAsync();
}
}