using System; using Xunit; using Newtonsoft.Json.Linq; using FluentAssertions; namespace raven_integration { public class NotificationOps { private static long GetUserIdFromToken(string token) { var payloadB64 = token.Split('.')[1].Replace('-', '+').Replace('_', '/'); while (payloadB64.Length % 4 != 0) payloadB64 += "="; var json = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(payloadB64)); return JObject.Parse(json)["id"].Value(); } /// /// Send a direct-message notification to self, verify it appears in the app-notification /// list, then delete it and confirm the count endpoint responds throughout. /// [Fact(Skip ="Works once every 12 hours due to bug at server case ")] public async Task DirectMessage_SendAndDelete() { var token = await Util.GetTokenAsync("BizAdmin"); var userId = GetUserIdFromToken(token); // GET new-count (baseline — count may be any value) ApiResponse a = await Util.GetAsync("notify/new-count", token); Util.ValidateDataReturnResponseOk(a); // POST direct-message to self var messageText = Util.Uniquify("Integration test notification"); var payload = $$""" {"message":"{{messageText}}","users":[{{userId}}]} """; a = await Util.PostAsync("notify/direct-message", token, payload); Util.ValidateHTTPStatusCode(a, 204); // 204 No Content — server sends but returns no body // Poll app-notifications until the message appears. // The server processes notify events on a background timer (20 s in debug, // 60 s in release), so the message may not be visible immediately. JToken matchedNotification = null; var deadline = DateTime.UtcNow.AddSeconds(65); while (DateTime.UtcNow < deadline) { a = await Util.GetAsync("notify/app-notifications", token); Util.ValidateDataReturnResponseOk(a); var notifications = (JArray)a.ObjectResponse["data"]; notifications.Should().NotBeNull("a JArray of notifications should be returned"); foreach (var notification in notifications) { var body = notification["body"]?.ToString() ?? string.Empty; var message = notification["message"]?.ToString() ?? string.Empty; if (body.Contains(messageText) || message.Contains(messageText)) { matchedNotification = notification; break; } } if (matchedNotification != null) break; await Task.Delay(3000); } matchedNotification.Should().NotBeNull("the direct message just sent should appear in app-notifications within 65 seconds"); long notificationId = matchedNotification["id"].Value(); // GET new-count again (just verify the endpoint responds) a = await Util.GetAsync("notify/new-count", token); Util.ValidateDataReturnResponseOk(a); // DELETE the notification a = await Util.DeleteAsync($"notify/{notificationId}", token); Util.ValidateHTTPStatusCode(a, 204); } }//eoc }//eons