Skip to content

Commit 4d6e4d3

Browse files
authored
[PM-18555] Notifications service tests (#5473)
* Add RelayPush Notifications Tests * Nullable Test Fixup * Azure Queue Notifications Tests * NotificationsHub Push Tests * Make common base for API based notifications * Register TimeProvider just in case * Format * React to TaskId * Remove completed TODO
1 parent c986cbb commit 4d6e4d3

10 files changed

+2826
-62
lines changed

src/Core/NotificationHub/NotificationHubPushNotificationService.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,22 @@ public class NotificationHubPushNotificationService : IPushNotificationService
3232
private readonly INotificationHubPool _notificationHubPool;
3333
private readonly ILogger _logger;
3434
private readonly IGlobalSettings _globalSettings;
35+
private readonly TimeProvider _timeProvider;
3536

3637
public NotificationHubPushNotificationService(
3738
IInstallationDeviceRepository installationDeviceRepository,
3839
INotificationHubPool notificationHubPool,
3940
IHttpContextAccessor httpContextAccessor,
4041
ILogger<NotificationHubPushNotificationService> logger,
41-
IGlobalSettings globalSettings)
42+
IGlobalSettings globalSettings,
43+
TimeProvider timeProvider)
4244
{
4345
_installationDeviceRepository = installationDeviceRepository;
4446
_httpContextAccessor = httpContextAccessor;
4547
_notificationHubPool = notificationHubPool;
4648
_logger = logger;
4749
_globalSettings = globalSettings;
48-
50+
_timeProvider = timeProvider;
4951
if (globalSettings.Installation.Id == Guid.Empty)
5052
{
5153
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
152154

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

157159
await SendPayloadToUserAsync(userId, type, message, excludeCurrentContext);
158160
}

src/Core/Platform/Push/Services/AzureQueuePushNotificationService.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ public class AzureQueuePushNotificationService : IPushNotificationService
2222
private readonly QueueClient _queueClient;
2323
private readonly IHttpContextAccessor _httpContextAccessor;
2424
private readonly IGlobalSettings _globalSettings;
25+
private readonly TimeProvider _timeProvider;
2526

2627
public AzureQueuePushNotificationService(
2728
[FromKeyedServices("notifications")] QueueClient queueClient,
2829
IHttpContextAccessor httpContextAccessor,
2930
IGlobalSettings globalSettings,
30-
ILogger<AzureQueuePushNotificationService> logger)
31+
ILogger<AzureQueuePushNotificationService> logger,
32+
TimeProvider timeProvider)
3133
{
3234
_queueClient = queueClient;
3335
_httpContextAccessor = httpContextAccessor;
3436
_globalSettings = globalSettings;
35-
37+
_timeProvider = timeProvider;
3638
if (globalSettings.Installation.Id == Guid.Empty)
3739
{
3840
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
140142

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

145147
await SendMessageAsync(type, message, excludeCurrentContext);
146148
}

src/Core/Platform/Push/Services/NotificationsApiPushNotificationService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ public class NotificationsApiPushNotificationService : BaseIdentityClientService
2424
{
2525
private readonly IGlobalSettings _globalSettings;
2626
private readonly IHttpContextAccessor _httpContextAccessor;
27+
private readonly TimeProvider _timeProvider;
2728

2829
public NotificationsApiPushNotificationService(
2930
IHttpClientFactory httpFactory,
3031
GlobalSettings globalSettings,
3132
IHttpContextAccessor httpContextAccessor,
32-
ILogger<NotificationsApiPushNotificationService> logger)
33+
ILogger<NotificationsApiPushNotificationService> logger,
34+
TimeProvider timeProvider)
3335
: base(
3436
httpFactory,
3537
globalSettings.BaseServiceUri.InternalNotifications,
@@ -41,6 +43,7 @@ public NotificationsApiPushNotificationService(
4143
{
4244
_globalSettings = globalSettings;
4345
_httpContextAccessor = httpContextAccessor;
46+
_timeProvider = timeProvider;
4447
}
4548

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

154157
await SendMessageAsync(type, message, excludeCurrentContext);

src/Core/Platform/Push/Services/RelayPushNotificationService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ public class RelayPushNotificationService : BaseIdentityClientService, IPushNoti
2727
private readonly IDeviceRepository _deviceRepository;
2828
private readonly IGlobalSettings _globalSettings;
2929
private readonly IHttpContextAccessor _httpContextAccessor;
30+
private readonly TimeProvider _timeProvider;
3031

3132
public RelayPushNotificationService(
3233
IHttpClientFactory httpFactory,
3334
IDeviceRepository deviceRepository,
3435
GlobalSettings globalSettings,
3536
IHttpContextAccessor httpContextAccessor,
36-
ILogger<RelayPushNotificationService> logger)
37+
ILogger<RelayPushNotificationService> logger,
38+
TimeProvider timeProvider)
3739
: base(
3840
httpFactory,
3941
globalSettings.PushRelayBaseUri,
@@ -46,6 +48,7 @@ public RelayPushNotificationService(
4648
_deviceRepository = deviceRepository;
4749
_globalSettings = globalSettings;
4850
_httpContextAccessor = httpContextAccessor;
51+
_timeProvider = timeProvider;
4952
}
5053

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

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

152155
await SendPayloadToUserAsync(userId, type, message, excludeCurrentContext);
153156
}

src/SharedWeb/Utilities/ServiceCollectionExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
using Microsoft.Extensions.Caching.Distributed;
6868
using Microsoft.Extensions.Configuration;
6969
using Microsoft.Extensions.DependencyInjection;
70+
using Microsoft.Extensions.DependencyInjection.Extensions;
7071
using Microsoft.Extensions.Hosting;
7172
using Microsoft.Extensions.Logging;
7273
using Microsoft.Extensions.Options;
@@ -279,6 +280,8 @@ public static void AddDefaultServices(this IServiceCollection services, GlobalSe
279280
services.AddSingleton<IMailDeliveryService, NoopMailDeliveryService>();
280281
}
281282

283+
services.TryAddSingleton(TimeProvider.System);
284+
282285
services.AddSingleton<IPushNotificationService, MultiServicePushNotificationService>();
283286
if (globalSettings.SelfHosted)
284287
{

0 commit comments

Comments
 (0)