1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 07:36:14 -05:00

[PM-15084] Push global notification creation to affected clients (#5079)

* PM-10600: Notification push notification

* PM-10600: Sending to specific client types for relay push notifications

* PM-10600: Sending to specific client types for other clients

* PM-10600: Send push notification on notification creation

* PM-10600: Explicit group names

* PM-10600: Id typos

* PM-10600: Revert global push notifications

* PM-10600: Added DeviceType claim

* PM-10600: Sent to organization typo

* PM-10600: UT coverage

* PM-10600: Small refactor, UTs coverage

* PM-10600: UTs coverage

* PM-10600: Startup fix

* PM-10600: Test fix

* PM-10600: Required attribute, organization group for push notification fix

* PM-10600: UT coverage

* PM-10600: Fix Mobile devices not registering to organization push notifications

We only register devices for organization push notifications when the organization is being created. This does not work, since we have a use case (Notification Center) of delivering notifications to all users of organization. This fixes it, by adding the organization id tag when device registers for push notifications.

* PM-10600: Unit Test coverage for NotificationHubPushRegistrationService

Fixed IFeatureService substitute mocking for Android tests.
Added user part of organization test with organizationId tags expectation.

* PM-10600: Unit Tests fix to NotificationHubPushRegistrationService after merge conflict

* PM-10600: Organization push notifications not sending to mobile device from self-hosted.

Self-hosted instance uses relay to register the mobile device against Bitwarden Cloud Api. Only the self-hosted server knows client's organization membership, which means it needs to pass in the organization id's information to the relay. Similarly, for Bitwarden Cloud, the organizaton id will come directly from the server.

* PM-10600: Fix self-hosted organization notification not being received by mobile device.

When mobile device registers on self-hosted through the relay, every single id, like user id, device id and now organization id needs to be prefixed with the installation id. This have been missing in the PushController that handles this for organization id.

* PM-10600: Broken NotificationsController integration test

Device type is now part of JWT access token, so the notification center results in the integration test are now scoped to client type web and all.

* PM-10600: Merge conflicts fix

* merge conflict fix

* PM-10600: Push notification with full notification center content.

Notification Center push notification now includes all the fields.

* PM-10564: Push notification updates to other clients

Cherry-picked and squashed commits:
d9711b6031 6e69c8a0ce 01c814595e 3885885d5f 1285a7e994 fcf346985f 28ff53c293 57804ae27c 1c9339b686

* PM-15084: Push global notification creation to affected clients

Cherry-picked and squashed commits:
ed5051e0eb 181f3e4ae6 49fe7c93fd a8efb45a63 7b4122c837 d21d4a67b3 186a09bb92 1531f564b5

* PM-15084: Log warning when invalid notification push notification sent

* explicit Guid default value

* push notification tests in wrong namespace

* Installation push notification not received for on global notification center message

* wrong merge conflict

* wrong merge conflict

* installation id type Guid in push registration request
This commit is contained in:
Maciej Zieniuk
2025-02-20 15:35:48 +01:00
committed by GitHub
parent 228ce3b2e9
commit 9f4aa1ab2b
30 changed files with 963 additions and 163 deletions

View File

@ -0,0 +1,288 @@
#nullable enable
using Bit.Api.Platform.Push;
using Bit.Core.Context;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Models.Api;
using Bit.Core.Platform.Push;
using Bit.Core.Settings;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using NSubstitute;
using Xunit;
namespace Bit.Api.Test.Platform.Push.Controllers;
[ControllerCustomize(typeof(PushController))]
[SutProviderCustomize]
public class PushControllerTests
{
[Theory]
[BitAutoData(false, true)]
[BitAutoData(false, false)]
[BitAutoData(true, true)]
public async Task SendAsync_InstallationIdNotSetOrSelfHosted_BadRequest(bool haveInstallationId, bool selfHosted,
SutProvider<PushController> sutProvider, Guid installationId, Guid userId, Guid organizationId)
{
sutProvider.GetDependency<IGlobalSettings>().SelfHosted = selfHosted;
if (haveInstallationId)
{
sutProvider.GetDependency<ICurrentContext>().InstallationId.Returns(installationId);
}
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.SendAsync(new PushSendRequestModel
{
Type = PushType.Notification,
UserId = userId.ToString(),
OrganizationId = organizationId.ToString(),
InstallationId = installationId.ToString(),
Payload = "test-payload"
}));
Assert.Equal("Not correctly configured for push relays.", exception.Message);
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToUserAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(), Arg.Any<string>(),
Arg.Any<string?>(), Arg.Any<ClientType?>());
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToOrganizationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToInstallationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
}
[Theory]
[BitAutoData]
public async Task SendAsync_UserIdAndOrganizationIdAndInstallationIdEmpty_NoPushNotificationSent(
SutProvider<PushController> sutProvider, Guid installationId)
{
sutProvider.GetDependency<IGlobalSettings>().SelfHosted = false;
sutProvider.GetDependency<ICurrentContext>().InstallationId.Returns(installationId);
await sutProvider.Sut.SendAsync(new PushSendRequestModel
{
Type = PushType.Notification,
UserId = null,
OrganizationId = null,
InstallationId = null,
Payload = "test-payload"
});
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToUserAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(), Arg.Any<string>(),
Arg.Any<string?>(), Arg.Any<ClientType?>());
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToOrganizationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToInstallationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
}
[Theory]
[RepeatingPatternBitAutoData([false, true], [false, true], [false, true])]
public async Task SendAsync_UserIdSet_SendPayloadToUserAsync(bool haveIdentifier, bool haveDeviceId,
bool haveOrganizationId, SutProvider<PushController> sutProvider, Guid installationId, Guid userId,
Guid identifier, Guid deviceId)
{
sutProvider.GetDependency<IGlobalSettings>().SelfHosted = false;
sutProvider.GetDependency<ICurrentContext>().InstallationId.Returns(installationId);
var expectedUserId = $"{installationId}_{userId}";
var expectedIdentifier = haveIdentifier ? $"{installationId}_{identifier}" : null;
var expectedDeviceId = haveDeviceId ? $"{installationId}_{deviceId}" : null;
await sutProvider.Sut.SendAsync(new PushSendRequestModel
{
Type = PushType.Notification,
UserId = userId.ToString(),
OrganizationId = haveOrganizationId ? Guid.NewGuid().ToString() : null,
InstallationId = null,
Payload = "test-payload",
DeviceId = haveDeviceId ? deviceId.ToString() : null,
Identifier = haveIdentifier ? identifier.ToString() : null,
ClientType = ClientType.All,
});
await sutProvider.GetDependency<IPushNotificationService>().Received(1)
.SendPayloadToUserAsync(expectedUserId, PushType.Notification, "test-payload", expectedIdentifier,
expectedDeviceId, ClientType.All);
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToOrganizationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToInstallationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
}
[Theory]
[RepeatingPatternBitAutoData([false, true], [false, true])]
public async Task SendAsync_OrganizationIdSet_SendPayloadToOrganizationAsync(bool haveIdentifier, bool haveDeviceId,
SutProvider<PushController> sutProvider, Guid installationId, Guid organizationId, Guid identifier,
Guid deviceId)
{
sutProvider.GetDependency<IGlobalSettings>().SelfHosted = false;
sutProvider.GetDependency<ICurrentContext>().InstallationId.Returns(installationId);
var expectedOrganizationId = $"{installationId}_{organizationId}";
var expectedIdentifier = haveIdentifier ? $"{installationId}_{identifier}" : null;
var expectedDeviceId = haveDeviceId ? $"{installationId}_{deviceId}" : null;
await sutProvider.Sut.SendAsync(new PushSendRequestModel
{
Type = PushType.Notification,
UserId = null,
OrganizationId = organizationId.ToString(),
InstallationId = null,
Payload = "test-payload",
DeviceId = haveDeviceId ? deviceId.ToString() : null,
Identifier = haveIdentifier ? identifier.ToString() : null,
ClientType = ClientType.All,
});
await sutProvider.GetDependency<IPushNotificationService>().Received(1)
.SendPayloadToOrganizationAsync(expectedOrganizationId, PushType.Notification, "test-payload",
expectedIdentifier, expectedDeviceId, ClientType.All);
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToUserAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(), Arg.Any<string>(),
Arg.Any<string?>(), Arg.Any<ClientType?>());
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToInstallationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
}
[Theory]
[RepeatingPatternBitAutoData([false, true], [false, true])]
public async Task SendAsync_InstallationIdSet_SendPayloadToInstallationAsync(bool haveIdentifier, bool haveDeviceId,
SutProvider<PushController> sutProvider, Guid installationId, Guid identifier, Guid deviceId)
{
sutProvider.GetDependency<IGlobalSettings>().SelfHosted = false;
sutProvider.GetDependency<ICurrentContext>().InstallationId.Returns(installationId);
var expectedIdentifier = haveIdentifier ? $"{installationId}_{identifier}" : null;
var expectedDeviceId = haveDeviceId ? $"{installationId}_{deviceId}" : null;
await sutProvider.Sut.SendAsync(new PushSendRequestModel
{
Type = PushType.Notification,
UserId = null,
OrganizationId = null,
InstallationId = installationId.ToString(),
Payload = "test-payload",
DeviceId = haveDeviceId ? deviceId.ToString() : null,
Identifier = haveIdentifier ? identifier.ToString() : null,
ClientType = ClientType.All,
});
await sutProvider.GetDependency<IPushNotificationService>().Received(1)
.SendPayloadToInstallationAsync(installationId.ToString(), PushType.Notification, "test-payload",
expectedIdentifier, expectedDeviceId, ClientType.All);
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToOrganizationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToUserAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(), Arg.Any<string>(),
Arg.Any<string?>(), Arg.Any<ClientType?>());
}
[Theory]
[BitAutoData]
public async Task SendAsync_InstallationIdNotMatching_BadRequest(SutProvider<PushController> sutProvider,
Guid installationId)
{
sutProvider.GetDependency<IGlobalSettings>().SelfHosted = false;
sutProvider.GetDependency<ICurrentContext>().InstallationId.Returns(installationId);
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.SendAsync(new PushSendRequestModel
{
Type = PushType.Notification,
UserId = null,
OrganizationId = null,
InstallationId = Guid.NewGuid().ToString(),
Payload = "test-payload",
DeviceId = null,
Identifier = null,
ClientType = ClientType.All,
}));
Assert.Equal("InstallationId does not match current context.", exception.Message);
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToInstallationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToOrganizationAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(),
Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<ClientType?>());
await sutProvider.GetDependency<IPushNotificationService>().Received(0)
.SendPayloadToUserAsync(Arg.Any<string>(), Arg.Any<PushType>(), Arg.Any<object>(), Arg.Any<string>(),
Arg.Any<string?>(), Arg.Any<ClientType?>());
}
[Theory]
[BitAutoData(false, true)]
[BitAutoData(false, false)]
[BitAutoData(true, true)]
public async Task RegisterAsync_InstallationIdNotSetOrSelfHosted_BadRequest(bool haveInstallationId,
bool selfHosted,
SutProvider<PushController> sutProvider, Guid installationId, Guid userId, Guid identifier, Guid deviceId)
{
sutProvider.GetDependency<IGlobalSettings>().SelfHosted = selfHosted;
if (haveInstallationId)
{
sutProvider.GetDependency<ICurrentContext>().InstallationId.Returns(installationId);
}
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.RegisterAsync(new PushRegistrationRequestModel
{
DeviceId = deviceId.ToString(),
PushToken = "test-push-token",
UserId = userId.ToString(),
Type = DeviceType.Android,
Identifier = identifier.ToString()
}));
Assert.Equal("Not correctly configured for push relays.", exception.Message);
await sutProvider.GetDependency<IPushRegistrationService>().Received(0)
.CreateOrUpdateRegistrationAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
Arg.Any<DeviceType>(), Arg.Any<IEnumerable<string>>(), Arg.Any<Guid>());
}
[Theory]
[BitAutoData]
public async Task? RegisterAsync_ValidModel_CreatedOrUpdatedRegistration(SutProvider<PushController> sutProvider,
Guid installationId, Guid userId, Guid identifier, Guid deviceId, Guid organizationId)
{
sutProvider.GetDependency<IGlobalSettings>().SelfHosted = false;
sutProvider.GetDependency<ICurrentContext>().InstallationId.Returns(installationId);
var expectedUserId = $"{installationId}_{userId}";
var expectedIdentifier = $"{installationId}_{identifier}";
var expectedDeviceId = $"{installationId}_{deviceId}";
var expectedOrganizationId = $"{installationId}_{organizationId}";
await sutProvider.Sut.RegisterAsync(new PushRegistrationRequestModel
{
DeviceId = deviceId.ToString(),
PushToken = "test-push-token",
UserId = userId.ToString(),
Type = DeviceType.Android,
Identifier = identifier.ToString(),
OrganizationIds = [organizationId.ToString()],
InstallationId = installationId
});
await sutProvider.GetDependency<IPushRegistrationService>().Received(1)
.CreateOrUpdateRegistrationAsync("test-push-token", expectedDeviceId, expectedUserId,
expectedIdentifier, DeviceType.Android, Arg.Do<IEnumerable<string>>(organizationIds =>
{
var organizationIdsList = organizationIds.ToList();
Assert.Contains(expectedOrganizationId, organizationIdsList);
Assert.Single(organizationIdsList);
}), installationId);
}
}