diff --git a/crates/server/src/approvals.rs b/crates/server/src/approvals.rs index 4ff51c1..d450d2e 100644 --- a/crates/server/src/approvals.rs +++ b/crates/server/src/approvals.rs @@ -224,3 +224,6 @@ async fn list(s: &crate::State, p: &Principal, q: Filters) -> Result = history["items"] + .as_array() + .unwrap() + .iter() + .map(|item| item["approval_state"].as_str().unwrap()) + .collect(); + assert_eq!(states.len(), 2); + assert!(states.contains(&"expired") && states.contains(&"rejected")); + let count = list( + &state, + &p, + Filters { + count_only: true, + ..Default::default() + }, + ) + .await + .unwrap(); + assert_eq!(count["pending_count"], 2); + assert_eq!(count["items"], json!([])); + assert_eq!( + list( + &state, + &p, + Filters { + tenant_id: Some(c), + ..Default::default() + } + ) + .await + .unwrap_err() + .0, + StatusCode::FORBIDDEN + ); + assert_eq!( + list( + &state, + &p, + Filters { + tenant_id: Some(Uuid::new_v4()), + ..Default::default() + } + ) + .await + .unwrap_err() + .0, + StatusCode::NOT_FOUND + ); + sqlx::query("UPDATE memberships SET permissions=ARRAY['operations:read'] WHERE principal=$1 AND tenant_id=$2") + .bind(&user).bind(b).execute(&admin).await.unwrap(); + assert_eq!( + list( + &state, + &p, + Filters { + cursor: Some(cursor), + ..Default::default() + } + ) + .await + .unwrap_err() + .0, + StatusCode::BAD_REQUEST + ); + p.tenants = vec![c]; + p.tenant = c; + let none = list(&state, &p, Filters::default()).await.unwrap(); + assert_eq!(none["pending_count"], 0); + assert_eq!(none["items"], json!([])); + assert_eq!(none["can_review"], false); + p.session_hash = None; + assert_eq!( + list(&state, &p, Filters::default()).await.unwrap_err().0, + StatusCode::FORBIDDEN + ); + std::fs::remove_dir_all(dir).unwrap(); +} diff --git a/crates/server/src/jobs.rs b/crates/server/src/jobs.rs index 14e3499..b0fb79c 100644 --- a/crates/server/src/jobs.rs +++ b/crates/server/src/jobs.rs @@ -1339,31 +1339,61 @@ async fn cancel_http( let p = principal(&s, &h, true).await?; Ok(Json(cancel(&s, &p, t, id, i).await?)) } +#[derive(Deserialize)] +#[serde(rename_all = "snake_case")] +enum OrderSort { + Recent, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct OrderListQuery { + sort: Option, + cursor: Option, + limit: Option, +} + async fn operations_http( State(s): State, h: HeaderMap, Path(t): Path, - axum::extract::Query(query): axum::extract::Query, + axum::extract::Query(query): axum::extract::Query, ) -> Result> { let p = principal(&s, &h, false).await?; scope(&p, t, false)?; let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?; crate::permissions::require_principal(&mut tx, &p, "operations:read").await?; - let mut page = crate::pagination::Cursor::parse(&s, &p, "operations:operation_id:asc", query) - .map_err(|status| Error(status, "VALIDATION_ERROR"))?; - let ids:Vec=sqlx::query_scalar("SELECT operation_id FROM approval_requests WHERE operation_id>$1 AND created_at<=$2 ORDER BY operation_id LIMIT $3").bind(page.last).bind(page.as_of).bind(i64::from(page.limit)+1).fetch_all(&mut *tx).await?; + let recent = matches!(query.sort, Some(OrderSort::Recent)); + let ordering = if recent { "operations:created_at:desc" } else { "operations:operation_id:asc" }; + let mut page = crate::pagination::Cursor::parse(&s, &p, ordering, crate::pagination::PageQuery { + cursor: query.cursor, limit: query.limit, + }).map_err(|status| Error(status, "VALIDATION_ERROR"))?; + let limit = i64::from(page.limit) + 1; + let ids: Vec<(Uuid, DateTime)> = if recent { + sqlx::query_as("SELECT operation_id,created_at FROM approval_requests WHERE created_at<=$1 + AND ($2::timestamptz IS NULL OR (created_at,operation_id)<($2,$3)) + ORDER BY created_at DESC,operation_id DESC LIMIT $4") + .bind(page.as_of).bind(page.last_created_at).bind(page.last).bind(limit) + .fetch_all(&mut *tx).await? + } else { + sqlx::query_as("SELECT operation_id,created_at FROM approval_requests WHERE operation_id>$1 AND created_at<=$2 + ORDER BY operation_id LIMIT $3") + .bind(page.last).bind(page.as_of).bind(limit).fetch_all(&mut *tx).await? + }; tx.commit().await?; let truncated = ids.len() > usize::from(page.limit); let next = if truncated { + let (id, date) = ids[usize::from(page.limit) - 1]; + page.last_created_at = recent.then_some(date); Some( - page.next(&s, ids[usize::from(page.limit) - 1]) + page.next(&s, id) .map_err(|status| Error(status, "VALIDATION_ERROR"))?, ) } else { None }; let mut items = Vec::new(); - for id in ids.into_iter().take(usize::from(page.limit)) { + for (id, _) in ids.into_iter().take(usize::from(page.limit)) { items.push(operation(&s, &p, t, id).await?) } Ok(Json( diff --git a/tests/local_access_database.py b/tests/local_access_database.py index 97348c2..52a9952 100644 --- a/tests/local_access_database.py +++ b/tests/local_access_database.py @@ -52,7 +52,7 @@ def main(): "--entrypoint", "cargo", "rust-discovery-windows-tls-full:local", "test", "--locked", "--offline", "-p", "rmm-server", ] - for test_filter in ("local_auth::tests", "mcp_tokens::tests", "devices::tests"): + for test_filter in ("local_auth::tests", "mcp_tokens::tests", "devices::tests", "approvals::tests"): output = command([*test_command, test_filter, "--", "--include-ignored", "--nocapture"], timeout=300) print(output.decode()) print("PASS all migrations under NOSUPERUSER/NOBYPASSRLS owner; local MFA and MCP tests in isolated PostgreSQL")