using System.Text.Json; using NTP.Application.Grains.Permits; namespace NTP.Tests; public class OneSignalPushTests { private static readonly OneSignalOptions Options = new() { AppId = "app-1", ApiKey = "secret", AndroidChannelId = "chan-1" }; [Fact] public void Android_body_has_the_channel_and_the_template_id() { using var doc = JsonDocument.Parse(NotificationRules.OneSignalBody(Options, "player-1", " عنوان ", " نص ", "ANDROID", 7)); var r = doc.RootElement; Assert.Equal("app-1", r.GetProperty("app_id").GetString()); Assert.Equal("player-1", r.GetProperty("include_player_ids")[0].GetString()); Assert.Equal("chan-1", r.GetProperty("android_channel_id").GetString()); Assert.Equal("عنوان", r.GetProperty("headings").GetProperty("en").GetString()); // trimmed, real Arabic (not \u escapes) Assert.Equal("نص", r.GetProperty("contents").GetProperty("en").GetString()); Assert.Equal("7", r.GetProperty("data").GetProperty("MSG_TEMPLATE_ID").GetString()); Assert.False(r.TryGetProperty("isIos", out _)); Assert.DoesNotContain("secret", r.GetRawText()); // the API key is never in the body } [Theory] [InlineData("IOS")] [InlineData("darwin")] public void Every_other_os_is_sent_as_ios_with_the_app_sound(string os) { using var doc = JsonDocument.Parse(NotificationRules.OneSignalBody(Options, "p", "t", "m", os, 1)); Assert.True(doc.RootElement.GetProperty("isIos").GetBoolean()); Assert.Equal("nafith_notification_sound.caf", doc.RootElement.GetProperty("ios_sound").GetString()); Assert.False(doc.RootElement.TryGetProperty("android_channel_id", out _)); } [Theory] [InlineData("{\"id\":\"abc\",\"recipients\":1}", true)] [InlineData("{\"errors\":[\"All included players are not subscribed\"]}", false)] [InlineData("{\"id\":\"\",\"errors\":[\"x\"]}", false)] [InlineData("", false)] [InlineData("bad gateway", false)] public void Answer_with_an_errors_key_or_not_json_is_a_failure(string answer, bool ok) => Assert.Equal(ok, NotificationRules.OneSignalSucceeded(answer)); [Fact] public void Push_needs_both_app_id_and_api_key() { Assert.True(Options.IsConfigured); Assert.False(new OneSignalOptions { AppId = "a" }.IsConfigured); Assert.False(new OneSignalOptions { ApiKey = "k" }.IsConfigured); } }