1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00
bitwarden-server/test/Api.Test/Billing/Utilities.cs
Alex Morask 398741cec4
[AC-2888] Improve consolidated billing error handling (#4548)
* Fix error handling in provider setup process

This update ensures that when 'enable-consolidated-billing' is on, any exception thrown during the Stripe customer or subscription setup process for the provider will block the remainder of the setup process so the provider does not enter an invalid state

* Refactor the way BillingException is thrown

Made it simpler to just use the exception constructor and also ensured it was added to the exception handling middleware so it could provide a simple response to the client

* Handle all Stripe exceptions in exception handling middleware

* Fixed error response output for billing's provider controllers

* Cleaned up billing owned provider controllers

Changes were made based on feature updates by product and stuff that's no longer needed. No need to expose sensitive endpoints when they're not being used.

* Reafctored get invoices

Removed unnecssarily bloated method from SubscriberService

* Updated error handling for generating the client invoice report

* Moved get provider subscription to controller

This is only used once and the service layer doesn't seem like the correct choice anymore when thinking about error handling with retrieval

* Handled bad request for update tax information

* Split out Stripe configuration from unauthorization

* Run dotnet format

* Addison's feedback
2024-07-31 09:26:44 -04:00

71 lines
2.2 KiB
C#

using Bit.Api.Billing.Controllers;
using Bit.Core;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.Enums.Provider;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Context;
using Bit.Core.Models.Api;
using Bit.Core.Services;
using Bit.Test.Common.AutoFixture;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using Xunit;
namespace Bit.Api.Test.Billing;
public static class Utilities
{
public static void AssertNotFound(IResult result)
{
Assert.IsType<NotFound<ErrorResponseModel>>(result);
var response = ((NotFound<ErrorResponseModel>)result).Value;
Assert.Equal("Resource not found.", response.Message);
}
public static void AssertUnauthorized(IResult result)
{
Assert.IsType<JsonHttpResult<ErrorResponseModel>>(result);
var response = (JsonHttpResult<ErrorResponseModel>)result;
Assert.Equal(StatusCodes.Status401Unauthorized, response.StatusCode);
Assert.Equal("Unauthorized.", response.Value.Message);
}
public static void ConfigureStableAdminInputs<T>(
Provider provider,
SutProvider<T> sutProvider) where T : BaseProviderController
{
ConfigureBaseInputs(provider, sutProvider);
sutProvider.GetDependency<ICurrentContext>().ProviderProviderAdmin(provider.Id)
.Returns(true);
}
public static void ConfigureStableServiceUserInputs<T>(
Provider provider,
SutProvider<T> sutProvider) where T : BaseProviderController
{
ConfigureBaseInputs(provider, sutProvider);
sutProvider.GetDependency<ICurrentContext>().ProviderUser(provider.Id)
.Returns(true);
}
private static void ConfigureBaseInputs<T>(
Provider provider,
SutProvider<T> sutProvider) where T : BaseProviderController
{
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
.Returns(true);
provider.Type = ProviderType.Msp;
provider.Status = ProviderStatusType.Billable;
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
}
}