1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 15:42:48 -05:00

[AC-2461] Scale provider seats on client organization deletion (#3996)

* Scaled provider seats on client organization deletion

* Thomas' feedback
This commit is contained in:
Alex Morask
2024-04-19 10:09:18 -04:00
committed by GitHub
parent e6bd8779a6
commit 821f7620b6
4 changed files with 118 additions and 9 deletions

View File

@ -2,8 +2,11 @@
using AutoFixture.Xunit2;
using Bit.Api.AdminConsole.Controllers;
using Bit.Api.AdminConsole.Models.Request.Organizations;
using Bit.Api.Auth.Models.Request.Accounts;
using Bit.Api.Models.Request.Organizations;
using Bit.Core;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Enums.Provider;
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationApiKeys.Interfaces;
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationCollectionEnhancements.Interfaces;
using Bit.Core.AdminConsole.Repositories;
@ -25,6 +28,7 @@ using Bit.Core.OrganizationFeatures.OrganizationSubscriptions.Interface;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Tools.Services;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
using Xunit;
@ -59,6 +63,8 @@ public class OrganizationsControllerTests : IDisposable
private readonly ISubscriberQueries _subscriberQueries;
private readonly IReferenceEventService _referenceEventService;
private readonly IOrganizationEnableCollectionEnhancementsCommand _organizationEnableCollectionEnhancementsCommand;
private readonly IProviderRepository _providerRepository;
private readonly IScaleSeatsCommand _scaleSeatsCommand;
private readonly OrganizationsController _sut;
@ -89,6 +95,8 @@ public class OrganizationsControllerTests : IDisposable
_subscriberQueries = Substitute.For<ISubscriberQueries>();
_referenceEventService = Substitute.For<IReferenceEventService>();
_organizationEnableCollectionEnhancementsCommand = Substitute.For<IOrganizationEnableCollectionEnhancementsCommand>();
_providerRepository = Substitute.For<IProviderRepository>();
_scaleSeatsCommand = Substitute.For<IScaleSeatsCommand>();
_sut = new OrganizationsController(
_organizationRepository,
@ -115,7 +123,9 @@ public class OrganizationsControllerTests : IDisposable
_cancelSubscriptionCommand,
_subscriberQueries,
_referenceEventService,
_organizationEnableCollectionEnhancementsCommand);
_organizationEnableCollectionEnhancementsCommand,
_providerRepository,
_scaleSeatsCommand);
}
public void Dispose()
@ -414,4 +424,39 @@ public class OrganizationsControllerTests : IDisposable
await _organizationEnableCollectionEnhancementsCommand.DidNotReceiveWithAnyArgs().EnableCollectionEnhancements(Arg.Any<Organization>());
await _pushNotificationService.DidNotReceiveWithAnyArgs().PushSyncOrganizationsAsync(Arg.Any<Guid>());
}
[Theory, AutoData]
public async Task Delete_OrganizationIsConsolidatedBillingClient_ScalesProvidersSeats(
Provider provider,
Organization organization,
User user,
Guid organizationId,
SecretVerificationRequestModel requestModel)
{
organization.Status = OrganizationStatusType.Managed;
organization.PlanType = PlanType.TeamsMonthly;
organization.Seats = 10;
provider.Type = ProviderType.Msp;
provider.Status = ProviderStatusType.Billable;
_currentContext.OrganizationOwner(organizationId).Returns(true);
_organizationRepository.GetByIdAsync(organizationId).Returns(organization);
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
_userService.VerifySecretAsync(user, requestModel.Secret).Returns(true);
_featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling).Returns(true);
_providerRepository.GetByOrganizationIdAsync(organization.Id).Returns(provider);
await _sut.Delete(organizationId.ToString(), requestModel);
await _scaleSeatsCommand.Received(1)
.ScalePasswordManagerSeats(provider, organization.PlanType, -organization.Seats.Value);
await _organizationService.Received(1).DeleteAsync(organization);
}
}