Skip to main content

bitwarden_vault/cipher/blob/conversions/
identity.rs

1use super::{IdentityDataV1, IdentityView};
2
3impl_bidirectional_from!(
4    IdentityView,
5    IdentityDataV1,
6    [
7        title,
8        first_name,
9        middle_name,
10        last_name,
11        address1,
12        address2,
13        address3,
14        city,
15        state,
16        postal_code,
17        country,
18        company,
19        email,
20        phone,
21        ssn,
22        username,
23        passport_number,
24        license_number,
25    ]
26);
27
28#[cfg(test)]
29mod tests {
30    use super::super::{CipherBlobV1, test_support::*};
31    use crate::cipher::{cipher::CipherType, identity::IdentityView};
32
33    #[test]
34    fn test_identity_cipher_round_trip() {
35        let (key_store, key_id) = create_test_key_store();
36        let mut ctx = key_store.context_mut();
37
38        let original = crate::CipherView {
39            name: "My Identity".to_string(),
40            notes: Some("Identity notes".to_string()),
41            r#type: CipherType::Identity,
42            identity: Some(IdentityView {
43                title: Some("Mr".to_string()),
44                first_name: Some("John".to_string()),
45                middle_name: None,
46                last_name: Some("Doe".to_string()),
47                address1: Some("123 Main St".to_string()),
48                address2: None,
49                address3: None,
50                city: Some("NYC".to_string()),
51                state: Some("NY".to_string()),
52                postal_code: Some("10001".to_string()),
53                country: Some("US".to_string()),
54                company: None,
55                email: Some("[email protected]".to_string()),
56                phone: None,
57                ssn: None,
58                username: Some("johndoe".to_string()),
59                passport_number: None,
60                license_number: None,
61            }),
62            ..create_shell_cipher_view(CipherType::Identity)
63        };
64
65        let blob = CipherBlobV1::from_cipher_view(&original, &mut ctx, key_id).unwrap();
66        let mut restored = create_shell_cipher_view(CipherType::Identity);
67        blob.apply_to_cipher_view(&mut restored, &mut ctx, key_id)
68            .unwrap();
69
70        assert_eq!(restored.name, "My Identity");
71        assert_eq!(restored.r#type, CipherType::Identity);
72        let identity = restored.identity.unwrap();
73        assert_eq!(identity.first_name, Some("John".to_string()));
74        assert_eq!(identity.last_name, Some("Doe".to_string()));
75        assert_eq!(identity.email, Some("[email protected]".to_string()));
76        assert!(restored.login.is_none());
77        assert!(restored.card.is_none());
78    }
79}