mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
PM-10600: Unit Test coverage for NotificationHubPushRegistrationService
Fixed IFeatureService substitute mocking for Android tests. Added user part of organization test with organizationId tags expectation.
This commit is contained in:
@ -1,11 +1,14 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
||||||
using Bit.Core.NotificationHub;
|
using Bit.Core.NotificationHub;
|
||||||
|
using Bit.Core.Repositories;
|
||||||
using Bit.Core.Services;
|
using Bit.Core.Services;
|
||||||
using Bit.Core.Utilities;
|
using Bit.Core.Utilities;
|
||||||
using Bit.Test.Common.AutoFixture;
|
using Bit.Test.Common.AutoFixture;
|
||||||
using Bit.Test.Common.AutoFixture.Attributes;
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
using Microsoft.Azure.NotificationHubs;
|
using Microsoft.Azure.NotificationHubs;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@ -37,7 +40,14 @@ public class NotificationHubPushRegistrationServiceTests
|
|||||||
{
|
{
|
||||||
var featureService = Substitute.For<IFeatureService>();
|
var featureService = Substitute.For<IFeatureService>();
|
||||||
featureService.IsEnabled(FeatureFlagKeys.AnhFcmv1Migration).Returns(true);
|
featureService.IsEnabled(FeatureFlagKeys.AnhFcmv1Migration).Returns(true);
|
||||||
sutProvider.GetDependency<IServiceProvider>().GetService(typeof(IFeatureService)).Returns(featureService);
|
var serviceProvider = Substitute.For<IServiceProvider>();
|
||||||
|
serviceProvider.GetService(typeof(IFeatureService)).Returns(featureService);
|
||||||
|
var serviceScope = Substitute.For<IServiceScope>();
|
||||||
|
serviceScope.ServiceProvider.Returns(serviceProvider);
|
||||||
|
var serviceScopeFactory = Substitute.For<IServiceScopeFactory>();
|
||||||
|
serviceScopeFactory.CreateScope().Returns(serviceScope);
|
||||||
|
sutProvider.GetDependency<IServiceProvider>().GetService(typeof(IServiceScopeFactory))
|
||||||
|
.Returns(serviceScopeFactory);
|
||||||
var notificationHubClient = Substitute.For<INotificationHubClient>();
|
var notificationHubClient = Substitute.For<INotificationHubClient>();
|
||||||
sutProvider.GetDependency<INotificationHubPool>().ClientFor(Arg.Any<Guid>()).Returns(notificationHubClient);
|
sutProvider.GetDependency<INotificationHubPool>().ClientFor(Arg.Any<Guid>()).Returns(notificationHubClient);
|
||||||
|
|
||||||
@ -260,6 +270,38 @@ public class NotificationHubPushRegistrationServiceTests
|
|||||||
installation.Templates.Count == 0));
|
installation.Templates.Count == 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData]
|
||||||
|
public async void CreateOrUpdateRegistrationAsync_UserPartOfOrganization_InstallationCreated(
|
||||||
|
SutProvider<NotificationHubPushRegistrationService> sutProvider, Guid deviceId, Guid userId, Guid identifier,
|
||||||
|
OrganizationUserOrganizationDetails organizationDetails)
|
||||||
|
{
|
||||||
|
var notificationHubClient = Substitute.For<INotificationHubClient>();
|
||||||
|
sutProvider.GetDependency<INotificationHubPool>().ClientFor(Arg.Any<Guid>()).Returns(notificationHubClient);
|
||||||
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
||||||
|
.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<INotificationHubPool>()
|
||||||
|
.Received(1)
|
||||||
|
.ClientFor(deviceId);
|
||||||
|
await notificationHubClient
|
||||||
|
.Received(1)
|
||||||
|
.CreateOrUpdateInstallationAsync(Arg.Is<Installation>(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<string, InstallationTemplate> templates, string key,
|
private static bool MatchingInstallationTemplate(IDictionary<string, InstallationTemplate> templates, string key,
|
||||||
string body, List<string?> tags)
|
string body, List<string?> tags)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user