Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit e168901

Browse files
test: add tests for if branches
1 parent 234cfb9 commit e168901

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

services/tests/test_billing.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,153 @@ def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscri
537537
assert owner.plan_activated_users == [4, 6, 3]
538538
assert owner.plan_user_count == 9
539539

540+
@freeze_time("2017-03-19T00:00:00")
541+
@patch("services.billing.stripe.Customer.retrieve")
542+
@patch("services.billing.stripe.Subscription.modify")
543+
@patch("services.billing.stripe.Customer.modify")
544+
@patch("services.billing.stripe.Refund.create")
545+
@patch("services.billing.stripe.Invoice.list")
546+
@patch("services.billing.stripe.Subscription.cancel")
547+
@patch("services.billing.stripe.Subscription.retrieve")
548+
@patch("services.billing.stripe.SubscriptionSchedule.release")
549+
def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscription_immediately_with_grace_year_but_no_invoices_to_refund(
550+
self,
551+
schedule_release_mock,
552+
retrieve_subscription_mock,
553+
cancel_sub_mock,
554+
list_invoice_mock,
555+
create_refund_mock,
556+
modify_customer_mock,
557+
modify_sub_mock,
558+
retrieve_customer_mock,
559+
):
560+
with open("./services/tests/samples/stripe_invoice.json") as f:
561+
stripe_invoice_response = json.load(f)
562+
for invoice in stripe_invoice_response["data"]:
563+
invoice["charge"] = None
564+
list_invoice_mock.return_value = stripe_invoice_response
565+
plan = PlanName.CODECOV_PRO_YEARLY.value
566+
stripe_subscription_id = "sub_1K77Y5GlVGuVgOrkJrLjRnne"
567+
stripe_schedule_id = "sub_sched_sch1K77Y5GlVGuVgOrkJrLjRnne"
568+
customer_id = "cus_HF6p8Zx7JdRS7A"
569+
owner = OwnerFactory(
570+
stripe_subscription_id=stripe_subscription_id,
571+
plan=plan,
572+
plan_activated_users=[4, 6, 3],
573+
plan_user_count=9,
574+
stripe_customer_id=customer_id,
575+
)
576+
subscription_params = {
577+
"schedule_id": stripe_schedule_id,
578+
"start_date": 1489799420,
579+
"end_date": 1492477820,
580+
"quantity": 10,
581+
"name": plan,
582+
"id": 215,
583+
"plan": {
584+
"new_plan": "plan_H6P3KZXwmAbqPS",
585+
"new_quantity": 7,
586+
"subscription_id": "sub_123",
587+
"interval": "year",
588+
},
589+
}
590+
591+
retrieve_subscription_mock.return_value = MockSubscription(subscription_params)
592+
retrieve_customer_mock.return_value = {
593+
"id": "cus_HF6p8Zx7JdRS7A",
594+
"metadata": {"autorefunds_remaining": "1"},
595+
}
596+
self.stripe.delete_subscription(owner)
597+
schedule_release_mock.assert_called_once_with(stripe_schedule_id)
598+
retrieve_customer_mock.assert_called_once_with(owner.stripe_customer_id)
599+
cancel_sub_mock.assert_called_once_with(stripe_subscription_id)
600+
list_invoice_mock.assert_called_once_with(
601+
subscription=stripe_subscription_id,
602+
status="paid",
603+
created={"created.gte": 1458263420, "created.lt": 1489799420},
604+
)
605+
create_refund_mock.assert_not_called()
606+
modify_customer_mock.assert_not_called()
607+
modify_sub_mock.assert_not_called()
608+
609+
owner.refresh_from_db()
610+
assert owner.stripe_subscription_id == stripe_subscription_id
611+
assert owner.plan == plan
612+
assert owner.plan_activated_users == [4, 6, 3]
613+
assert owner.plan_user_count == 9
614+
615+
@freeze_time("2017-03-19T00:00:00")
616+
@patch("services.billing.stripe.Customer.retrieve")
617+
@patch("services.billing.stripe.Subscription.modify")
618+
@patch("services.billing.stripe.Customer.modify")
619+
@patch("services.billing.stripe.Refund.create")
620+
@patch("services.billing.stripe.Invoice.list")
621+
@patch("services.billing.stripe.Subscription.cancel")
622+
@patch("services.billing.stripe.Subscription.retrieve")
623+
@patch("services.billing.stripe.SubscriptionSchedule.release")
624+
def test_delete_subscription_with_schedule_releases_schedule_and_cancels_subscription_at_end_of_billing_cycle_as_no_more_autorefunds_available(
625+
self,
626+
schedule_release_mock,
627+
retrieve_subscription_mock,
628+
cancel_sub_mock,
629+
list_invoice_mock,
630+
create_refund_mock,
631+
modify_customer_mock,
632+
modify_sub_mock,
633+
retrieve_customer_mock,
634+
):
635+
with open("./services/tests/samples/stripe_invoice.json") as f:
636+
stripe_invoice_response = json.load(f)
637+
list_invoice_mock.return_value = stripe_invoice_response
638+
plan = PlanName.CODECOV_PRO_YEARLY.value
639+
stripe_subscription_id = "sub_1K77Y5GlVGuVgOrkJrLjRnne"
640+
stripe_schedule_id = "sub_sched_sch1K77Y5GlVGuVgOrkJrLjRnne"
641+
customer_id = "cus_HF6p8Zx7JdRS7A"
642+
owner = OwnerFactory(
643+
stripe_subscription_id=stripe_subscription_id,
644+
plan=plan,
645+
plan_activated_users=[4, 6, 3],
646+
plan_user_count=9,
647+
stripe_customer_id=customer_id,
648+
)
649+
subscription_params = {
650+
"schedule_id": stripe_schedule_id,
651+
"start_date": 1489799420,
652+
"end_date": 1492477820,
653+
"quantity": 10,
654+
"name": plan,
655+
"id": 215,
656+
"plan": {
657+
"new_plan": "plan_H6P3KZXwmAbqPS",
658+
"new_quantity": 7,
659+
"subscription_id": "sub_123",
660+
"interval": "year",
661+
},
662+
}
663+
664+
retrieve_subscription_mock.return_value = MockSubscription(subscription_params)
665+
retrieve_customer_mock.return_value = {
666+
"id": "cus_HF6p8Zx7JdRS7A",
667+
"metadata": {"autorefunds_remaining": "0"},
668+
}
669+
self.stripe.delete_subscription(owner)
670+
schedule_release_mock.assert_called_once_with(stripe_schedule_id)
671+
retrieve_customer_mock.assert_called_once_with(owner.stripe_customer_id)
672+
cancel_sub_mock.assert_not_called()
673+
create_refund_mock.assert_not_called()
674+
modify_customer_mock.assert_not_called()
675+
modify_sub_mock.assert_called_once_with(
676+
stripe_subscription_id,
677+
cancel_at_period_end=True,
678+
proration_behavior="none",
679+
)
680+
681+
owner.refresh_from_db()
682+
assert owner.stripe_subscription_id == stripe_subscription_id
683+
assert owner.plan == plan
684+
assert owner.plan_activated_users == [4, 6, 3]
685+
assert owner.plan_user_count == 9
686+
540687
@patch("services.billing.stripe.Subscription.modify")
541688
@patch("services.billing.stripe.Subscription.retrieve")
542689
def test_modify_subscription_without_schedule_increases_user_count_immediately(

0 commit comments

Comments
 (0)