2023-07-29 18:47:25 +00:00
|
|
|
use std::collections::HashMap;
|
2024-01-09 16:57:52 +00:00
|
|
|
use std::future::Future;
|
2023-07-29 18:47:25 +00:00
|
|
|
use std::sync::Arc;
|
2024-01-09 16:57:52 +00:00
|
|
|
use std::thread::Builder;
|
2023-07-29 18:47:25 +00:00
|
|
|
|
2023-08-30 18:01:30 +00:00
|
|
|
use surrealdb::dbs::capabilities::Capabilities;
|
2023-07-29 18:47:25 +00:00
|
|
|
use surrealdb::dbs::Session;
|
2023-08-30 18:01:30 +00:00
|
|
|
use surrealdb::err::Error;
|
2023-07-29 18:47:25 +00:00
|
|
|
use surrealdb::iam::{Auth, Level, Role};
|
|
|
|
use surrealdb::kvs::Datastore;
|
2024-04-08 23:04:44 +00:00
|
|
|
use surrealdb_core::dbs::Response;
|
2023-07-29 18:47:25 +00:00
|
|
|
|
2023-08-30 18:01:30 +00:00
|
|
|
pub async fn new_ds() -> Result<Datastore, Error> {
|
2024-02-27 15:18:25 +00:00
|
|
|
Ok(Datastore::new("memory").await?.with_capabilities(Capabilities::all()).with_notifications())
|
2023-08-30 18:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2023-07-29 18:47:25 +00:00
|
|
|
pub async fn iam_run_case(
|
|
|
|
prepare: &str,
|
|
|
|
test: &str,
|
|
|
|
check: &str,
|
2024-03-08 10:58:07 +00:00
|
|
|
check_expected_result: &[&str],
|
2023-07-29 18:47:25 +00:00
|
|
|
ds: &Datastore,
|
|
|
|
sess: &Session,
|
|
|
|
should_succeed: bool,
|
|
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
2024-05-22 13:57:25 +00:00
|
|
|
// Use the session as the test statement, but change the Auth to run the check with full permissions
|
2023-07-29 18:47:25 +00:00
|
|
|
let mut owner_sess = sess.clone();
|
|
|
|
owner_sess.au = Arc::new(Auth::for_root(Role::Owner));
|
|
|
|
|
|
|
|
// Prepare statement
|
|
|
|
{
|
|
|
|
if !prepare.is_empty() {
|
|
|
|
let resp = ds.execute(prepare, &owner_sess, None).await.unwrap();
|
|
|
|
for r in resp.into_iter() {
|
|
|
|
let tmp = r.output();
|
|
|
|
if tmp.is_err() {
|
|
|
|
return Err(format!("Prepare statement failed: {}", tmp.unwrap_err()).into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute statement
|
|
|
|
let mut resp = ds.execute(test, sess, None).await.unwrap();
|
|
|
|
|
|
|
|
// Check datastore state first
|
|
|
|
{
|
|
|
|
let resp = ds.execute(check, &owner_sess, None).await.unwrap();
|
|
|
|
if resp.len() != check_expected_result.len() {
|
|
|
|
return Err(format!(
|
|
|
|
"Check statement failed for test: expected {} results, got {}",
|
|
|
|
check_expected_result.len(),
|
|
|
|
resp.len()
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i, r) in resp.into_iter().enumerate() {
|
|
|
|
let tmp = r.output();
|
|
|
|
if tmp.is_err() {
|
|
|
|
return Err(
|
|
|
|
format!("Check statement errored for test: {}", tmp.unwrap_err()).into()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let tmp = tmp.unwrap().to_string();
|
2023-10-04 09:51:34 +00:00
|
|
|
if tmp != check_expected_result[i] {
|
2023-07-29 18:47:25 +00:00
|
|
|
return Err(format!(
|
|
|
|
"Check statement failed for test: expected value '{}' doesn't match '{}'",
|
|
|
|
check_expected_result[i], tmp
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check statement result. If the statement should succeed, check that the result is Ok, otherwise check that the result is a 'Not Allowed' error
|
|
|
|
let res = resp.pop().unwrap().output();
|
|
|
|
if should_succeed {
|
|
|
|
if res.is_err() {
|
|
|
|
return Err(format!("Test statement failed: {}", res.unwrap_err()).into());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if res.is_ok() {
|
|
|
|
return Err(
|
|
|
|
format!("Test statement succeeded when it should have failed: {:?}", res).into()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let err = res.unwrap_err().to_string();
|
|
|
|
if !err.contains("Not enough permissions to perform this action") {
|
|
|
|
return Err(format!("Test statement failed with unexpected error: {}", err).into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-04 09:51:34 +00:00
|
|
|
type CaseIter<'a> = std::slice::Iter<'a, ((Level, Role), (&'a str, &'a str), bool)>;
|
|
|
|
|
2023-08-30 18:01:30 +00:00
|
|
|
#[allow(dead_code)]
|
2023-07-29 18:47:25 +00:00
|
|
|
pub async fn iam_check_cases(
|
2023-10-04 09:51:34 +00:00
|
|
|
cases: CaseIter<'_>,
|
2023-07-29 18:47:25 +00:00
|
|
|
scenario: &HashMap<&str, &str>,
|
|
|
|
check_results: [Vec<&str>; 2],
|
|
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let prepare = scenario.get("prepare").unwrap();
|
|
|
|
let test = scenario.get("test").unwrap();
|
|
|
|
let check = scenario.get("check").unwrap();
|
|
|
|
|
|
|
|
for ((level, role), (ns, db), should_succeed) in cases {
|
|
|
|
println!("* Testing '{test}' for '{level}Actor({role})' on '({ns}, {db})'");
|
|
|
|
let sess = Session::for_level(level.to_owned(), role.to_owned()).with_ns(ns).with_db(db);
|
|
|
|
let expected_result = if *should_succeed {
|
2024-03-08 10:58:07 +00:00
|
|
|
check_results.first().unwrap()
|
2023-07-29 18:47:25 +00:00
|
|
|
} else {
|
|
|
|
check_results.get(1).unwrap()
|
|
|
|
};
|
|
|
|
// Auth enabled
|
|
|
|
{
|
2023-08-30 18:01:30 +00:00
|
|
|
let ds = new_ds().await.unwrap().with_auth_enabled(true);
|
2023-10-04 09:51:34 +00:00
|
|
|
iam_run_case(prepare, test, check, expected_result, &ds, &sess, *should_succeed)
|
2023-07-29 18:47:25 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth disabled
|
|
|
|
{
|
2023-08-30 18:01:30 +00:00
|
|
|
let ds = new_ds().await.unwrap().with_auth_enabled(false);
|
2023-10-04 09:51:34 +00:00
|
|
|
iam_run_case(prepare, test, check, expected_result, &ds, &sess, *should_succeed)
|
2023-07-29 18:47:25 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anonymous user
|
|
|
|
let ns = "NS";
|
|
|
|
let db = "DB";
|
|
|
|
for auth_enabled in [true, false].into_iter() {
|
|
|
|
{
|
|
|
|
println!(
|
|
|
|
"* Testing '{test}' for 'Anonymous' on '({ns}, {db})' with {auth_enabled}",
|
2023-10-04 09:51:34 +00:00
|
|
|
auth_enabled = if auth_enabled {
|
|
|
|
"auth enabled"
|
|
|
|
} else {
|
|
|
|
"auth disabled"
|
|
|
|
}
|
2023-07-29 18:47:25 +00:00
|
|
|
);
|
2023-08-30 18:01:30 +00:00
|
|
|
let ds = new_ds().await.unwrap().with_auth_enabled(auth_enabled);
|
2023-07-29 18:47:25 +00:00
|
|
|
let expected_result = if auth_enabled {
|
|
|
|
check_results.get(1).unwrap()
|
|
|
|
} else {
|
2024-03-08 10:58:07 +00:00
|
|
|
check_results.first().unwrap()
|
2023-07-29 18:47:25 +00:00
|
|
|
};
|
|
|
|
iam_run_case(
|
|
|
|
prepare,
|
|
|
|
test,
|
|
|
|
check,
|
2023-10-04 09:51:34 +00:00
|
|
|
expected_result,
|
2023-07-29 18:47:25 +00:00
|
|
|
&ds,
|
|
|
|
&Session::default().with_ns(ns).with_db(db),
|
|
|
|
!auth_enabled,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-01-09 16:57:52 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn with_enough_stack(
|
|
|
|
fut: impl Future<Output = Result<(), Error>> + Send + 'static,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut builder = Builder::new();
|
|
|
|
|
|
|
|
// Roughly how much stack is allocated for surreal server workers in release mode
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
{
|
|
|
|
builder = builder.stack_size(10_000_000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same for debug mode
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
builder = builder.stack_size(24_000_000);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder
|
|
|
|
.spawn(|| {
|
|
|
|
let runtime = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
|
|
|
runtime.block_on(fut)
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
.join()
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-04-08 23:04:44 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn skip_ok(res: &mut Vec<Response>, skip: usize) -> Result<(), Error> {
|
2024-05-24 18:09:10 +00:00
|
|
|
for i in 0..skip {
|
|
|
|
let r = res.remove(0).result;
|
|
|
|
let _ = r.is_err_and(|e| {
|
|
|
|
panic!("Statement #{i} fails with: {e}");
|
|
|
|
});
|
2024-04-08 23:04:44 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|