def test_cancel_subscription(self): self.mock_fetch_customer.return_value = self.valid_customer3 self.mock_retrieve_stripe_subscriptions.return_value = [ self.subscription_test3 ] self.mock_stripe_modify_subscription.return_value = self.subscription_test3 cancelled = cancel_subscription("cus_test3", "sub_test3") assert cancelled[1] == 201
def test_cancel_subscription_with_invalid_subhub_user(monkeypatch): """ GIVEN an active subscription WHEN provided an api_token and an invalid userid THEN return customer not found error """ cancelled, code = payments.cancel_subscription("invalid_user", "subscription_id") assert 404 == code assert cancelled["message"] == "Customer does not exist."
def test_cancel_subscription_without_valid_user(monkeypatch): """ GIVEN call to cancel subscription WHEN an invalid customer is sent THEN return the appropriate message """ customer = Mock(return_value=None) monkeypatch.setattr("sub.customer.fetch_customer", customer) cancel_sub, code = payments.cancel_subscription("bob_123", "sub_123") assert "Customer does not exist." in cancel_sub["message"] assert code == 404
def test_cancel_subscription_no_subscription_found(monkeypatch): """ GIVEN call to cancel subscription WHEN there is no active subscription THEN return the appropriate message Subscription.modify(sub_id, cancel_at_period_end=True) """ with monkeypatch.context() as m: user = Mock(return_value=MockSubhubUser()) subscription_data = { "id": "sub_123", "status": "deleted", "current_period_end": 1566833524, "current_period_start": 1564155124, "ended_at": None, "plan": { "id": "plan_123", "nickname": "Monthly VPN Subscription" }, "cancel_at_period_end": False, } customer = MagicMock( return_value={ "id": "123", "cust_id": "cust_123", "metadata": { "userid": "123" }, "subscriptions": { "data": [subscription_data] }, }) cancel_response = mock( { "id": "cus_tester3", "object": "customer", "subscriptions": { "data": [] }, "sources": { "data": [{ "id": "sub_123", "cancel_at_period_end": True }] }, }, spec=Customer, ) delete_response = mock( { "id": "cus_tester3", "object": "customer", "sources": [] }, spec=Customer) when(Customer).delete_source("cus_tester3", "src_123").thenReturn(delete_response) m.setattr("flask.g.subhub_account.get_user", user) m.setattr("stripe.Customer.retrieve", customer) m.setattr("sub.customer.fetch_customer", customer) m.setattr("sub.payments.retrieve_stripe_subscriptions", cancel_response) cancel_sub, code = payments.cancel_subscription("123", "sub_123") logger.info("cancel sub", cancel_sub=cancel_sub) assert "Subscription not available." in cancel_sub["message"] assert code == 400