fake_stripe_client.rs

 1use std::sync::Arc;
 2
 3use anyhow::Result;
 4use async_trait::async_trait;
 5use collections::HashMap;
 6use parking_lot::Mutex;
 7use uuid::Uuid;
 8
 9use crate::stripe_client::{CreateCustomerParams, StripeClient, StripeCustomer, StripeCustomerId};
10
11pub struct FakeStripeClient {
12    pub customers: Arc<Mutex<HashMap<StripeCustomerId, StripeCustomer>>>,
13}
14
15impl FakeStripeClient {
16    pub fn new() -> Self {
17        Self {
18            customers: Arc::new(Mutex::new(HashMap::default())),
19        }
20    }
21}
22
23#[async_trait]
24impl StripeClient for FakeStripeClient {
25    async fn list_customers_by_email(&self, email: &str) -> Result<Vec<StripeCustomer>> {
26        Ok(self
27            .customers
28            .lock()
29            .values()
30            .filter(|customer| customer.email.as_deref() == Some(email))
31            .cloned()
32            .collect())
33    }
34
35    async fn create_customer(&self, params: CreateCustomerParams<'_>) -> Result<StripeCustomer> {
36        let customer = StripeCustomer {
37            id: StripeCustomerId(format!("cus_{}", Uuid::new_v4()).into()),
38            email: params.email.map(|email| email.to_string()),
39        };
40
41        self.customers
42            .lock()
43            .insert(customer.id.clone(), customer.clone());
44
45        Ok(customer)
46    }
47}