2024-06-24 18:18:33 +03:00
|
|
|
//! Note that these tests *must* be run in their own process, as they rely on having a deterministic
|
|
|
|
//! credential store. Due to its global nature, tests cannot run in parallel
|
|
|
|
//! (or mixed with parallel tests that set their own credential store)
|
2024-07-28 21:48:13 +03:00
|
|
|
use gitbutler_secret::{secret, Sensitive};
|
2024-06-25 10:40:37 +03:00
|
|
|
use serial_test::serial;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn retrieve_unknown_is_none() {
|
2024-06-24 18:18:33 +03:00
|
|
|
credentials::setup();
|
2024-06-28 21:31:32 +03:00
|
|
|
for ns in all_namespaces() {
|
|
|
|
assert!(secret::retrieve("does not exist for sure", *ns)
|
|
|
|
.expect("no error to ask for non-existing")
|
|
|
|
.is_none());
|
|
|
|
}
|
2024-06-25 10:40:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn store_and_retrieve() -> anyhow::Result<()> {
|
2024-06-24 18:18:33 +03:00
|
|
|
credentials::setup();
|
2024-06-28 21:31:32 +03:00
|
|
|
for ns in all_namespaces() {
|
|
|
|
secret::persist("new", &Sensitive("secret".into()), *ns)?;
|
|
|
|
let secret = secret::retrieve("new", *ns)?.expect("it was just stored");
|
|
|
|
assert_eq!(
|
|
|
|
secret.0, "secret",
|
|
|
|
"note that this works only if the engine supports actual persistence, \
|
2024-06-25 10:40:37 +03:00
|
|
|
which should be the default outside of tests"
|
2024-06-28 21:31:32 +03:00
|
|
|
);
|
|
|
|
}
|
2024-06-25 10:40:37 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-26 17:57:41 +03:00
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn store_empty_equals_deletion() -> anyhow::Result<()> {
|
|
|
|
credentials::setup();
|
2024-06-28 21:31:32 +03:00
|
|
|
for ns in all_namespaces() {
|
|
|
|
secret::persist("new", &Sensitive("secret".into()), *ns)?;
|
|
|
|
assert_eq!(credentials::count(), 1);
|
2024-06-26 17:57:41 +03:00
|
|
|
|
2024-06-28 21:31:32 +03:00
|
|
|
secret::persist("new", &Sensitive("".into()), *ns)?;
|
|
|
|
assert_eq!(
|
|
|
|
secret::retrieve("new", *ns)?.map(|s| s.0),
|
|
|
|
None,
|
|
|
|
"empty passwords are automatically deleted"
|
|
|
|
);
|
|
|
|
assert_eq!(credentials::count(), 0);
|
|
|
|
}
|
2024-06-26 17:57:41 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-28 21:31:32 +03:00
|
|
|
fn all_namespaces() -> &'static [secret::Namespace] {
|
|
|
|
&[secret::Namespace::Global, secret::Namespace::BuildKind]
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:18:33 +03:00
|
|
|
pub(crate) mod credentials;
|
|
|
|
mod users;
|