Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(user_agents): add credential storage tests

Steven Enamakel936fbf6499d4
diff --git a/crates/openhuman-core/src/user_agents/credentials_tests.rs b/crates/openhuman-core/src/user_agents/credentials_tests.rs
new file mode 100644
index 0000000000..1e446914ea
--- /dev/null
+++ b/crates/openhuman-core/src/user_agents/credentials_tests.rs
@@ -0,0 +1,66 @@
+use super::*;
+use crate::security::credentials::session_support::{resolve_backend_credential, BackendCredential};
+use crate::user_agents::layout::{agent_config, UserAgentLayout};
+use crate::user_agents::UserAgentId;
+
+fn agent(tmp: &tempfile::TempDir, user: &str) -> Config {
+ let id = UserAgentId::for_user(user).unwrap();
+ let layout = UserAgentLayout::new(tmp.path(), &id);
+ std::fs::create_dir_all(&layout.workspace_dir).unwrap();
+ agent_config(&layout, &id)
+}
+
+#[test]
+fn each_agent_resolves_only_its_own_credential() {
+ let tmp = tempfile::tempdir().unwrap();
+ let alice = agent(&tmp, "alice");
+ let bob = agent(&tmp, "bob");
+ store(&alice, UserCredentialKind::Session, "alice-jwt", None).unwrap();
+ store(&bob, UserCredentialKind::ApiKey, "bob-key", None).unwrap();
+
+ assert_eq!(
+ resolve_backend_credential(&alice).unwrap(),
+ BackendCredential::Session("alice-jwt".into())
+ );
+ assert_eq!(
+ resolve_backend_credential(&bob).unwrap(),
+ BackendCredential::ApiKey("bob-key".into())
+ );
+}
+
+#[test]
+fn clearing_one_agent_leaves_the_other() {
+ let tmp = tempfile::tempdir().unwrap();
+ let alice = agent(&tmp, "alice");
+ let bob = agent(&tmp, "bob");
+ store(&alice, UserCredentialKind::Session, "alice-jwt", None).unwrap();
+ store(&bob, UserCredentialKind::Session, "bob-jwt", None).unwrap();
+ assert!(clear(&alice).unwrap());
+ assert!(!has(&alice));
+ assert!(has(&bob));
+ assert!(!clear(&alice).unwrap(), "nothing left to clear");
+}
+
+#[test]
+fn an_expired_session_is_rejected_locally() {
+ let tmp = tempfile::tempdir().unwrap();
+ let alice = agent(&tmp, "alice");
+ store(
+ &alice,
+ UserCredentialKind::Session,
+ "alice-jwt",
+ Some("2000-01-01T00:00:00Z"),
+ )
+ .unwrap();
+ let err = resolve_backend_credential(&alice).unwrap_err();
+ assert!(err.contains("SESSION_EXPIRED"), "{err}");
+}
+
+#[test]
+fn bad_input_is_refused() {
+ let tmp = tempfile::tempdir().unwrap();
+ let alice = agent(&tmp, "alice");
+ assert!(store(&alice, UserCredentialKind::Session, " ", None).is_err());
+ assert!(store(&alice, UserCredentialKind::Session, "t", Some("tomorrow")).is_err());
+ assert!(!has(&alice));
+}