1
0
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:
Maciej Zieniuk
2024-10-22 21:55:05 +01:00
parent e0d46d2dd3
commit ab370a3227
2 changed files with 224 additions and 27 deletions

View File

@ -0,0 +1,76 @@
#nullable enable
using System.ComponentModel.DataAnnotations;
using Bit.Core.Enums;
using Bit.Core.Models.Api;
using Xunit;
namespace Bit.Core.Test.Models.Api.Request;
public class PushSendRequestModelTests
{
[Theory]
[InlineData(null, null)]
[InlineData(null, "")]
[InlineData(null, " ")]
[InlineData("", null)]
[InlineData(" ", null)]
[InlineData("", "")]
[InlineData(" ", " ")]
public void Validate_UserIdOrganizationIdNullOrEmpty_Invalid(string? userId, string? organizationId)
{
var model = new PushSendRequestModel
{
UserId = userId,
OrganizationId = organizationId,
Type = PushType.SyncCiphers,
Payload = "test"
};
var results = Validate(model);
Assert.Single(results);
Assert.Contains(results, result => result.ErrorMessage == "UserId or OrganizationId is required.");
}
[Fact]
public void Validate_RequiredPayloadFieldNotProvided_Invalid()
{
var model = new PushSendRequestModel
{
UserId = Guid.NewGuid().ToString(),
OrganizationId = Guid.NewGuid().ToString(),
Type = PushType.SyncCiphers
};
var results = Validate(model);
Assert.Single(results);
Assert.Contains(results, result => result.ErrorMessage == "The Payload field is required.");
}
[Fact]
public void Validate_AllFieldsPresent_Valid()
{
var model = new PushSendRequestModel
{
UserId = Guid.NewGuid().ToString(),
OrganizationId = Guid.NewGuid().ToString(),
Type = PushType.SyncCiphers,
Payload = "test payload",
Identifier = Guid.NewGuid().ToString(),
ClientType = ClientType.All,
DeviceId = Guid.NewGuid().ToString()
};
var results = Validate(model);
Assert.Empty(results);
}
private static List<ValidationResult> Validate(PushSendRequestModel model)
{
var results = new List<ValidationResult>();
Validator.TryValidateObject(model, new ValidationContext(model), results);
return results;
}
}

View File

