mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 00:22:50 -05:00
PM-10600: UT coverage
This commit is contained in:
@ -23,10 +23,10 @@ public class QueueClientBuilder : ISpecimenBuilder
|
|||||||
|
|
||||||
public class QueueClientCustomizeAttribute : BitCustomizeAttribute
|
public class QueueClientCustomizeAttribute : BitCustomizeAttribute
|
||||||
{
|
{
|
||||||
public override ICustomization GetCustomization() => new QueueClientFixture();
|
public override ICustomization GetCustomization() => new QueueClientFixtures();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class QueueClientFixture : ICustomization
|
public class QueueClientFixtures : ICustomization
|
||||||
{
|
{
|
||||||
public void Customize(IFixture fixture)
|
public void Customize(IFixture fixture)
|
||||||
{
|
{
|
@ -1,7 +1,10 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Text.Json;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.Models.Api;
|
using Bit.Core.Models.Api;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Bit.Core.Test.Models.Api.Request;
|
namespace Bit.Core.Test.Models.Api.Request;
|
||||||
@ -32,20 +35,35 @@ public class PushSendRequestModelTests
|
|||||||
Assert.Contains(results, result => result.ErrorMessage == "UserId or OrganizationId is required.");
|
Assert.Contains(results, result => result.ErrorMessage == "UserId or OrganizationId is required.");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public void Validate_RequiredPayloadFieldNotProvided_Invalid()
|
[BitAutoData("Payload")]
|
||||||
|
[BitAutoData("Type")]
|
||||||
|
public void Validate_RequiredFieldNotProvided_Invalid(string requiredField)
|
||||||
{
|
{
|
||||||
var model = new PushSendRequestModel
|
var model = new PushSendRequestModel
|
||||||
{
|
{
|
||||||
UserId = Guid.NewGuid().ToString(),
|
UserId = Guid.NewGuid().ToString(),
|
||||||
OrganizationId = Guid.NewGuid().ToString(),
|
OrganizationId = Guid.NewGuid().ToString(),
|
||||||
Type = PushType.SyncCiphers
|
Type = PushType.SyncCiphers,
|
||||||
|
Payload = "test"
|
||||||
};
|
};
|
||||||
|
|
||||||
var results = Validate(model);
|
var dictionary = new Dictionary<string, object?>();
|
||||||
|
foreach (var property in model.GetType().GetProperties())
|
||||||
|
{
|
||||||
|
if (property.Name == requiredField)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Assert.Single(results);
|
dictionary[property.Name] = property.GetValue(model);
|
||||||
Assert.Contains(results, result => result.ErrorMessage == "The Payload field is required.");
|
}
|
||||||
|
|
||||||
|
var serialized = JsonSerializer.Serialize(dictionary, JsonHelpers.IgnoreWritingNull);
|
||||||
|
var jsonException =
|
||||||
|
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<PushSendRequestModel>(serialized));
|
||||||
|
Assert.Contains($"missing required properties, including the following: {requiredField}",
|
||||||
|
jsonException.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -70,7 +88,7 @@ public class PushSendRequestModelTests
|
|||||||
private static List<ValidationResult> Validate(PushSendRequestModel model)
|
private static List<ValidationResult> Validate(PushSendRequestModel model)
|
||||||
{
|
{
|
||||||
var results = new List<ValidationResult>();
|
var results = new List<ValidationResult>();
|
||||||
Validator.TryValidateObject(model, new ValidationContext(model), results);
|
Validator.TryValidateObject(model, new ValidationContext(model), results, true);
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System.Text.Json;
|
#nullable enable
|
||||||
|
using System.Text.Json;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.Models;
|
using Bit.Core.Models;
|
||||||
using Bit.Core.Models.Data;
|
using Bit.Core.Models.Data;
|
||||||
@ -133,6 +134,78 @@ public class NotificationHubPushNotificationServiceTests
|
|||||||
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
|
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData([null])]
|
||||||
|
[BitAutoData(ClientType.All)]
|
||||||
|
public async void SendPayloadToUserAsync_ClientTypeNullOrAll_SentToUser(ClientType? clientType,
|
||||||
|
SutProvider<NotificationHubPushNotificationService> sutProvider, Guid userId, PushType pushType, string payload,
|
||||||
|
string identifier)
|
||||||
|
{
|
||||||
|
await sutProvider.Sut.SendPayloadToUserAsync(userId.ToString(), pushType, payload, identifier, null,
|
||||||
|
clientType);
|
||||||
|
|
||||||
|
await AssertSendTemplateNotificationAsync(sutProvider, pushType, payload,
|
||||||
|
$"(template:payload_userId:{userId} && !deviceIdentifier:{identifier})");
|
||||||
|
await sutProvider.GetDependency<IInstallationDeviceRepository>()
|
||||||
|
.Received(0)
|
||||||
|
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData(ClientType.Browser)]
|
||||||
|
[BitAutoData(ClientType.Desktop)]
|
||||||
|
[BitAutoData(ClientType.Mobile)]
|
||||||
|
[BitAutoData(ClientType.Web)]
|
||||||
|
public async void SendPayloadToUserAsync_ClientTypeExplicit_SentToUserAndClientType(ClientType clientType,
|
||||||
|
SutProvider<NotificationHubPushNotificationService> sutProvider, Guid userId, PushType pushType, string payload,
|
||||||
|
string identifier)
|
||||||
|
{
|
||||||
|
await sutProvider.Sut.SendPayloadToUserAsync(userId.ToString(), pushType, payload, identifier, null,
|
||||||
|
clientType);
|
||||||
|
|
||||||
|
await AssertSendTemplateNotificationAsync(sutProvider, pushType, payload,
|
||||||
|
$"(template:payload_userId:{userId} && !deviceIdentifier:{identifier} && clientType:{clientType})");
|
||||||
|
await sutProvider.GetDependency<IInstallationDeviceRepository>()
|
||||||
|
.Received(0)
|
||||||
|
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData([null])]
|
||||||
|
[BitAutoData(ClientType.All)]
|
||||||
|
public async void SendPayloadToOrganizationAsync_ClientTypeNullOrAll_SentToOrganization(ClientType? clientType,
|
||||||
|
SutProvider<NotificationHubPushNotificationService> sutProvider, Guid organizationId, PushType pushType,
|
||||||
|
string payload, string identifier)
|
||||||
|
{
|
||||||
|
await sutProvider.Sut.SendPayloadToOrganizationAsync(organizationId.ToString(), pushType, payload, identifier,
|
||||||
|
null, clientType);
|
||||||
|
|
||||||
|
await AssertSendTemplateNotificationAsync(sutProvider, pushType, payload,
|
||||||
|
$"(template:payload && organizationId:{organizationId} && !deviceIdentifier:{identifier})");
|
||||||
|
await sutProvider.GetDependency<IInstallationDeviceRepository>()
|
||||||
|
.Received(0)
|
||||||
|
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData(ClientType.Browser)]
|
||||||
|
[BitAutoData(ClientType.Desktop)]
|
||||||
|
[BitAutoData(ClientType.Mobile)]
|
||||||
|
[BitAutoData(ClientType.Web)]
|
||||||
|
public async void SendPayloadToOrganizationAsync_ClientTypeExplicit_SentToOrganizationAndClientType(
|
||||||
|
ClientType clientType, SutProvider<NotificationHubPushNotificationService> sutProvider, Guid organizationId,
|
||||||
|
PushType pushType, string payload, string identifier)
|
||||||
|
{
|
||||||
|
await sutProvider.Sut.SendPayloadToOrganizationAsync(organizationId.ToString(), pushType, payload, identifier,
|
||||||
|
null, clientType);
|
||||||
|
|
||||||
|
await AssertSendTemplateNotificationAsync(sutProvider, pushType, payload,
|
||||||
|
$"(template:payload && organizationId:{organizationId} && !deviceIdentifier:{identifier} && clientType:{clientType})");
|
||||||
|
await sutProvider.GetDependency<IInstallationDeviceRepository>()
|
||||||
|
.Received(0)
|
||||||
|
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
|
||||||
|
}
|
||||||
|
|
||||||
private static SyncNotificationPushNotification ToSyncNotificationPushNotification(Notification notification) =>
|
private static SyncNotificationPushNotification ToSyncNotificationPushNotification(Notification notification) =>
|
||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
|
@ -1,45 +1,63 @@
|
|||||||
using AutoFixture;
|
#nullable enable
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.NotificationCenter.Entities;
|
||||||
using Bit.Core.Services;
|
using Bit.Core.Services;
|
||||||
|
using Bit.Core.Test.NotificationCenter.AutoFixture;
|
||||||
using Bit.Test.Common.AutoFixture;
|
using Bit.Test.Common.AutoFixture;
|
||||||
using Microsoft.Extensions.Logging;
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using GlobalSettingsCustomization = Bit.Test.Common.AutoFixture.GlobalSettings;
|
|
||||||
|
|
||||||
namespace Bit.Core.Test.Services;
|
namespace Bit.Core.Test.Services;
|
||||||
|
|
||||||
|
[SutProviderCustomize]
|
||||||
public class MultiServicePushNotificationServiceTests
|
public class MultiServicePushNotificationServiceTests
|
||||||
{
|
{
|
||||||
private readonly MultiServicePushNotificationService _sut;
|
[Theory]
|
||||||
|
[BitAutoData]
|
||||||
private readonly ILogger<MultiServicePushNotificationService> _logger;
|
[NotificationCustomize]
|
||||||
private readonly ILogger<RelayPushNotificationService> _relayLogger;
|
public async Task PushSyncNotificationAsync_Notification_Sent(
|
||||||
private readonly ILogger<NotificationsApiPushNotificationService> _hubLogger;
|
SutProvider<MultiServicePushNotificationService> sutProvider, Notification notification)
|
||||||
private readonly IEnumerable<IPushNotificationService> _services;
|
|
||||||
private readonly Settings.GlobalSettings _globalSettings;
|
|
||||||
|
|
||||||
public MultiServicePushNotificationServiceTests()
|
|
||||||
{
|
{
|
||||||
_logger = Substitute.For<ILogger<MultiServicePushNotificationService>>();
|
await sutProvider.Sut.PushSyncNotificationAsync(notification);
|
||||||
_relayLogger = Substitute.For<ILogger<RelayPushNotificationService>>();
|
|
||||||
_hubLogger = Substitute.For<ILogger<NotificationsApiPushNotificationService>>();
|
|
||||||
|
|
||||||
var fixture = new Fixture().WithAutoNSubstitutions().Customize(new GlobalSettingsCustomization());
|
await sutProvider.GetDependency<IEnumerable<IPushNotificationService>>()
|
||||||
_services = fixture.CreateMany<IPushNotificationService>();
|
.First()
|
||||||
_globalSettings = fixture.Create<Settings.GlobalSettings>();
|
.Received(1)
|
||||||
|
.PushSyncNotificationAsync(notification);
|
||||||
_sut = new MultiServicePushNotificationService(
|
|
||||||
_services,
|
|
||||||
_logger,
|
|
||||||
_globalSettings
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove this test when we add actual tests. It only proves that
|
[Theory]
|
||||||
// we've properly constructed the system under test.
|
[BitAutoData([null, null])]
|
||||||
[Fact]
|
[BitAutoData(ClientType.All, null)]
|
||||||
public void ServiceExists()
|
[BitAutoData([null, "test device id"])]
|
||||||
|
[BitAutoData(ClientType.All, "test device id")]
|
||||||
|
public async Task SendPayloadToUserAsync_Message_Sent(ClientType? clientType, string? deviceId, string userId,
|
||||||
|
PushType type, object payload, string identifier, SutProvider<MultiServicePushNotificationService> sutProvider)
|
||||||
{
|
{
|
||||||
Assert.NotNull(_sut);
|
await sutProvider.Sut.SendPayloadToUserAsync(userId, type, payload, identifier, deviceId, clientType);
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IEnumerable<IPushNotificationService>>()
|
||||||
|
.First()
|
||||||
|
.Received(1)
|
||||||
|
.SendPayloadToUserAsync(userId, type, payload, identifier, deviceId, clientType);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData([null, null])]
|
||||||
|
[BitAutoData(ClientType.All, null)]
|
||||||
|
[BitAutoData([null, "test device id"])]
|
||||||
|
[BitAutoData(ClientType.All, "test device id")]
|
||||||
|
public async Task SendPayloadToOrganizationAsync_Message_Sent(ClientType? clientType, string? deviceId,
|
||||||
|
string organizationId, PushType type, object payload, string identifier,
|
||||||
|
SutProvider<MultiServicePushNotificationService> sutProvider)
|
||||||
|
{
|
||||||
|
await sutProvider.Sut.SendPayloadToOrganizationAsync(organizationId, type, payload, identifier, deviceId,
|
||||||
|
clientType);
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IEnumerable<IPushNotificationService>>()
|
||||||
|
.First()
|
||||||
|
.Received(1)
|
||||||
|
.SendPayloadToOrganizationAsync(organizationId, type, payload, identifier, deviceId, clientType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user