Files
git.stella-ops.org/src/Notify/__Tests/StellaOps.Notify.WebService.Tests/NormalizeEndpointsTests.cs

93 lines
3.5 KiB
C#

using System.Net.Http.Json;
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
using Xunit.v3;
using StellaOps.TestKit;
namespace StellaOps.Notify.WebService.Tests;
public sealed class NormalizeEndpointsTests : IClassFixture<WebApplicationFactory<Program>>, IAsyncLifetime
{
private readonly WebApplicationFactory<Program> _factory;
public NormalizeEndpointsTests(WebApplicationFactory<Program> factory)
{
_factory = factory.WithWebHostBuilder(builder =>
{
builder.UseSetting("notify:storage:driver", "memory");
builder.UseSetting("notify:authority:enabled", "false");
builder.UseSetting("notify:authority:developmentSigningKey", "normalize-tests-signing-key-1234567890");
builder.UseSetting("notify:authority:issuer", "test-issuer");
builder.UseSetting("notify:authority:audiences:0", "notify");
builder.UseSetting("notify:telemetry:enableRequestLogging", "false");
});
}
public ValueTask InitializeAsync() => ValueTask.CompletedTask;
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
[Trait("Category", TestCategories.Unit)]
[Fact]
public async Task RuleNormalizeAddsSchemaVersion()
{
var client = _factory.CreateClient();
var payload = LoadSampleNode("notify-rule@1.sample.json");
payload!.AsObject().Remove("schemaVersion");
var response = await client.PostAsJsonAsync("/internal/notify/rules/normalize", payload, CancellationToken.None);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync(CancellationToken.None);
var normalized = JsonNode.Parse(content);
Assert.Equal("notify.rule@1", normalized?["schemaVersion"]?.GetValue<string>());
}
[Trait("Category", TestCategories.Unit)]
[Fact]
public async Task ChannelNormalizeAddsSchemaVersion()
{
var client = _factory.CreateClient();
var payload = LoadSampleNode("notify-channel@1.sample.json");
payload!.AsObject().Remove("schemaVersion");
var response = await client.PostAsJsonAsync("/internal/notify/channels/normalize", payload, CancellationToken.None);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync(CancellationToken.None);
var normalized = JsonNode.Parse(content);
Assert.Equal("notify.channel@1", normalized?["schemaVersion"]?.GetValue<string>());
}
[Trait("Category", TestCategories.Unit)]
[Fact]
public async Task TemplateNormalizeAddsSchemaVersion()
{
var client = _factory.CreateClient();
var payload = LoadSampleNode("notify-template@1.sample.json");
payload!.AsObject().Remove("schemaVersion");
var response = await client.PostAsJsonAsync("/internal/notify/templates/normalize", payload, CancellationToken.None);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync(CancellationToken.None);
var normalized = JsonNode.Parse(content);
Assert.Equal("notify.template@1", normalized?["schemaVersion"]?.GetValue<string>());
}
private static JsonNode? LoadSampleNode(string fileName)
{
var path = Path.Combine(AppContext.BaseDirectory, fileName);
if (!File.Exists(path))
{
throw new FileNotFoundException($"Unable to load sample '{fileName}'.", path);
}
return JsonNode.Parse(File.ReadAllText(path));
}
}