From 31316ee62f068fa54f434ff5f132cd259663247d Mon Sep 17 00:00:00 2001 From: Maciej Zieniuk Date: Tue, 5 Nov 2024 20:52:41 +0000 Subject: [PATCH] PM-10600: Unit Test coverage for NotificationHubPushRegistrationService Fixed IFeatureService substitute mocking for Android tests. Added user part of organization test with organizationId tags expectation. --- ...ficationHubPushRegistrationServiceTests.cs | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/test/Core.Test/NotificationHub/NotificationHubPushRegistrationServiceTests.cs b/test/Core.Test/NotificationHub/NotificationHubPushRegistrationServiceTests.cs index 69b953333c..7348d56f86 100644 --- a/test/Core.Test/NotificationHub/NotificationHubPushRegistrationServiceTests.cs +++ b/test/Core.Test/NotificationHub/NotificationHubPushRegistrationServiceTests.cs @@ -1,11 +1,14 @@ #nullable enable using Bit.Core.Enums; +using Bit.Core.Models.Data.Organizations.OrganizationUsers; using Bit.Core.NotificationHub; +using Bit.Core.Repositories; using Bit.Core.Services; using Bit.Core.Utilities; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using Microsoft.Azure.NotificationHubs; +using Microsoft.Extensions.DependencyInjection; using NSubstitute; using Xunit; @@ -37,7 +40,14 @@ public class NotificationHubPushRegistrationServiceTests { var featureService = Substitute.For(); featureService.IsEnabled(FeatureFlagKeys.AnhFcmv1Migration).Returns(true); - sutProvider.GetDependency().GetService(typeof(IFeatureService)).Returns(featureService); + var serviceProvider = Substitute.For(); + serviceProvider.GetService(typeof(IFeatureService)).Returns(featureService); + var serviceScope = Substitute.For(); + serviceScope.ServiceProvider.Returns(serviceProvider); + var serviceScopeFactory = Substitute.For(); + serviceScopeFactory.CreateScope().Returns(serviceScope); + sutProvider.GetDependency().GetService(typeof(IServiceScopeFactory)) + .Returns(serviceScopeFactory); var notificationHubClient = Substitute.For(); sutProvider.GetDependency().ClientFor(Arg.Any()).Returns(notificationHubClient); @@ -260,6 +270,38 @@ public class NotificationHubPushRegistrationServiceTests installation.Templates.Count == 0)); } + [Theory] + [BitAutoData] + public async void CreateOrUpdateRegistrationAsync_UserPartOfOrganization_InstallationCreated( + SutProvider sutProvider, Guid deviceId, Guid userId, Guid identifier, + OrganizationUserOrganizationDetails organizationDetails) + { + var notificationHubClient = Substitute.For(); + sutProvider.GetDependency().ClientFor(Arg.Any()).Returns(notificationHubClient); + sutProvider.GetDependency() + .GetManyDetailsByUserAsync(userId, OrganizationUserStatusType.Confirmed) + .Returns([organizationDetails]); + + var pushToken = "test push token"; + + await sutProvider.Sut.CreateOrUpdateRegistrationAsync(pushToken, deviceId.ToString(), userId.ToString(), + identifier.ToString(), DeviceType.ChromeBrowser); + + sutProvider.GetDependency() + .Received(1) + .ClientFor(deviceId); + await notificationHubClient + .Received(1) + .CreateOrUpdateInstallationAsync(Arg.Is(installation => + installation.InstallationId == deviceId.ToString() && + installation.PushChannel == pushToken && + installation.Tags.Count == 4 && + installation.Tags.Contains($"userId:{userId}") && + installation.Tags.Contains($"clientType:{DeviceTypes.ToClientType(DeviceType.ChromeBrowser)}") && + installation.Tags.Contains($"deviceIdentifier:{identifier}") && + installation.Tags.Contains($"organizationId:{organizationDetails.OrganizationId}"))); + } + private static bool MatchingInstallationTemplate(IDictionary templates, string key, string body, List tags) {