#nullable enable using System.Text.Json; using Azure.Storage.Queues; using Bit.Core.Context; using Bit.Core.Enums; using Bit.Core.Models; using Bit.Core.NotificationCenter.Entities; using Bit.Core.Services; using Bit.Core.Test.AutoFixture; using Bit.Core.Test.AutoFixture.CurrentContextFixtures; using Bit.Core.Test.NotificationCenter.AutoFixture; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using Microsoft.AspNetCore.Http; using NSubstitute; using Xunit; namespace Bit.Core.Test.Services; [QueueClientCustomize] [SutProviderCustomize] public class AzureQueuePushNotificationServiceTests { [Theory] [BitAutoData] [NotificationCustomize] [CurrentContextCustomize] public async Task PushSyncNotificationAsync_Notification_Sent( SutProvider sutProvider, Notification notification, Guid deviceIdentifier, ICurrentContext currentContext) { currentContext.DeviceIdentifier.Returns(deviceIdentifier.ToString()); sutProvider.GetDependency().HttpContext!.RequestServices .GetService(Arg.Any()).Returns(currentContext); await sutProvider.Sut.PushSyncNotificationAsync(notification); await sutProvider.GetDependency().Received(1) .SendMessageAsync(Arg.Is(message => MatchMessage(PushType.SyncNotification, message, new SyncNotificationEquals(notification, null), deviceIdentifier.ToString()))); } [Theory] [BitAutoData] [NotificationCustomize] [NotificationStatusCustomize] [CurrentContextCustomize] public async Task PushSyncNotificationStatusAsync_Notification_Sent( SutProvider sutProvider, Notification notification, Guid deviceIdentifier, ICurrentContext currentContext, NotificationStatus notificationStatus) { currentContext.DeviceIdentifier.Returns(deviceIdentifier.ToString()); sutProvider.GetDependency().HttpContext!.RequestServices .GetService(Arg.Any()).Returns(currentContext); await sutProvider.Sut.PushSyncNotificationStatusAsync(notification, notificationStatus); await sutProvider.GetDependency().Received(1) .SendMessageAsync(Arg.Is(message => MatchMessage(PushType.SyncNotificationStatus, message, new SyncNotificationEquals(notification, notificationStatus), deviceIdentifier.ToString()))); } private static bool MatchMessage(PushType pushType, string message, IEquatable expectedPayloadEquatable, string contextId) { var pushNotificationData = JsonSerializer.Deserialize>(message); return pushNotificationData != null && pushNotificationData.Type == pushType && expectedPayloadEquatable.Equals(pushNotificationData.Payload) && pushNotificationData.ContextId == contextId; } private class SyncNotificationEquals(Notification notification, NotificationStatus? notificationStatus) : IEquatable { public bool Equals(SyncNotificationPushNotification? other) { return other != null && other.Id == notification.Id && other.UserId == notification.UserId && other.OrganizationId == notification.OrganizationId && other.ClientType == notification.ClientType && other.RevisionDate == notification.RevisionDate && other.ReadDate == notificationStatus?.ReadDate && other.DeletedDate == notificationStatus?.DeletedDate; } } }