Skip to content

Commit

Permalink
Add some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Feb 5, 2024
1 parent 74c1cb5 commit 2cc612c
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
112 changes: 112 additions & 0 deletions crates/bitwarden/src/tool/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,115 @@ impl From<ExportFormat> for bitwarden_exporters::Format {
}
}
}

#[cfg(test)]
mod tests {
use chrono::{DateTime, Utc};

use crate::vault::{login::LoginView, CipherRepromptType};

use super::*;

#[test]
fn test_try_from_folder_view() {
let view = FolderView {
id: Some("fd411a1a-fec8-4070-985d-0e6560860e69".parse().unwrap()),
name: "test_name".to_string(),
revision_date: "2024-01-30T17:55:36.150Z".parse().unwrap(),
};

let f: bitwarden_exporters::Folder = view.try_into().unwrap();

assert_eq!(
f.id,
"fd411a1a-fec8-4070-985d-0e6560860e69".parse().unwrap()
);
assert_eq!(f.name, "test_name".to_string());
}

#[test]
fn test_try_from_cipher_view_login() {
let cipher_view = CipherView {
r#type: CipherType::Login,
login: Some(LoginView {
username: Some("test_username".to_string()),
password: Some("test_password".to_string()),
password_revision_date: None,
uris: None,
totp: None,
autofill_on_page_load: None,
}),
id: "fd411a1a-fec8-4070-985d-0e6560860e69".parse().ok(),
organization_id: None,
folder_id: None,
collection_ids: vec![],
key: None,
name: "My login".to_string(),
notes: None,
identity: None,
card: None,
secure_note: None,
favorite: false,
reprompt: CipherRepromptType::None,
organization_use_totp: true,
edit: true,
view_password: true,
local_data: None,
attachments: None,
fields: None,
password_history: None,
creation_date: "2024-01-30T17:55:36.150Z".parse().unwrap(),
deleted_date: None,
revision_date: "2024-01-30T17:55:36.150Z".parse().unwrap(),
};

let cipher: bitwarden_exporters::Cipher = cipher_view.try_into().unwrap();

assert_eq!(
cipher.id,
"fd411a1a-fec8-4070-985d-0e6560860e69".parse().unwrap()
);
assert_eq!(cipher.folder_id, None);
assert_eq!(cipher.name, "My login".to_string());
assert_eq!(cipher.notes, None);
assert!(!cipher.favorite);
assert_eq!(cipher.reprompt, 0);
assert!(cipher.fields.is_empty());
assert_eq!(
cipher.revision_date,
"2024-01-30T17:55:36.150Z".parse::<DateTime<Utc>>().unwrap()
);
assert_eq!(
cipher.creation_date,
"2024-01-30T17:55:36.150Z".parse::<DateTime<Utc>>().unwrap()
);
assert_eq!(cipher.deleted_date, None);

if let bitwarden_exporters::CipherType::Login(l) = cipher.r#type {
assert_eq!(l.username, Some("test_username".to_string()));
assert_eq!(l.password, Some("test_password".to_string()));
assert!(l.login_uris.is_empty());
assert_eq!(l.totp, None);
} else {
panic!("Expected login type");
}
}

#[test]
fn test_from_export_format() {
assert!(matches!(
bitwarden_exporters::Format::from(ExportFormat::Csv),
bitwarden_exporters::Format::Csv
));
assert!(matches!(
bitwarden_exporters::Format::from(ExportFormat::Json),
bitwarden_exporters::Format::Json
));
assert!(matches!(
bitwarden_exporters::Format::from(ExportFormat::EncryptedJson {
password: "password".to_string()
}),
bitwarden_exporters::Format::EncryptedJson { .. }
));
}
}
2 changes: 1 addition & 1 deletion crates/bitwarden/src/vault/cipher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ pub(crate) mod secure_note;
pub use attachment::{
Attachment, AttachmentEncryptResult, AttachmentFile, AttachmentFileView, AttachmentView,
};
pub use cipher::{Cipher, CipherListView, CipherType, CipherView};
pub use cipher::{Cipher, CipherListView, CipherRepromptType, CipherType, CipherView};
pub use field::FieldView;
pub use secure_note::SecureNoteType;

0 comments on commit 2cc612c

Please sign in to comment.