mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
Shard notification hub (#4450)
* Allow for binning of comb IDs by date and value
* Introduce notification hub pool
* Replace device type sharding with comb + range sharding
* Fix proxy interface
* Use enumerable services for multiServiceNotificationHub
* Fix push interface usage
* Fix push notification service dependencies
* Fix push notification keys
* Fixup documentation
* Remove deprecated settings
* Fix tests
* PascalCase method names
* Remove unused request model properties
* Remove unused setting
* Improve DateFromComb precision
* Prefer readonly service enumerable
* Pascal case template holes
* Name TryParse methods TryParse
* Apply suggestions from code review
Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
* AllClients is a set of clients and must be deduplicated
* Fix registration start time
* Add logging to initialization of a notification hub
* more logging
* Add lower level logging for hub settings
* Log when connection is resolved
* Improve log message
* Log pushes to notification hub
* temporarily elevate log messages for visibility
* Log in multi-service when relaying to another push service
* Revert to more reasonable logging free of user information
* Fixup merge
Deleting user was extracted to a command in #4803, this updates that work to use just the device ids as I did elsewhere in abd67e8ec
* Do not use bouncy castle exception types
* Add required services for logging
---------
Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
Co-authored-by: bnagawiecki <107435978+bnagawiecki@users.noreply.github.com>
This commit is contained in:
205
test/Core.Test/NotificationHub/NotificationHubConnectionTests.cs
Normal file
205
test/Core.Test/NotificationHub/NotificationHubConnectionTests.cs
Normal file
@ -0,0 +1,205 @@
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.NotificationHub;
|
||||
|
||||
public class NotificationHubConnectionTests
|
||||
{
|
||||
[Fact]
|
||||
public void IsValid_ConnectionStringIsNull_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = null,
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
};
|
||||
|
||||
// Act
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Assert
|
||||
Assert.False(connection.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValid_HubNameIsNull_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = "Endpoint=sb://example.servicebus.windows.net/;",
|
||||
HubName = null,
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
};
|
||||
|
||||
// Act
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Assert
|
||||
Assert.False(connection.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValid_ConnectionStringAndHubNameAreNotNull_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
};
|
||||
|
||||
// Act
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Assert
|
||||
Assert.True(connection.IsValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegistrationEnabled_QueryTimeIsBeforeStartDate_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow.AddDays(1),
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(2)
|
||||
};
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Act
|
||||
var result = connection.RegistrationEnabled(DateTime.UtcNow);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegistrationEnabled_QueryTimeIsAfterEndDate_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
};
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Act
|
||||
var result = connection.RegistrationEnabled(DateTime.UtcNow.AddDays(2));
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegistrationEnabled_NullStartDate_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = null,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
};
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Act
|
||||
var result = connection.RegistrationEnabled(DateTime.UtcNow);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegistrationEnabled_QueryTimeIsBetweenStartDateAndEndDate_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
};
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Act
|
||||
var result = connection.RegistrationEnabled(DateTime.UtcNow.AddHours(1));
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegistrationEnabled_CombTimeIsBeforeStartDate_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow.AddDays(1),
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(2)
|
||||
};
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Act
|
||||
var result = connection.RegistrationEnabled(CoreHelpers.GenerateComb(Guid.NewGuid(), DateTime.UtcNow));
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegistrationEnabled_CombTimeIsAfterEndDate_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
};
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Act
|
||||
var result = connection.RegistrationEnabled(CoreHelpers.GenerateComb(Guid.NewGuid(), DateTime.UtcNow.AddDays(2)));
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegistrationEnabled_CombTimeIsBetweenStartDateAndEndDate_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var hub = new GlobalSettings.NotificationHubSettings()
|
||||
{
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
};
|
||||
var connection = NotificationHubConnection.From(hub);
|
||||
|
||||
// Act
|
||||
var result = connection.RegistrationEnabled(CoreHelpers.GenerateComb(Guid.NewGuid(), DateTime.UtcNow.AddHours(1)));
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
}
|
156
test/Core.Test/NotificationHub/NotificationHubPoolTests.cs
Normal file
156
test/Core.Test/NotificationHub/NotificationHubPoolTests.cs
Normal file
@ -0,0 +1,156 @@
|
||||
using Bit.Core.NotificationHub;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
using static Bit.Core.Settings.GlobalSettings;
|
||||
|
||||
namespace Bit.Core.Test.NotificationHub;
|
||||
|
||||
public class NotificationHubPoolTests
|
||||
{
|
||||
[Fact]
|
||||
public void NotificationHubPool_WarnsOnMissingConnectionString()
|
||||
{
|
||||
// Arrange
|
||||
var globalSettings = new GlobalSettings()
|
||||
{
|
||||
NotificationHubPool = new NotificationHubPoolSettings()
|
||||
{
|
||||
NotificationHubs = new() {
|
||||
new() {
|
||||
ConnectionString = null,
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var logger = Substitute.For<ILogger<NotificationHubPool>>();
|
||||
|
||||
// Act
|
||||
var sut = new NotificationHubPool(logger, globalSettings);
|
||||
|
||||
// Assert
|
||||
logger.Received().Log(LogLevel.Warning, Arg.Any<EventId>(),
|
||||
Arg.Is<object>(o => o.ToString() == "Invalid notification hub settings: hub"),
|
||||
null,
|
||||
Arg.Any<Func<object, Exception, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotificationHubPool_WarnsOnMissingHubName()
|
||||
{
|
||||
// Arrange
|
||||
var globalSettings = new GlobalSettings()
|
||||
{
|
||||
NotificationHubPool = new NotificationHubPoolSettings()
|
||||
{
|
||||
NotificationHubs = new() {
|
||||
new() {
|
||||
ConnectionString = "connection",
|
||||
HubName = null,
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var logger = Substitute.For<ILogger<NotificationHubPool>>();
|
||||
|
||||
// Act
|
||||
var sut = new NotificationHubPool(logger, globalSettings);
|
||||
|
||||
// Assert
|
||||
logger.Received().Log(LogLevel.Warning, Arg.Any<EventId>(),
|
||||
Arg.Is<object>(o => o.ToString() == "Invalid notification hub settings: hub name missing"),
|
||||
null,
|
||||
Arg.Any<Func<object, Exception, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotificationHubPool_ClientFor_ThrowsOnNoValidHubs()
|
||||
{
|
||||
// Arrange
|
||||
var globalSettings = new GlobalSettings()
|
||||
{
|
||||
NotificationHubPool = new NotificationHubPoolSettings()
|
||||
{
|
||||
NotificationHubs = new() {
|
||||
new() {
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = null,
|
||||
RegistrationEndDate = null,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var logger = Substitute.For<ILogger<NotificationHubPool>>();
|
||||
var sut = new NotificationHubPool(logger, globalSettings);
|
||||
|
||||
// Act
|
||||
Action act = () => sut.ClientFor(Guid.NewGuid());
|
||||
|
||||
// Assert
|
||||
Assert.Throws<InvalidOperationException>(act);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotificationHubPool_ClientFor_ReturnsClient()
|
||||
{
|
||||
// Arrange
|
||||
var globalSettings = new GlobalSettings()
|
||||
{
|
||||
NotificationHubPool = new NotificationHubPoolSettings()
|
||||
{
|
||||
NotificationHubs = new() {
|
||||
new() {
|
||||
ConnectionString = "Endpoint=sb://example.servicebus.windows.net/;SharedAccessKey=example///example=",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow.AddMinutes(-1),
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var logger = Substitute.For<ILogger<NotificationHubPool>>();
|
||||
var sut = new NotificationHubPool(logger, globalSettings);
|
||||
|
||||
// Act
|
||||
var client = sut.ClientFor(CoreHelpers.GenerateComb(Guid.NewGuid(), DateTime.UtcNow));
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(client);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NotificationHubPool_AllClients_ReturnsProxy()
|
||||
{
|
||||
// Arrange
|
||||
var globalSettings = new GlobalSettings()
|
||||
{
|
||||
NotificationHubPool = new NotificationHubPoolSettings()
|
||||
{
|
||||
NotificationHubs = new() {
|
||||
new() {
|
||||
ConnectionString = "connection",
|
||||
HubName = "hub",
|
||||
RegistrationStartDate = DateTime.UtcNow,
|
||||
RegistrationEndDate = DateTime.UtcNow.AddDays(1),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var logger = Substitute.For<ILogger<NotificationHubPool>>();
|
||||
var sut = new NotificationHubPool(logger, globalSettings);
|
||||
|
||||
// Act
|
||||
var proxy = sut.AllClients;
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(proxy);
|
||||
}
|
||||
}
|
40
test/Core.Test/NotificationHub/NotificationHubProxyTests.cs
Normal file
40
test/Core.Test/NotificationHub/NotificationHubProxyTests.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using AutoFixture;
|
||||
using Bit.Core.NotificationHub;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Microsoft.Azure.NotificationHubs;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.NotificationHub;
|
||||
|
||||
public class NotificationHubProxyTests
|
||||
{
|
||||
private readonly IEnumerable<INotificationHubClient> _clients;
|
||||
public NotificationHubProxyTests()
|
||||
{
|
||||
_clients = new Fixture().WithAutoNSubstitutions().CreateMany<INotificationHubClient>();
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> ClientMethods =
|
||||
[
|
||||
[
|
||||
(NotificationHubClientProxy c) => c.SendTemplateNotificationAsync(new Dictionary<string, string>() { { "key", "value" } }, "tag"),
|
||||
(INotificationHubClient c) => c.SendTemplateNotificationAsync(Arg.Is<Dictionary<string, string>>((a) => a.Keys.Count == 1 && a.ContainsKey("key") && a["key"] == "value"), "tag"),
|
||||
],
|
||||
];
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ClientMethods))]
|
||||
public async void CallsAllClients(Func<NotificationHubClientProxy, Task> proxyMethod, Func<INotificationHubClient, Task> clientMethod)
|
||||
{
|
||||
var clients = _clients.ToArray();
|
||||
var proxy = new NotificationHubClientProxy(clients);
|
||||
|
||||
await proxyMethod(proxy);
|
||||
|
||||
foreach (var client in clients)
|
||||
{
|
||||
await clientMethod(client.Received());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
using Bit.Core.NotificationHub;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.NotificationHub;
|
||||
|
||||
public class NotificationHubPushNotificationServiceTests
|
||||
{
|
||||
private readonly NotificationHubPushNotificationService _sut;
|
||||
|
||||
private readonly IInstallationDeviceRepository _installationDeviceRepository;
|
||||
private readonly INotificationHubPool _notificationHubPool;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly ILogger<NotificationsApiPushNotificationService> _logger;
|
||||
|
||||
public NotificationHubPushNotificationServiceTests()
|
||||
{
|
||||
_installationDeviceRepository = Substitute.For<IInstallationDeviceRepository>();
|
||||
_httpContextAccessor = Substitute.For<IHttpContextAccessor>();
|
||||
_notificationHubPool = Substitute.For<INotificationHubPool>();
|
||||
_logger = Substitute.For<ILogger<NotificationsApiPushNotificationService>>();
|
||||
|
||||
_sut = new NotificationHubPushNotificationService(
|
||||
_installationDeviceRepository,
|
||||
_notificationHubPool,
|
||||
_httpContextAccessor,
|
||||
_logger
|
||||
);
|
||||
}
|
||||
|
||||
// Remove this test when we add actual tests. It only proves that
|
||||
// we've properly constructed the system under test.
|
||||
[Fact(Skip = "Needs additional work")]
|
||||
public void ServiceExists()
|
||||
{
|
||||
Assert.NotNull(_sut);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
using Bit.Core.NotificationHub;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Settings;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.NotificationHub;
|
||||
|
||||
public class NotificationHubPushRegistrationServiceTests
|
||||
{
|
||||
private readonly NotificationHubPushRegistrationService _sut;
|
||||
|
||||
private readonly IInstallationDeviceRepository _installationDeviceRepository;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILogger<NotificationHubPushRegistrationService> _logger;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly INotificationHubPool _notificationHubPool;
|
||||
|
||||
public NotificationHubPushRegistrationServiceTests()
|
||||
{
|
||||
_installationDeviceRepository = Substitute.For<IInstallationDeviceRepository>();
|
||||
_serviceProvider = Substitute.For<IServiceProvider>();
|
||||
_logger = Substitute.For<ILogger<NotificationHubPushRegistrationService>>();
|
||||
_globalSettings = new GlobalSettings();
|
||||
_notificationHubPool = Substitute.For<INotificationHubPool>();
|
||||
|
||||
_sut = new NotificationHubPushRegistrationService(
|
||||
_installationDeviceRepository,
|
||||
_globalSettings,
|
||||
_notificationHubPool,
|
||||
_serviceProvider,
|
||||
_logger
|
||||
);
|
||||
}
|
||||
|
||||
// Remove this test when we add actual tests. It only proves that
|
||||
// we've properly constructed the system under test.
|
||||
[Fact(Skip = "Needs additional work")]
|
||||
public void ServiceExists()
|
||||
{
|
||||
Assert.NotNull(_sut);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user