rucksack_db/csv/
chrome.rs

1use serde::{Deserialize, Serialize};
2use url::Url;
3
4use crate::records::DecryptedRecord;
5
6use super::firefox;
7
8#[derive(Debug, Deserialize, Serialize)]
9pub struct Record {
10    pub name: String,
11    pub url: String,
12    pub username: String,
13    pub password: String,
14}
15
16impl Record {
17    pub fn to_decrypted(&self) -> DecryptedRecord {
18        let ffr = firefox::new_with_password(
19            self.url.clone(),
20            self.username.clone(),
21            self.password.clone(),
22        );
23        ffr.to_decrypted()
24    }
25}
26
27pub fn from_decrypted(r: DecryptedRecord) -> Record {
28    let url = r.metadata().url;
29    let parsed = Url::parse(&url).unwrap();
30    Record {
31        name: parsed.host_str().unwrap().to_string(),
32        url,
33        username: r.user(),
34        password: r.password(),
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41    use crate::records;
42
43    #[test]
44    fn test_record_to_decrypted() {
45        let chrome_record = Record {
46            name: "example.com".to_string(),
47            url: "https://example.com".to_string(),
48            username: "testuser".to_string(),
49            password: "testpass".to_string(),
50        };
51
52        let decrypted = chrome_record.to_decrypted();
53        assert_eq!(decrypted.secrets.user, "testuser");
54        assert_eq!(decrypted.secrets.password, "testpass");
55        assert_eq!(decrypted.metadata.url, "https://example.com");
56    }
57
58    #[test]
59    fn test_from_decrypted() {
60        let mut decrypted = records::DecryptedRecord {
61            secrets: records::secrets_from_user_pass("testuser", "testpass"),
62            metadata: records::default_metadata(),
63            history: Vec::new(),
64        };
65        decrypted.metadata.url = "https://example.com/login".to_string();
66
67        let chrome_record = from_decrypted(decrypted);
68        assert_eq!(chrome_record.name, "example.com");
69        assert_eq!(chrome_record.url, "https://example.com/login");
70        assert_eq!(chrome_record.username, "testuser");
71        assert_eq!(chrome_record.password, "testpass");
72    }
73
74    #[test]
75    fn test_roundtrip_conversion() {
76        let original = Record {
77            name: "test.com".to_string(),
78            url: "https://test.com".to_string(),
79            username: "user".to_string(),
80            password: "pass".to_string(),
81        };
82
83        let decrypted = original.to_decrypted();
84        let converted_back = from_decrypted(decrypted);
85
86        assert_eq!(converted_back.username, "user");
87        assert_eq!(converted_back.password, "pass");
88        assert_eq!(converted_back.url, "https://test.com");
89    }
90}