|
| 1 | +use credential_exchange_format::NoteCredential; |
| 2 | + |
| 3 | +/// Extract note content from a CXF Note credential |
| 4 | +/// The way notes are handled (in import.rs) depends on their context: |
| 5 | +/// - If part of an item, use parent type and map content to Cipher::notes |
| 6 | +/// - If standalone, map to SecureNote |
| 7 | +/// |
| 8 | +/// That's why we only have this small utility function and tests here. |
| 9 | +pub(super) fn extract_note_content(note: &NoteCredential) -> String { |
| 10 | + note.content.value.0.clone() |
| 11 | +} |
| 12 | + |
| 13 | +#[cfg(test)] |
| 14 | +mod tests { |
| 15 | + use super::*; |
| 16 | + |
| 17 | + #[test] |
| 18 | + fn test_extract_note_content_with_content() { |
| 19 | + let note = NoteCredential { |
| 20 | + content: "This is a test note with important information." |
| 21 | + .to_owned() |
| 22 | + .into(), |
| 23 | + }; |
| 24 | + |
| 25 | + let content = extract_note_content(¬e); |
| 26 | + assert_eq!( |
| 27 | + content, |
| 28 | + "This is a test note with important information.".to_string() |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + #[test] |
| 33 | + fn test_extract_note_content_empty_string() { |
| 34 | + let note = NoteCredential { |
| 35 | + content: "".to_owned().into(), |
| 36 | + }; |
| 37 | + |
| 38 | + let content = extract_note_content(¬e); |
| 39 | + assert_eq!(content, "".to_string()); |
| 40 | + } |
| 41 | + |
| 42 | + #[test] |
| 43 | + fn test_extract_note_content_multiline() { |
| 44 | + let note = NoteCredential { |
| 45 | + content: "Line 1\nLine 2\nLine 3".to_owned().into(), |
| 46 | + }; |
| 47 | + |
| 48 | + let content = extract_note_content(¬e); |
| 49 | + assert_eq!(content, "Line 1\nLine 2\nLine 3".to_string()); |
| 50 | + } |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn test_extract_note_content_special_characters() { |
| 54 | + let note = NoteCredential { |
| 55 | + content: "Note with emojis 🔐 and special chars: @#$%^&*()" |
| 56 | + .to_owned() |
| 57 | + .into(), |
| 58 | + }; |
| 59 | + |
| 60 | + let content = extract_note_content(¬e); |
| 61 | + assert_eq!( |
| 62 | + content, |
| 63 | + "Note with emojis 🔐 and special chars: @#$%^&*()".to_string() |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_extract_note_content_very_long() { |
| 69 | + let long_content = "A".repeat(10000); |
| 70 | + let note = NoteCredential { |
| 71 | + content: long_content.clone().into(), |
| 72 | + }; |
| 73 | + |
| 74 | + let content = extract_note_content(¬e); |
| 75 | + assert_eq!(content, long_content); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_cxf_example_note_integration() { |
| 80 | + use std::fs; |
| 81 | + |
| 82 | + use crate::{cxf::import::parse_cxf_spec, CipherType}; |
| 83 | + |
| 84 | + // Read the actual CXF example file |
| 85 | + let cxf_data = fs::read_to_string("resources/cxf_example.json") |
| 86 | + .expect("Should be able to read cxf_example.json"); |
| 87 | + |
| 88 | + let items = parse_cxf_spec(cxf_data).expect("Should parse CXF data successfully"); |
| 89 | + |
| 90 | + // Find the note item (Home alarm) |
| 91 | + let note_cipher = items |
| 92 | + .iter() |
| 93 | + .find(|cipher| cipher.name == "Home alarm") |
| 94 | + .expect("Should find Home alarm note item"); |
| 95 | + |
| 96 | + // Validate it's a SecureNote cipher |
| 97 | + match ¬e_cipher.r#type { |
| 98 | + CipherType::SecureNote(_) => (), // Successfully identified as SecureNote |
| 99 | + _ => panic!("Expected SecureNote for standalone note credential"), |
| 100 | + } |
| 101 | + |
| 102 | + // Validate the note content |
| 103 | + assert_eq!( |
| 104 | + note_cipher.notes, |
| 105 | + Some("some instructionts to enable/disable the alarm".to_string()) |
| 106 | + ); |
| 107 | + |
| 108 | + // Should have no custom fields since it's a standalone note |
| 109 | + assert_eq!(note_cipher.fields.len(), 0); |
| 110 | + |
| 111 | + // Validate basic properties |
| 112 | + assert_eq!(note_cipher.name, "Home alarm"); |
| 113 | + assert_eq!(note_cipher.folder_id, None); |
| 114 | + assert!(!note_cipher.favorite); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_standalone_note_credential() { |
| 119 | + use credential_exchange_format::{Credential, Item}; |
| 120 | + |
| 121 | + use crate::{cxf::import::parse_item, CipherType, ImportingCipher}; |
| 122 | + |
| 123 | + let item = Item { |
| 124 | + id: [0, 1, 2, 3, 4, 5, 6].as_ref().into(), |
| 125 | + creation_at: Some(1706613834), |
| 126 | + modified_at: Some(1706623773), |
| 127 | + title: "My Important Note".to_string(), |
| 128 | + subtitle: None, |
| 129 | + favorite: None, |
| 130 | + credentials: vec![Credential::Note(Box::new(NoteCredential { |
| 131 | + content: |
| 132 | + "This is a standalone secure note with important information.\nLine 2\nLine 3" |
| 133 | + .to_string() |
| 134 | + .into(), |
| 135 | + }))], |
| 136 | + tags: None, |
| 137 | + extensions: None, |
| 138 | + scope: None, |
| 139 | + }; |
| 140 | + |
| 141 | + let ciphers: Vec<ImportingCipher> = parse_item(item); |
| 142 | + assert_eq!(ciphers.len(), 1); |
| 143 | + let cipher = ciphers.first().unwrap(); |
| 144 | + |
| 145 | + assert_eq!(cipher.folder_id, None); |
| 146 | + assert_eq!(cipher.name, "My Important Note"); |
| 147 | + assert_eq!( |
| 148 | + cipher.notes, |
| 149 | + Some( |
| 150 | + "This is a standalone secure note with important information.\nLine 2\nLine 3" |
| 151 | + .to_string() |
| 152 | + ) |
| 153 | + ); |
| 154 | + |
| 155 | + match &cipher.r#type { |
| 156 | + CipherType::SecureNote(_) => (), // Successfully created a SecureNote |
| 157 | + _ => panic!("Expected SecureNote"), |
| 158 | + }; |
| 159 | + |
| 160 | + assert_eq!(cipher.fields.len(), 0); // Notes don't have custom fields |
| 161 | + } |
| 162 | + |
| 163 | + // TODO: Consider moving this logic to import.rs since it's more about how notes are handled |
| 164 | + // during the import process |
| 165 | + #[test] |
| 166 | + fn test_note_as_part_of_login() { |
| 167 | + use credential_exchange_format::{BasicAuthCredential, Credential, Item}; |
| 168 | + |
| 169 | + use crate::{cxf::import::parse_item, CipherType, ImportingCipher}; |
| 170 | + |
| 171 | + let item = Item { |
| 172 | + id: [0, 1, 2, 3, 4, 5, 6].as_ref().into(), |
| 173 | + creation_at: Some(1706613834), |
| 174 | + modified_at: Some(1706623773), |
| 175 | + title: "Login with Note".to_string(), |
| 176 | + subtitle: None, |
| 177 | + favorite: None, |
| 178 | + credentials: vec![ |
| 179 | + Credential::BasicAuth(Box::new(BasicAuthCredential { |
| 180 | + username: Some("testuser".to_string().into()), |
| 181 | + password: Some("testpass".to_string().into()), |
| 182 | + })), |
| 183 | + Credential::Note(Box::new(NoteCredential { |
| 184 | + content: "This note should be added to the login cipher." |
| 185 | + .to_string() |
| 186 | + .into(), |
| 187 | + })), |
| 188 | + ], |
| 189 | + tags: None, |
| 190 | + extensions: None, |
| 191 | + scope: None, |
| 192 | + }; |
| 193 | + |
| 194 | + let ciphers: Vec<ImportingCipher> = parse_item(item); |
| 195 | + assert_eq!(ciphers.len(), 1); // Should create only one cipher (Login with note content) |
| 196 | + let cipher = ciphers.first().unwrap(); |
| 197 | + |
| 198 | + assert_eq!(cipher.name, "Login with Note"); |
| 199 | + assert_eq!( |
| 200 | + cipher.notes, |
| 201 | + Some("This note should be added to the login cipher.".to_string()) |
| 202 | + ); |
| 203 | + |
| 204 | + match &cipher.r#type { |
| 205 | + CipherType::Login(_) => (), // Should be a Login cipher |
| 206 | + _ => panic!("Expected Login cipher with note content"), |
| 207 | + }; |
| 208 | + } |
| 209 | + |
| 210 | + #[test] |
| 211 | + fn test_note_as_part_of_api_key() { |
| 212 | + use credential_exchange_format::{ApiKeyCredential, Credential, Item}; |
| 213 | + |
| 214 | + use crate::{cxf::import::parse_item, CipherType, ImportingCipher}; |
| 215 | + |
| 216 | + let item = Item { |
| 217 | + id: [0, 1, 2, 3, 4, 5, 6].as_ref().into(), |
| 218 | + creation_at: Some(1706613834), |
| 219 | + modified_at: Some(1706623773), |
| 220 | + title: "API Key with Note".to_string(), |
| 221 | + subtitle: None, |
| 222 | + favorite: None, |
| 223 | + credentials: vec![ |
| 224 | + Credential::ApiKey(Box::new(ApiKeyCredential { |
| 225 | + key: Some("api-key-12345".to_string().into()), |
| 226 | + username: Some("api-user".to_string().into()), |
| 227 | + key_type: Some("Bearer".to_string().into()), |
| 228 | + url: None, |
| 229 | + valid_from: None, |
| 230 | + expiry_date: None, |
| 231 | + })), |
| 232 | + Credential::Note(Box::new(NoteCredential { |
| 233 | + content: "This note should be added to the API key cipher." |
| 234 | + .to_string() |
| 235 | + .into(), |
| 236 | + })), |
| 237 | + ], |
| 238 | + tags: None, |
| 239 | + extensions: None, |
| 240 | + scope: None, |
| 241 | + }; |
| 242 | + |
| 243 | + let ciphers: Vec<ImportingCipher> = parse_item(item); |
| 244 | + assert_eq!(ciphers.len(), 1); // Should create only one cipher (SecureNote with note content) |
| 245 | + let cipher = ciphers.first().unwrap(); |
| 246 | + |
| 247 | + assert_eq!(cipher.name, "API Key with Note"); |
| 248 | + assert_eq!( |
| 249 | + cipher.notes, |
| 250 | + Some("This note should be added to the API key cipher.".to_string()) |
| 251 | + ); |
| 252 | + |
| 253 | + match &cipher.r#type { |
| 254 | + CipherType::SecureNote(_) => (), // Should be a SecureNote cipher |
| 255 | + _ => panic!("Expected SecureNote cipher with note content"), |
| 256 | + }; |
| 257 | + |
| 258 | + // Should have API key fields |
| 259 | + assert!(!cipher.fields.is_empty()); |
| 260 | + } |
| 261 | +} |
0 commit comments