Skip to main content

bitwarden_vault/cipher/blob/conversions/
secure_note.rs

1use super::{SecureNoteDataV1, SecureNoteView};
2
3impl_bidirectional_from!(SecureNoteView, SecureNoteDataV1, [r#type]);
4
5#[cfg(test)]
6mod tests {
7    use chrono::{TimeZone, Utc};
8
9    use super::super::{CipherBlobV1, test_support::*};
10    use crate::{
11        PasswordHistoryView,
12        cipher::{
13            cipher::CipherType,
14            field::{FieldType, FieldView},
15            secure_note::{SecureNoteType, SecureNoteView},
16        },
17    };
18
19    #[test]
20    fn test_secure_note_cipher_round_trip() {
21        let (key_store, key_id) = create_test_key_store();
22        let mut ctx = key_store.context_mut();
23
24        let original = crate::CipherView {
25            name: "My Secure Note".to_string(),
26            notes: Some("Secret notes".to_string()),
27            r#type: CipherType::SecureNote,
28            secure_note: Some(SecureNoteView {
29                r#type: SecureNoteType::Generic,
30            }),
31            fields: Some(vec![FieldView {
32                name: Some("field1".to_string()),
33                value: Some("value1".to_string()),
34                r#type: FieldType::Text,
35                linked_id: None,
36            }]),
37            password_history: Some(vec![PasswordHistoryView {
38                password: "old-pass".to_string(),
39                last_used_date: Utc.with_ymd_and_hms(2023, 6, 1, 0, 0, 0).unwrap(),
40            }]),
41            ..create_shell_cipher_view(CipherType::SecureNote)
42        };
43
44        let blob = CipherBlobV1::from_cipher_view(&original, &mut ctx, key_id).unwrap();
45        let mut restored = create_shell_cipher_view(CipherType::SecureNote);
46        blob.apply_to_cipher_view(&mut restored, &mut ctx, key_id)
47            .unwrap();
48
49        assert_eq!(restored.name, original.name);
50        assert_eq!(restored.notes, original.notes);
51        assert_eq!(restored.r#type, CipherType::SecureNote);
52        assert!(restored.secure_note.is_some());
53        assert!(restored.login.is_none());
54        assert!(restored.card.is_none());
55        assert!(restored.identity.is_none());
56        assert!(restored.ssh_key.is_none());
57        assert_eq!(restored.fields.as_ref().unwrap().len(), 1);
58        assert_eq!(
59            restored.fields.as_ref().unwrap()[0].name,
60            Some("field1".to_string())
61        );
62        assert_eq!(restored.password_history.as_ref().unwrap().len(), 1);
63        assert_eq!(
64            restored.password_history.as_ref().unwrap()[0].password,
65            "old-pass"
66        );
67    }
68}