@ -1,42 +1,163 @@
using Bit.Core.NotificationHub; using System.Text.Json;
using Bit.Core.Enums;
using Bit.Core.Models;
using Bit.Core.Models.Data;
using Bit.Core.NotificationCenter.Entities;
using Bit.Core.NotificationHub;
using Bit.Core.Repositories; using Bit.Core.Repositories;
using Bit.Core.Services; using Bit.Core.Test.NotificationCenter.AutoFixture;
using Microsoft.AspNetCore.Http; using Bit.Test.Common.AutoFixture;
using Microsoft.Extensions.Logging; using Bit.Test.Common.AutoFixture.Attributes;
using NSubstitute; using NSubstitute;
using Xunit; using Xunit;
namespace Bit.Core.Test.NotificationHub; namespace Bit.Core.Test.NotificationHub;
[SutProviderCustomize]
public class NotificationHubPushNotificationServiceTests public class NotificationHubPushNotificationServiceTests
{ {
private readonly NotificationHubPushNotificationService _sut; [Theory]
[BitAutoData]
private readonly IInstallationDeviceRepository _installationDeviceRepository; [NotificationCustomize]
private readonly INotificationHubPool _notificationHubPool; public async void PushSyncNotificationAsync_Global_NotSent(
private readonly IHttpContextAccessor _httpContextAccessor; SutProvider<NotificationHubPushNotificationService> sutProvider, Notification notification)
private readonly ILogger<NotificationsApiPushNotificationService> _logger;
public NotificationHubPushNotificationServiceTests()
{ {
_installationDeviceRepository = Substitute.For<IInstallationDeviceRepository>(); await sutProvider.Sut.PushSyncNotificationAsync(notification);
_httpContextAccessor = Substitute.For<IHttpContextAccessor>();
_notificationHubPool = Substitute.For<INotificationHubPool>();
_logger = Substitute.For<ILogger<NotificationsApiPushNotificationService>>();
_sut = new NotificationHubPushNotificationService( await sutProvider.GetDependency<INotificationHubPool>()
_installationDeviceRepository, .Received(0)
_notificationHubPool, .AllClients
_httpContextAccessor, .Received(0)
_logger .SendTemplateNotificationAsync(Arg.Any<IDictionary<string, string>>(), Arg.Any<string>());
); await sutProvider.GetDependency<IInstallationDeviceRepository>()
.Received(0)
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
} }
// Remove this test when we add actual tests. It only proves that [Theory]
// we've properly constructed the system under test. [BitAutoData(false)]
[Fact(Skip = "Needs additional work")] [BitAutoData(true)]
public void ServiceExists() [NotificationCustomize(false)]
public async void PushSyncNotificationAsync_UserIdProvidedClientTypeAll_SentToUser(
bool organizationIdNull, SutProvider<NotificationHubPushNotificationService> sutProvider,
Notification notification)
{ {
Assert.NotNull(_sut); if (organizationIdNull)
{
notification.OrganizationId = null;
}
notification.ClientType = ClientType.All;
var expectedSyncNotification = ToSyncNotificationPushNotification(notification);
await sutProvider.Sut.PushSyncNotificationAsync(notification);
await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotification, expectedSyncNotification,
$"(template:payload_userId:{notification.UserId})");
await sutProvider.GetDependency<IInstallationDeviceRepository>()
.Received(0)
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
}
[Theory]
[BitAutoData(false, ClientType.Browser)]
[BitAutoData(false, ClientType.Desktop)]
[BitAutoData(false, ClientType.Web)]
[BitAutoData(false, ClientType.Mobile)]
[BitAutoData(true, ClientType.Browser)]
[BitAutoData(true, ClientType.Desktop)]
[BitAutoData(true, ClientType.Web)]
[BitAutoData(true, ClientType.Mobile)]
[NotificationCustomize(false)]
public async void PushSyncNotificationAsync_UserIdProvidedClientTypeNotAll_SentToUser(bool organizationIdNull,
ClientType clientType, SutProvider<NotificationHubPushNotificationService> sutProvider,
Notification notification)
{
if (organizationIdNull)
{
notification.OrganizationId = null;
}
notification.ClientType = clientType;
var expectedSyncNotification = ToSyncNotificationPushNotification(notification);
await sutProvider.Sut.PushSyncNotificationAsync(notification);
await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotification, expectedSyncNotification,
$"(template:payload_userId:{notification.UserId} && clientType:{clientType})");
await sutProvider.GetDependency<IInstallationDeviceRepository>()
.Received(0)
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
}
[Theory]
[BitAutoData]
[NotificationCustomize(false)]
public async void PushSyncNotificationAsync_UserIdNullOrganizationIdProvidedClientTypeAll_SentToOrganization(
SutProvider<NotificationHubPushNotificationService> sutProvider, Notification notification)
{
notification.UserId = null;
notification.ClientType = ClientType.All;
var expectedSyncNotification = ToSyncNotificationPushNotification(notification);
await sutProvider.Sut.PushSyncNotificationAsync(notification);
await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotification, expectedSyncNotification,
$"(template:payload && organizationId:{notification.OrganizationId})");
await sutProvider.GetDependency<IInstallationDeviceRepository>()
.Received(0)
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
}
[Theory]
[BitAutoData(ClientType.Browser)]
[BitAutoData(ClientType.Desktop)]
[BitAutoData(ClientType.Web)]
[BitAutoData(ClientType.Mobile)]
[NotificationCustomize(false)]
public async void PushSyncNotificationAsync_UserIdNullOrganizationIdProvidedClientTypeNotAll_SentToOrganization(
ClientType clientType, SutProvider<NotificationHubPushNotificationService> sutProvider,
Notification notification)
{
notification.UserId = null;
notification.ClientType = clientType;
var expectedSyncNotification = ToSyncNotificationPushNotification(notification);
await sutProvider.Sut.PushSyncNotificationAsync(notification);
await AssertSendTemplateNotificationAsync(sutProvider, PushType.SyncNotification, expectedSyncNotification,
$"(template:payload && organizationId:{notification.OrganizationId} && clientType:{clientType})");
await sutProvider.GetDependency<IInstallationDeviceRepository>()
.Received(0)
.UpsertAsync(Arg.Any<InstallationDeviceEntity>());
}
private static SyncNotificationPushNotification ToSyncNotificationPushNotification(Notification notification) =>
new()
{
Id = notification.Id,
UserId = notification.UserId,
OrganizationId = notification.OrganizationId,
ClientType = notification.ClientType,
RevisionDate = notification.RevisionDate
};
private static async Task AssertSendTemplateNotificationAsync(
SutProvider<NotificationHubPushNotificationService> sutProvider, PushType type, object payload, string tag)
{
await sutProvider.GetDependency<INotificationHubPool>()
.Received(1)
.AllClients
.Received(1)
.SendTemplateNotificationAsync(
Arg.Is<IDictionary<string, string>>(dictionary => MatchingSendPayload(dictionary, type, payload)),
tag);
}
private static bool MatchingSendPayload(IDictionary<string, string> dictionary, PushType type, object payload)
{
return dictionary.ContainsKey("type") && dictionary["type"].Equals(((byte)type).ToString()) &&
dictionary.ContainsKey("payload") && dictionary["payload"].Equals(JsonSerializer.Serialize(payload));
} }
} }