Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
add bitwarden deserialization unit tests & fix notes field
Browse files Browse the repository at this point in the history
  • Loading branch information
BlowaXD committed Mar 29, 2024
1 parent 7de54a1 commit c5103df
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/bitwarden_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub enum BitwardenError {
#[serde(rename_all = "camelCase")]
pub struct BitwardenItem {
pub id: String,
#[serde(rename = "notes")]
pub note: Option<String>,
pub fields: Option<Vec<BitwardenItemField>>,
}
Expand Down Expand Up @@ -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(())
}
}
29 changes: 29 additions & 0 deletions tests/bitwarden-fields.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
21 changes: 21 additions & 0 deletions tests/bitwarden-note.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit c5103df

Please sign in to comment.