server/test/Api.Test/Platform/Push/Controllers/PushControllerTests.cs
Justin Baur e5159a3ba2
[PM-19659] Clean up Notifications code (#6244)
* Move PushType to Platform Folder

- Move the PushType next to the rest of push notification code
- Specifically exclude it from needing Platform code review
- Add tests establishing rules Platform has for usage of this enum, making it safe to have no owner

* Move NotificationHub code into Platform/Push directory

* Update NotificationHub namespace imports

* Add attribute for storing push type metadata

* Rename Push Engines to have PushEngine suffix

* Move Push Registration items to their own directory

* Push code move

* Add expected usage comment

* Add Push feature registration method

- Make method able to be called multipes times with no ill effects

* Add Push Registration service entrypoint and tests

* Use new service entrypoints

* Test changes
2025-08-26 13:30:37 -04:00

100 lines
4.0 KiB
C#

#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.Platform.PushRegistration;
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 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<PushRegistrationData>(), Arg.Any<string>(), Arg.Any<string>(),
Arg.Any<string>(), Arg.Any<DeviceType>(), Arg.Any<IEnumerable<string>>(), Arg.Any<Guid>());
}
[Theory]
[BitAutoData(false)]
[BitAutoData(true)]
public async Task RegisterAsync_ValidModel_CreatedOrUpdatedRegistration(bool haveOrganizationId,
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}";
var model = new PushRegistrationRequestModel
{
DeviceId = deviceId.ToString(),
PushToken = "test-push-token",
UserId = userId.ToString(),
Type = DeviceType.Android,
Identifier = identifier.ToString(),
OrganizationIds = haveOrganizationId ? [organizationId.ToString()] : null,
InstallationId = installationId
};
await sutProvider.Sut.RegisterAsync(model);
await sutProvider.GetDependency<IPushRegistrationService>().Received(1)
.CreateOrUpdateRegistrationAsync(
Arg.Is<PushRegistrationData>(data => data == new PushRegistrationData(model.PushToken)),
expectedDeviceId, expectedUserId,
expectedIdentifier, DeviceType.Android, Arg.Do<IEnumerable<string>>(organizationIds =>
{
Assert.NotNull(organizationIds);
var organizationIdsList = organizationIds.ToList();
if (haveOrganizationId)
{
Assert.Contains(expectedOrganizationId, organizationIdsList);
Assert.Single(organizationIdsList);
}
else
{
Assert.Empty(organizationIdsList);
}
}), installationId);
}
}