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 { [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(); services.AddSingleton(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(); 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().GetAllAsync()); } }