1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00
Files
bitwarden/test/Core.Test/Services/AzureQueuePushNotificationServiceTests.cs
2024-10-23 14:15:10 +01:00

68 lines
2.6 KiB
C#

#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 void PushSyncNotificationAsync_Notification_Sent(
SutProvider<AzureQueuePushNotificationService> sutProvider, Notification notification, Guid deviceIdentifier,
ICurrentContext currentContext)
{
currentContext.DeviceIdentifier.Returns(deviceIdentifier.ToString());
sutProvider.GetDependency<IHttpContextAccessor>().HttpContext!.RequestServices
.GetService(Arg.Any<Type>()).Returns(currentContext);
await sutProvider.Sut.PushSyncNotificationAsync(notification);
await sutProvider.GetDependency<QueueClient>().Received(1)
.SendMessageAsync(Arg.Is<string>(message =>
MatchMessage(PushType.SyncNotification, message, new SyncNotificationEquals(notification),
deviceIdentifier.ToString())));
}
private static bool MatchMessage<T>(PushType pushType, string message, IEquatable<T> expectedPayloadEquatable,
string contextId)
{
var pushNotificationData =
JsonSerializer.Deserialize<PushNotificationData<T>>(message);
return pushNotificationData != null &&
pushNotificationData.Type == pushType &&
expectedPayloadEquatable.Equals(pushNotificationData.Payload) &&
pushNotificationData.ContextId == contextId;
}
private class SyncNotificationEquals(Notification notification) : IEquatable<SyncNotificationPushNotification>
{
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;
}
}
}