| 1 | use super::*; |
| 2 | |
| 3 | fn elevated_due( |
| 4 | manager: &AutomationManager, |
| 5 | id: &str, |
| 6 | allow_shell: Option<bool>, |
| 7 | trust_mode: Option<bool>, |
| 8 | auto_approve: Option<bool>, |
| 9 | ) -> AutomationRecord { |
| 10 | let mut automation = |
| 11 | automation_record_with_settings(None, allow_shell, trust_mode, auto_approve); |
| 12 | automation.id = id.to_owned(); |
| 13 | automation.created_at = Utc::now() - Duration::hours(2); |
| 14 | automation.updated_at = Utc::now() - Duration::minutes(5); |
| 15 | automation.next_run_at = Some(Utc::now() - Duration::minutes(1)); |
| 16 | manager.save_automation(&automation).expect("save fixture"); |
| 17 | automation |
| 18 | } |
| 19 | |
| 20 | #[tokio::test] |
| 21 | async fn saved_automation_with_elevated_settings_is_paused_once_and_never_dispatched() -> Result<()> |
| 22 | { |
| 23 | let root = tempfile::tempdir()?; |
| 24 | let manager = AutomationManager::open_for_test(root.path().join("automations"))?; |
| 25 | let variants = [ |
| 26 | ("auto", None, None, Some(true)), |
| 27 | ("trust", None, Some(true), None), |
| 28 | ("shell", Some(true), None, None), |
| 29 | ("all", Some(true), Some(true), Some(true)), |
| 30 | ]; |
| 31 | let mut saved = Vec::new(); |
| 32 | for (id, allow_shell, trust_mode, auto_approve) in variants { |
| 33 | saved.push(elevated_due( |
| 34 | &manager, |
| 35 | id, |
| 36 | allow_shell, |
| 37 | trust_mode, |
| 38 | auto_approve, |
| 39 | )); |
| 40 | } |
| 41 | let safe = fixture_due_automation(&manager, "safe", 1); |
| 42 | let shared = Arc::new(Mutex::new(manager)); |
| 43 | let dispatched = Arc::new(std::sync::Mutex::new(Vec::new())); |
| 44 | let tasks_root = root.path().join("tasks"); |
| 45 | fs::create_dir_all(&tasks_root)?; |
| 46 | |
| 47 | let tick = |shared: Arc<Mutex<AutomationManager>>, |
| 48 | dispatched: Arc<std::sync::Mutex<Vec<String>>>| { |
| 49 | let tasks_root = tasks_root.clone(); |
| 50 | async move { |
| 51 | scheduler_tick_with(&shared, &tasks_root, move |run| { |
| 52 | let dispatched = dispatched.clone(); |
| 53 | async move { |
| 54 | dispatched.lock().unwrap().push(run.automation_id.clone()); |
| 55 | run |
| 56 | } |
| 57 | }) |
| 58 | .await |
| 59 | } |
| 60 | }; |
| 61 | tick(shared.clone(), dispatched.clone()).await?; |
| 62 | assert_eq!(*dispatched.lock().unwrap(), vec![safe.id.clone()]); |
| 63 | |
| 64 | let after_first: Vec<_> = { |
| 65 | let manager = shared.lock().await; |
| 66 | saved |
| 67 | .iter() |
| 68 | .map(|before| { |
| 69 | let after = manager.get_automation(&before.id).expect("stored record"); |
| 70 | assert_eq!(after.status, AutomationStatus::Paused, "{}", before.id); |
| 71 | assert_eq!(after.next_run_at, None, "{}", before.id); |
| 72 | assert_eq!(after.allow_shell, before.allow_shell, "{}", before.id); |
| 73 | assert_eq!(after.trust_mode, before.trust_mode, "{}", before.id); |
| 74 | assert_eq!(after.auto_approve, before.auto_approve, "{}", before.id); |
| 75 | assert_eq!(after.revision, before.revision + 1, "{}", before.id); |
| 76 | assert!(manager.list_runs(&before.id, None).unwrap().is_empty()); |
| 77 | after |
| 78 | }) |
| 79 | .collect() |
| 80 | }; |
| 81 | |
| 82 | dispatched.lock().unwrap().clear(); |
| 83 | tick(shared.clone(), dispatched.clone()).await?; |
| 84 | assert!( |
| 85 | dispatched.lock().unwrap().iter().all(|id| id == &safe.id), |
| 86 | "a paused automation is not offered to the dispatcher again" |
| 87 | ); |
| 88 | let manager = shared.lock().await; |
| 89 | for before in &after_first { |
| 90 | assert_eq!( |
| 91 | manager.get_automation(&before.id)?, |
| 92 | *before, |
| 93 | "the second tick leaves a paused record untouched" |
| 94 | ); |
| 95 | } |
| 96 | Ok(()) |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn listing_shows_an_unswept_elevated_automation_as_paused_with_the_reason() -> Result<()> { |
| 101 | let root = tempfile::tempdir()?; |
| 102 | let manager = AutomationManager::open_for_test(root.path().join("automations"))?; |
| 103 | let elevated = elevated_due(&manager, "elevated", None, None, Some(true)); |
| 104 | let safe = fixture_due_automation(&manager, "safe", 1); |
| 105 | |
| 106 | let listed = manager.list_automation_views()?; |
| 107 | let view = listed |
| 108 | .iter() |
| 109 | .find(|view| view.record.id == elevated.id) |
| 110 | .expect("listed"); |
| 111 | assert_eq!(view.record.status, AutomationStatus::Paused); |
| 112 | assert!(!view.scheduled_here); |
| 113 | assert_eq!(view.paused_reason, Some(ELEVATED_AUTOMATION_PAUSE_REASON)); |
| 114 | assert_eq!(view.record.auto_approve, Some(true)); |
| 115 | let json = serde_json::to_value(view)?; |
| 116 | assert_eq!(json["status"], "paused"); |
| 117 | assert_eq!(json["paused_reason"], ELEVATED_AUTOMATION_PAUSE_REASON); |
| 118 | assert_eq!( |
| 119 | manager.get_automation(&elevated.id)?.status, |
| 120 | AutomationStatus::Active, |
| 121 | "reading never rewrites the stored record" |
| 122 | ); |
| 123 | let single = manager.get_automation_view(&elevated.id)?; |
| 124 | assert_eq!(single.paused_reason, Some(ELEVATED_AUTOMATION_PAUSE_REASON)); |
| 125 | |
| 126 | let safe_view = listed |
| 127 | .iter() |
| 128 | .find(|view| view.record.id == safe.id) |
| 129 | .expect("listed"); |
| 130 | assert_eq!(safe_view.record.status, AutomationStatus::Active); |
| 131 | assert_eq!(safe_view.paused_reason, None); |
| 132 | assert!( |
| 133 | serde_json::to_value(safe_view)? |
| 134 | .get("paused_reason") |
| 135 | .is_none() |
| 136 | ); |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn elevated_automation_resumes_only_after_its_elevated_settings_are_removed() -> Result<()> { |
| 142 | let root = tempfile::tempdir()?; |
| 143 | let manager = AutomationManager::open_for_test(root.path().join("automations"))?; |
| 144 | let mut elevated = elevated_due(&manager, "elevated", Some(true), Some(true), Some(true)); |
| 145 | elevated.status = AutomationStatus::Paused; |
| 146 | elevated.next_run_at = None; |
| 147 | manager.save_automation(&elevated)?; |
| 148 | |
| 149 | let refused = manager |
| 150 | .resume_automation(&elevated.id) |
| 151 | .expect_err("resume while elevated is refused"); |
| 152 | assert_eq!(refused.to_string(), ELEVATED_AUTOMATION_PAUSE_REASON); |
| 153 | let still = manager.get_automation(&elevated.id)?; |
| 154 | assert_eq!(still.status, AutomationStatus::Paused); |
| 155 | assert_eq!(still.auto_approve, Some(true)); |
| 156 | |
| 157 | let partly_removed = manager.update_automation( |
| 158 | &elevated.id, |
| 159 | UpdateAutomationRequest { |
| 160 | auto_approve: Some(false), |
| 161 | trust_mode: Some(false), |
| 162 | ..UpdateAutomationRequest::default() |
| 163 | }, |
| 164 | )?; |
| 165 | assert_eq!(partly_removed.status, AutomationStatus::Paused); |
| 166 | assert_eq!( |
| 167 | partly_removed.elevated_pause_reason(), |
| 168 | Some(ELEVATED_AUTOMATION_PAUSE_REASON) |
| 169 | ); |
| 170 | assert!(manager.resume_automation(&elevated.id).is_err()); |
| 171 | |
| 172 | let edited = manager.update_automation( |
| 173 | &elevated.id, |
| 174 | UpdateAutomationRequest { |
| 175 | allow_shell: Some(false), |
| 176 | ..UpdateAutomationRequest::default() |
| 177 | }, |
| 178 | )?; |
| 179 | assert_eq!(edited.status, AutomationStatus::Paused); |
| 180 | assert_eq!(edited.elevated_pause_reason(), None); |
| 181 | assert_eq!( |
| 182 | manager.get_automation_view(&elevated.id)?.paused_reason, |
| 183 | None |
| 184 | ); |
| 185 | |
| 186 | let resumed = manager.resume_automation(&elevated.id)?; |
| 187 | assert_eq!(resumed.status, AutomationStatus::Active); |
| 188 | assert!(resumed.next_run_at.is_some()); |
| 189 | Ok(()) |
| 190 | } |
| 191 | |
| 192 | #[test] |
| 193 | fn removing_elevated_settings_and_resuming_in_one_edit_is_accepted() -> Result<()> { |
| 194 | let root = tempfile::tempdir()?; |
| 195 | let manager = AutomationManager::open_for_test(root.path().join("automations"))?; |
| 196 | let mut elevated = elevated_due(&manager, "elevated", None, None, Some(true)); |
| 197 | elevated.status = AutomationStatus::Paused; |
| 198 | elevated.next_run_at = None; |
| 199 | manager.save_automation(&elevated)?; |
| 200 | |
| 201 | let resumed = manager.update_automation( |
| 202 | &elevated.id, |
| 203 | UpdateAutomationRequest { |
| 204 | auto_approve: Some(false), |
| 205 | status: Some(AutomationStatus::Active), |
| 206 | ..UpdateAutomationRequest::default() |
| 207 | }, |
| 208 | )?; |
| 209 | assert_eq!(resumed.status, AutomationStatus::Active); |
| 210 | assert!(resumed.next_run_at.is_some()); |
| 211 | Ok(()) |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn editing_an_elevated_automation_never_leaves_it_active() -> Result<()> { |
| 216 | let root = tempfile::tempdir()?; |
| 217 | let manager = AutomationManager::open_for_test(root.path().join("automations"))?; |
| 218 | let elevated = elevated_due(&manager, "elevated", Some(true), None, None); |
| 219 | |
| 220 | let edited = manager.update_automation( |
| 221 | &elevated.id, |
| 222 | UpdateAutomationRequest { |
| 223 | name: Some("Renamed".into()), |
| 224 | ..UpdateAutomationRequest::default() |
| 225 | }, |
| 226 | )?; |
| 227 | assert_eq!(edited.status, AutomationStatus::Paused); |
| 228 | assert_eq!(edited.next_run_at, None); |
| 229 | assert_eq!(edited.allow_shell, Some(true)); |
| 230 | Ok(()) |
| 231 | } |
| 232 | |
| 233 | #[tokio::test] |
| 234 | async fn queued_run_bound_with_elevated_settings_fails_instead_of_retrying() -> Result<()> { |
| 235 | let tempdir = tempfile::tempdir()?; |
| 236 | let task_manager = TaskManager::start_with_executor( |
| 237 | automation_task_config(tempdir.path().join("tasks")), |
| 238 | Arc::new(AutomationNoopExecutor), |
| 239 | ) |
| 240 | .await?; |
| 241 | let safe = automation_record_with_settings(None, None, None, None); |
| 242 | let mut run = queued_run_for(&safe); |
| 243 | bind_run_dispatch(&mut run, &safe, &task_manager.data_dir(), false)?; |
| 244 | let task_id = run.task_id.clone().expect("task id"); |
| 245 | run.dispatch |
| 246 | .as_mut() |
| 247 | .expect("dispatch") |
| 248 | .request |
| 249 | .auto_approve = Some(true); |
| 250 | |
| 251 | enqueue_run_task(&mut run, &task_manager).await; |
| 252 | |
| 253 | assert_eq!(run.status, AutomationRunStatus::Failed); |
| 254 | assert!(run.ended_at.is_some()); |
| 255 | assert!( |
| 256 | run.error |
| 257 | .as_deref() |
| 258 | .is_some_and(|error| error.contains("Full Access, trust or shell")), |
| 259 | "{:?}", |
| 260 | run.error |
| 261 | ); |
| 262 | assert!(task_manager.get_task(&task_id).await.is_err()); |
| 263 | task_manager.shutdown(); |
| 264 | Ok(()) |
| 265 | } |
| 266 | |
| 267 | #[tokio::test] |
| 268 | async fn run_admitted_with_elevated_settings_before_the_refusal_still_reports_its_task() |
| 269 | -> Result<()> { |
| 270 | let tempdir = tempfile::tempdir()?; |
| 271 | let task_manager = TaskManager::start_with_executor( |
| 272 | automation_task_config(tempdir.path().join("tasks")), |
| 273 | Arc::new(AutomationNoopExecutor), |
| 274 | ) |
| 275 | .await?; |
| 276 | let safe = automation_record_with_settings(None, None, None, None); |
| 277 | let mut run = queued_run_for(&safe); |
| 278 | bind_run_dispatch(&mut run, &safe, &task_manager.data_dir(), false)?; |
| 279 | let task_id = run.task_id.clone().expect("task id"); |
| 280 | { |
| 281 | let dispatch = run.dispatch.as_mut().expect("dispatch"); |
| 282 | dispatch.request.auto_approve = Some(true); |
| 283 | dispatch.request.permission_posture = Some("full_access".into()); |
| 284 | task_manager |
| 285 | .recover_task_admission(dispatch.request.clone(), task_id.clone()) |
| 286 | .await?; |
| 287 | dispatch.accepted = true; |
| 288 | } |
| 289 | |
| 290 | enqueue_run_task(&mut run, &task_manager).await; |
| 291 | |
| 292 | assert!(run.error.is_none(), "{:?}", run.error); |
| 293 | assert_eq!( |
| 294 | run.thread_id, |
| 295 | task_manager.get_task(&task_id).await?.thread_id |
| 296 | ); |
| 297 | task_manager.shutdown(); |
| 298 | Ok(()) |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn removing_settings_from_an_unswept_elevated_automation_keeps_it_paused() -> Result<()> { |
| 303 | let root = tempfile::tempdir()?; |
| 304 | let manager = AutomationManager::open_for_test(root.path().join("automations"))?; |
| 305 | let elevated = elevated_due(&manager, "elevated", Some(true), Some(true), Some(true)); |
| 306 | assert_eq!( |
| 307 | manager.get_automation(&elevated.id)?.status, |
| 308 | AutomationStatus::Active |
| 309 | ); |
| 310 | |
| 311 | let edited = manager.update_automation( |
| 312 | &elevated.id, |
| 313 | UpdateAutomationRequest { |
| 314 | allow_shell: Some(false), |
| 315 | trust_mode: Some(false), |
| 316 | auto_approve: Some(false), |
| 317 | ..UpdateAutomationRequest::default() |
| 318 | }, |
| 319 | )?; |
| 320 | assert_eq!(edited.status, AutomationStatus::Paused); |
| 321 | assert_eq!(edited.next_run_at, None); |
| 322 | assert_eq!(edited.elevated_pause_reason(), None); |
| 323 | |
| 324 | let resumed = manager.resume_automation(&elevated.id)?; |
| 325 | assert_eq!(resumed.status, AutomationStatus::Active); |
| 326 | Ok(()) |
| 327 | } |
| 328 |