bitwarden_vault/
collection_client.rs

1use bitwarden_core::Client;
2
3use crate::{error::DecryptError, Collection, CollectionView};
4
5#[allow(missing_docs)]
6pub struct CollectionsClient {
7    pub(crate) client: Client,
8}
9
10impl CollectionsClient {
11    #[allow(missing_docs)]
12    pub fn decrypt(&self, collection: Collection) -> Result<CollectionView, DecryptError> {
13        let key_store = self.client.internal.get_key_store();
14        let view = key_store.decrypt(&collection)?;
15        Ok(view)
16    }
17
18    #[allow(missing_docs)]
19    pub fn decrypt_list(
20        &self,
21        collections: Vec<Collection>,
22    ) -> Result<Vec<CollectionView>, DecryptError> {
23        let key_store = self.client.internal.get_key_store();
24        let views = key_store.decrypt_list(&collections)?;
25        Ok(views)
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use bitwarden_core::client::test_accounts::test_bitwarden_com_account;
32
33    use super::*;
34    use crate::VaultClientExt;
35
36    #[tokio::test]
37    async fn test_decrypt_list() {
38        let client = Client::init_test_account(test_bitwarden_com_account()).await;
39
40        let dec = client.vault().collections().decrypt_list(vec![Collection {
41            id: Some("66c5ca57-0868-4c7e-902f-b181009709c0".parse().unwrap()),
42            organization_id: "1bc9ac1e-f5aa-45f2-94bf-b181009709b8".parse().unwrap(),
43            name: "2.EI9Km5BfrIqBa1W+WCccfA==|laWxNnx+9H3MZww4zm7cBSLisjpi81zreaQntRhegVI=|x42+qKFf5ga6DIL0OW5pxCdLrC/gm8CXJvf3UASGteI=".parse().unwrap(),
44            external_id: None,
45            hide_passwords: false,
46            read_only: false,
47            manage: false,
48        }]).unwrap();
49
50        assert_eq!(dec[0].name, "Default collection");
51    }
52
53    #[tokio::test]
54    async fn test_decrypt() {
55        let client = Client::init_test_account(test_bitwarden_com_account()).await;
56
57        let dec = client.vault().collections().decrypt(Collection {
58            id: Some("66c5ca57-0868-4c7e-902f-b181009709c0".parse().unwrap()),
59            organization_id: "1bc9ac1e-f5aa-45f2-94bf-b181009709b8".parse().unwrap(),
60            name: "2.EI9Km5BfrIqBa1W+WCccfA==|laWxNnx+9H3MZww4zm7cBSLisjpi81zreaQntRhegVI=|x42+qKFf5ga6DIL0OW5pxCdLrC/gm8CXJvf3UASGteI=".parse().unwrap(),
61            external_id: None,
62            hide_passwords: false,
63            read_only: false,
64            manage: false,
65        }).unwrap();
66
67        assert_eq!(dec.name, "Default collection");
68    }
69}