use super::*;
async fn push(ctx: &RpcContext, id: u64, namespace: &str, entries: &[(&str, &str)]) -> Reply {
put_secrets(ctx, id, namespace, "production", entries).await
}
#[tokio::test(start_paused = true)]
async fn a_push_lands_in_the_registry_the_supervisor_reads() {
let h = harness(vec![]);
let reply = push(&h.ctx, 1, "vercel", &[("API_KEY", "sk_live"), ("B", "2")]).await;
assert_eq!(reply.result.unwrap(), Response::SecretsPut { accepted: 2 });
let snapshot = h.ctx.provider_secrets.snapshot();
assert_eq!(
snapshot.namespace("vercel").unwrap()["API_KEY"]["production"],
"sk_live"
);
assert!(snapshot.pushed["vercel"].contains("production"));
}
#[tokio::test(start_paused = true)]
async fn a_namespace_outside_the_grammar_is_refused_and_stores_nothing() {
let h = harness(vec![]);
let reply = push(&h.ctx, 1, "ver/cel", &[("A", "1")]).await;
let Err(err) = reply.result else {
panic!("a namespace carrying a separator must be refused")
};
assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{err:?}");
assert!(h.ctx.provider_secrets.pushed().is_empty());
}
#[tokio::test(start_paused = true)]
async fn an_entry_key_outside_the_grammar_refuses_the_whole_push() {
let h = harness(vec![]);
let reply = push(&h.ctx, 1, "vercel", &[("GOOD", "1"), ("A/B", "2")]).await;
let Err(err) = reply.result else {
panic!("a key carrying a separator must be refused")
};
assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{err:?}");
assert!(err.message.contains("A/B"), "names the key: {err:?}");
assert!(
h.ctx.provider_secrets.pushed().is_empty(),
"the whole push is refused, not the one key"
);
}
#[tokio::test(start_paused = true)]
async fn an_oversized_value_refuses_the_whole_push_without_quoting_it() {
let h = harness(vec![]);
let huge = "x".repeat(shep_core::secrets::MAX_VALUE_BYTES + 1);
let reply = push(&h.ctx, 1, "vercel", &[("BIG", &huge)]).await;
let Err(err) = reply.result else {
panic!("a value over the cap must be refused")
};
assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{err:?}");
assert!(err.message.contains("BIG"), "names the key: {err:?}");
assert!(
!err.message.contains("xxxx"),
"never the value itself: {err:?}"
);
assert!(h.ctx.provider_secrets.pushed().is_empty());
}
#[tokio::test(start_paused = true)]
async fn a_value_at_the_cap_is_accepted() {
let h = harness(vec![]);
let big = "x".repeat(shep_core::secrets::MAX_VALUE_BYTES);
assert!(
push(&h.ctx, 1, "vercel", &[("BIG", &big)])
.await
.result
.is_ok()
);
}
#[tokio::test(start_paused = true)]
async fn an_environment_outside_the_grammar_is_refused() {
let h = harness(vec![]);
let reply = put_secrets(&h.ctx, 1, "vercel", "", &[]).await;
let Err(err) = reply.result else {
panic!("an empty environment must be refused")
};
assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{err:?}");
}
#[tokio::test(start_paused = true)]
async fn persist_false_in_dogs_toml_keeps_the_values_off_disk() {
let h = harness(vec![]);
std::fs::write(&h.ctx.dogs_config, "[vercel]\npersist = false\n").unwrap();
assert!(
push(&h.ctx, 1, "vercel", &[("A", "1")])
.await
.result
.is_ok()
);
assert!(!h.ctx.paths.secrets_cache.exists());
assert!(h.ctx.provider_secrets.pushed().contains_key("vercel"));
}
#[tokio::test(start_paused = true)]
async fn a_dog_that_says_nothing_about_persist_gets_a_cache() {
let h = harness(vec![]);
assert!(
push(&h.ctx, 1, "vercel", &[("A", "1")])
.await
.result
.is_ok()
);
assert!(h.ctx.paths.secrets_cache.exists());
}
#[tokio::test(start_paused = true)]
async fn describe_sweeps_past_a_dog_and_still_answers_when_one_is_named() {
let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
start_web(&h).await;
let dog = enable_dog(&h.ctx, 2, "bark").await;
let hits = describe(&h.ctx, 3, SelectorSpec::All).await;
assert_eq!(
hits.iter().map(|i| i.name.as_str()).collect::<Vec<_>>(),
vec!["web"],
"`all` is the flock, not the kennel"
);
let hits = describe(&h.ctx, 4, SelectorSpec::Name("bark".to_string())).await;
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].id, dog.id);
}
#[tokio::test(start_paused = true)]
async fn enabling_a_dog_over_a_sheeps_name_is_refused_rather_than_faked() {
let h = harness(vec![ProcScript::never_exits()]);
let started = reply_of(
dispatch(
envelope(
1,
Request::Start {
apps: vec![AppConfig::minimal("bark", "./srv")],
},
),
&h.ctx,
)
.await,
);
assert!(started.result.is_ok(), "{:?}", started.result);
let reply = reply_of(
dispatch(
envelope(
2,
Request::EnableDog {
name: "bark".to_string(),
env: BTreeMap::default(),
source: DogSource::BuiltIn {
grants: None,
events: Vec::new(),
},
},
),
&h.ctx,
)
.await,
);
let Err(err) = reply.result else {
panic!("expected a refusal, got {:?}", reply.result)
};
assert_eq!(err.code, RpcErrorCode::InvalidConfig);
assert!(
err.message.contains("bark"),
"the refusal names the collision: {}",
err.message
);
let listed = h.ctx.supervisor.list().await;
assert_eq!(listed.len(), 1, "nothing was started: {listed:?}");
assert_eq!(listed[0].dog, None);
}