Skip to main content

bitwarden_api_key_connector/apis/
user_keys_api.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4use bitwarden_api_base::{AuthRequired, Configuration};
5use mockall::automock;
6use reqwest::Method;
7use serde::Serialize;
8
9use crate::{
10    apis::Error,
11    models::{
12        user_key_request_model::UserKeyKeyRequestModel,
13        user_key_response_model::UserKeyResponseModel,
14    },
15};
16
17#[automock]
18#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
19#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
20pub trait UserKeysApi: Send + Sync {
21    /// GET /user-keys
22    async fn get_user_key(&self) -> Result<UserKeyResponseModel, Error>;
23
24    /// POST /user-keys
25    async fn post_user_key(&self, request_model: UserKeyKeyRequestModel) -> Result<(), Error>;
26
27    /// PUT /user-keys
28    async fn put_user_key(&self, request_model: UserKeyKeyRequestModel) -> Result<(), Error>;
29}
30
31pub struct UserKeysApiClient {
32    configuration: Arc<Configuration>,
33}
34
35impl UserKeysApiClient {
36    pub fn new(configuration: Arc<Configuration>) -> Self {
37        Self { configuration }
38    }
39}
40
41#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
42#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
43impl UserKeysApi for UserKeysApiClient {
44    async fn get_user_key(&self) -> Result<UserKeyResponseModel, Error> {
45        let response = request(&self.configuration, Method::GET, None::<()>).await?;
46
47        let body = response.text().await?;
48        let response_model = serde_json::from_str::<UserKeyResponseModel>(&body)?;
49        Ok(response_model)
50    }
51
52    async fn post_user_key(&self, request_model: UserKeyKeyRequestModel) -> Result<(), Error> {
53        request(&self.configuration, Method::POST, Some(request_model)).await?;
54
55        Ok(())
56    }
57
58    async fn put_user_key(&self, request_model: UserKeyKeyRequestModel) -> Result<(), Error> {
59        request(&self.configuration, Method::PUT, Some(request_model)).await?;
60
61        Ok(())
62    }
63}
64
65async fn request(
66    configuration: &Arc<Configuration>,
67    method: Method,
68    body: Option<impl Serialize>,
69) -> Result<reqwest::Response, Error> {
70    let url = format!("{}/user-keys", configuration.base_path);
71
72    let mut request = configuration
73        .client
74        .request(method, url)
75        .header(reqwest::header::CONTENT_TYPE, "application/json")
76        .header(reqwest::header::ACCEPT, "application/json")
77        .with_extension(AuthRequired::Bearer);
78
79    if let Some(ref body) = body {
80        request =
81            request.body(serde_json::to_string(&body).expect("Serialize should be infallible"))
82    }
83
84    let response = request.send().await?;
85
86    Ok(response.error_for_status()?)
87}
88
89#[cfg(test)]
90mod tests {
91    use std::sync::Arc;
92
93    use bitwarden_api_base::Configuration;
94    use wiremock::{
95        Mock, MockServer, ResponseTemplate,
96        matchers::{header, method, path},
97    };
98
99    use crate::{
100        apis::user_keys_api::{UserKeysApi, UserKeysApiClient},
101        models::user_key_request_model::UserKeyKeyRequestModel,
102    };
103
104    const KEY_CONNECTOR_KEY: &str = "test_key_connector_key";
105
106    async fn setup_mock_server_with_auth() -> (MockServer, Configuration) {
107        let server = MockServer::start().await;
108
109        let configuration = Configuration {
110            base_path: format!("http://{}", server.address()),
111            client: reqwest::Client::new().into(),
112        };
113
114        (server, configuration)
115    }
116
117    #[tokio::test]
118    async fn test_get() {
119        let (server, configuration) = setup_mock_server_with_auth().await;
120
121        Mock::given(method("GET"))
122            .and(path("/user-keys"))
123            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
124                "key": KEY_CONNECTOR_KEY.to_string()
125            })))
126            .expect(1)
127            .mount(&server)
128            .await;
129
130        let api_client = UserKeysApiClient::new(Arc::new(configuration));
131
132        let result = api_client.get_user_key().await;
133
134        assert!(result.is_ok());
135        assert_eq!(KEY_CONNECTOR_KEY, result.unwrap().key);
136    }
137
138    #[tokio::test]
139    async fn test_post() {
140        let (server, configuration) = setup_mock_server_with_auth().await;
141
142        Mock::given(method("POST"))
143            .and(path("/user-keys"))
144            .and(header("content-type", "application/json"))
145            .respond_with(ResponseTemplate::new(200))
146            .expect(1)
147            .mount(&server)
148            .await;
149
150        let request_model = UserKeyKeyRequestModel {
151            key: KEY_CONNECTOR_KEY.to_string(),
152        };
153
154        let api_client = UserKeysApiClient::new(Arc::new(configuration));
155
156        let result = api_client.post_user_key(request_model).await;
157
158        assert!(result.is_ok());
159    }
160
161    #[tokio::test]
162    async fn test_put() {
163        let (server, configuration) = setup_mock_server_with_auth().await;
164
165        Mock::given(method("PUT"))
166            .and(path("/user-keys"))
167            .and(header("content-type", "application/json"))
168            .respond_with(ResponseTemplate::new(200))
169            .expect(1)
170            .mount(&server)
171            .await;
172
173        let request_model = UserKeyKeyRequestModel {
174            key: KEY_CONNECTOR_KEY.to_string(),
175        };
176
177        let api_client = UserKeysApiClient::new(Arc::new(configuration));
178
179        let result = api_client.put_user_key(request_model).await;
180
181        assert!(result.is_ok());
182    }
183}