Skip to main content

bitwarden_vault/cipher/cipher_client/admin/
delete_attachment.rs

1use bitwarden_core::ApiError;
2use bitwarden_error::bitwarden_error;
3use thiserror::Error;
4#[cfg(feature = "wasm")]
5use wasm_bindgen::prelude::wasm_bindgen;
6
7use crate::{CipherId, cipher_client::admin::CipherAdminClient};
8
9#[allow(missing_docs)]
10#[bitwarden_error(flat)]
11#[derive(Debug, Error)]
12pub enum DeleteAttachmentAdminError {
13    #[error(transparent)]
14    Api(#[from] ApiError),
15}
16
17async fn delete_attachment(
18    cipher_id: CipherId,
19    attachment_id: &str,
20    api_client: &bitwarden_api_api::apis::ApiClient,
21) -> Result<(), ApiError> {
22    let api = api_client.ciphers_api();
23    api.delete_attachment_admin(cipher_id.into(), attachment_id)
24        .await?;
25    Ok(())
26}
27
28#[cfg_attr(feature = "wasm", wasm_bindgen)]
29impl CipherAdminClient {
30    /// Deletes an attachment from a cipher using the admin endpoint.
31    /// Affects server data only, does not modify local state.
32    pub async fn delete_attachment(
33        &self,
34        cipher_id: CipherId,
35        attachment_id: String,
36    ) -> Result<(), DeleteAttachmentAdminError> {
37        Ok(delete_attachment(
38            cipher_id,
39            &attachment_id,
40            &self.client.internal.get_api_configurations().api_client,
41        )
42        .await?)
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    const TEST_CIPHER_ID: &str = "5faa9684-c793-4a2d-8a12-b33900187097";
51    const TEST_ATTACHMENT_ID: &str = "uf7bkexzag04d3cw04jsbqqkbpbwhxs0";
52
53    #[tokio::test]
54    async fn test_delete_attachment_as_admin() {
55        delete_attachment(
56            TEST_CIPHER_ID.parse().unwrap(),
57            TEST_ATTACHMENT_ID,
58            &bitwarden_api_api::apis::ApiClient::new_mocked(|mock| {
59                mock.ciphers_api.expect_delete_attachment_admin().returning(
60                    move |id, attachment_id| {
61                        assert_eq!(&id.to_string(), TEST_CIPHER_ID);
62                        assert_eq!(attachment_id, TEST_ATTACHMENT_ID);
63                        Ok(Default::default())
64                    },
65                );
66            }),
67        )
68        .await
69        .unwrap()
70    }
71}