bitwarden_vault/cipher/attachment_client/
renew.rs1use bitwarden_core::{ApiError, MissingFieldError};
2use bitwarden_error::bitwarden_error;
3use thiserror::Error;
4
5use crate::{AttachmentsClient, CipherId};
6
7#[allow(missing_docs)]
8#[bitwarden_error(flat)]
9#[derive(Debug, Error)]
10pub enum CipherRenewFileUploadUrlError {
11 #[error(transparent)]
12 Api(#[from] ApiError),
13 #[error(transparent)]
14 MissingField(#[from] MissingFieldError),
15}
16
17#[bitwarden_ffi::wasm_export]
18impl AttachmentsClient {
19 pub async fn renew_file_upload_url(
22 &self,
23 cipher_id: CipherId,
24 attachment_id: String,
25 ) -> Result<String, CipherRenewFileUploadUrlError> {
26 let response = self
27 .api_configurations
28 .api_client
29 .ciphers_api()
30 .renew_file_upload_url(cipher_id.into(), &attachment_id)
31 .await?;
32
33 response.url.ok_or_else(|| MissingFieldError("url").into())
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use std::sync::Arc;
40
41 use bitwarden_api_api::{apis::ApiClient, models::AttachmentUploadDataResponseModel};
42 use bitwarden_core::{client::ApiConfigurations, key_management::KeySlotIds};
43 use bitwarden_crypto::KeyStore;
44 use reqwest::StatusCode;
45
46 use super::*;
47
48 const TEST_CIPHER_ID: &str = "5faa9684-c793-4a2d-8a12-b33900187097";
49 const TEST_ATTACHMENT_ID: &str = "uf7bkexzag04d3cw04jsbqqkbpbwhxs0";
50 const TEST_RENEW_URL: &str = "http://localhost:4000/attachments/test/renewed";
51
52 fn client_with_api(api_client: ApiClient) -> AttachmentsClient {
53 AttachmentsClient {
54 key_store: KeyStore::<KeySlotIds>::default(),
55 api_configurations: Arc::new(ApiConfigurations::from_api_client(api_client)),
56 repository: None,
57 }
58 }
59
60 #[tokio::test]
61 async fn returns_url_from_api_response() {
62 let api_client = ApiClient::new_mocked(|mock| {
63 mock.ciphers_api
64 .expect_renew_file_upload_url()
65 .returning(|id, attachment_id| {
66 assert_eq!(&id.to_string(), TEST_CIPHER_ID);
67 assert_eq!(attachment_id, TEST_ATTACHMENT_ID);
68 Ok(AttachmentUploadDataResponseModel {
69 url: Some(TEST_RENEW_URL.to_string()),
70 ..Default::default()
71 })
72 });
73 });
74
75 let client = client_with_api(api_client);
76 let cipher_id: CipherId = TEST_CIPHER_ID.parse().unwrap();
77 let url = client
78 .renew_file_upload_url(cipher_id, TEST_ATTACHMENT_ID.to_string())
79 .await
80 .unwrap();
81
82 assert_eq!(url, TEST_RENEW_URL);
83 }
84
85 #[tokio::test]
86 async fn returns_missing_field_when_response_has_no_url() {
87 let api_client = ApiClient::new_mocked(|mock| {
88 mock.ciphers_api
89 .expect_renew_file_upload_url()
90 .returning(|_id, _attachment_id| {
91 Ok(AttachmentUploadDataResponseModel {
92 url: None,
93 ..Default::default()
94 })
95 });
96 });
97
98 let client = client_with_api(api_client);
99 let cipher_id: CipherId = TEST_CIPHER_ID.parse().unwrap();
100 let err = client
101 .renew_file_upload_url(cipher_id, TEST_ATTACHMENT_ID.to_string())
102 .await
103 .unwrap_err();
104
105 assert!(matches!(
106 err,
107 CipherRenewFileUploadUrlError::MissingField(_)
108 ));
109 }
110
111 #[tokio::test]
112 async fn propagates_api_errors() {
113 let api_client = ApiClient::new_mocked(|mock| {
114 mock.ciphers_api
115 .expect_renew_file_upload_url()
116 .returning(|_id, _attachment_id| {
117 Err(bitwarden_api_api::ApiError::Response(
118 bitwarden_api_api::ResponseContent {
119 status: StatusCode::INTERNAL_SERVER_ERROR,
120 message: "boom".to_string(),
121 },
122 ))
123 });
124 });
125
126 let client = client_with_api(api_client);
127 let cipher_id: CipherId = TEST_CIPHER_ID.parse().unwrap();
128 let err = client
129 .renew_file_upload_url(cipher_id, TEST_ATTACHMENT_ID.to_string())
130 .await
131 .unwrap_err();
132
133 assert!(matches!(err, CipherRenewFileUploadUrlError::Api(_)));
134 }
135}