def test_create_payload_reactivated(self): self.mock_product.return_value = self.product self.mock_invoice.return_value = self.invoice self.mock_charge.return_value = self.charge user_id = "user123" event_name = "customer.subscription.reactivated" expected_payload = dict( Event_Id__c="evt_1FXDCFJNcmPzuWtRrogbWpRZ", Event_Name__c=event_name, FxA_Id__c=user_id, PMT_Cust_Id__c="cus_FCUzOhOp9iutWa", PMT_Subscription_ID__c="sub_FCUzkHmNY3Mbj1", Amount=100, Name="Project Guardian", CloseDate=1571949975, Billing_Cycle_End__c=1572036371, Credit_Card_Type__c="Visa", Last_4_Digits__c="0019", ) actual_payload = StripeCustomerSubscriptionUpdated( self.subscription_reactivate_event ).create_payload(event_type=event_name, user_id=user_id, previous_plan=None) assert actual_payload == expected_payload
def test_get_FxA_Id__c_deleted_cust(self): self.mock_customer.return_value = self.deleted_customer with self.assertRaises(ClientError): StripeCustomerSubscriptionUpdated( self.subscription_updated_event_no_match ).get_user_id("cust_1")
def test_create_payload_cancelled(self): self.mock_product.return_value = self.product user_id = "user123" event_name = "customer.subscription_cancelled" expected_payload = dict( Event_Id__c="evt_1FXDCFJNcmPzuWtRrogbWpRZ", Event_Name__c=event_name, FxA_Id__c=user_id, PMT_Cust_Id__c="cus_FCUzOhOp9iutWa", PMT_Subscription_ID__c="sub_FCUzkHmNY3Mbj1", Amount=100, Name="Project Guardian", # canceled_at=None, CloseDate=None, # cancel_at_period_end=True, Billing_Cycle_Start__c=1571949971, Billing_Cycle_End__c=1572036371, PMT_Invoice_ID__c="in_1FXDCFJNcmPzuWtRT9U5Xvcz", ) actual_payload = StripeCustomerSubscriptionUpdated( self.subscription_cancelled_event ).create_payload(event_type=event_name, user_id=user_id, previous_plan=None) assert actual_payload == expected_payload
def test_create_payload_reactivated(self): self.mock_product.return_value = self.product self.mock_invoice.return_value = self.invoice self.mock_charge.return_value = self.charge user_id = "user123" event_name = "customer.subscription.reactivated" expected_payload = dict( event_id="evt_1FXDCFJNcmPzuWtRrogbWpRZ", event_type=event_name, uid=user_id, customer_id="cus_FCUzOhOp9iutWa", subscription_id="sub_FCUzkHmNY3Mbj1", plan_amount=100, nickname="Project Guardian (Daily)", close_date=1571949975, current_period_end=1572036371, brand="Visa", last4="0019", ) actual_payload = StripeCustomerSubscriptionUpdated( self.subscription_reactivate_event).create_payload( event_type=event_name, user_id=user_id, previous_plan=None) assert actual_payload == expected_payload
def test_run_no_action(self): self.mock_customer.return_value = self.customer did_route = StripeCustomerSubscriptionUpdated( self.subscription_updated_event_no_match ).run() assert did_route is False
def test_create_payload_cancelled(self): self.mock_product.return_value = self.product user_id = "user123" event_name = "customer.subscription_cancelled" expected_payload = dict( event_id="evt_1FXDCFJNcmPzuWtRrogbWpRZ", event_type=event_name, uid=user_id, customer_id="cus_FCUzOhOp9iutWa", subscription_id="sub_FCUzkHmNY3Mbj1", plan_amount=100, nickname="Project Guardian (Daily)", canceled_at=None, cancel_at=None, cancel_at_period_end=True, current_period_start=1571949971, current_period_end=1572036371, invoice_id="in_1FXDCFJNcmPzuWtRT9U5Xvcz", ) actual_payload = StripeCustomerSubscriptionUpdated( self.subscription_cancelled_event).create_payload( event_type=event_name, user_id=user_id, previous_plan=None) assert actual_payload == expected_payload
def test_get_user_id_missing(self): self.mock_customer.return_value = self.customer_missing_user with self.assertRaises(ClientError): StripeCustomerSubscriptionUpdated( self.subscription_updated_event_no_match).get_user_id( "cust_123")
def test_get_user_id_fetch_error(self): self.mock_customer.side_effect = InvalidRequestError( message="invalid data", param="bad data") with self.assertRaises(InvalidRequestError): StripeCustomerSubscriptionUpdated( self.subscription_updated_event_no_match).get_user_id( "cust_123")
def test_run_cancel(self): self.mock_customer.return_value = self.customer self.mock_product.return_value = self.product self.mock_run_pipeline.return_value = None did_route = StripeCustomerSubscriptionUpdated( self.subscription_cancelled_event).run() assert did_route
def test_create_payload_error(self): self.mock_product.side_effect = InvalidRequestError( message="invalid data", param="bad data") with self.assertRaises(InvalidRequestError): StripeCustomerSubscriptionUpdated( self.subscription_updated_event_no_match).create_payload( event_type="event.type", user_id="user_123", previous_plan=None)
def test_run_reactivate(self): self.mock_customer.return_value = self.customer self.mock_product.return_value = self.product self.mock_invoice.return_value = self.invoice self.mock_charge.return_value = self.charge self.mock_run_pipeline.return_value = None did_route = StripeCustomerSubscriptionUpdated( self.subscription_reactivate_event).run() assert did_route
def test_create_payload_recurring_charge(self): self.mock_product.return_value = self.product self.mock_invoice.return_value = self.invoice self.mock_charge.return_value = self.charge self.mock_upcoming_invoice.return_value = self.upcoming_invoice user_id = "user123" event_name = "customer.recurring_charge" expected_payload = dict( event_id="evt_1FXDCFJNcmPzuWtRrogbWpRZ", event_type=event_name, uid=user_id, customer_id="cus_FCUzOhOp9iutWa", subscription_id="sub_FCUzkHmNY3Mbj1", plan_amount=100, nickname="Project Guardian (Daily)", canceled_at=None, cancel_at=None, cancel_at_period_end=False, current_period_start=1571949971, current_period_end=1572036371, next_invoice_date=1555354567, invoice_id="in_1FXDCFJNcmPzuWtRT9U5Xvcz", active=True, subscriptionId="sub_FCUzkHmNY3Mbj1", # required by FxA productName="Project Guardian", eventCreatedAt=1571949975, # required by FxA messageCreatedAt=int(time.time()), # required by FxA created=1559767571, eventId="evt_1FXDCFJNcmPzuWtRrogbWpRZ", # required by FxA currency="usd", invoice_number="3B74E3D0-0001", brand="Visa", last4="0019", charge="ch_test1", proration_amount=1000, total_amount=1499, ) actual_payload = StripeCustomerSubscriptionUpdated( self.subscription_charge_event).create_payload( event_type=event_name, user_id=user_id, previous_plan=None) assert actual_payload == expected_payload
def run(self) -> None: event_type = self.payload["type"] if event_type == "customer.subscription.created": StripeCustomerSubscriptionCreated(self.payload).run() elif event_type == "customer.subscription.updated": StripeCustomerSubscriptionUpdated(self.payload).run() elif event_type == "customer.subscription.deleted": StripeCustomerSubscriptionDeleted(self.payload).run() elif event_type == "customer.created": StripeCustomerCreated(self.payload).run() elif event_type == "customer.deleted": StripeCustomerDeleted(self.payload).run() elif event_type == "customer.source.expiring": StripeCustomerSourceExpiring(self.payload).run() elif event_type == "invoice.payment_failed": StripeInvoicePaymentFailed(self.payload).run() else: pass
def test_get_subscription_change(self): self.mock_customer.return_value = self.product self.mock_invoice.return_value = self.invoice self.mock_upcoming_invoice.return_value = self.upcoming_invoice self.mock_product.return_value = self.product self.mock_plan_retrieve.return_value = self.previous_plan expected_sub_change = dict( CloseDate=1571949975, Nickname_Old__c="Previous Product", Service_Plan__c="Test Plan Original", Event_Name__c="customer.subscription.upgrade", Plan_Amount_Old__c=499, Amount=999, Proration_Amount__c=1000, Billing_Cycle_End__c=1572036371, Invoice_Number__c="3B74E3D0-0001", PMT_Invoice_ID__c="in_test1", Payment_Interval__c="month", ) payload = dict( Event_Id__c="evt_change_test", Event_Name__c="customer.subscription.updated", uid=None, PMT_Cust_Id__c="cus_123", PMT_Subscription_ID__c="sub_123", plan_amount=999, nickname="Test Plan Original", ) actual_sub_change = StripeCustomerSubscriptionUpdated( self.subscription_change_event ).get_subscription_change( payload=payload, previous_plan=self.previous_plan, new_product=self.new_product, ) assert expected_sub_change == actual_sub_change
def test_get_subscription_change(self): self.mock_customer.return_value = self.product self.mock_invoice.return_value = self.invoice self.mock_upcoming_invoice.return_value = self.upcoming_invoice self.mock_product.return_value = self.product self.mock_plan_retrieve.return_value = self.previous_plan expected_sub_change = dict( close_date=1571949975, nickname_old="Previous Product (Monthly)", nickname_new="Test Plan Original", event_type="customer.subscription.upgrade", plan_amount_old=499, plan_amount_new=999, proration_amount=1000, current_period_end=1572036371, invoice_number="3B74E3D0-0001", invoice_id="in_test1", interval="month", ) payload = dict( event_id="evt_change_test", event_type="customer.subscription.updated", uid=None, customer_id="cus_123", subscription_id="sub_123", plan_amount=999, nickname="Test Plan Original", ) actual_sub_change = StripeCustomerSubscriptionUpdated( self.subscription_change_event).get_subscription_change( payload=payload, previous_plan=self.previous_plan, new_product=self.new_product, ) assert expected_sub_change == actual_sub_change