1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 23:52:50 -05:00

PM-10564: Notification create push notification simplification.

NotificationStatus not needed.
This commit is contained in:
Maciej Zieniuk
2024-11-21 22:43:14 +00:00
parent 01c814595e
commit 3885885d5f
12 changed files with 63 additions and 108 deletions

View File

@ -37,7 +37,7 @@ public class CreateNotificationCommand : ICreateNotificationCommand
var newNotification = await _notificationRepository.CreateAsync(notification); var newNotification = await _notificationRepository.CreateAsync(notification);
await _pushNotificationService.PushSyncNotificationCreateAsync(newNotification, null); await _pushNotificationService.PushSyncNotificationCreateAsync(newNotification);
return newNotification; return newNotification;
} }

View File

@ -182,7 +182,7 @@ public class NotificationHubPushNotificationService : IPushNotificationService
await PushAuthRequestAsync(authRequest, PushType.AuthRequestResponse); await PushAuthRequestAsync(authRequest, PushType.AuthRequestResponse);
} }
public async Task PushSyncNotificationCreateAsync(Notification notification, NotificationStatus? notificationStatus) public async Task PushSyncNotificationCreateAsync(Notification notification)
{ {
var message = new SyncNotificationPushNotification var message = new SyncNotificationPushNotification
{ {
@ -190,9 +190,7 @@ public class NotificationHubPushNotificationService : IPushNotificationService
UserId = notification.UserId, UserId = notification.UserId,
OrganizationId = notification.OrganizationId, OrganizationId = notification.OrganizationId,
ClientType = notification.ClientType, ClientType = notification.ClientType,
RevisionDate = notification.RevisionDate, RevisionDate = notification.RevisionDate
ReadDate = notificationStatus?.ReadDate,
DeletedDate = notificationStatus?.DeletedDate
}; };
if (notification.UserId.HasValue) if (notification.UserId.HasValue)

View File

@ -24,7 +24,7 @@ public interface IPushNotificationService
Task PushSyncSendCreateAsync(Send send); Task PushSyncSendCreateAsync(Send send);
Task PushSyncSendUpdateAsync(Send send); Task PushSyncSendUpdateAsync(Send send);
Task PushSyncSendDeleteAsync(Send send); Task PushSyncSendDeleteAsync(Send send);
Task PushSyncNotificationCreateAsync(Notification notification, NotificationStatus? notificationStatus); Task PushSyncNotificationCreateAsync(Notification notification);
Task PushSyncNotificationUpdateAsync(Notification notification, NotificationStatus? notificationStatus); Task PushSyncNotificationUpdateAsync(Notification notification, NotificationStatus? notificationStatus);
Task PushAuthRequestAsync(AuthRequest authRequest); Task PushAuthRequestAsync(AuthRequest authRequest);
Task PushAuthRequestResponseAsync(AuthRequest authRequest); Task PushAuthRequestResponseAsync(AuthRequest authRequest);

View File

@ -165,7 +165,7 @@ public class AzureQueuePushNotificationService : IPushNotificationService
await PushSendAsync(send, PushType.SyncSendDelete); await PushSendAsync(send, PushType.SyncSendDelete);
} }
public async Task PushSyncNotificationCreateAsync(Notification notification, NotificationStatus? notificationStatus) public async Task PushSyncNotificationCreateAsync(Notification notification)
{ {
var message = new SyncNotificationPushNotification var message = new SyncNotificationPushNotification
{ {
@ -173,9 +173,7 @@ public class AzureQueuePushNotificationService : IPushNotificationService
UserId = notification.UserId, UserId = notification.UserId,
OrganizationId = notification.OrganizationId, OrganizationId = notification.OrganizationId,
ClientType = notification.ClientType, ClientType = notification.ClientType,
RevisionDate = notification.RevisionDate, RevisionDate = notification.RevisionDate
ReadDate = notificationStatus?.ReadDate,
DeletedDate = notificationStatus?.DeletedDate
}; };
await SendMessageAsync(PushType.SyncNotificationCreate, message, true); await SendMessageAsync(PushType.SyncNotificationCreate, message, true);

View File

@ -146,9 +146,9 @@ public class MultiServicePushNotificationService : IPushNotificationService
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task PushSyncNotificationCreateAsync(Notification notification, NotificationStatus? notificationStatus) public Task PushSyncNotificationCreateAsync(Notification notification)
{ {
PushToServices((s) => s.PushSyncNotificationCreateAsync(notification, notificationStatus)); PushToServices((s) => s.PushSyncNotificationCreateAsync(notification));
return Task.CompletedTask; return Task.CompletedTask;
} }

View File

@ -172,7 +172,7 @@ public class NotificationsApiPushNotificationService : BaseIdentityClientService
await PushSendAsync(send, PushType.SyncSendDelete); await PushSendAsync(send, PushType.SyncSendDelete);
} }
public async Task PushSyncNotificationCreateAsync(Notification notification, NotificationStatus? notificationStatus) public async Task PushSyncNotificationCreateAsync(Notification notification)
{ {
var message = new SyncNotificationPushNotification var message = new SyncNotificationPushNotification
{ {
@ -180,9 +180,7 @@ public class NotificationsApiPushNotificationService : BaseIdentityClientService
UserId = notification.UserId, UserId = notification.UserId,
OrganizationId = notification.OrganizationId, OrganizationId = notification.OrganizationId,
ClientType = notification.ClientType, ClientType = notification.ClientType,
RevisionDate = notification.RevisionDate, RevisionDate = notification.RevisionDate
ReadDate = notificationStatus?.ReadDate,
DeletedDate = notificationStatus?.DeletedDate
}; };
await SendMessageAsync(PushType.SyncNotificationCreate, message, true); await SendMessageAsync(PushType.SyncNotificationCreate, message, true);

View File

@ -190,7 +190,7 @@ public class RelayPushNotificationService : BaseIdentityClientService, IPushNoti
await SendPayloadToUserAsync(authRequest.UserId, type, message, true); await SendPayloadToUserAsync(authRequest.UserId, type, message, true);
} }
public async Task PushSyncNotificationCreateAsync(Notification notification, NotificationStatus? notificationStatus) public async Task PushSyncNotificationCreateAsync(Notification notification)
{ {
var message = new SyncNotificationPushNotification var message = new SyncNotificationPushNotification
{ {
@ -198,9 +198,7 @@ public class RelayPushNotificationService : BaseIdentityClientService, IPushNoti
UserId = notification.UserId, UserId = notification.UserId,
OrganizationId = notification.OrganizationId, OrganizationId = notification.OrganizationId,
ClientType = notification.ClientType, ClientType = notification.ClientType,
RevisionDate = notification.RevisionDate, RevisionDate = notification.RevisionDate
ReadDate = notificationStatus?.ReadDate,
DeletedDate = notificationStatus?.DeletedDate
}; };
if (notification.UserId.HasValue) if (notification.UserId.HasValue)

View File

@ -106,7 +106,7 @@ public class NoopPushNotificationService : IPushNotificationService
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task PushSyncNotificationCreateAsync(Notification notification, NotificationStatus? notificationStatus) => Task.CompletedTask; public Task PushSyncNotificationCreateAsync(Notification notification) => Task.CompletedTask;
public Task PushSyncNotificationUpdateAsync(Notification notification, NotificationStatus? notificationStatus) => Task.CompletedTask; public Task PushSyncNotificationUpdateAsync(Notification notification, NotificationStatus? notificationStatus) => Task.CompletedTask;
} }

