mirror of
https://github.com/bitwarden/server
synced 2025-12-06 00:03:34 +00:00
* ignore serena * removing unused properties from org metadata * removing further properties that can already be fetched on the client side using available data * new vnext endpoint for org metadata plus caching metadata first pass including new feature flag # Conflicts: # src/Core/Constants.cs * [PM-25379] decided against cache and new query shouldn't use the service * pr feedback removing unneeded response model * run dotnet format
122 lines
4.3 KiB
C#
122 lines
4.3 KiB
C#
using Bit.Core.AdminConsole.Entities;
|
|
using Bit.Core.Billing.Constants;
|
|
using Bit.Core.Billing.Organizations.Services;
|
|
using Bit.Core.Billing.Pricing;
|
|
using Bit.Core.Billing.Services;
|
|
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Core.Utilities;
|
|
using Bit.Test.Common.AutoFixture;
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
using NSubstitute;
|
|
using Stripe;
|
|
using Xunit;
|
|
|
|
namespace Bit.Core.Test.Billing.Services;
|
|
|
|
[SutProviderCustomize]
|
|
public class OrganizationBillingServiceTests
|
|
{
|
|
#region GetMetadata
|
|
|
|
[Theory, BitAutoData]
|
|
public async Task GetMetadata_Succeeds(
|
|
Guid organizationId,
|
|
Organization organization,
|
|
SutProvider<OrganizationBillingService> sutProvider)
|
|
{
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organizationId).Returns(organization);
|
|
sutProvider.GetDependency<IPricingClient>().ListPlans().Returns(StaticStore.Plans.ToList());
|
|
|
|
sutProvider.GetDependency<IPricingClient>().GetPlanOrThrow(organization.PlanType)
|
|
.Returns(StaticStore.GetPlan(organization.PlanType));
|
|
|
|
var subscriberService = sutProvider.GetDependency<ISubscriberService>();
|
|
var organizationSeatCount = new OrganizationSeatCounts { Users = 1, Sponsored = 0 };
|
|
var customer = new Customer
|
|
{
|
|
Discount = new Discount
|
|
{
|
|
Coupon = new Coupon
|
|
{
|
|
Id = StripeConstants.CouponIDs.SecretsManagerStandalone,
|
|
AppliesTo = new CouponAppliesTo
|
|
{
|
|
Products = ["product_id"]
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
subscriberService
|
|
.GetCustomer(organization, Arg.Is<CustomerGetOptions>(options =>
|
|
options.Expand.Contains("discount.coupon.applies_to")))
|
|
.Returns(customer);
|
|
|
|
subscriberService.GetSubscription(organization).Returns(new Subscription
|
|
{
|
|
Items = new StripeList<SubscriptionItem>
|
|
{
|
|
Data =
|
|
[
|
|
new SubscriptionItem
|
|
{
|
|
Plan = new Plan
|
|
{
|
|
ProductId = "product_id"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
});
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>()
|
|
.GetOccupiedSeatCountByOrganizationIdAsync(organization.Id)
|
|
.Returns(new OrganizationSeatCounts { Users = 1, Sponsored = 0 });
|
|
|
|
var metadata = await sutProvider.Sut.GetMetadata(organizationId);
|
|
|
|
Assert.True(metadata!.IsOnSecretsManagerStandalone);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetMetadata - Null Customer or Subscription
|
|
|
|
[Theory, BitAutoData]
|
|
public async Task GetMetadata_WhenCustomerOrSubscriptionIsNull_ReturnsDefaultMetadata(
|
|
Guid organizationId,
|
|
Organization organization,
|
|
SutProvider<OrganizationBillingService> sutProvider)
|
|
{
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organizationId).Returns(organization);
|
|
|
|
sutProvider.GetDependency<IPricingClient>().ListPlans().Returns(StaticStore.Plans.ToList());
|
|
|
|
sutProvider.GetDependency<IPricingClient>().GetPlanOrThrow(organization.PlanType)
|
|
.Returns(StaticStore.GetPlan(organization.PlanType));
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>()
|
|
.GetOccupiedSeatCountByOrganizationIdAsync(organization.Id)
|
|
.Returns(new OrganizationSeatCounts { Users = 1, Sponsored = 0 });
|
|
|
|
var subscriberService = sutProvider.GetDependency<ISubscriberService>();
|
|
|
|
// Set up subscriber service to return null for customer
|
|
subscriberService
|
|
.GetCustomer(organization, Arg.Is<CustomerGetOptions>(options => options.Expand.FirstOrDefault() == "discount.coupon.applies_to"))
|
|
.Returns((Customer)null);
|
|
|
|
// Set up subscriber service to return null for subscription
|
|
subscriberService.GetSubscription(organization).Returns((Subscription)null);
|
|
|
|
var metadata = await sutProvider.Sut.GetMetadata(organizationId);
|
|
|
|
Assert.NotNull(metadata);
|
|
Assert.False(metadata!.IsOnSecretsManagerStandalone);
|
|
Assert.Equal(1, metadata.OrganizationOccupiedSeats);
|
|
}
|
|
|
|
#endregion
|
|
}
|