|
| 1 | +//! Exercises the authenticated reminder transport against a local HTTP fixture. |
| 2 | +use super::disconnect::serve_reconnect_requests; |
| 3 | +use super::*; |
| 4 | +use crate::app_server_session::ThreadParamsMode; |
| 5 | +use crate::security_setup::Identity; |
| 6 | +use app_test_support::ChatGptAuthFixture; |
| 7 | +use app_test_support::write_chatgpt_auth; |
| 8 | +use codex_app_server_client::AppServerEvent; |
| 9 | +use codex_app_server_protocol::AccountUpdatedNotification; |
| 10 | +use codex_app_server_protocol::AuthMode; |
| 11 | +use codex_login::AuthCredentialsStoreMode; |
| 12 | +use pretty_assertions::assert_eq; |
| 13 | +use serde_json::json; |
| 14 | +use tokio::net::TcpListener; |
| 15 | + |
| 16 | +#[tokio::test] |
| 17 | +async fn security_setup_fetch_with_default_features_uses_authenticated_codex_endpoint() -> Result<()> |
| 18 | +{ |
| 19 | + let (mut app, mut events, _ops) = make_test_app_with_channels().await; |
| 20 | + let backend = wiremock::MockServer::start().await; |
| 21 | + app.config.chatgpt_base_url = backend.uri(); |
| 22 | + app.config.cli_auth_credentials_store_mode = AuthCredentialsStoreMode::File; |
| 23 | + std::fs::write( |
| 24 | + app.config.codex_home.join("config.toml"), |
| 25 | + format!("chatgpt_base_url = {:?}\n", backend.uri()), |
| 26 | + )?; |
| 27 | + write_chatgpt_auth( |
| 28 | + &app.config.codex_home, |
| 29 | + ChatGptAuthFixture::new("test-token") |
| 30 | + .account_id("account") |
| 31 | + .chatgpt_user_id("user"), |
| 32 | + AuthCredentialsStoreMode::File, |
| 33 | + ) |
| 34 | + .expect("write synthetic auth"); |
| 35 | + app_test_support::mount_workspace_routing(&backend).await; |
| 36 | + let mut server = Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?; |
| 37 | + wiremock::Mock::given(wiremock::matchers::path("/wham/security-setup")) |
| 38 | + .and(wiremock::matchers::header( |
| 39 | + "authorization", |
| 40 | + "Bearer test-token", |
| 41 | + )) |
| 42 | + .and(wiremock::matchers::header("ChatGPT-Account-ID", "account")) |
| 43 | + .and(wiremock::matchers::header( |
| 44 | + "User-Agent", |
| 45 | + codex_login::default_client::get_codex_user_agent(), |
| 46 | + )) |
| 47 | + .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(json!({ |
| 48 | + "notice": { |
| 49 | + "title": "Keep using Daybreak mode", "description": "Set up security.", |
| 50 | + "action": {"label": "Set up security", "url": "https://chatgpt.com/cyber"} |
| 51 | + } |
| 52 | + }))) |
| 53 | + .expect(3) |
| 54 | + .mount(&backend) |
| 55 | + .await; |
| 56 | + let request_id = app.chat_widget.security_setup_request_id; |
| 57 | + let mut tui = crate::tui::test_support::make_test_tui()?; |
| 58 | + let init = app.chatwidget_init_for_forked_or_resumed_thread( |
| 59 | + &mut tui, |
| 60 | + app.config.clone(), |
| 61 | + /*initial_user_message*/ None, |
| 62 | + ); |
| 63 | + app.replace_chat_widget(ChatWidget::new_with_app_event(init)); |
| 64 | + assert_eq!(app.chat_widget.security_setup_request_id, request_id); |
| 65 | + crate::security_setup::prefetch(&app.config, &server, app.app_event_tx.clone(), request_id); |
| 66 | + let notice = tokio::time::timeout(Duration::from_secs(5), async { |
| 67 | + loop { |
| 68 | + if let AppEvent::SecuritySetupLoaded { |
| 69 | + request_id: actual, |
| 70 | + identity, |
| 71 | + notice, |
| 72 | + } = events.recv().await.unwrap() |
| 73 | + { |
| 74 | + assert_eq!(actual, request_id); |
| 75 | + assert_eq!( |
| 76 | + identity, |
| 77 | + Identity { |
| 78 | + account: "account".into(), |
| 79 | + user: "user".into() |
| 80 | + } |
| 81 | + ); |
| 82 | + break notice; |
| 83 | + } |
| 84 | + } |
| 85 | + }) |
| 86 | + .await?; |
| 87 | + assert!(notice.valid()); |
| 88 | + let identity = Identity { |
| 89 | + account: "account".into(), |
| 90 | + user: "user".into(), |
| 91 | + }; |
| 92 | + app.handle_event( |
| 93 | + &mut tui, |
| 94 | + &mut server, |
| 95 | + AppEvent::SecuritySetupLoaded { |
| 96 | + request_id, |
| 97 | + identity: identity.clone(), |
| 98 | + notice: notice.clone(), |
| 99 | + }, |
| 100 | + ) |
| 101 | + .await?; |
| 102 | + assert!(render_bottom_popup(&app.chat_widget, /*width*/ 70).contains("Set up security")); |
| 103 | + app.chat_widget |
| 104 | + .handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); |
| 105 | + assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 70).contains("Set up security")); |
| 106 | + |
| 107 | + // Reconnect can emit AccountUpdated for the same identity more than once. |
| 108 | + for _ in 0..2 { |
| 109 | + let stale_request_id = app.chat_widget.security_setup_request_id; |
| 110 | + app.handle_app_server_event( |
| 111 | + &server, |
| 112 | + AppServerEvent::ServerNotification(Box::new(ServerNotification::AccountUpdated( |
| 113 | + AccountUpdatedNotification { |
| 114 | + auth_mode: Some(AuthMode::Chatgpt), |
| 115 | + plan_type: None, |
| 116 | + }, |
| 117 | + ))), |
| 118 | + ) |
| 119 | + .await; |
| 120 | + let request_id = app.chat_widget.security_setup_request_id; |
| 121 | + assert_ne!(request_id, stale_request_id); |
| 122 | + app.handle_event( |
| 123 | + &mut tui, |
| 124 | + &mut server, |
| 125 | + AppEvent::SecuritySetupLoaded { |
| 126 | + request_id: stale_request_id, |
| 127 | + identity: Identity { |
| 128 | + account: "stale-account".into(), |
| 129 | + ..identity.clone() |
| 130 | + }, |
| 131 | + notice: notice.clone(), |
| 132 | + }, |
| 133 | + ) |
| 134 | + .await?; |
| 135 | + assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 70).contains("Set up security")); |
| 136 | + |
| 137 | + let init = app.chatwidget_init_for_forked_or_resumed_thread( |
| 138 | + &mut tui, |
| 139 | + app.config.clone(), |
| 140 | + /*initial_user_message*/ None, |
| 141 | + ); |
| 142 | + app.replace_chat_widget(ChatWidget::new_with_app_event(init)); |
| 143 | + let event = tokio::time::timeout(Duration::from_secs(5), async { |
| 144 | + loop { |
| 145 | + let event = events.recv().await.unwrap(); |
| 146 | + if let AppEvent::SecuritySetupLoaded { |
| 147 | + request_id: actual, |
| 148 | + identity: actual_identity, |
| 149 | + .. |
| 150 | + } = &event |
| 151 | + { |
| 152 | + assert_eq!(*actual, request_id); |
| 153 | + assert_eq!(actual_identity, &identity); |
| 154 | + break event; |
| 155 | + } |
| 156 | + } |
| 157 | + }) |
| 158 | + .await?; |
| 159 | + app.handle_event(&mut tui, &mut server, event).await?; |
| 160 | + assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 70).contains("Set up security")); |
| 161 | + } |
| 162 | + backend.verify().await; |
| 163 | + server.shutdown().await?; |
| 164 | + Ok(()) |
| 165 | +} |
| 166 | + |
| 167 | +#[tokio::test] |
| 168 | +async fn security_setup_skips_fetch_when_server_auth_does_not_match_saved_login() -> Result<()> { |
| 169 | + for (auth_method, auth_token) in [ |
| 170 | + ( |
| 171 | + Some(AuthMode::ChatgptAuthTokens), |
| 172 | + Some("other-account-token"), |
| 173 | + ), |
| 174 | + (Some(AuthMode::ChatgptAuthTokens), Some("saved-token")), |
| 175 | + (Some(AuthMode::Chatgpt), Some("other-account-token")), |
| 176 | + (Some(AuthMode::Chatgpt), None), |
| 177 | + (None, None), |
| 178 | + ] { |
| 179 | + let (mut app, _events, _ops) = make_test_app_with_channels().await; |
| 180 | + let backend = wiremock::MockServer::start().await; |
| 181 | + app.config.chatgpt_base_url = backend.uri(); |
| 182 | + app.config.cli_auth_credentials_store_mode = AuthCredentialsStoreMode::File; |
| 183 | + write_chatgpt_auth( |
| 184 | + &app.config.codex_home, |
| 185 | + ChatGptAuthFixture::new("saved-token") |
| 186 | + .account_id("saved-account") |
| 187 | + .chatgpt_user_id("saved-user"), |
| 188 | + AuthCredentialsStoreMode::File, |
| 189 | + ) |
| 190 | + .expect("write synthetic auth"); |
| 191 | + wiremock::Mock::given(wiremock::matchers::path("/wham/security-setup")) |
| 192 | + .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(json!({ |
| 193 | + "notice": { |
| 194 | + "title": "Keep using Daybreak mode", "description": "Set up security.", |
| 195 | + "action": {"label": "Set up security", "url": "https://chatgpt.com/cyber"} |
| 196 | + } |
| 197 | + }))) |
| 198 | + .expect(0) |
| 199 | + .mount(&backend) |
| 200 | + .await; |
| 201 | + |
| 202 | + let listener = TcpListener::bind("127.0.0.1:0").await?; |
| 203 | + let endpoint = crate::resolve_remote_addr(&format!("ws://{}", listener.local_addr()?))?; |
| 204 | + let daemon = tokio::spawn(async move { |
| 205 | + let (stream, _) = listener.accept().await?; |
| 206 | + serve_reconnect_requests(tokio_tungstenite::accept_async(stream).await?, |request| { |
| 207 | + assert_eq!(request.method, "getAuthStatus"); |
| 208 | + assert_eq!( |
| 209 | + request.params, |
| 210 | + Some(json!({"includeToken": true, "refreshToken": false})) |
| 211 | + ); |
| 212 | + std::future::ready(Some(json!({"result": { |
| 213 | + "authMethod": auth_method, "authToken": auth_token, |
| 214 | + "requiresOpenaiAuth": true |
| 215 | + }}))) |
| 216 | + }) |
| 217 | + .await |
| 218 | + }); |
| 219 | + let server = AppServerSession::new( |
| 220 | + crate::connect_remote_app_server(endpoint).await?, |
| 221 | + ThreadParamsMode::Embedded, |
| 222 | + ); |
| 223 | + let (tx, mut events) = mpsc::unbounded_channel(); |
| 224 | + crate::security_setup::prefetch( |
| 225 | + &app.config, |
| 226 | + &server, |
| 227 | + AppEventSender::new(tx), |
| 228 | + app.chat_widget.security_setup_request_id, |
| 229 | + ); |
| 230 | + // The fetch owns the only sender, so channel closure proves it completed. |
| 231 | + assert!( |
| 232 | + tokio::time::timeout(Duration::from_secs(5), events.recv()) |
| 233 | + .await? |
| 234 | + .is_none() |
| 235 | + ); |
| 236 | + backend.verify().await; |
| 237 | + server.shutdown().await?; |
| 238 | + let methods = daemon.await??; |
| 239 | + assert!(methods.iter().any(|method| method == "getAuthStatus")); |
| 240 | + } |
| 241 | + Ok(()) |
| 242 | +} |
0 commit comments