View File

@ -43,7 +43,7 @@ public class CreateNotificationCommandTest
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.CreateAsync(notification)); await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.CreateAsync(notification));
await sutProvider.GetDependency<IPushNotificationService>() await sutProvider.GetDependency<IPushNotificationService>()
.Received(0) .Received(0)
.PushSyncNotificationCreateAsync(Arg.Any<Notification>(), Arg.Any<NotificationStatus?>()); .PushSyncNotificationCreateAsync(Arg.Any<Notification>());
} }
[Theory] [Theory]
@ -61,6 +61,6 @@ public class CreateNotificationCommandTest
Assert.Equal(notification.CreationDate, notification.RevisionDate); Assert.Equal(notification.CreationDate, notification.RevisionDate);
await sutProvider.GetDependency<IPushNotificationService>() await sutProvider.GetDependency<IPushNotificationService>()
.Received(1) .Received(1)
.PushSyncNotificationCreateAsync(newNotification, null); .PushSyncNotificationCreateAsync(newNotification);
} }
} }

View File

@ -19,15 +19,12 @@ namespace Bit.Core.Test.NotificationHub;
public class NotificationHubPushNotificationServiceTests public class NotificationHubPushNotificationServiceTests
{ {
[Theory] [Theory]
[BitAutoData(false)] [BitAutoData]
[BitAutoData(true)]
[NotificationCustomize] [NotificationCustomize]
public async Task PushSyncNotificationCreateAsync_Global_NotSent(bool notificationStatusNull, public async Task PushSyncNotificationCreateAsync_Global_NotSent(
SutProvider<NotificationHubPushNotificationService> sutProvider, Notification notification, SutProvider<NotificationHubPushNotificationService> sutProvider, Notification notification)
NotificationStatus notificationStatus)
{ {
await sutProvider.Sut.PushSyncNotificationCreateAsync(notification, await sutProvider.Sut.PushSyncNotificationCreateAsync(notification);
notificationStatusNull ? null : notificationStatus);
await sutProvider.GetDependency<INotificationHubPool>() await sutProvider.GetDependency<INotificationHubPool>()
.Received(0) .Received(0)
@ -40,15 +37,12 @@ public class NotificationHubPushNotificationServiceTests
} }
[Theory] [Theory]
[BitAutoData(false, false)] [BitAutoData(false)]
[BitAutoData(false, true)] [BitAutoData(true)]
[BitAutoData(true, false)]
[BitAutoData(true, true)]
[NotificationCustomize(false)] [NotificationCustomize(false)]
public async Task PushSyncNotificationCreateAsync_UserIdProvidedClientTypeAll_SentToUser( public async Task PushSyncNotificationCreateAsync_UserIdProvidedClientTypeAll_SentToUser(
bool organizationIdNull, bool notificationStatusNull, bool organizationIdNull, SutProvider<NotificationHubPushNotificationService> sutProvider,
SutProvider<NotificationHubPushNotificationService> sutProvider, Notification notification)
Notification notification, NotificationStatus notificationStatus)
{ {
if (organizationIdNull) if (organizationIdNull)
{ {
@ -56,10 +50,9 @@ public class NotificationHubPushNotificationServiceTests
} }
notification.ClientType = ClientType.All; notification.ClientType = ClientType.All;
var expectedNotificationStatus = notificationStatusNull ? null : notificationStatus; var expectedSyncNotification = ToSyncNotificationPushNotification(notification, null);
var expectedSyncNotification = ToSyncNotificationPushNotification(notification, expectedNotificationStatus);
await sutProvider.Sut.PushSyncNotificationCreateAsync(notification, expectedNotificationStatus); await sutProvider.Sut.PushSyncNotificationCreateAsync(notification);
await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate, await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate,
expectedSyncNotification, expectedSyncNotification,
@ -70,26 +63,20 @@ public class NotificationHubPushNotificationServiceTests
} }
[Theory] [Theory]
[BitAutoData(false, ClientType.Browser)] [BitAutoData(ClientType.Browser)]
[BitAutoData(false, ClientType.Desktop)] [BitAutoData(ClientType.Desktop)]
[BitAutoData(false, ClientType.Web)] [BitAutoData(ClientType.Web)]
[BitAutoData(false, ClientType.Mobile)] [BitAutoData(ClientType.Mobile)]
[BitAutoData(true, ClientType.Browser)]
[BitAutoData(true, ClientType.Desktop)]
[BitAutoData(true, ClientType.Web)]
[BitAutoData(true, ClientType.Mobile)]
[NotificationCustomize(false)] [NotificationCustomize(false)]
public async Task PushSyncNotificationCreateAsync_UserIdProvidedOrganizationIdNullClientTypeNotAll_SentToUser( public async Task PushSyncNotificationCreateAsync_UserIdProvidedOrganizationIdNullClientTypeNotAll_SentToUser(
bool notificationStatusNull, ClientType clientType, ClientType clientType, SutProvider<NotificationHubPushNotificationService> sutProvider,
SutProvider<NotificationHubPushNotificationService> sutProvider, Notification notification, Notification notification)
NotificationStatus notificationStatus)
{ {
notification.OrganizationId = null; notification.OrganizationId = null;
notification.ClientType = clientType; notification.ClientType = clientType;
var expectedNotificationStatus = notificationStatusNull ? null : notificationStatus; var expectedSyncNotification = ToSyncNotificationPushNotification(notification, null);
var expectedSyncNotification = ToSyncNotificationPushNotification(notification, expectedNotificationStatus);
await sutProvider.Sut.PushSyncNotificationCreateAsync(notification, expectedNotificationStatus); await sutProvider.Sut.PushSyncNotificationCreateAsync(notification);
await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate, await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate,
expectedSyncNotification, expectedSyncNotification,
@ -100,25 +87,19 @@ public class NotificationHubPushNotificationServiceTests
} }
[Theory] [Theory]
[BitAutoData(false, ClientType.Browser)] [BitAutoData(ClientType.Browser)]
[BitAutoData(false, ClientType.Desktop)] [BitAutoData(ClientType.Desktop)]
[BitAutoData(false, ClientType.Web)] [BitAutoData(ClientType.Web)]
[BitAutoData(false, ClientType.Mobile)] [BitAutoData(ClientType.Mobile)]
[BitAutoData(true, ClientType.Browser)]
[BitAutoData(true, ClientType.Desktop)]
[BitAutoData(true, ClientType.Web)]
[BitAutoData(true, ClientType.Mobile)]
[NotificationCustomize(false)] [NotificationCustomize(false)]
public async Task PushSyncNotificationCreateAsync_UserIdProvidedOrganizationIdProvidedClientTypeNotAll_SentToUser( public async Task PushSyncNotificationCreateAsync_UserIdProvidedOrganizationIdProvidedClientTypeNotAll_SentToUser(
bool notificationStatusNull, ClientType clientType, ClientType clientType, SutProvider<NotificationHubPushNotificationService> sutProvider,
SutProvider<NotificationHubPushNotificationService> sutProvider, Notification notification)
Notification notification, NotificationStatus notificationStatus)
{ {
notification.ClientType = clientType; notification.ClientType = clientType;
var expectedNotificationStatus = notificationStatusNull ? null : notificationStatus; var expectedSyncNotification = ToSyncNotificationPushNotification(notification, null);
var expectedSyncNotification = ToSyncNotificationPushNotification(notification, expectedNotificationStatus);
await sutProvider.Sut.PushSyncNotificationCreateAsync(notification, expectedNotificationStatus); await sutProvider.Sut.PushSyncNotificationCreateAsync(notification);
await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate, await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate,
expectedSyncNotification, expectedSyncNotification,
@ -129,19 +110,16 @@ public class NotificationHubPushNotificationServiceTests
} }
[Theory] [Theory]
[BitAutoData(false)] [BitAutoData]
[BitAutoData(true)]
[NotificationCustomize(false)] [NotificationCustomize(false)]
public async Task PushSyncNotificationCreateAsync_UserIdNullOrganizationIdProvidedClientTypeAll_SentToOrganization( public async Task PushSyncNotificationCreateAsync_UserIdNullOrganizationIdProvidedClientTypeAll_SentToOrganization(
bool notificationStatusNull, SutProvider<NotificationHubPushNotificationService> sutProvider, SutProvider<NotificationHubPushNotificationService> sutProvider, Notification notification)
Notification notification, NotificationStatus notificationStatus)
{ {
notification.UserId = null; notification.UserId = null;
notification.ClientType = ClientType.All; notification.ClientType = ClientType.All;
var expectedNotificationStatus = notificationStatusNull ? null : notificationStatus; var expectedSyncNotification = ToSyncNotificationPushNotification(notification, null);
var expectedSyncNotification = ToSyncNotificationPushNotification(notification, expectedNotificationStatus);
await sutProvider.Sut.PushSyncNotificationCreateAsync(notification, expectedNotificationStatus); await sutProvider.Sut.PushSyncNotificationCreateAsync(notification);
await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate, await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate,
expectedSyncNotification, expectedSyncNotification,
@ -152,27 +130,21 @@ public class NotificationHubPushNotificationServiceTests
} }
[Theory] [Theory]
[BitAutoData(false, ClientType.Browser)] [BitAutoData(ClientType.Browser)]
[BitAutoData(false, ClientType.Desktop)] [BitAutoData(ClientType.Desktop)]
[BitAutoData(false, ClientType.Web)] [BitAutoData(ClientType.Web)]
[BitAutoData(false, ClientType.Mobile)] [BitAutoData(ClientType.Mobile)]
[BitAutoData(true, ClientType.Browser)]
[BitAutoData(true, ClientType.Desktop)]
[BitAutoData(true, ClientType.Web)]
[BitAutoData(true, ClientType.Mobile)]
[NotificationCustomize(false)] [NotificationCustomize(false)]
public async Task public async Task
PushSyncNotificationCreateAsync_UserIdNullOrganizationIdProvidedClientTypeNotAll_SentToOrganization( PushSyncNotificationCreateAsync_UserIdNullOrganizationIdProvidedClientTypeNotAll_SentToOrganization(
bool notificationStatusNull, ClientType clientType, ClientType clientType, SutProvider<NotificationHubPushNotificationService> sutProvider,
SutProvider<NotificationHubPushNotificationService> sutProvider, Notification notification, Notification notification)
NotificationStatus notificationStatus)
{ {
notification.UserId = null; notification.UserId = null;
notification.ClientType = clientType; notification.ClientType = clientType;
var expectedNotificationStatus = notificationStatusNull ? null : notificationStatus; var expectedSyncNotification = ToSyncNotificationPushNotification(notification, null);
var expectedSyncNotification = ToSyncNotificationPushNotification(notification, expectedNotificationStatus);
await sutProvider.Sut.PushSyncNotificationCreateAsync(notification, expectedNotificationStatus); await sutProvider.Sut.PushSyncNotificationCreateAsync(notification);
await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate, await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotificationCreate,
expectedSyncNotification, expectedSyncNotification,

View File

@ -22,26 +22,22 @@ namespace Bit.Core.Test.Services;
public class AzureQueuePushNotificationServiceTests public class AzureQueuePushNotificationServiceTests
{ {
[Theory] [Theory]
[BitAutoData(false)] [BitAutoData]
[BitAutoData(true)]
[NotificationCustomize] [NotificationCustomize]
[NotificationStatusCustomize]
[CurrentContextCustomize] [CurrentContextCustomize]
public async Task PushSyncNotificationCreateAsync_Notification_Sent(bool notificationStatusNull, public async Task PushSyncNotificationCreateAsync_Notification_Sent(
SutProvider<AzureQueuePushNotificationService> sutProvider, Notification notification, Guid deviceIdentifier, SutProvider<AzureQueuePushNotificationService> sutProvider, Notification notification, Guid deviceIdentifier,
ICurrentContext currentContext, NotificationStatus notificationStatus) ICurrentContext currentContext)
{ {
var expectedNotificationStatus = notificationStatusNull ? null : notificationStatus;
currentContext.DeviceIdentifier.Returns(deviceIdentifier.ToString()); currentContext.DeviceIdentifier.Returns(deviceIdentifier.ToString());
sutProvider.GetDependency<IHttpContextAccessor>().HttpContext!.RequestServices sutProvider.GetDependency<IHttpContextAccessor>().HttpContext!.RequestServices
.GetService(Arg.Any<Type>()).Returns(currentContext); .GetService(Arg.Any<Type>()).Returns(currentContext);
await sutProvider.Sut.PushSyncNotificationCreateAsync(notification, expectedNotificationStatus); await sutProvider.Sut.PushSyncNotificationCreateAsync(notification);
await sutProvider.GetDependency<QueueClient>().Received(1) await sutProvider.GetDependency<QueueClient>().Received(1)
.SendMessageAsync(Arg.Is<string>(message => .SendMessageAsync(Arg.Is<string>(message =>
MatchMessage(PushType.SyncNotificationCreate, message, MatchMessage(PushType.SyncNotificationCreate, message, new SyncNotificationEquals(notification, null),
new SyncNotificationEquals(notification, expectedNotificationStatus),
deviceIdentifier.ToString()))); deviceIdentifier.ToString())));
} }

View File

@ -14,22 +14,17 @@ namespace Bit.Core.Test.Services;
public class MultiServicePushNotificationServiceTests public class MultiServicePushNotificationServiceTests
{ {
[Theory] [Theory]
[BitAutoData(false)] [BitAutoData]
[BitAutoData(true)]
[NotificationCustomize] [NotificationCustomize]
[NotificationStatusCustomize] public async Task PushSyncNotificationCreateAsync_Notification_Sent(
public async Task PushSyncNotificationCreateAsync_Notification_Sent(bool notificationStatusNull, SutProvider<MultiServicePushNotificationService> sutProvider, Notification notification)
SutProvider<MultiServicePushNotificationService> sutProvider, Notification notification,
NotificationStatus notificationStatus)
{ {
await sutProvider.Sut.PushSyncNotificationCreateAsync(notification, await sutProvider.Sut.PushSyncNotificationCreateAsync(notification);
notificationStatusNull ? null : notificationStatus);
var expectedNotificationStatus = notificationStatusNull ? null : notificationStatus;
await sutProvider.GetDependency<IEnumerable<IPushNotificationService>>() await sutProvider.GetDependency<IEnumerable<IPushNotificationService>>()
.First() .First()
.Received(1) .Received(1)
.PushSyncNotificationCreateAsync(notification, expectedNotificationStatus); .PushSyncNotificationCreateAsync(notification);
} }
[Theory] [Theory]