From b933007f091aa9fd1ff3d707362a912ae8ef882b Mon Sep 17 00:00:00 2001 From: jrmccannon Date: Fri, 28 Mar 2025 09:09:44 -0500 Subject: [PATCH] Removed unused classes --- .../Validation/SecretsManager/Errors.cs | 32 --- .../SecretsManagerInviteUserValidation.cs | 43 ---- .../SecretsManagerSubscriptionUpdate.cs | 91 -------- ...SecretsManagerInviteUserValidationTests.cs | 194 ------------------ 4 files changed, 360 deletions(-) delete mode 100644 src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/Errors.cs delete mode 100644 src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/SecretsManagerInviteUserValidation.cs delete mode 100644 src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/SecretsManagerSubscriptionUpdate.cs delete mode 100644 test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManagerInviteUserValidationTests.cs diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/Errors.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/Errors.cs deleted file mode 100644 index 227665a0c5..0000000000 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/Errors.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Bit.Core.AdminConsole.Errors; - -namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Validation.SecretsManager; - -public record OrganizationNoSecretsManagerError(SecretsManagerSubscriptionUpdate InvalidRequest) - : Error(Code, InvalidRequest) -{ - public const string Code = "Organization has no access to Secrets Manager"; -} - -public record SecretsManagerAdditionalSeatLimitReachedError(SecretsManagerSubscriptionUpdate InvalidRequest) - : Error(GetErrorMessage(InvalidRequest), InvalidRequest) -{ - public const string Code = "You have reached the maximum number of Secrets Manager seats ({0}) for this plan."; - - public static string GetErrorMessage(SecretsManagerSubscriptionUpdate invalidRequest) => - string.Format(Code, - invalidRequest.SecretsManagerPlan.BaseSeats + - invalidRequest.SecretsManagerPlan.MaxAdditionalSeats.GetValueOrDefault()); -} - -public record SecretsManagerSeatLimitReachedError(SecretsManagerSubscriptionUpdate InvalidRequest) - : Error(Code, InvalidRequest) -{ - public const string Code = "Secrets Manager seat limit has been reached."; -} - -public record SecretsManagerCannotExceedPasswordManagerError(SecretsManagerSubscriptionUpdate InvalidRequest) - : Error(Code, InvalidRequest) -{ - public const string Code = "You cannot have more Secrets Manager seats than Password Manager seats."; -} diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/SecretsManagerInviteUserValidation.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/SecretsManagerInviteUserValidation.cs deleted file mode 100644 index 610dbd2b7a..0000000000 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/SecretsManagerInviteUserValidation.cs +++ /dev/null @@ -1,43 +0,0 @@ -using Bit.Core.AdminConsole.Shared.Validation; - -namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Validation.SecretsManager; - -public static class SecretsManagerInviteUserValidation -{ - public static ValidationResult Validate( - SecretsManagerSubscriptionUpdate subscriptionUpdate) => - subscriptionUpdate switch - { - { UseSecretsManger: false, NewUsersToAdd: > 0 } => - new Invalid( - new OrganizationNoSecretsManagerError(subscriptionUpdate)), - - { UseSecretsManger: false, NewUsersToAdd: 0 } or { UseSecretsManger: true, Seats: null } => - new Valid(subscriptionUpdate), - - { UseSecretsManger: true, SecretsManagerPlan.HasAdditionalSeatsOption: false } => - new Invalid( - new SecretsManagerAdditionalSeatLimitReachedError(subscriptionUpdate)), - - { UseSecretsManger: true, SecretsManagerPlan.MaxAdditionalSeats: var planMaxSeats } - when planMaxSeats < subscriptionUpdate.NewUsersToAdd => - new Invalid( - new SecretsManagerAdditionalSeatLimitReachedError(subscriptionUpdate)), - - { UseSecretsManger: true, UpdatedSeatTotal: var updateSeatTotal, MaxAutoScaleSeats: var maxAutoScaleSeats } - when updateSeatTotal > maxAutoScaleSeats => - new Invalid( - new SecretsManagerSeatLimitReachedError(subscriptionUpdate)), - - { - UseSecretsManger: true, - PasswordManagerUpdatedSeatTotal: var passwordManagerUpdatedSeatTotal, - UpdatedSeatTotal: var secretsManagerUpdatedSeatTotal - } - when passwordManagerUpdatedSeatTotal < secretsManagerUpdatedSeatTotal => - new Invalid( - new SecretsManagerCannotExceedPasswordManagerError(subscriptionUpdate)), - - _ => new Valid(subscriptionUpdate) - }; -} diff --git a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/SecretsManagerSubscriptionUpdate.cs b/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/SecretsManagerSubscriptionUpdate.cs deleted file mode 100644 index eeae1cf947..0000000000 --- a/src/Core/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManager/SecretsManagerSubscriptionUpdate.cs +++ /dev/null @@ -1,91 +0,0 @@ -using Bit.Core.AdminConsole.Models.Business; -using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Models; -using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Validation.PasswordManager; -using Bit.Core.Models.StaticStore; - -namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Validation.SecretsManager; - -public class SecretsManagerSubscriptionUpdate -{ - public bool UseSecretsManger { get; } - - /// - /// Seats the organization has - /// - public int? Seats { get; } - - /// - /// Max number of seats that the organization can have - /// - public int? MaxAutoScaleSeats { get; } - - /// - /// Seats currently occupied by current users - /// - public int OccupiedSeats { get; } - - /// - /// Users to add to the organization seats - /// - public int NewUsersToAdd { get; } - - public int? PasswordManagerUpdatedSeatTotal { get; } - public Plan.SecretsManagerPlanFeatures SecretsManagerPlan { get; } - - /// - /// Number of seats available for users - /// - public int? AvailableSeats => Seats - OccupiedSeats; - - /// - /// Number of seats to scale the organization to. - /// - /// If Organization has no seat limit (Seats is null), then there are no new seats to add. - /// - public int SeatsRequiredToAdd => AvailableSeats.HasValue ? Math.Max(NewUsersToAdd - AvailableSeats.Value, 0) : 0; - - /// - /// New total of seats for the organization - /// - public int? UpdatedSeatTotal => Seats + SeatsRequiredToAdd; - - private SecretsManagerSubscriptionUpdate(bool useSecretsManger, - int? organizationSeats, - int? organizationAutoScaleSeatLimit, - int currentSeats, - int seatsToAdd, - int? passwordManagerUpdatedSeatTotal, - Plan.SecretsManagerPlanFeatures plan) - { - UseSecretsManger = useSecretsManger; - Seats = organizationSeats; - MaxAutoScaleSeats = organizationAutoScaleSeatLimit; - OccupiedSeats = currentSeats; - NewUsersToAdd = seatsToAdd; - PasswordManagerUpdatedSeatTotal = passwordManagerUpdatedSeatTotal; - SecretsManagerPlan = plan; - } - - public SecretsManagerSubscriptionUpdate(InviteOrganization inviteOrganization, int occupiedSeats, int seatsToAdd, int passwordManagerSeatTotal) : - this( - useSecretsManger: inviteOrganization.UseSecretsManager, - organizationSeats: inviteOrganization.SmSeats, - organizationAutoScaleSeatLimit: inviteOrganization.SmMaxAutoScaleSeats, - currentSeats: occupiedSeats, - seatsToAdd: seatsToAdd, - passwordManagerUpdatedSeatTotal: passwordManagerSeatTotal, - plan: inviteOrganization.Plan.SecretsManager) - { } - - public SecretsManagerSubscriptionUpdate(InviteUserOrganizationValidationRequest request, - PasswordManagerSubscriptionUpdate passwordManagerSubscriptionUpdate) : - this( - useSecretsManger: request.InviteOrganization.UseSecretsManager, - organizationSeats: request.InviteOrganization.SmSeats, - organizationAutoScaleSeatLimit: request.InviteOrganization.SmMaxAutoScaleSeats, - currentSeats: request.OccupiedSmSeats, - seatsToAdd: request.Invites.Count(x => x.AccessSecretsManager), - passwordManagerUpdatedSeatTotal: passwordManagerSubscriptionUpdate.UpdatedSeatTotal, - plan: request.InviteOrganization.Plan.SecretsManager) - { } -} diff --git a/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManagerInviteUserValidationTests.cs b/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManagerInviteUserValidationTests.cs deleted file mode 100644 index 0240f43348..0000000000 --- a/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/InviteUsers/Validation/SecretsManagerInviteUserValidationTests.cs +++ /dev/null @@ -1,194 +0,0 @@ -using Bit.Core.AdminConsole.Entities; -using Bit.Core.AdminConsole.Models.Business; -using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Models; -using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Validation.PasswordManager; -using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Validation.SecretsManager; -using Bit.Core.AdminConsole.Shared.Validation; -using Bit.Core.Billing.Enums; -using Bit.Core.Billing.Models.StaticStore.Plans; -using Bit.Core.Enums; -using Bit.Core.Models.Data; -using Bit.Test.Common.AutoFixture.Attributes; -using Xunit; - -namespace Bit.Core.Test.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Validation; - -[SutProviderCustomize] -public class SecretsManagerInviteUserValidationTests -{ - [Theory] - [BitAutoData] - public void Validate_GivenOrganizationDoesNotHaveSecretsManagerAndNotTryingToAddSecretsManagerUser_ThenTheRequestIsValid( - Organization organization) - { - organization.UseSecretsManager = false; - - var organizationDto = new InviteOrganization(organization, new FreePlan()); - var subscriptionUpdate = new PasswordManagerSubscriptionUpdate(organizationDto, 0, 0); - - var request = new InviteUserOrganizationValidationRequest - { - Invites = [new OrganizationUserInvite("test@email.com", "", false)], - InviteOrganization = organizationDto, - PerformedBy = Guid.Empty, - PerformedAt = default, - OccupiedPmSeats = 0, - OccupiedSmSeats = 0 - }; - - var update = new SecretsManagerSubscriptionUpdate(request, subscriptionUpdate); - - var result = SecretsManagerInviteUserValidation.Validate(update); - - Assert.IsType>(result); - } - - [Theory] - [BitAutoData] - public void Validate_GivenOrganizationDoesNotHaveSecretsManagerAndTryingToAddSecretsManagerUser_ThenShouldReturnInvalidMessage( - Organization organization) - { - organization.UseSecretsManager = false; - - var organizationDto = new InviteOrganization(organization, new FreePlan()); - var subscriptionUpdate = new PasswordManagerSubscriptionUpdate(organizationDto, 0, 0); - - var invite = new OrganizationUserInvite( - email: "email@test.com", - assignedCollections: [], - groups: [], - type: OrganizationUserType.User, - permissions: new Permissions(), - externalId: string.Empty, - accessSecretsManager: true); - - var request = new InviteUserOrganizationValidationRequest - { - Invites = [invite], - InviteOrganization = organizationDto, - PerformedBy = Guid.Empty, - PerformedAt = default, - OccupiedPmSeats = 0, - OccupiedSmSeats = 0 - }; - - var update = new SecretsManagerSubscriptionUpdate(request, subscriptionUpdate); - - var result = SecretsManagerInviteUserValidation.Validate(update); - - Assert.IsType>(result); - Assert.Equal(OrganizationNoSecretsManagerError.Code, (result as Invalid)!.ErrorMessageString); - } - - [Theory] - [BitAutoData] - public void Validate_GivenOrganizationHasSecretsManagerWithoutASeatLimit_ThenShouldBeAllowedToAddSecretsManagerUsers( - Organization organization) - { - organization.SmSeats = null; - organization.UseSecretsManager = true; - - var organizationDto = new InviteOrganization(organization, new FreePlan()); - var subscriptionUpdate = new PasswordManagerSubscriptionUpdate(organizationDto, 0, 0); - - var request = new InviteUserOrganizationValidationRequest - { - Invites = [new OrganizationUserInvite( - email: "email@test.com", - assignedCollections: [], - groups: [], - type: OrganizationUserType.User, - permissions: new Permissions(), - externalId: string.Empty, - accessSecretsManager: true)], - InviteOrganization = organizationDto, - PerformedBy = Guid.Empty, - PerformedAt = default, - OccupiedPmSeats = 0, - OccupiedSmSeats = 0 - }; - - var update = new SecretsManagerSubscriptionUpdate(request, subscriptionUpdate); - - var result = SecretsManagerInviteUserValidation.Validate(update); - - Assert.IsType>(result); - } - - [Theory] - [BitAutoData] - public void Validate_GivenOrganizationPlanDoesNotAllowAdditionalSeats_ThenShouldNotBeAllowedToAddSecretsManagerUsers( - Organization organization) - { - organization.SmSeats = 4; - organization.MaxAutoscaleSmSeats = 4; - organization.UseSecretsManager = true; - organization.PlanType = PlanType.EnterpriseAnnually; - - var organizationDto = new InviteOrganization(organization, new Enterprise2023Plan(isAnnual: true)); - var subscriptionUpdate = new PasswordManagerSubscriptionUpdate(organizationDto, 0, 0); - - var request = new InviteUserOrganizationValidationRequest - { - Invites = [new OrganizationUserInvite( - email: "email@test.com", - assignedCollections: [], - groups: [], - type: OrganizationUserType.User, - permissions: new Permissions(), - externalId: string.Empty, - accessSecretsManager: true)], - InviteOrganization = organizationDto, - PerformedBy = Guid.Empty, - PerformedAt = default, - OccupiedPmSeats = 0, - OccupiedSmSeats = 4 - }; - - var update = new SecretsManagerSubscriptionUpdate(request, subscriptionUpdate); - - var result = SecretsManagerInviteUserValidation.Validate(update); - - Assert.IsType>(result); - Assert.Equal(SecretsManagerSeatLimitReachedError.Code, (result as Invalid)!.ErrorMessageString); - } - - [Theory] - [BitAutoData] - public void Validate_GivenPasswordManagerSeatsAreTheSameAsSecretsManagerSeats_WhenAttemptingToAddASecretManagerSeatOnly_ThenShouldNotBeAllowedToAddSecretsManagerUsers( - Organization organization) - { - organization.Seats = 0; - organization.SmSeats = 4; - organization.MaxAutoscaleSmSeats = 5; - organization.UseSecretsManager = true; - organization.PlanType = PlanType.EnterpriseAnnually; - - var inviteOrganization = new InviteOrganization(organization, new Enterprise2023Plan(isAnnual: true)); - var subscriptionUpdate = new PasswordManagerSubscriptionUpdate(inviteOrganization, 0, 0); - - var request = new InviteUserOrganizationValidationRequest - { - Invites = [new OrganizationUserInvite( - email: "email@test.com", - assignedCollections: [], - groups: [], - type: OrganizationUserType.User, - permissions: new Permissions(), - externalId: string.Empty, - accessSecretsManager: true)], - InviteOrganization = inviteOrganization, - PerformedBy = Guid.Empty, - PerformedAt = default, - OccupiedPmSeats = 0, - OccupiedSmSeats = 4 - }; - - var update = new SecretsManagerSubscriptionUpdate(request, subscriptionUpdate); - - var result = SecretsManagerInviteUserValidation.Validate(update); - - Assert.IsType>(result); - Assert.Equal(SecretsManagerCannotExceedPasswordManagerError.Code, (result as Invalid)!.ErrorMessageString); - } -}