1
0
mirror of https://github.com/bitwarden/server synced 2026-02-17 09:59:14 +00:00

Move enable/disable operations to SubscriberService

This commit is contained in:
Alex Morask
2026-02-03 12:54:46 -06:00
parent 158cbb9447
commit fe0bc1516b
12 changed files with 394 additions and 210 deletions

View File

@@ -1,12 +1,19 @@
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.OrganizationFeatures.Organizations.Interfaces;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.AdminConsole.Services;
using Bit.Core.Billing.Caches;
using Bit.Core.Billing.Constants;
using Bit.Core.Billing.Models;
using Bit.Core.Billing.Notifications;
using Bit.Core.Billing.Services;
using Bit.Core.Billing.Services.Implementations;
using Bit.Core.Billing.Subscriptions.Models;
using Bit.Core.Billing.Tax.Models;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
@@ -1771,4 +1778,238 @@ public class SubscriberServiceTests
}
#endregion
#region DisableSubscriberAsync
[Theory, BitAutoData]
public async Task DisableSubscriberAsync_UserId_DisablesPremium(
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var userId = Guid.NewGuid();
var subscriberId = new UserId(userId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var userService = sutProvider.GetDependency<IUserService>();
// Act
await sutProvider.Sut.DisableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await userService.Received(1).DisablePremiumAsync(userId, currentPeriodEnd);
}
[Theory, BitAutoData]
public async Task DisableSubscriberAsync_OrganizationId_DisablesOrganizationAndNotifies(
Organization organization,
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var organizationId = organization.Id;
var subscriberId = new OrganizationId(organizationId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var organizationDisableCommand = sutProvider.GetDependency<IOrganizationDisableCommand>();
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
var pushNotificationAdapter = sutProvider.GetDependency<IPushNotificationAdapter>();
organizationRepository.GetByIdAsync(organizationId).Returns(organization);
// Act
await sutProvider.Sut.DisableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await organizationDisableCommand.Received(1).DisableAsync(organizationId, currentPeriodEnd);
await organizationRepository.Received(1).GetByIdAsync(organizationId);
await pushNotificationAdapter.Received(1).NotifyEnabledChangedAsync(organization);
}
[Theory, BitAutoData]
public async Task DisableSubscriberAsync_OrganizationId_OrganizationNotFound_DoesNotNotify(
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var organizationId = Guid.NewGuid();
var subscriberId = new OrganizationId(organizationId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var organizationDisableCommand = sutProvider.GetDependency<IOrganizationDisableCommand>();
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
var pushNotificationAdapter = sutProvider.GetDependency<IPushNotificationAdapter>();
organizationRepository.GetByIdAsync(organizationId).ReturnsNull();
// Act
await sutProvider.Sut.DisableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await organizationDisableCommand.Received(1).DisableAsync(organizationId, currentPeriodEnd);
await organizationRepository.Received(1).GetByIdAsync(organizationId);
await pushNotificationAdapter.DidNotReceive().NotifyEnabledChangedAsync(Arg.Any<Organization>());
}
[Theory, BitAutoData]
public async Task DisableSubscriberAsync_ProviderId_DisablesProvider(
Provider provider,
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var providerId = provider.Id;
provider.Enabled = true;
var subscriberId = new ProviderId(providerId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
var providerService = sutProvider.GetDependency<IProviderService>();
providerRepository.GetByIdAsync(providerId).Returns(provider);
// Act
await sutProvider.Sut.DisableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await providerRepository.Received(1).GetByIdAsync(providerId);
await providerService.Received(1).UpdateAsync(Arg.Is<Provider>(p => p.Id == providerId && p.Enabled == false));
}
[Theory, BitAutoData]
public async Task DisableSubscriberAsync_ProviderId_ProviderNotFound_DoesNotUpdate(
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var providerId = Guid.NewGuid();
var subscriberId = new ProviderId(providerId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
var providerService = sutProvider.GetDependency<IProviderService>();
providerRepository.GetByIdAsync(providerId).ReturnsNull();
// Act
await sutProvider.Sut.DisableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await providerRepository.Received(1).GetByIdAsync(providerId);
await providerService.DidNotReceive().UpdateAsync(Arg.Any<Provider>());
}
#endregion
#region EnableSubscriberAsync
[Theory, BitAutoData]
public async Task EnableSubscriberAsync_UserId_EnablesPremium(
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var userId = Guid.NewGuid();
var subscriberId = new UserId(userId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var userService = sutProvider.GetDependency<IUserService>();
// Act
await sutProvider.Sut.EnableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await userService.Received(1).EnablePremiumAsync(userId, currentPeriodEnd);
}
[Theory, BitAutoData]
public async Task EnableSubscriberAsync_OrganizationId_EnablesOrganizationAndNotifies(
Organization organization,
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var organizationId = organization.Id;
var subscriberId = new OrganizationId(organizationId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var organizationEnableCommand = sutProvider.GetDependency<IOrganizationEnableCommand>();
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
var pushNotificationAdapter = sutProvider.GetDependency<IPushNotificationAdapter>();
organizationRepository.GetByIdAsync(organizationId).Returns(organization);
// Act
await sutProvider.Sut.EnableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await organizationEnableCommand.Received(1).EnableAsync(organizationId, currentPeriodEnd);
await organizationRepository.Received(1).GetByIdAsync(organizationId);
await pushNotificationAdapter.Received(1).NotifyEnabledChangedAsync(organization);
}
[Theory, BitAutoData]
public async Task EnableSubscriberAsync_OrganizationId_OrganizationNotFound_DoesNotNotify(
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var organizationId = Guid.NewGuid();
var subscriberId = new OrganizationId(organizationId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var organizationEnableCommand = sutProvider.GetDependency<IOrganizationEnableCommand>();
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
var pushNotificationAdapter = sutProvider.GetDependency<IPushNotificationAdapter>();
organizationRepository.GetByIdAsync(organizationId).ReturnsNull();
// Act
await sutProvider.Sut.EnableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await organizationEnableCommand.Received(1).EnableAsync(organizationId, currentPeriodEnd);
await organizationRepository.Received(1).GetByIdAsync(organizationId);
await pushNotificationAdapter.DidNotReceive().NotifyEnabledChangedAsync(Arg.Any<Organization>());
}
[Theory, BitAutoData]
public async Task EnableSubscriberAsync_ProviderId_EnablesProvider(
Provider provider,
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var providerId = provider.Id;
provider.Enabled = false;
var subscriberId = new ProviderId(providerId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
var providerService = sutProvider.GetDependency<IProviderService>();
providerRepository.GetByIdAsync(providerId).Returns(provider);
// Act
await sutProvider.Sut.EnableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await providerRepository.Received(1).GetByIdAsync(providerId);
await providerService.Received(1).UpdateAsync(Arg.Is<Provider>(p => p.Id == providerId && p.Enabled == true));
}
[Theory, BitAutoData]
public async Task EnableSubscriberAsync_ProviderId_ProviderNotFound_DoesNotUpdate(
SutProvider<SubscriberService> sutProvider)
{
// Arrange
var providerId = Guid.NewGuid();
var subscriberId = new ProviderId(providerId);
var currentPeriodEnd = DateTime.UtcNow.AddDays(30);
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
var providerService = sutProvider.GetDependency<IProviderService>();
providerRepository.GetByIdAsync(providerId).ReturnsNull();
// Act
await sutProvider.Sut.EnableSubscriberAsync(subscriberId, currentPeriodEnd);
// Assert
await providerRepository.Received(1).GetByIdAsync(providerId);
await providerService.DidNotReceive().UpdateAsync(Arg.Any<Provider>());
}
#endregion
}