Skip to main content

bitwarden_vault/cipher/cipher_client/
delete_attachment.rs

1use bitwarden_core::{ApiError, MissingFieldError};
2use bitwarden_error::bitwarden_error;
3use bitwarden_state::repository::{Repository, RepositoryError};
4use thiserror::Error;
5#[cfg(feature = "wasm")]
6use wasm_bindgen::prelude::wasm_bindgen;
7
8use crate::{Cipher, CipherId, CiphersClient, VaultParseError, cipher::cipher::PartialCipher};
9
10#[allow(missing_docs)]
11#[bitwarden_error(flat)]
12#[derive(Debug, Error)]
13pub enum CipherDeleteAttachmentError {
14    #[error(transparent)]
15    Api(#[from] ApiError),
16    #[error(transparent)]
17    Repository(#[from] RepositoryError),
18    #[error(transparent)]
19    MissingField(#[from] MissingFieldError),
20    #[error(transparent)]
21    VaultParse(#[from] VaultParseError),
22}
23
24impl<T> From<bitwarden_api_api::apis::Error<T>> for CipherDeleteAttachmentError {
25    fn from(value: bitwarden_api_api::apis::Error<T>) -> Self {
26        Self::Api(value.into())
27    }
28}
29
30/// Deletes an attachment from a cipher, and updates the local repository with the new cipher data
31/// returned from the API.
32pub async fn delete_attachment<R: Repository<Cipher> + ?Sized>(
33    cipher_id: CipherId,
34    attachment_id: &str,
35    api_client: &bitwarden_api_api::apis::ApiClient,
36    repository: &R,
37) -> Result<Cipher, CipherDeleteAttachmentError> {
38    let api = api_client.ciphers_api();
39
40    let response = api
41        .delete_attachment(cipher_id.into(), attachment_id)
42        .await?;
43
44    let existing_cipher = repository.get(cipher_id).await?;
45    let cipher_response = response
46        .cipher
47        .map(|c| *c)
48        .ok_or(MissingFieldError("cipher"))?;
49    let cipher = cipher_response.merge_with_cipher(existing_cipher)?;
50
51    repository.set(cipher_id, cipher.clone()).await?;
52
53    Ok(cipher)
54}
55
56#[cfg_attr(feature = "wasm", wasm_bindgen)]
57impl CiphersClient {
58    /// Deletes an attachment from a cipher, and updates the local repository with the new cipher
59    /// data returned from the API.
60    pub async fn delete_attachment(
61        &self,
62        cipher_id: CipherId,
63        attachment_id: String,
64    ) -> Result<Cipher, CipherDeleteAttachmentError> {
65        let configs = self.client.internal.get_api_configurations();
66        delete_attachment(
67            cipher_id,
68            &attachment_id,
69            &configs.api_client,
70            &*self.get_repository()?,
71        )
72        .await
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use bitwarden_api_api::{
79        apis::ApiClient,
80        models::{CipherMiniResponseModel, DeleteAttachmentResponseModel},
81    };
82    use bitwarden_state::repository::Repository;
83    use bitwarden_test::MemoryRepository;
84
85    use super::*;
86    use crate::Attachment;
87
88    const TEST_CIPHER_ID: &str = "5faa9684-c793-4a2d-8a12-b33900187097";
89    const TEST_ATTACHMENT_ID: &str = "uf7bkexzag04d3cw04jsbqqkbpbwhxs0";
90
91    fn generate_test_cipher() -> Cipher {
92        Cipher {
93            id: TEST_CIPHER_ID.parse().ok(),
94            name: "2.pMS6/icTQABtulw52pq2lg==|XXbxKxDTh+mWiN1HjH2N1w==|Q6PkuT+KX/axrgN9ubD5Ajk2YNwxQkgs3WJM0S0wtG8=".parse().unwrap(),
95            r#type: crate::CipherType::Login,
96            attachments: Some(vec![Attachment {
97                id: Some(TEST_ATTACHMENT_ID.to_string()),
98                url: Some("http://localhost:4000/attachments/test".to_string()),
99                file_name: Some("2.mV50WiLq6duhwGbhM1TO0A==|dTufWNH8YTPP0EMlNLIpFA==|QHp+7OM8xHtEmCfc9QPXJ0Ro2BeakzvLgxJZ7NdLuDc=".parse().unwrap()),
100                key: None,
101                size: Some("65".to_string()),
102                size_name: Some("65 Bytes".to_string()),
103            }]),
104            notes: Default::default(),
105            organization_id: Default::default(),
106            folder_id: Default::default(),
107            favorite: Default::default(),
108            reprompt: Default::default(),
109            fields: Default::default(),
110            collection_ids: Default::default(),
111            key: Default::default(),
112            login: Default::default(),
113            identity: Default::default(),
114            card: Default::default(),
115            secure_note: Default::default(),
116            ssh_key: Default::default(),
117            organization_use_totp: Default::default(),
118            edit: Default::default(),
119            permissions: Default::default(),
120            view_password: Default::default(),
121            local_data: Default::default(),
122            password_history: Default::default(),
123            creation_date: Default::default(),
124            deleted_date: Default::default(),
125            revision_date: Default::default(),
126            archived_date: Default::default(),
127            data: Default::default(),
128        }
129    }
130
131    #[tokio::test]
132    async fn test_delete_attachment() {
133        let cipher = generate_test_cipher();
134        let cipher_id: CipherId = TEST_CIPHER_ID.parse().unwrap();
135
136        let api_client = ApiClient::new_mocked(move |mock| {
137            mock.ciphers_api
138                .expect_delete_attachment()
139                .returning(move |id, attachment_id| {
140                    assert_eq!(&id.to_string(), TEST_CIPHER_ID);
141                    assert_eq!(attachment_id, TEST_ATTACHMENT_ID);
142                    Ok(DeleteAttachmentResponseModel {
143                        object: None,
144                        cipher: Some(Box::new(CipherMiniResponseModel {
145                            id: Some(TEST_CIPHER_ID.try_into().unwrap()),
146                            name: Some("2.pMS6/icTQABtulw52pq2lg==|XXbxKxDTh+mWiN1HjH2N1w==|Q6PkuT+KX/axrgN9ubD5Ajk2YNwxQkgs3WJM0S0wtG8=".to_string()),
147                            r#type: Some(bitwarden_api_api::models::CipherType::Login),
148                            creation_date: Some("2024-05-31T11:20:58.4566667Z".to_string()),
149                            revision_date: Some("2024-05-31T11:20:58.4566667Z".to_string()),
150                            attachments: None,
151                            ..Default::default()
152                        })),
153                    })
154                });
155        });
156
157        let repository = MemoryRepository::<Cipher>::default();
158        repository.set(cipher_id, cipher).await.unwrap();
159
160        let result = delete_attachment(cipher_id, TEST_ATTACHMENT_ID, &api_client, &repository)
161            .await
162            .unwrap();
163
164        // The returned cipher should have no attachments (API response had none)
165        assert!(result.attachments.is_none());
166
167        // Verify the repository was updated
168        let repo_cipher = repository.get(cipher_id).await.unwrap().unwrap();
169        assert!(repo_cipher.attachments.is_none());
170    }
171
172    #[tokio::test]
173    async fn test_delete_attachment_missing_cipher_in_response() {
174        let cipher = generate_test_cipher();
175        let cipher_id: CipherId = TEST_CIPHER_ID.parse().unwrap();
176
177        let api_client = ApiClient::new_mocked(move |mock| {
178            mock.ciphers_api
179                .expect_delete_attachment()
180                .returning(move |_id, _attachment_id| {
181                    Ok(DeleteAttachmentResponseModel {
182                        object: None,
183                        cipher: None,
184                    })
185                });
186        });
187
188        let repository = MemoryRepository::<Cipher>::default();
189        repository.set(cipher_id, cipher).await.unwrap();
190
191        let result =
192            delete_attachment(cipher_id, TEST_ATTACHMENT_ID, &api_client, &repository).await;
193
194        assert!(
195            result.is_err(),
196            "Should fail when API response has no cipher"
197        );
198    }
199}