Skip to content

[PM-18555] Notifications service tests #5473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 14, 2025
Original file line number Diff line number Diff line change
@@ -32,20 +32,22 @@ public class NotificationHubPushNotificationService : IPushNotificationService
private readonly INotificationHubPool _notificationHubPool;
private readonly ILogger _logger;
private readonly IGlobalSettings _globalSettings;
private readonly TimeProvider _timeProvider;

public NotificationHubPushNotificationService(
IInstallationDeviceRepository installationDeviceRepository,
INotificationHubPool notificationHubPool,
IHttpContextAccessor httpContextAccessor,
ILogger<NotificationHubPushNotificationService> logger,
IGlobalSettings globalSettings)
IGlobalSettings globalSettings,
TimeProvider timeProvider)
{
_installationDeviceRepository = installationDeviceRepository;
_httpContextAccessor = httpContextAccessor;
_notificationHubPool = notificationHubPool;
_logger = logger;
_globalSettings = globalSettings;

_timeProvider = timeProvider;
if (globalSettings.Installation.Id == Guid.Empty)
{
logger.LogWarning("Installation ID is not set. Push notifications for installations will not work.");
@@ -152,7 +154,7 @@ public async Task PushLogOutAsync(Guid userId, bool excludeCurrentContext = fals

private async Task PushUserAsync(Guid userId, PushType type, bool excludeCurrentContext = false)
{
var message = new UserPushNotification { UserId = userId, Date = DateTime.UtcNow };
var message = new UserPushNotification { UserId = userId, Date = _timeProvider.GetUtcNow().UtcDateTime };

await SendPayloadToUserAsync(userId, type, message, excludeCurrentContext);
}
Original file line number Diff line number Diff line change
@@ -22,17 +22,19 @@ public class AzureQueuePushNotificationService : IPushNotificationService
private readonly QueueClient _queueClient;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IGlobalSettings _globalSettings;
private readonly TimeProvider _timeProvider;

public AzureQueuePushNotificationService(
[FromKeyedServices("notifications")] QueueClient queueClient,
IHttpContextAccessor httpContextAccessor,
IGlobalSettings globalSettings,
ILogger<AzureQueuePushNotificationService> logger)
ILogger<AzureQueuePushNotificationService> logger,
TimeProvider timeProvider)
{
_queueClient = queueClient;
_httpContextAccessor = httpContextAccessor;
_globalSettings = globalSettings;

_timeProvider = timeProvider;
if (globalSettings.Installation.Id == Guid.Empty)
{
logger.LogWarning("Installation ID is not set. Push notifications for installations will not work.");
@@ -140,7 +142,7 @@ public async Task PushLogOutAsync(Guid userId, bool excludeCurrentContext = fals

private async Task PushUserAsync(Guid userId, PushType type, bool excludeCurrentContext = false)
{
var message = new UserPushNotification { UserId = userId, Date = DateTime.UtcNow };
var message = new UserPushNotification { UserId = userId, Date = _timeProvider.GetUtcNow().UtcDateTime };

await SendMessageAsync(type, message, excludeCurrentContext);
}
Original file line number Diff line number Diff line change
@@ -24,12 +24,14 @@ public class NotificationsApiPushNotificationService : BaseIdentityClientService
{
private readonly IGlobalSettings _globalSettings;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly TimeProvider _timeProvider;

public NotificationsApiPushNotificationService(
IHttpClientFactory httpFactory,
GlobalSettings globalSettings,
IHttpContextAccessor httpContextAccessor,
ILogger<NotificationsApiPushNotificationService> logger)
ILogger<NotificationsApiPushNotificationService> logger,
TimeProvider timeProvider)
: base(
httpFactory,
globalSettings.BaseServiceUri.InternalNotifications,
@@ -41,6 +43,7 @@ public NotificationsApiPushNotificationService(
{
_globalSettings = globalSettings;
_httpContextAccessor = httpContextAccessor;
_timeProvider = timeProvider;
}

public async Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
@@ -148,7 +151,7 @@ private async Task PushUserAsync(Guid userId, PushType type, bool excludeCurrent
var message = new UserPushNotification
{
UserId = userId,
Date = DateTime.UtcNow
Date = _timeProvider.GetUtcNow().UtcDateTime,
};

await SendMessageAsync(type, message, excludeCurrentContext);
Original file line number Diff line number Diff line change
@@ -27,13 +27,15 @@ public class RelayPushNotificationService : BaseIdentityClientService, IPushNoti
private readonly IDeviceRepository _deviceRepository;
private readonly IGlobalSettings _globalSettings;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly TimeProvider _timeProvider;

public RelayPushNotificationService(
IHttpClientFactory httpFactory,
IDeviceRepository deviceRepository,
GlobalSettings globalSettings,
IHttpContextAccessor httpContextAccessor,
ILogger<RelayPushNotificationService> logger)
ILogger<RelayPushNotificationService> logger,
TimeProvider timeProvider)
: base(
httpFactory,
globalSettings.PushRelayBaseUri,
@@ -46,6 +48,7 @@ public RelayPushNotificationService(
_deviceRepository = deviceRepository;
_globalSettings = globalSettings;
_httpContextAccessor = httpContextAccessor;
_timeProvider = timeProvider;
}

public async Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
@@ -147,7 +150,7 @@ public async Task PushLogOutAsync(Guid userId, bool excludeCurrentContext = fals

private async Task PushUserAsync(Guid userId, PushType type, bool excludeCurrentContext = false)
{
var message = new UserPushNotification { UserId = userId, Date = DateTime.UtcNow };
var message = new UserPushNotification { UserId = userId, Date = _timeProvider.GetUtcNow().UtcDateTime };

await SendPayloadToUserAsync(userId, type, message, excludeCurrentContext);
}
3 changes: 3 additions & 0 deletions src/SharedWeb/Utilities/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@@ -279,6 +280,8 @@ public static void AddDefaultServices(this IServiceCollection services, GlobalSe
services.AddSingleton<IMailDeliveryService, NoopMailDeliveryService>();
}

services.TryAddSingleton(TimeProvider.System);

services.AddSingleton<IPushNotificationService, MultiServicePushNotificationService>();
if (globalSettings.SelfHosted)
{

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

498 changes: 498 additions & 0 deletions test/Core.Test/Platform/Push/Services/PushTestBase.cs

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading