Files
windmill/backend/tests/list_paths_scope.rs
T

79 lines
2.5 KiB
Rust

//! `GET /flows/list_paths` and `GET /scripts/list_paths` must honor a path-scoped token:
//! RLS knows nothing of token scopes, so the handlers filter by them.
use sqlx::{Pool, Postgres};
use windmill_test_utils::*;
async fn list_paths(port: u16, kind: &str, token: &str) -> anyhow::Result<Vec<String>> {
let resp = reqwest::Client::new()
.get(format!(
"http://localhost:{port}/api/w/test-workspace/{kind}/list_paths"
))
.header("Authorization", format!("Bearer {token}"))
.send()
.await?;
assert_eq!(
resp.status(),
200,
"{kind}/list_paths: {}",
resp.text().await?
);
let mut paths: Vec<String> = resp.json().await?;
paths.sort();
Ok(paths)
}
#[sqlx::test(fixtures("base"))]
async fn test_list_paths_filters_by_token_scope(db: Pool<Postgres>) -> anyhow::Result<()> {
initialize_tracing().await;
for (hash, path) in [(9100001_i64, "f/allowed/s"), (9100002, "f/other/s")] {
sqlx::query(
"INSERT INTO script (workspace_id, created_by, content, schema, summary, description, path, hash, language, lock)
VALUES ('test-workspace', 'test-user', 'echo', '{}', '', '', $1, $2, 'bash', '')",
)
.bind(path)
.bind(hash)
.execute(&db)
.await?;
}
for path in ["f/allowed/f", "f/other/f"] {
sqlx::query(
"INSERT INTO flow (workspace_id, summary, description, path, versions, schema, value, edited_by)
VALUES ('test-workspace', '', '', $1, '{}', '{}', '{\"modules\": []}', 'test-user')",
)
.bind(path)
.execute(&db)
.await?;
}
sqlx::query(
"INSERT INTO token (token_hash, token_prefix, token, email, label, scopes)
VALUES (encode(sha256('SCOPED_TOKEN'::bytea), 'hex'), 'SCOPED_TOK', 'SCOPED_TOKEN',
'test@windmill.dev', 'scoped',
ARRAY['flows:read:f/allowed/*', 'scripts:read:f/allowed/*'])",
)
.execute(&db)
.await?;
let server = ApiServer::start(db.clone()).await?;
let port = server.addr.port();
assert_eq!(
list_paths(port, "flows", "SECRET_TOKEN").await?,
["f/allowed/f", "f/other/f"]
);
assert_eq!(
list_paths(port, "scripts", "SECRET_TOKEN").await?,
["f/allowed/s", "f/other/s"]
);
assert_eq!(
list_paths(port, "flows", "SCOPED_TOKEN").await?,
["f/allowed/f"]
);
assert_eq!(
list_paths(port, "scripts", "SCOPED_TOKEN").await?,
["f/allowed/s"]
);
Ok(())
}