1
0
mirror of https://github.com/bitwarden/server synced 2026-02-27 09:53:42 +00:00
Files
server/test/Billing.Test/Services/CouponDeletedHandlerTests.cs
Kyle Denney 5b20ee9184 [PM-30275] stripe coupon deleted handler (#7073)
* [PM-30275] stripe coupon deleted handler

* will commit this in a different PR
2026-02-25 09:06:51 -06:00

73 lines
2.4 KiB
C#

using Bit.Billing.Services.Implementations;
using Bit.Core.Billing.Subscriptions.Entities;
using Bit.Core.Billing.Subscriptions.Repositories;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Stripe;
using Xunit;
namespace Bit.Billing.Test.Services;
public class CouponDeletedHandlerTests
{
private readonly ILogger<CouponDeletedHandler> _logger = Substitute.For<ILogger<CouponDeletedHandler>>();
private readonly ISubscriptionDiscountRepository _subscriptionDiscountRepository = Substitute.For<ISubscriptionDiscountRepository>();
private readonly CouponDeletedHandler _sut;
public CouponDeletedHandlerTests()
{
_sut = new CouponDeletedHandler(_logger, _subscriptionDiscountRepository);
}
[Fact]
public async Task HandleAsync_EventObjectNotCoupon_ReturnsWithoutDeleting()
{
// Arrange
var stripeEvent = new Event
{
Id = "evt_test",
Data = new EventData { Object = new Customer { Id = "cus_unexpected" } }
};
// Act
await _sut.HandleAsync(stripeEvent);
// Assert
await _subscriptionDiscountRepository.DidNotReceiveWithAnyArgs()
.GetByStripeCouponIdAsync(null!);
await _subscriptionDiscountRepository.DidNotReceiveWithAnyArgs()
.DeleteAsync(null!);
}
[Fact]
public async Task HandleAsync_CouponNotInDatabase_DoesNotDeleteAnything()
{
// Arrange
var stripeEvent = new Event { Data = new EventData { Object = new Coupon { Id = "cou_test" } } };
_subscriptionDiscountRepository.GetByStripeCouponIdAsync("cou_test").Returns((SubscriptionDiscount?)null);
// Act
await _sut.HandleAsync(stripeEvent);
// Assert
await _subscriptionDiscountRepository.DidNotReceiveWithAnyArgs().DeleteAsync(null!);
}
[Fact]
public async Task HandleAsync_CouponExistsInDatabase_DeletesDiscount()
{
// Arrange
var stripeEvent = new Event { Data = new EventData { Object = new Coupon { Id = "cou_test" } } };
var discount = new SubscriptionDiscount { StripeCouponId = "cou_test" };
_subscriptionDiscountRepository.GetByStripeCouponIdAsync("cou_test").Returns(discount);
// Act
await _sut.HandleAsync(stripeEvent);
// Assert
await _subscriptionDiscountRepository.Received(1).DeleteAsync(discount);
}
}