mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-09-02 09:39:37 +00:00
refactor: centralize test credential constants into testing::credentials (#829)
* refactor: centralize test credential constants into testing::credentials Scattered test credential strings (API keys, OAuth tokens, crypto keys, Telegram tokens, session tokens) across ~25 files made security auditing harder and created unnecessary duplication. Centralize all test-only fake credentials into a new `src/testing/credentials.rs` module with named constants and a shared `test_secrets_store()` helper. - Convert `src/testing.rs` to directory module (`src/testing/mod.rs`) - Add `src/testing/credentials.rs` with ~30 named constants - Replace hardcoded literals in 24 source files - Deduplicate `test_store()` helper (was copy-pasted in 3 files) - Leave leak_detector/shell/signature tests as-is (inline values aid readability for pattern detection tests) [skip-regression-check] Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * refactor: replace real Telegram bot token with obviously fake test stub Co-Authored-By: Claude Sonnet 4.6 <[email protected]> * Update src/testing/credentials.rs Co-authored-by: Copilot <[email protected]> * Update src/testing/credentials.rs Co-authored-by: Copilot <[email protected]> * refactor: address PR review feedback on test credentials - Fix TEST_CRYPTO_KEY doc comment ("32-byte hex" → "32-character key string") - Rename confusing "real"/"fake" Anthropic constant names and values - Change TEST_STRIPE_KEY from "sk-live" to "sk_test_fake123" to avoid scanners - Use test_secrets_store() helper in orchestrator and http tool tests - Clarify config_round_trip.rs doc comment about integration test visibility Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]> Co-authored-by: Copilot <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 4.6
Copilot
parent
24d4fbb8a7
commit
76375f2eaa
@@ -365,22 +365,18 @@ fn base64_encode(input: &[u8]) -> String {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use secrecy::SecretString;
|
||||
|
||||
use crate::secrets::{
|
||||
CreateSecretParams, CredentialLocation, CredentialMapping, InMemorySecretsStore,
|
||||
SecretsCrypto, SecretsStore,
|
||||
SecretsStore,
|
||||
};
|
||||
use crate::testing::credentials::{TEST_OPENAI_API_KEY, test_secrets_store};
|
||||
use crate::tools::wasm::credential_injector::{
|
||||
CredentialInjector, base64_encode, host_matches_pattern,
|
||||
};
|
||||
|
||||
fn test_store() -> InMemorySecretsStore {
|
||||
let key = "0123456789abcdef0123456789abcdef";
|
||||
let crypto = Arc::new(SecretsCrypto::new(SecretString::from(key.to_string())).unwrap());
|
||||
InMemorySecretsStore::new(crypto)
|
||||
test_secrets_store()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -406,7 +402,10 @@ mod tests {
|
||||
async fn test_inject_bearer() {
|
||||
let store = test_store();
|
||||
store
|
||||
.create("user1", CreateSecretParams::new("openai_key", "sk-test123"))
|
||||
.create(
|
||||
"user1",
|
||||
CreateSecretParams::new("openai_key", TEST_OPENAI_API_KEY),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -428,7 +427,7 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
result.headers.get("Authorization"),
|
||||
Some(&"Bearer sk-test123".to_string())
|
||||
Some(&format!("Bearer {TEST_OPENAI_API_KEY}"))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -694,6 +694,7 @@ mod tests {
|
||||
|
||||
use tempfile::TempDir;
|
||||
|
||||
use crate::testing::credentials::{TEST_OAUTH_CLIENT_ID, TEST_OAUTH_CLIENT_SECRET};
|
||||
use crate::tools::wasm::loader::{WasmLoadError, check_wit_version_compat, discover_tools};
|
||||
|
||||
#[test]
|
||||
@@ -834,8 +835,8 @@ mod tests {
|
||||
oauth: Some(OAuthConfigSchema {
|
||||
authorization_url: "https://accounts.google.com/o/oauth2/v2/auth".to_string(),
|
||||
token_url: "https://oauth2.googleapis.com/token".to_string(),
|
||||
client_id: Some("test-client-id".to_string()),
|
||||
client_secret: Some("test-client-secret".to_string()),
|
||||
client_id: Some(TEST_OAUTH_CLIENT_ID.to_string()),
|
||||
client_secret: Some(TEST_OAUTH_CLIENT_SECRET.to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
..Default::default()
|
||||
@@ -848,8 +849,11 @@ mod tests {
|
||||
|
||||
let config = config.unwrap();
|
||||
assert_eq!(config.token_url, "https://oauth2.googleapis.com/token");
|
||||
assert_eq!(config.client_id, "test-client-id");
|
||||
assert_eq!(config.client_secret, Some("test-client-secret".to_string()));
|
||||
assert_eq!(config.client_id, TEST_OAUTH_CLIENT_ID);
|
||||
assert_eq!(
|
||||
config.client_secret,
|
||||
Some(TEST_OAUTH_CLIENT_SECRET.to_string())
|
||||
);
|
||||
assert_eq!(config.secret_name, "google_oauth_token");
|
||||
assert_eq!(config.provider, Some("google".to_string()));
|
||||
}
|
||||
|
||||
+29
-49
@@ -1223,6 +1223,11 @@ fn coerce_params_to_schema(
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::testing::credentials::{
|
||||
TEST_BEARER_TOKEN_123, TEST_GOOGLE_OAUTH_FRESH, TEST_GOOGLE_OAUTH_LEGACY,
|
||||
TEST_GOOGLE_OAUTH_TOKEN, TEST_OAUTH_CLIENT_ID, TEST_OAUTH_CLIENT_SECRET,
|
||||
test_secrets_store,
|
||||
};
|
||||
use crate::tools::wasm::capabilities::Capabilities;
|
||||
use crate::tools::wasm::runtime::{WasmRuntimeConfig, WasmToolRuntime};
|
||||
|
||||
@@ -1290,12 +1295,12 @@ mod tests {
|
||||
let mut h = HashMap::new();
|
||||
h.insert(
|
||||
"Authorization".to_string(),
|
||||
"Bearer test-token-123".to_string(),
|
||||
format!("Bearer {TEST_BEARER_TOKEN_123}"),
|
||||
);
|
||||
h
|
||||
},
|
||||
query_params: HashMap::new(),
|
||||
secret_value: "test-token-123".to_string(),
|
||||
secret_value: TEST_BEARER_TOKEN_123.to_string(),
|
||||
}];
|
||||
|
||||
let store_data = StoreData::new(
|
||||
@@ -1311,7 +1316,7 @@ mod tests {
|
||||
store_data.inject_host_credentials("www.googleapis.com", &mut headers, &mut url);
|
||||
assert_eq!(
|
||||
headers.get("Authorization"),
|
||||
Some(&"Bearer test-token-123".to_string())
|
||||
Some(&format!("Bearer {TEST_BEARER_TOKEN_123}"))
|
||||
);
|
||||
|
||||
// Should not inject for non-matching host
|
||||
@@ -1387,13 +1392,9 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_resolve_host_credentials_no_http_cap() {
|
||||
use crate::secrets::{InMemorySecretsStore, SecretsCrypto};
|
||||
use crate::tools::wasm::wrapper::resolve_host_credentials;
|
||||
use secrecy::SecretString;
|
||||
|
||||
let key = "0123456789abcdef0123456789abcdef";
|
||||
let crypto = Arc::new(SecretsCrypto::new(SecretString::from(key.to_string())).unwrap());
|
||||
let store = InMemorySecretsStore::new(crypto);
|
||||
let store = test_secrets_store();
|
||||
|
||||
let caps = Capabilities::default();
|
||||
let result = resolve_host_credentials(&caps, Some(&store), "user1", None).await;
|
||||
@@ -1405,21 +1406,17 @@ mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::secrets::{
|
||||
CreateSecretParams, CredentialLocation, CredentialMapping, InMemorySecretsStore,
|
||||
SecretsCrypto, SecretsStore,
|
||||
CreateSecretParams, CredentialLocation, CredentialMapping, SecretsStore,
|
||||
};
|
||||
use crate::tools::wasm::capabilities::HttpCapability;
|
||||
use crate::tools::wasm::wrapper::resolve_host_credentials;
|
||||
use secrecy::SecretString;
|
||||
|
||||
let key = "0123456789abcdef0123456789abcdef";
|
||||
let crypto = Arc::new(SecretsCrypto::new(SecretString::from(key.to_string())).unwrap());
|
||||
let store = InMemorySecretsStore::new(crypto);
|
||||
let store = test_secrets_store();
|
||||
|
||||
store
|
||||
.create(
|
||||
"user1",
|
||||
CreateSecretParams::new("google_oauth_token", "ya29.test-token"),
|
||||
CreateSecretParams::new("google_oauth_token", TEST_GOOGLE_OAUTH_TOKEN),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -1447,7 +1444,7 @@ mod tests {
|
||||
assert_eq!(result[0].host_patterns, vec!["www.googleapis.com"]);
|
||||
assert_eq!(
|
||||
result[0].headers.get("Authorization"),
|
||||
Some(&"Bearer ya29.test-token".to_string())
|
||||
Some(&format!("Bearer {TEST_GOOGLE_OAUTH_TOKEN}"))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1455,16 +1452,11 @@ mod tests {
|
||||
async fn test_resolve_host_credentials_missing_secret() {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::secrets::{
|
||||
CredentialLocation, CredentialMapping, InMemorySecretsStore, SecretsCrypto,
|
||||
};
|
||||
use crate::secrets::{CredentialLocation, CredentialMapping};
|
||||
use crate::tools::wasm::capabilities::HttpCapability;
|
||||
use crate::tools::wasm::wrapper::resolve_host_credentials;
|
||||
use secrecy::SecretString;
|
||||
|
||||
let key = "0123456789abcdef0123456789abcdef";
|
||||
let crypto = Arc::new(SecretsCrypto::new(SecretString::from(key.to_string())).unwrap());
|
||||
let store = InMemorySecretsStore::new(crypto);
|
||||
let store = test_secrets_store();
|
||||
|
||||
// No secret stored, should silently skip
|
||||
let mut credentials = HashMap::new();
|
||||
@@ -1494,23 +1486,19 @@ mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::secrets::{
|
||||
CreateSecretParams, CredentialLocation, CredentialMapping, InMemorySecretsStore,
|
||||
SecretsCrypto, SecretsStore,
|
||||
CreateSecretParams, CredentialLocation, CredentialMapping, SecretsStore,
|
||||
};
|
||||
use crate::tools::wasm::capabilities::HttpCapability;
|
||||
use crate::tools::wasm::wrapper::{OAuthRefreshConfig, resolve_host_credentials};
|
||||
use secrecy::SecretString;
|
||||
|
||||
let key = "0123456789abcdef0123456789abcdef";
|
||||
let crypto = Arc::new(SecretsCrypto::new(SecretString::from(key.to_string())).unwrap());
|
||||
let store = InMemorySecretsStore::new(crypto);
|
||||
let store = test_secrets_store();
|
||||
|
||||
// Store a token that expires 2 hours from now (well within buffer)
|
||||
let expires_at = chrono::Utc::now() + chrono::Duration::hours(2);
|
||||
store
|
||||
.create(
|
||||
"user1",
|
||||
CreateSecretParams::new("google_oauth_token", "ya29.fresh-token")
|
||||
CreateSecretParams::new("google_oauth_token", TEST_GOOGLE_OAUTH_FRESH)
|
||||
.with_expiry(expires_at),
|
||||
)
|
||||
.await
|
||||
@@ -1536,8 +1524,8 @@ mod tests {
|
||||
|
||||
let oauth_config = OAuthRefreshConfig {
|
||||
token_url: "https://oauth2.googleapis.com/token".to_string(),
|
||||
client_id: "test-client-id".to_string(),
|
||||
client_secret: Some("test-client-secret".to_string()),
|
||||
client_id: TEST_OAUTH_CLIENT_ID.to_string(),
|
||||
client_secret: Some(TEST_OAUTH_CLIENT_SECRET.to_string()),
|
||||
secret_name: "google_oauth_token".to_string(),
|
||||
provider: Some("google".to_string()),
|
||||
};
|
||||
@@ -1548,7 +1536,7 @@ mod tests {
|
||||
assert_eq!(result.len(), 1);
|
||||
assert_eq!(
|
||||
result[0].headers.get("Authorization"),
|
||||
Some(&"Bearer ya29.fresh-token".to_string())
|
||||
Some(&format!("Bearer {TEST_GOOGLE_OAUTH_FRESH}"))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1557,16 +1545,12 @@ mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::secrets::{
|
||||
CreateSecretParams, CredentialLocation, CredentialMapping, InMemorySecretsStore,
|
||||
SecretsCrypto, SecretsStore,
|
||||
CreateSecretParams, CredentialLocation, CredentialMapping, SecretsStore,
|
||||
};
|
||||
use crate::tools::wasm::capabilities::HttpCapability;
|
||||
use crate::tools::wasm::wrapper::resolve_host_credentials;
|
||||
use secrecy::SecretString;
|
||||
|
||||
let key = "0123456789abcdef0123456789abcdef";
|
||||
let crypto = Arc::new(SecretsCrypto::new(SecretString::from(key.to_string())).unwrap());
|
||||
let store = InMemorySecretsStore::new(crypto);
|
||||
let store = test_secrets_store();
|
||||
|
||||
// Store an expired token
|
||||
let expires_at = chrono::Utc::now() - chrono::Duration::hours(1);
|
||||
@@ -1606,22 +1590,18 @@ mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::secrets::{
|
||||
CreateSecretParams, CredentialLocation, CredentialMapping, InMemorySecretsStore,
|
||||
SecretsCrypto, SecretsStore,
|
||||
CreateSecretParams, CredentialLocation, CredentialMapping, SecretsStore,
|
||||
};
|
||||
use crate::tools::wasm::capabilities::HttpCapability;
|
||||
use crate::tools::wasm::wrapper::{OAuthRefreshConfig, resolve_host_credentials};
|
||||
use secrecy::SecretString;
|
||||
|
||||
let key = "0123456789abcdef0123456789abcdef";
|
||||
let crypto = Arc::new(SecretsCrypto::new(SecretString::from(key.to_string())).unwrap());
|
||||
let store = InMemorySecretsStore::new(crypto);
|
||||
let store = test_secrets_store();
|
||||
|
||||
// Legacy token: no expires_at set
|
||||
store
|
||||
.create(
|
||||
"user1",
|
||||
CreateSecretParams::new("google_oauth_token", "ya29.legacy-token"),
|
||||
CreateSecretParams::new("google_oauth_token", TEST_GOOGLE_OAUTH_LEGACY),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -1646,8 +1626,8 @@ mod tests {
|
||||
|
||||
let oauth_config = OAuthRefreshConfig {
|
||||
token_url: "https://oauth2.googleapis.com/token".to_string(),
|
||||
client_id: "test-client-id".to_string(),
|
||||
client_secret: Some("test-client-secret".to_string()),
|
||||
client_id: TEST_OAUTH_CLIENT_ID.to_string(),
|
||||
client_secret: Some(TEST_OAUTH_CLIENT_SECRET.to_string()),
|
||||
secret_name: "google_oauth_token".to_string(),
|
||||
provider: Some("google".to_string()),
|
||||
};
|
||||
@@ -1658,7 +1638,7 @@ mod tests {
|
||||
assert_eq!(result.len(), 1);
|
||||
assert_eq!(
|
||||
result[0].headers.get("Authorization"),
|
||||
Some(&"Bearer ya29.legacy-token".to_string())
|
||||
Some(&format!("Bearer {TEST_GOOGLE_OAUTH_LEGACY}"))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user