def test_ingest_new_customer_duplicate_fxa_id( dbsession, stripe_customer, example_contact ): """StripeIngestFxAIdConflict is raised when an existing customer has the same FxA ID.""" data = stripe_customer_data() existing_id = data["id"] fxa_id = data["description"] data["id"] = fake_stripe_id("cust", "duplicate_fxa_id") with pytest.raises(StripeIngestFxAIdConflict) as excinfo: ingest_stripe_customer(dbsession, data) exception = excinfo.value assert ( str(exception) == f"Existing StripeCustomer '{existing_id}' has FxA ID '{fxa_id}'." ) assert repr(exception) == f"StripeIngestFxAIdConflict('{existing_id}', '{fxa_id}')"
def test_ingest_existing_but_deleted_customer( dbsession, stripe_customer, example_contact ): """A deleted Stripe Customer is noted for later deletion.""" assert not stripe_customer.deleted data = { "deleted": True, "id": stripe_customer.stripe_id, "object": "customer", } customer, actions = ingest_stripe_customer(dbsession, data) dbsession.commit() dbsession.refresh(customer) assert customer.deleted assert customer.default_source_id == stripe_customer.default_source_id assert ( customer.invoice_settings_default_payment_method_id == stripe_customer.invoice_settings_default_payment_method_id ) assert customer.get_email_id() == example_contact.email.email_id assert actions == { "updated": { f"customer:{data['id']}", } }
def test_ingest_update_customer(dbsession, stripe_customer): """A Stripe Customer can be updated.""" data = stripe_customer_data() # Change payment method new_source_id = fake_stripe_id("card", "new credit card") data["default_source"] = new_source_id data["invoice_settings"]["default_payment_method"] = None with StatementWatcher(dbsession.connection()) as watcher: customer, actions = ingest_stripe_customer(dbsession, data) dbsession.commit() assert watcher.count == 2 stmt1 = watcher.statements[0][0] assert stmt1.startswith("SELECT stripe_customer."), stmt1 assert stmt1.endswith(" FOR UPDATE"), stmt1 stmt2 = watcher.statements[1][0] assert stmt2.startswith("UPDATE stripe_customer SET "), stmt2 assert customer.default_source_id == new_source_id assert customer.invoice_settings_default_payment_method_id is None assert actions == { "updated": { f"customer:{data['id']}", } }
def test_ingest_existing_contact(dbsession, example_contact): """A Stripe Customer is associated with the existing contact.""" data = stripe_customer_data() data["description"] = example_contact.fxa.fxa_id data["email"] = example_contact.fxa.primary_email with StatementWatcher(dbsession.connection()) as watcher: customer, actions = ingest_stripe_customer(dbsession, data) dbsession.commit() assert watcher.count == 3 stmt1 = watcher.statements[0][0] assert stmt1.startswith("SELECT stripe_customer."), stmt1 assert stmt1.endswith(" FOR UPDATE"), stmt1 stmt2 = watcher.statements[1][0] assert stmt2.startswith("SELECT stripe_customer."), stmt2 assert stmt2.endswith(" FOR UPDATE"), stmt2 stmt3 = watcher.statements[2][0] assert stmt3.startswith("INSERT INTO stripe_customer "), stmt3 assert customer.stripe_id == FAKE_STRIPE_ID["Customer"] assert not customer.deleted assert customer.default_source_id is None assert ( customer.invoice_settings_default_payment_method_id == FAKE_STRIPE_ID["Payment Method"] ) assert customer.fxa_id == example_contact.fxa.fxa_id assert customer.get_email_id() == example_contact.email.email_id assert actions == { "created": { f"customer:{customer.stripe_id}", } }
def test_ingest_update_customer_duplicate_fxa_id(dbsession, stripe_customer): """StripeIngestFxAIdConflict is raised when updating to a different customer's FxA ID.""" existing_customer_data = SAMPLE_STRIPE_DATA["Customer"].copy() existing_id = fake_stripe_id("cust", "duplicate_fxa_id") fxa_id = str(uuid4()) existing_customer_data.update({"stripe_id": existing_id, "fxa_id": fxa_id}) create_stripe_customer( dbsession, StripeCustomerCreateSchema(**existing_customer_data) ) dbsession.commit() data = stripe_customer_data() data["description"] = fxa_id with pytest.raises(StripeIngestFxAIdConflict) as excinfo: ingest_stripe_customer(dbsession, data) exception = excinfo.value assert exception.stripe_id == existing_id assert exception.fxa_id == fxa_id
def test_ingest_without_contact(dbsession): """A Stripe Customer can be ingested without a contact.""" data = stripe_customer_data() customer, actions = ingest_stripe_customer(dbsession, data) dbsession.commit() dbsession.refresh(customer) assert customer.email is None assert actions == { "created": { f"customer:{data['id']}", } }
def test_ingest_deleted_customer(dbsession): """A deleted Stripe Customer is not ingested.""" data = { "deleted": True, "id": FAKE_STRIPE_ID["Customer"], "object": "customer", } customer, actions = ingest_stripe_customer(dbsession, data) assert customer is None assert actions == { "skipped": { f"customer:{data['id']}", } }