stripe_billing_tests.rs

 1use std::sync::Arc;
 2
 3use pretty_assertions::assert_eq;
 4
 5use crate::stripe_billing::StripeBilling;
 6use crate::stripe_client::FakeStripeClient;
 7
 8fn make_stripe_billing() -> (StripeBilling, Arc<FakeStripeClient>) {
 9    let stripe_client = Arc::new(FakeStripeClient::new());
10    let stripe_billing = StripeBilling::test(stripe_client.clone());
11
12    (stripe_billing, stripe_client)
13}
14
15#[gpui::test]
16async fn test_find_or_create_customer_by_email() {
17    let (stripe_billing, stripe_client) = make_stripe_billing();
18
19    // Create a customer with an email that doesn't yet correspond to a customer.
20    {
21        let email = "user@example.com";
22
23        let customer_id = stripe_billing
24            .find_or_create_customer_by_email(Some(email))
25            .await
26            .unwrap();
27
28        let customer = stripe_client
29            .customers
30            .lock()
31            .get(&customer_id)
32            .unwrap()
33            .clone();
34        assert_eq!(customer.email.as_deref(), Some(email));
35    }
36
37    // Create a customer with an email that corresponds to an existing customer.
38    {
39        let email = "user2@example.com";
40
41        let existing_customer_id = stripe_billing
42            .find_or_create_customer_by_email(Some(email))
43            .await
44            .unwrap();
45
46        let customer_id = stripe_billing
47            .find_or_create_customer_by_email(Some(email))
48            .await
49            .unwrap();
50        assert_eq!(customer_id, existing_customer_id);
51
52        let customer = stripe_client
53            .customers
54            .lock()
55            .get(&customer_id)
56            .unwrap()
57            .clone();
58        assert_eq!(customer.email.as_deref(), Some(email));
59    }
60}