diff --git a/Cargo.toml b/Cargo.toml index bbde885..e2722d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,10 +3,10 @@ name = "bitwarden-operator-rs" version = "0.1.0" edition = "2021" - -[[bin]] -name = "crdgen" +[[example]] +name = "crd" path = "src/crdgen.rs" +test = false [[bin]] name = "bitwarden-operator-rs" diff --git a/README.md b/README.md index c8458df..a8140b3 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ powered cluster management. - [x] [OpenTelemetry](https://opentelemetry.io/) Traces ## TODOs -- [ ] Unit testing +- [ ] Unit testing (partially implemented) - [ ] More metrics/observability ## Getting started @@ -98,6 +98,13 @@ spec: test: hello-world ``` +## Generating the CRD + +Use this command to output the CRD if you need to modify it +```shell +cargo run --example crd +``` + ## Credits/Thanks - [Bitwarden](https://bitwarden.com/) for their product diff --git a/src/bitwarden_cli/mod.rs b/src/bitwarden_cli/mod.rs index 6136941..1c94ad5 100644 --- a/src/bitwarden_cli/mod.rs +++ b/src/bitwarden_cli/mod.rs @@ -52,6 +52,7 @@ pub enum BitwardenError { #[serde(rename_all = "camelCase")] pub struct BitwardenItem { pub id: String, + #[serde(rename = "notes")] pub note: Option, pub fields: Option>, } @@ -260,3 +261,42 @@ impl BitwardenCliClient { } } } + +#[cfg(test)] +mod tests { + use crate::bitwarden_cli::BitwardenItem; + use std::fs; + + const BITWARDEN_FIELDS: &str = "tests/bitwarden-fields.json"; + const BITWARDEN_NOTES: &str = "tests/bitwarden-note.json"; + + #[test] + fn deserialize_bitwarden_fields() -> eyre::Result<()> { + let bitwarden_item = fs::read_to_string(BITWARDEN_FIELDS) + .unwrap_or_else(|_| panic!("Couldn't deserialize {BITWARDEN_FIELDS}")); + + let bitwarden_item: BitwardenItem = + serde_json::from_str(&bitwarden_item).expect("Couldn't deserialize to BitwardenItem"); + + let fields = bitwarden_item.fields.expect("Couldn't deserialize fields"); + assert_eq!(bitwarden_item.id, "00000000-0000-0000-0000-000000000000"); + assert_eq!(fields[0].name, "super-secret-field"); + assert_eq!(fields[0].value, "super-secret"); + assert_eq!(bitwarden_item.note, None); + Ok(()) + } + + #[test] + fn deserialize_bitwarden_notes() -> eyre::Result<()> { + let bitwarden_item = fs::read_to_string(BITWARDEN_NOTES) + .unwrap_or_else(|_| panic!("Couldn't deserialize {BITWARDEN_NOTES}")); + + let bitwarden_item: BitwardenItem = + serde_json::from_str(&bitwarden_item).expect("Couldn't deserialize to BitwardenItem"); + + assert_eq!(bitwarden_item.id, "00000000-0000-0000-0000-000000000000"); + assert!(bitwarden_item.fields.is_none()); + assert_eq!(bitwarden_item.note.unwrap(), "hello-world"); + Ok(()) + } +} diff --git a/tests/bitwarden-fields.json b/tests/bitwarden-fields.json new file mode 100644 index 0000000..c475872 --- /dev/null +++ b/tests/bitwarden-fields.json @@ -0,0 +1,29 @@ +{ + "passwordHistory": null, + "revisionDate": "2024-01-01T00:00:00.000Z", + "creationDate": "2024-01-01T00:00:00.000Z", + "deletedDate": null, + "object": "item", + "id": "00000000-0000-0000-0000-000000000000", + "organizationId": "00000000-0000-0000-0000-000000000000", + "folderId": null, + "type": 2, + "reprompt": 0, + "name": "bitwarden-fields", + "notes": null, + "favorite": false, + "fields": [ + { + "name": "super-secret-field", + "value": "super-secret", + "type": 1, + "linkedId": null + } + ], + "secureNote": { + "type": 0 + }, + "collectionIds": [ + "00000000-0000-0000-0000-000000000000" + ] +} diff --git a/tests/bitwarden-note.json b/tests/bitwarden-note.json new file mode 100644 index 0000000..27c28a5 --- /dev/null +++ b/tests/bitwarden-note.json @@ -0,0 +1,21 @@ +{ + "passwordHistory": null, + "revisionDate": "2024-01-01T00:00:00.000Z", + "creationDate": "2024-01-01T00:00:00.000Z", + "deletedDate": null, + "object": "item", + "id": "00000000-0000-0000-0000-000000000000", + "organizationId": "00000000-0000-0000-0000-000000000000", + "folderId": null, + "type": 2, + "reprompt": 0, + "name": "bitwarden-note", + "notes": "hello-world", + "favorite": false, + "secureNote": { + "type": 0 + }, + "collectionIds": [ + "00000000-0000-0000-0000-000000000000" + ] +}