def test_check_mark_delete(self): self.mock_customer.return_value = self.customer_active_sub delete_cus = StripeCustomerSubscriptionDeleted(self.subscription_deleted_event) check_mark_deleted = delete_cus.check_mark_delete( customer=self.customer_active_sub ) assert check_mark_deleted is True
def test_create_payload(self): expected_payload = { "uid": "user123", "active": False, "subscriptionId": "sub_00000000000000", "productId": "prod_00000000000000", "eventId": "evt_00000000000000", "eventCreatedAt": 1326853478, "messageCreatedAt": int(time.time()), } actual_payload = StripeCustomerSubscriptionDeleted( self.subscription_deleted_event).create_payload("user123") self.assertEqual(actual_payload.keys(), expected_payload.keys()) if actual_payload["messageCreatedAt"] != expected_payload[ "messageCreatedAt"]: self.assertAlmostEqual( actual_payload["messageCreatedAt"], expected_payload["messageCreatedAt"], delta=10, ) expected_payload["messageCreatedAt"] = actual_payload[ "messageCreatedAt"] self.assertEqual(actual_payload, expected_payload)
def test_update_deleted_user(self): deleted_user = StripeCustomerSubscriptionDeleted( self.subscription_deleted_event ) deleted_user.add_user_to_deleted_users_record( user_id="test1", cust_id="cust_1", origin_system="origin1", subscription_info=[ { "nickname": "test sub 1", "plan_amount": 100, "productId": "prod_tes1", "current_period_start": 1234567, "current_period_end": 1234567, "subscription_id": "sub_test1", } ], ) updated_user = deleted_user.update_deleted_user( "test1", "cust_1", [ { "nickname": "test sub 2", "plan_amount": 100, "productId": "prod_tes2", "current_period_start": 1234567, "current_period_end": 1234567, "subscription_id": "sub_test2", } ], ) logger.info("update deleted user", updated_user=updated_user) assert len(updated_user) == 2
def test_check_for_deleted_user(self): self.mock_delete_customer.return_value = self.deleted_customer to_delete_cus = StripeCustomerSubscriptionDeleted( self.subscription_deleted_event ) deleted_cus = to_delete_cus.delete_customer(self.mock_delete_customer) assert deleted_cus.get("deleted") is True
def test_delete_customer(self): self.mock_delete_customer.return_value = self.deleted_customer deleted_user = StripeCustomerSubscriptionDeleted( self.subscription_deleted_event ) deleted_cust = deleted_user.delete_customer("cust_test1") assert deleted_cust.get("id") == "cust_test1" assert deleted_cust.get("deleted") is True
def test_get_subscription_info(self): self.mock_customer.return_value = self.customer_active_sub delete_cus = StripeCustomerSubscriptionDeleted(self.subscription_deleted_event) logger.info("get sub", subs=self.customer_active_sub.get("subscriptions")) logger.info("sub to delete", to_delete=self.sub_to_delete) check_sub_info = delete_cus.get_subscription_info( subscriptions=self.customer_active_sub.get("subscriptions"), current_sub=self.sub_to_delete, ) assert check_sub_info[0].get("nickname") == "test plan"
def test_get_user_id_fetch_error(self): self.mock_customer.side_effect = InvalidRequestError( message="invalid data", param="bad data") with self.assertRaises(InvalidRequestError): StripeCustomerSubscriptionDeleted( self.subscription_deleted_event).get_user_id("cust_123")
def test_get_user_id(self): self.mock_customer.return_value = self.customer expected_user_id = "user123" user_id = StripeCustomerSubscriptionDeleted( self.subscription_deleted_event).get_user_id("cust_123") assert user_id == expected_user_id
def test_run(self): self.mock_customer.return_value = self.customer self.mock_run_pipeline.return_value = None did_run = StripeCustomerSubscriptionDeleted( self.subscription_deleted_event).run() assert did_run
def test_add_user_to_deleted_users_record(self): deleted_user = StripeCustomerSubscriptionDeleted( self.subscription_deleted_event ) add_deleted_user = deleted_user.add_user_to_deleted_users_record( user_id="test1", cust_id="cust_1", origin_system="origin1", subscription_info=[ { "nickname": "test sub 1", "plan_amount": 100, "productId": "prod_tes1", "current_period_start": 1234567, "current_period_end": 1234567, "subscription_id": "sub_test1", } ], ) assert add_deleted_user
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_customer(self): self.mock_customer.return_value = self.customer logger.info("get customer", cus=self.customer, cus_id=self.customer.get("id")) delete_cus = StripeCustomerSubscriptionDeleted(self.subscription_deleted_event) get_customer = delete_cus.get_customer(self.customer.get("id")) assert get_customer.id == "cus_test1"
def test_get_user_id(self): deleted_user = StripeCustomerSubscriptionDeleted( self.subscription_deleted_event ) user_id = deleted_user.get_user_id(self.customer) assert user_id == "user123"
def test_get_origin_system(self): deleted_user = StripeCustomerSubscriptionDeleted( self.subscription_deleted_event ) origin_test = deleted_user.get_origin_system(self.customer) assert origin_test == "unknown"
def test_get_user_id_none_error(self): self.mock_customer.return_value = self.customer_missing_user with self.assertRaises(ClientError): StripeCustomerSubscriptionDeleted( self.subscription_deleted_event).get_user_id("cust_123")