返回 CodeWhale
oauth_tests.rs
根目录 / crates / tui / src / config_stay_tests / oauth_tests.rs
1 use codewhale_runtime::config::Config;
2
3 use codewhale_config::ProviderKind;
4
5 use codewhale_runtime::oauth::*;
6
7 use std::fs;
8
9 // ────────────────────────────────────────────────────────────────────
10 // Ported from the deleted per-provider modules (§D security pins).
11 // ────────────────────────────────────────────────────────────────────
12
13 use tempfile::TempDir;
14
15 /// #4763 root trigger, re-pinned for #5772. A returning xAI-OAuth user
16 /// whose only material is an external Grok CLI grant loses readiness the
17 /// moment that CLI's short-lived access token expires, even though a
18 /// refresh token sits right beside it — read-only consent deliberately
19 /// never refreshes or rewrites another CLI's file, so there is nothing to
20 /// renew it with. `needs_api_key` therefore flips to true and onboarding
21 /// reopens. That is the intended invariant, not a leak, and it is what
22 /// stops a surviving consent record from reading as a stored credential;
23 /// this test pins it so the onboarding entry point stays explainable.
24 #[test]
25 fn expired_external_grok_grant_reads_as_missing_key_despite_refresh_token() {
26 let _guard = crate::test_support::lock_test_env();
27 let dir = TempDir::new().unwrap();
28 let root = dir.path().canonicalize().expect("canonical temp root");
29 let path = root.join("external-grok-auth.json");
30 let scope = format!("https://auth.x.ai::{GROK_OIDC_CLIENT_ID}");
31 fs::write(
32 &path,
33 serde_json::json!({
34 scope.clone(): {
35 "key": "expired-external-access",
36 "refresh_token": "present-but-unusable-under-read-only-consent",
37 "expires_at": (chrono::Utc::now() - chrono::Duration::hours(1)).to_rfc3339(),
38 "oidc_client_id": GROK_OIDC_CLIENT_ID,
39 }
40 })
41 .to_string(),
42 )
43 .unwrap();
44 let _home_guard =
45 crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", root.join("codewhale-owned"));
46 let _path_guard = crate::test_support::EnvVarGuard::set("GROK_AUTH_PATH", &path);
47 let _key_guard = crate::test_support::EnvVarGuard::remove("XAI_API_KEY");
48 let config = {
49 let __cw_config_init_0_0 = Some(ProviderKind::Xai.as_str().to_string());
50 let __cw_config_init_0_1 = Some(codewhale_runtime::config::ProvidersConfig {
51 xai: codewhale_runtime::config::ProviderConfig {
52 auth_mode: Some("oauth".to_string()),
53 external_credentials: Some(
54 codewhale_config::ExternalCredentialConsentToml::read_only(
55 codewhale_config::ProviderKind::Xai,
56 codewhale_config::ExternalCredentialSource::GrokCli,
57 path.clone(),
58 ),
59 ),
60 ..Default::default()
61 },
62 ..Default::default()
63 });
64 let mut __cw_config_init_0_value: Config = Default::default();
65 __cw_config_init_0_value.provider = __cw_config_init_0_0;
66 __cw_config_init_0_value.providers = __cw_config_init_0_1;
67 __cw_config_init_0_value
68 };
69
70 codewhale_runtime::external_credentials::reset_side_effect_trap();
71 assert!(
72 !credentials_present(OAuthProvider::Xai, &config),
73 "an expired external access token is not usable material"
74 );
75 assert!(
76 !codewhale_engine::provider_readiness::has_api_key_for(
77 &config,
78 &(config).test_identity_for_kind(ProviderKind::Xai)
79 ),
80 "expired external xAI OAuth must fall through to the missing-key path"
81 );
82 assert_eq!(
83 codewhale_runtime::external_credentials::complete_side_effect_trap_counts().2,
84 0,
85 "a consented read never rewrites Codewhale-owned storage"
86 );
87 assert_eq!(
88 codewhale_runtime::external_credentials::complete_side_effect_trap_counts().3,
89 0,
90 "a consented read never refreshes another CLI's token"
91 );
92
93 // The same file with a live access token is ready, so the check is
94 // expiry-driven rather than a blanket rejection of external grants.
95 fs::write(
96 &path,
97 serde_json::json!({
98 scope: {
99 "key": "fresh-external-access",
100 "refresh_token": "unused",
101 "expires_at": (chrono::Utc::now() + chrono::Duration::hours(1)).to_rfc3339(),
102 "oidc_client_id": GROK_OIDC_CLIENT_ID,
103 }
104 })
105 .to_string(),
106 )
107 .unwrap();
108 assert!(credentials_present(OAuthProvider::Xai, &config));
109 assert!(codewhale_engine::provider_readiness::has_api_key_for(
110 &config,
111 &(config).test_identity_for_kind(ProviderKind::Xai)
112 ));
113 }
114
114 lines RUST