test(credentials): seed identity/node_key in test helper so encrypt/decrypt works

Credentials tests created a fresh tempdir and immediately invoked
encrypt/decrypt, but load_encryption_key reads <dir>/identity/node_key
which did not exist, so every test failed with "node key not found".
Add a test_dir_with_node_key() helper that writes a deterministic 32-byte
key and switch all 8 call sites to it.
This commit is contained in:
archipelago 2026-04-23 13:02:28 -04:00
parent 7af048cc1a
commit 4edc420459

View File

@ -129,9 +129,21 @@ pub fn is_revoked(vc: &VerifiableCredential) -> bool {
mod tests {
use super::*;
/// Create a tempdir with a dummy `identity/node_key` so that the
/// credential store's encrypt/decrypt path can derive a key.
/// Returns the tempdir guard (drop it to clean up).
fn test_dir_with_node_key() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
let identity_dir = dir.path().join("identity");
std::fs::create_dir_all(&identity_dir).unwrap();
// 32 bytes of deterministic test material; never a real key.
std::fs::write(identity_dir.join("node_key"), [0xAB; 32]).unwrap();
dir
}
#[tokio::test]
async fn test_issue_credential_w3c_format() {
let dir = tempfile::tempdir().unwrap();
let dir = test_dir_with_node_key();
let vc = issue_credential(
dir.path(),
"did:key:issuer",
@ -163,7 +175,7 @@ mod tests {
#[tokio::test]
async fn test_issue_credential_serializes_as_jsonld() {
let dir = tempfile::tempdir().unwrap();
let dir = test_dir_with_node_key();
let vc = issue_credential(
dir.path(),
"did:key:issuer",
@ -185,7 +197,7 @@ mod tests {
#[tokio::test]
async fn test_save_and_load_roundtrip() {
let dir = tempfile::tempdir().unwrap();
let dir = test_dir_with_node_key();
issue_credential(
dir.path(),
"did:key:a",
@ -205,7 +217,7 @@ mod tests {
#[tokio::test]
async fn test_issue_credential_sign_fn_failure_propagates() {
let dir = tempfile::tempdir().unwrap();
let dir = test_dir_with_node_key();
let result = issue_credential(
dir.path(),
"did:key:issuer",
@ -257,7 +269,7 @@ mod tests {
#[tokio::test]
async fn test_revoke_credential() {
let dir = tempfile::tempdir().unwrap();
let dir = test_dir_with_node_key();
let vc = issue_credential(
dir.path(),
"did:key:issuer",
@ -288,7 +300,7 @@ mod tests {
#[tokio::test]
async fn test_revoke_nonexistent_credential_fails() {
let dir = tempfile::tempdir().unwrap();
let dir = test_dir_with_node_key();
let result = revoke_credential(dir.path(), "urn:uuid:does-not-exist").await;
assert!(result.is_err());
assert!(result
@ -299,7 +311,7 @@ mod tests {
#[tokio::test]
async fn test_list_credentials_no_filter() {
let dir = tempfile::tempdir().unwrap();
let dir = test_dir_with_node_key();
issue_credential(
dir.path(),
"did:key:a",
@ -329,7 +341,7 @@ mod tests {
#[tokio::test]
async fn test_list_credentials_filter_by_did() {
let dir = tempfile::tempdir().unwrap();
let dir = test_dir_with_node_key();
issue_credential(
dir.path(),
"did:key:alice",