|
| 1 | +//! Tests for the `cloud_auth` config surface + `apply_cloud_login` persistence. |
| 2 | +//! Uses save/load round-trips and a plaintext credential store, so nothing touches the real |
| 3 | +//! OS keyring. |
| 4 | +
|
| 5 | +use redisctl_core::{ |
| 6 | + CloudAuthConfig, Config, CredentialStore, DeploymentType, MintedCredentials, Profile, |
| 7 | + ProfileCredentials, |
| 8 | +}; |
| 9 | + |
| 10 | +fn cloud_profile(api_url: &str) -> Profile { |
| 11 | + Profile { |
| 12 | + deployment_type: DeploymentType::Cloud, |
| 13 | + credentials: ProfileCredentials::Cloud { |
| 14 | + api_key: "acct-key".to_string(), |
| 15 | + api_secret: "user-secret".to_string(), |
| 16 | + api_url: api_url.to_string(), |
| 17 | + }, |
| 18 | + files_api_key: None, |
| 19 | + tags: Vec::new(), |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +fn qa_cloud_auth() -> CloudAuthConfig { |
| 24 | + CloudAuthConfig { |
| 25 | + okta_issuer: "https://okta.example.com/oauth2/default".to_string(), |
| 26 | + okta_client_id: "test-client-id".to_string(), |
| 27 | + sm_api_url: "https://sm.example.com/api/v1".to_string(), |
| 28 | + capi_url: "https://api.example.com/v1".to_string(), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +fn roundtrip(config: &Config) -> (String, Config) { |
| 33 | + let dir = tempfile::tempdir().unwrap(); |
| 34 | + let path = dir.path().join("config.toml"); |
| 35 | + config.save_to_path(&path).unwrap(); |
| 36 | + let toml = std::fs::read_to_string(&path).unwrap(); |
| 37 | + let loaded = Config::load_from_path(&path).unwrap(); |
| 38 | + (toml, loaded) |
| 39 | +} |
| 40 | + |
| 41 | +#[test] |
| 42 | +fn config_without_cloud_auth_roundtrips_and_omits_the_key() { |
| 43 | + let mut config = Config::default(); |
| 44 | + config.set_profile( |
| 45 | + "prod".to_string(), |
| 46 | + cloud_profile("https://api.redislabs.com/v1"), |
| 47 | + ); |
| 48 | + |
| 49 | + let (toml, loaded) = roundtrip(&config); |
| 50 | + // Backward compatible: no cloud_auth section is written when none is set. |
| 51 | + assert!( |
| 52 | + !toml.contains("cloud_auth"), |
| 53 | + "unexpected cloud_auth in:\n{toml}" |
| 54 | + ); |
| 55 | + assert!(loaded.profiles.contains_key("prod")); |
| 56 | + assert_eq!( |
| 57 | + loaded.profiles["prod"].cloud_credentials().map(|c| c.2), |
| 58 | + Some("https://api.redislabs.com/v1") |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn config_with_cloud_auth_roundtrips() { |
| 64 | + let mut config = Config::default(); |
| 65 | + config.set_profile( |
| 66 | + "qa".to_string(), |
| 67 | + cloud_profile("https://api.example.com/v1"), |
| 68 | + ); |
| 69 | + config.cloud_auth.insert("qa".to_string(), qa_cloud_auth()); |
| 70 | + |
| 71 | + let (toml, loaded) = roundtrip(&config); |
| 72 | + assert!( |
| 73 | + toml.contains("[cloud_auth.qa]"), |
| 74 | + "expected cloud_auth table in:\n{toml}" |
| 75 | + ); |
| 76 | + assert_eq!(loaded.resolve_cloud_auth("qa"), qa_cloud_auth()); |
| 77 | +} |
| 78 | + |
| 79 | +#[test] |
| 80 | +fn resolve_cloud_auth_falls_back_to_prod_defaults() { |
| 81 | + let config = Config::default(); |
| 82 | + let resolved = config.resolve_cloud_auth("anything"); |
| 83 | + assert_eq!(resolved, CloudAuthConfig::prod_defaults()); |
| 84 | + // Prod endpoints aren't provisioned yet, so it isn't complete, but the CAPI base is known. |
| 85 | + assert!(!resolved.is_complete()); |
| 86 | + assert_eq!(resolved.capi_url, "https://api.redislabs.com/v1"); |
| 87 | +} |
| 88 | + |
| 89 | +#[test] |
| 90 | +fn apply_cloud_login_writes_profile_default_and_endpoints() { |
| 91 | + let mut config = Config::default(); |
| 92 | + let store = CredentialStore::plaintext(); // never touches the keyring |
| 93 | + let creds = MintedCredentials { |
| 94 | + account_id: Some("112117".to_string()), |
| 95 | + email: Some("u@e.com".to_string()), |
| 96 | + api_key: "ACCT-KEY".to_string(), |
| 97 | + api_secret: "USER-SECRET".to_string(), |
| 98 | + api_url: "https://api.example.com/v1".to_string(), |
| 99 | + refresh_token: Some("RT".to_string()), |
| 100 | + capi_key_name: "redisctl-demo".to_string(), |
| 101 | + redisctl_key_count: 1, |
| 102 | + }; |
| 103 | + |
| 104 | + config |
| 105 | + .apply_cloud_login(&store, "qa", &creds, Some(qa_cloud_auth())) |
| 106 | + .unwrap(); |
| 107 | + |
| 108 | + // Default cloud profile is set. |
| 109 | + assert_eq!(config.default_cloud.as_deref(), Some("qa")); |
| 110 | + // Profile is a Cloud profile; plaintext store records the values as-is. |
| 111 | + let (key, secret, url) = config.profiles["qa"].cloud_credentials().unwrap(); |
| 112 | + assert_eq!(key, "ACCT-KEY"); |
| 113 | + assert_eq!(secret, "USER-SECRET"); |
| 114 | + assert_eq!(url, "https://api.example.com/v1"); |
| 115 | + // Login endpoints were recorded for re-login. |
| 116 | + assert_eq!(config.resolve_cloud_auth("qa"), qa_cloud_auth()); |
| 117 | + |
| 118 | + // And it survives a save/load round-trip. |
| 119 | + let (_, loaded) = roundtrip(&config); |
| 120 | + assert_eq!(loaded.default_cloud.as_deref(), Some("qa")); |
| 121 | + assert_eq!(loaded.resolve_cloud_auth("qa"), qa_cloud_auth()); |
| 122 | +} |
| 123 | + |
| 124 | +/// Mirrors what `cloud auth logout` does at the config layer: it removes the profile (and its |
| 125 | +/// credentials) but must PRESERVE the `[cloud_auth.<profile>]` login endpoints, so a later |
| 126 | +/// `auth login` still resolves them instead of failing with "not configured". |
| 127 | +#[test] |
| 128 | +fn logout_removes_profile_but_preserves_cloud_auth_endpoints() { |
| 129 | + let mut config = Config::default(); |
| 130 | + config.set_profile("qa".to_string(), cloud_profile("https://api-qa.example/v1")); |
| 131 | + config.cloud_auth.insert("qa".to_string(), qa_cloud_auth()); |
| 132 | + |
| 133 | + // The logout sequence: save endpoints, remove the profile, restore endpoints. |
| 134 | + let saved = config.cloud_auth.get("qa").cloned(); |
| 135 | + config.remove_profile("qa"); |
| 136 | + if let Some(auth) = saved { |
| 137 | + config.cloud_auth.insert("qa".to_string(), auth); |
| 138 | + } |
| 139 | + |
| 140 | + let (_, loaded) = roundtrip(&config); |
| 141 | + // Profile (credentials) is gone… |
| 142 | + assert!(!loaded.list_profiles().iter().any(|(n, _)| *n == "qa")); |
| 143 | + // …but the login endpoints survive so re-login works. |
| 144 | + let auth = loaded.resolve_cloud_auth("qa"); |
| 145 | + assert!(auth.is_complete()); |
| 146 | + assert_eq!(auth.okta_client_id, "test-client-id"); |
| 147 | +} |
0 commit comments