bitgo_api/
mock.rs

1use super::client::BitGoClient;
2use crate::error::Result;
3use async_trait::async_trait;
4use mockall::mock;
5
6use crate::{transfer::BitGoTransferAPI, wallet::BitGoWalletAPI, webhook::BitGoWebhookAPI};
7
8mock! {
9    pub BitGoClient {
10
11    }
12    #[async_trait]
13    impl BitGoTransferAPI for BitGoClient {
14        async fn get_transaction(
15            &self,
16            wallet_id: &str,
17            identifier: &str,
18            transfer_id: &str,
19        ) -> Result<serde_json::Value>;
20
21        async fn transfer_list(&self, wallet_id: &str, identifier: &str) -> Result<serde_json::Value>;
22        async fn get_fee(
23            &self,
24            identifier: &str,
25            num_blocks: &i32,
26            recipient: &str,
27            data: &str,
28            amount: &str,
29            hop: bool,
30        ) -> Result<serde_json::Value>;
31        async fn change_fee(
32            &self,
33            identifier: &str,
34            wallet_id: &str,
35            tx_id: &str,
36            fee: &str,
37        ) -> Result<serde_json::Value>;
38    }
39
40    #[async_trait]
41    impl BitGoWalletAPI for BitGoClient {
42        async fn generate_wallet(
43            &self,
44            name: &str,
45            identifier: &str,
46            passphrase: &str,
47        ) -> Result<serde_json::Value>;
48        async fn generate_enterprise_wallet(
49            &self,
50            name: &str,
51            identifier: &str,
52            passphrase: &str,
53            enterprise_id:&str,
54        ) -> Result<serde_json::Value>;
55        async fn create_address(
56            &self,
57            wallet_id: &str,
58            identifier: &str,
59            forwarde_version: i32,
60        ) -> Result<serde_json::Value>;
61        async fn get_wallet_list(
62            &self
63        ) -> Result<serde_json::Value>;
64    }
65
66    #[async_trait]
67    impl BitGoWebhookAPI for BitGoClient {
68        async fn add_wallet_webhook(
69            &self,
70            wallet_id: &str,
71            identifier: &str,
72            webhook_label: &str,
73            webhook_type: &str,
74            webhook_url: &str,
75            num_confirmation:i32,
76            all_token: bool,
77            listen_failure_states: bool,
78        ) -> Result<serde_json::Value>;
79
80
81
82        async fn add_block_webhook(
83            &self,
84            identifier: &str,
85            webhook_type: &str,
86            webhook_label: &str,
87            webhook_url: &str,
88            num_confirmation:i32
89        ) -> Result<serde_json::Value>;
90        async fn list_wallet_webhook(&self, wallet_id: &str, identifier: &str) -> Result<serde_json::Value>;
91        async fn list_block_webhook(&self,identifier: &str) -> Result<serde_json::Value>;
92        async fn remove_wallet_webhook(
93            &self,
94            wallet_id: &str,
95            identifier: &str,
96            webhook_type: &str,
97            webhook_url: &str,
98            webhook_id:&str,
99        ) -> Result<serde_json::Value>;
100
101        async fn remove_block_webhook(
102            &self,
103            identifier: &str,
104            webhook_type: &str,
105            webhook_url: &str,
106            webhook_id: &str,
107        ) -> Result<serde_json::Value>;
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114    use serde_json::json;
115
116    #[tokio::test]
117    #[ignore = "required bitgo express to be running with tls certificate"]
118    async fn test_tls_mocking() {
119        let client = BitGoClient::new(
120            "https:://127.0.0.1:4000".to_string(),
121            "".to_string(),
122            Some("{path}/cert.pem".to_string()),
123        );
124        if let Ok(cl) = client {
125            let res = cl
126                .get_api("https://localhost:4000/api/v2/ping", &"".to_string())
127                .await
128                .unwrap();
129            print!("{:?}", res)
130        } else {
131            todo!()
132        }
133    }
134    #[tokio::test]
135    #[ignore = "required bitgo server to be running without tls certificate"]
136    async fn test_without_tls() {
137        let client = BitGoClient::new("https:://localhost:4000".to_string(), "".to_string(), None);
138        if let Ok(cl) = client {
139            let res = cl
140                .get_api("https://localhost:4000/api/v2/ping", &"".to_string())
141                .await;
142            assert!(res.is_err())
143        } else {
144            todo!()
145        }
146    }
147
148    #[tokio::test]
149    #[ignore = "required bitgo server to be running without tls certificate"]
150    async fn test_connect_local_bitgo_express() {
151        let client = BitGoClient::new("http:://localhost:4000".to_string(), "".to_string(), None);
152        if let Ok(cl) = client {
153            let res = cl
154                .get_api("http://localhost:4000/api/v2/ping", &"".to_string())
155                .await
156                .unwrap();
157            print!("{:?}", res)
158        } else {
159            todo!()
160        }
161    }
162
163    #[tokio::test]
164    #[ignore = "required bitgo server to be running"]
165    async fn test_tls_creat_wallet() {
166        let client = BitGoClient::new(
167            "https://localhost:4000".to_string(),
168            "".to_string(),
169            Some("{path}/cert.pem".to_string()),
170        );
171
172        if let Ok(cl) = client {
173            let res = cl
174                .create_address("60def63ab9390d000630211559c1544d", "tbtc", 0)
175                .await
176                .unwrap();
177            print!("{:?}", res)
178        } else {
179            todo!()
180        }
181    }
182    #[tokio::test]
183    #[ignore = "required bitgo server to be running"]
184    async fn test_tls_get_wallet() {
185        let client = BitGoClient::new(
186            "https://localhost:4000".to_string(),
187            "".to_string(),
188            Some("{path}/cert.pem".to_string()),
189        );
190
191        if let Ok(cl) = client {
192            let res = cl.get_wallet_list().await.unwrap();
193            print!("{:?}", res)
194        } else {
195            todo!()
196        }
197    }
198    #[tokio::test]
199    async fn test_mocking() {
200        let mut mock = MockBitGoClient::new();
201        mock.expect_create_address().return_const(Ok(
202            json!({ "address": "2MvrwRYBAuRtPTiZ5MyKg42Ke55W3fZJfZS" }),
203        ));
204
205        let v = mock.create_address("any", " any", 0).await.unwrap();
206
207        assert_eq!(
208            v.get("address").unwrap().to_owned(),
209            "2MvrwRYBAuRtPTiZ5MyKg42Ke55W3fZJfZS"
210        );
211    }
212}