-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[PM-40292] fix: Handle deleted Stripe customer on Admin user and provider edit pages #8276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
amorask-bitwarden
merged 4 commits into
main
from
billing/pm-40292/unable-to-access-provider-invalid-customerid
Aug 28, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
85b44cd
[PM-40292] fix: Handle deleted Stripe customer on Admin user and prov…
amorask-bitwarden 9a4101a
Merge branch 'main' into billing/PM-40292/unable-to-access-provider-i…
amorask-bitwarden 4c6823b
Guard billing partial in user edit view against null billing info
amorask-bitwarden 3e6a747
Merge branch 'main' into billing/pm-40292/unable-to-access-provider-i…
amorask-bitwarden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| using Bit.Admin.Controllers; | ||
| using Bit.Admin.Models; | ||
| using Bit.Core.Billing.Constants; | ||
| using Bit.Core.Billing.Models; | ||
| using Bit.Core.Billing.Services; | ||
| using Bit.Core.Entities; | ||
| using Bit.Core.Repositories; | ||
| using Bit.Core.Vault.Repositories; | ||
| using Bit.Test.Common.AutoFixture; | ||
| using Bit.Test.Common.AutoFixture.Attributes; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
| using NSubstitute; | ||
| using NSubstitute.ExceptionExtensions; | ||
| using Stripe; | ||
|
|
||
| namespace Admin.Test.Controllers; | ||
|
|
||
| [ControllerCustomize(typeof(UsersController))] | ||
| [SutProviderCustomize] | ||
| public class UsersControllerTests | ||
| { | ||
| private static void StubEditGetDependencies(SutProvider<UsersController> sutProvider, User user) | ||
| { | ||
| sutProvider.GetDependency<IUserRepository>().GetByIdAsync(user.Id).Returns(user); | ||
| sutProvider.GetDependency<ICipherRepository>() | ||
| .GetManyByUserIdAsync(user.Id, false) | ||
| .Returns(new List<Bit.Core.Vault.Models.Data.CipherDetails>()); | ||
|
|
||
| sutProvider.Sut.TempData = | ||
| new TempDataDictionary(new DefaultHttpContext(), Substitute.For<ITempDataProvider>()); | ||
| } | ||
|
|
||
| [BitAutoData] | ||
| [SutProviderCustomize] | ||
| [Theory] | ||
| public async Task Edit_Get_BillingLoadThrows_StillRendersPageWithWarning( | ||
| User user, | ||
| SutProvider<UsersController> sutProvider) | ||
| { | ||
| // PM-40292: a deleted Stripe customer makes GetBillingAsync throw. The page must still | ||
| // render so an admin can correct the Gateway Customer ID rather than being locked out. | ||
| StubEditGetDependencies(sutProvider, user); | ||
|
|
||
| sutProvider.GetDependency<IStripePaymentService>() | ||
| .GetBillingAsync(user) | ||
| .ThrowsAsync(new StripeException | ||
| { | ||
| StripeError = new StripeError { Code = StripeConstants.ErrorCodes.ResourceMissing } | ||
| }); | ||
|
|
||
| var result = await sutProvider.Sut.Edit(user.Id); | ||
|
|
||
| var view = Assert.IsType<ViewResult>(result); | ||
| var model = Assert.IsType<UserEditModel>(view.Model); | ||
| Assert.Null(model.BillingInfo); | ||
| Assert.Null(model.BillingHistoryInfo); | ||
| Assert.True(sutProvider.Sut.TempData.ContainsKey("Warning")); | ||
| Assert.Equal( | ||
| "Billing information could not be loaded. The Stripe customer may have been deleted. " + | ||
| "You can still edit the user and set a valid Gateway Customer ID.", | ||
| (string)sutProvider.Sut.TempData["Warning"]); | ||
| } | ||
|
|
||
| [BitAutoData] | ||
| [SutProviderCustomize] | ||
| [Theory] | ||
| public async Task Edit_Get_BillingHistoryLoadThrows_StillRendersPageWithWarning( | ||
| User user, | ||
| BillingInfo billingInfo, | ||
| SutProvider<UsersController> sutProvider) | ||
| { | ||
| // PM-40292: GetBillingAsync can succeed while GetBillingHistoryAsync throws. The catch must | ||
| // reset both values so the billing section is hidden and the page renders, rather than | ||
| // falling through with a non-null BillingInfo and a null BillingHistoryInfo. | ||
| StubEditGetDependencies(sutProvider, user); | ||
|
|
||
| sutProvider.GetDependency<IStripePaymentService>() | ||
| .GetBillingAsync(user) | ||
| .Returns(billingInfo); | ||
| sutProvider.GetDependency<IStripePaymentService>() | ||
| .GetBillingHistoryAsync(user) | ||
| .ThrowsAsync(new StripeException | ||
| { | ||
| StripeError = new StripeError { Code = StripeConstants.ErrorCodes.ResourceMissing } | ||
| }); | ||
|
|
||
| var result = await sutProvider.Sut.Edit(user.Id); | ||
|
|
||
| var view = Assert.IsType<ViewResult>(result); | ||
| var model = Assert.IsType<UserEditModel>(view.Model); | ||
| Assert.Null(model.BillingInfo); | ||
| Assert.Null(model.BillingHistoryInfo); | ||
| Assert.True(sutProvider.Sut.TempData.ContainsKey("Warning")); | ||
| Assert.Equal( | ||
| "Billing information could not be loaded. The Stripe customer may have been deleted. " + | ||
| "You can still edit the user and set a valid Gateway Customer ID.", | ||
| (string)sutProvider.Sut.TempData["Warning"]); | ||
| } | ||
|
|
||
| [BitAutoData] | ||
| [SutProviderCustomize] | ||
| [Theory] | ||
| public async Task Edit_Get_BillingLoadThrowsUnexpectedError_StillRendersPageWithErrorToast( | ||
| User user, | ||
| SutProvider<UsersController> sutProvider) | ||
| { | ||
| // PM-40292: a billing-load failure that is NOT a missing Stripe customer (resource_missing) | ||
| // must fall through to the generic catch, which surfaces a neutral error toast rather than | ||
| // asserting the customer was deleted. | ||
| StubEditGetDependencies(sutProvider, user); | ||
|
|
||
| sutProvider.GetDependency<IStripePaymentService>() | ||
| .GetBillingAsync(user) | ||
| .ThrowsAsync(new StripeException { StripeError = new StripeError { Code = "api_error" } }); | ||
|
|
||
| var result = await sutProvider.Sut.Edit(user.Id); | ||
|
|
||
| var view = Assert.IsType<ViewResult>(result); | ||
| var model = Assert.IsType<UserEditModel>(view.Model); | ||
| Assert.Null(model.BillingInfo); | ||
| Assert.Null(model.BillingHistoryInfo); | ||
| Assert.False(sutProvider.Sut.TempData.ContainsKey("Warning")); | ||
| Assert.True(sutProvider.Sut.TempData.ContainsKey("Error")); | ||
| Assert.Equal( | ||
| "Billing information could not be loaded. You can still edit the user or try reloading the page. " + | ||
| "Contact support if the problem persists.", | ||
| (string)sutProvider.Sut.TempData["Error"]); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
test/Core.Test/Billing/Extensions/CustomerExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using Bit.Core.Billing.Constants; | ||
| using Bit.Core.Billing.Extensions; | ||
| using Stripe; | ||
| using Xunit; | ||
|
|
||
| namespace Bit.Core.Test.Billing.Extensions; | ||
|
|
||
| public class CustomerExtensionsTests | ||
| { | ||
| [Fact] | ||
| public void ApprovedToPayByInvoice_NullMetadata_ReturnsFalse() | ||
| { | ||
| // PM-40292: a deleted Stripe customer retrieve returns a stub with null Metadata. | ||
| // The unguarded TryGetValue previously NRE'd here, crashing the Provider admin page. | ||
| var customer = new Customer { Metadata = null }; | ||
|
|
||
| Assert.False(customer.ApprovedToPayByInvoice()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ApprovedToPayByInvoice_Approved_ReturnsTrue() | ||
| { | ||
| var customer = new Customer | ||
| { | ||
| Metadata = new Dictionary<string, string> { [StripeConstants.MetadataKeys.InvoiceApproved] = "1" } | ||
| }; | ||
|
|
||
| Assert.True(customer.ApprovedToPayByInvoice()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ApprovedToPayByInvoice_NotApproved_ReturnsFalse() | ||
| { | ||
| var customer = new Customer | ||
| { | ||
| Metadata = new Dictionary<string, string> { [StripeConstants.MetadataKeys.InvoiceApproved] = "0" } | ||
| }; | ||
|
|
||
| Assert.False(customer.ApprovedToPayByInvoice()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ApprovedToPayByInvoice_KeyMissing_ReturnsFalse() | ||
| { | ||
| var customer = new Customer { Metadata = new Dictionary<string, string>() }; | ||
|
|
||
| Assert.False(customer.ApprovedToPayByInvoice()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.