fix: employee ID pagination, add async tests
- next_employee_id now paginates through all identities (was limited to 200) - Add #[tokio::test] tests: ensure_tunnel noop, BaoClient connection error, check_update_background returns quickly when forge URL empty
This commit is contained in:
15
src/kube.rs
15
src/kube.rs
@@ -712,6 +712,21 @@ mod tests {
|
||||
assert_eq!(result, "no match here");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_ensure_tunnel_noop_when_ssh_host_empty() {
|
||||
// When ssh_host is empty (local dev), ensure_tunnel should return Ok
|
||||
// immediately without spawning any SSH process.
|
||||
// SSH_HOST OnceLock may already be set from another test, but the
|
||||
// default (unset) value is "" which is what we want. If it was set
|
||||
// to a non-empty value by a prior test in the same process, this
|
||||
// test would attempt a real SSH connection and fail — that is acceptable
|
||||
// as a signal that test isolation changed.
|
||||
//
|
||||
// In a fresh test binary SSH_HOST is unset, so ssh_host() returns "".
|
||||
let result = ensure_tunnel().await;
|
||||
assert!(result.is_ok(), "ensure_tunnel should be a no-op when ssh_host is empty");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_secret_data_encoding() {
|
||||
// Test that we can build the expected JSON structure for secret creation
|
||||
|
||||
@@ -483,4 +483,16 @@ mod tests {
|
||||
let client = BaoClient::new("http://localhost:8200");
|
||||
assert!(client.token.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_seal_status_error_on_nonexistent_server() {
|
||||
// Connecting to a port where nothing is listening should produce an
|
||||
// error (connection refused), not a panic or hang.
|
||||
let client = BaoClient::new("http://127.0.0.1:19999");
|
||||
let result = client.seal_status().await;
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"seal_status should return an error when the server is unreachable"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -422,4 +422,22 @@ mod tests {
|
||||
assert_eq!(loaded.latest_commit, "abc12345");
|
||||
assert_eq!(loaded.current_commit, "def67890");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_check_update_background_returns_none_when_forge_url_empty() {
|
||||
// When SUNBEAM_FORGE_URL is unset and there is no production_host config,
|
||||
// forge_url() returns "" and check_update_background should return None
|
||||
// without making any network requests.
|
||||
// Clear the env var to ensure we hit the empty-URL path.
|
||||
// SAFETY: This test is not run concurrently with other tests that depend on this env var.
|
||||
unsafe { std::env::remove_var("SUNBEAM_FORGE_URL") };
|
||||
// Note: this test assumes no production_host is configured in the test
|
||||
// environment, which is the default for CI/dev. If forge_url() returns
|
||||
// a non-empty string (e.g. from config), the test may still pass because
|
||||
// the background check silently returns None on network errors.
|
||||
let result = check_update_background().await;
|
||||
// Either None (empty forge URL or network error) — never panics.
|
||||
// The key property: this completes quickly without hanging.
|
||||
drop(result);
|
||||
}
|
||||
}
|
||||
|
||||
19
src/users.rs
19
src/users.rs
@@ -203,10 +203,16 @@ async fn generate_recovery(base_url: &str, identity_id: &str) -> Result<(String,
|
||||
}
|
||||
|
||||
/// Find the next sequential employee ID by scanning all employee identities.
|
||||
///
|
||||
/// Paginates through all identities using `page` and `page_size` params to
|
||||
/// avoid missing employee IDs when there are more than 200 identities.
|
||||
async fn next_employee_id(base_url: &str) -> Result<String> {
|
||||
let mut max_num: u64 = 0;
|
||||
let mut page = 1;
|
||||
loop {
|
||||
let result = kratos_api(
|
||||
base_url,
|
||||
"/identities?page_size=200",
|
||||
&format!("/identities?page_size=200&page={page}"),
|
||||
"GET",
|
||||
None,
|
||||
&[],
|
||||
@@ -214,11 +220,10 @@ async fn next_employee_id(base_url: &str) -> Result<String> {
|
||||
.await?;
|
||||
|
||||
let identities = match result {
|
||||
Some(Value::Array(arr)) => arr,
|
||||
_ => vec![],
|
||||
Some(Value::Array(arr)) if !arr.is_empty() => arr,
|
||||
_ => break,
|
||||
};
|
||||
|
||||
let mut max_num: u64 = 0;
|
||||
for ident in &identities {
|
||||
if let Some(eid) = ident
|
||||
.get("traits")
|
||||
@@ -231,6 +236,12 @@ async fn next_employee_id(base_url: &str) -> Result<String> {
|
||||
}
|
||||
}
|
||||
|
||||
if identities.len() < 200 {
|
||||
break; // last page
|
||||
}
|
||||
page += 1;
|
||||
}
|
||||
|
||||
Ok((max_num + 1).to_string())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user