Files
agrynco aba4173827 feat(templates): add localized push templates and actions
Share template keys across email and push with independent editors and revisions. Render localized actions and preserve version-one delivery.

Ref: IT-1627
2026-10-31 09:38:05 +02:00

56 lines
2.8 KiB
C#

using System.Net.Http.Json;
using System.Text.Json;
using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.Abstract.PushChannels;
using HrynCo.NotificationService.Worker.Services.Push;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace HrynCo.NotificationService.Web.IntegrationTests;
public sealed class PushChannelFormatTests(TemplateDatabaseFixture fixture) : IClassFixture<TemplateDatabaseFixture>
{
[Theory]
[InlineData(1, false, true)]
[InlineData(2, true, true)]
[InlineData(2, false, false)]
[InlineData(3, true, false)]
public async Task TestEndpointSelectsFormatAndValidatesBeforeProviderWithoutChangingChannel(int version, bool hasBinding, bool accepted)
{
await using var factory = new TemplateWebApplicationFactory(await fixture.CreateDatabaseAsync());
var firebase = new RecordingFirebasePushClient();
await using var app = factory.WithWebHostBuilder(builder => builder.ConfigureTestServices(services =>
{
services.RemoveAll<IFirebasePushClient>();
services.AddSingleton<IFirebasePushClient>(firebase);
}));
using var client = app.CreateClient();
var html = await client.GetStringAsync("/admin/push-channels/create");
Assert.Contains("Version 2 — notification with a button", html);
var fields = TemplateEditorTestFields.Read(html);
var binding = hasBinding ? Guid.NewGuid() : (Guid?)null;
using var request = new HttpRequestMessage(HttpMethod.Post, "/admin/push-channels/test-send");
request.Headers.Add("RequestVerificationToken", fields["__RequestVerificationToken"]);
request.Content = JsonContent.Create(new
{
firebaseProjectId = "test-project", credentialsJson = PushChannelTestCredentials.Create(),
recipientToken = "test-token", version, bindingId = binding, actionKey = "openBasket", buttonLabel = "Перейти в розділ"
});
var response = await client.SendAsync(request);
var result = await response.Content.ReadFromJsonAsync<JsonElement>();
Assert.Equal(accepted, result.GetProperty("success").GetBoolean());
Assert.DoesNotContain("test-token", await response.Content.ReadAsStringAsync());
if (accepted)
{
var sent = Assert.Single(firebase.Messages);
Assert.Equal(version, sent.Version);
Assert.Equal(binding, sent.BindingId);
Assert.Equal("Перейти в розділ", sent.ButtonLabel);
}
else Assert.Empty(firebase.Messages);
using var scope = app.Services.CreateScope();
Assert.Empty(await scope.ServiceProvider.GetRequiredService<IPushChannelRepository>().GetAllAsync());
}
}