2023-05-10 02:08:09 +00:00
|
|
|
mod parse;
|
2023-08-30 18:01:30 +00:00
|
|
|
use parse::Parse;
|
2023-05-10 02:08:09 +00:00
|
|
|
|
2023-07-29 18:47:25 +00:00
|
|
|
mod helpers;
|
|
|
|
use helpers::*;
|
|
|
|
|
2023-07-18 18:32:38 +00:00
|
|
|
#[macro_use]
|
|
|
|
mod util;
|
|
|
|
|
2023-07-29 18:47:25 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2023-05-10 02:08:09 +00:00
|
|
|
use surrealdb::dbs::Session;
|
|
|
|
use surrealdb::err::Error;
|
2023-07-29 18:47:25 +00:00
|
|
|
use surrealdb::iam::Role;
|
2023-09-26 13:02:53 +00:00
|
|
|
use surrealdb::kvs::{LockType::*, TransactionType::*};
|
2023-05-10 02:08:09 +00:00
|
|
|
use surrealdb::sql::Value;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn remove_statement_table() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE TABLE test SCHEMALESS;
|
|
|
|
REMOVE TABLE test;
|
|
|
|
INFO FOR DB;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-05-10 02:08:09 +00:00
|
|
|
assert_eq!(res.len(), 3);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2024-05-22 13:57:25 +00:00
|
|
|
accesses: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
analyzers: {},
|
|
|
|
functions: {},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
params: {},
|
2023-07-29 18:47:25 +00:00
|
|
|
tables: {},
|
|
|
|
users: {}
|
2023-05-10 02:08:09 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn remove_statement_analyzer() -> Result<(), Error> {
|
|
|
|
let sql = "
|
2023-06-19 18:41:13 +00:00
|
|
|
DEFINE ANALYZER english TOKENIZERS blank,class FILTERS lowercase,snowball(english);
|
2023-05-10 02:08:09 +00:00
|
|
|
REMOVE ANALYZER english;
|
|
|
|
INFO FOR DB;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-05-10 02:08:09 +00:00
|
|
|
assert_eq!(res.len(), 3);
|
|
|
|
// Analyzer is defined
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
// Analyzer is removed
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
// Check infos output
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
2024-05-22 13:57:25 +00:00
|
|
|
accesses: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
analyzers: {},
|
|
|
|
functions: {},
|
2023-12-12 13:51:43 +00:00
|
|
|
models: {},
|
2023-05-31 12:35:29 +00:00
|
|
|
params: {},
|
2023-07-29 18:47:25 +00:00
|
|
|
tables: {},
|
|
|
|
users: {}
|
2023-05-10 02:08:09 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-10 08:24:47 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn remove_statement_index() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
DEFINE INDEX uniq_isbn ON book FIELDS isbn UNIQUE;
|
|
|
|
DEFINE INDEX idx_author ON book FIELDS author;
|
|
|
|
DEFINE ANALYZER simple TOKENIZERS blank,class FILTERS lowercase;
|
|
|
|
DEFINE INDEX ft_title ON book FIELDS title SEARCH ANALYZER simple BM25 HIGHLIGHTS;
|
|
|
|
CREATE book:1 SET title = 'Rust Web Programming', isbn = '978-1803234694', author = 'Maxwell Flitton';
|
|
|
|
REMOVE INDEX uniq_isbn ON book;
|
|
|
|
REMOVE INDEX idx_author ON book;
|
|
|
|
REMOVE INDEX ft_title ON book;
|
|
|
|
INFO FOR TABLE book;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-10 08:24:47 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 9);
|
|
|
|
for _ in 0..8 {
|
|
|
|
let tmp = res.remove(0).result;
|
|
|
|
assert!(tmp.is_ok());
|
|
|
|
}
|
|
|
|
// Check infos output
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"{
|
|
|
|
events: {},
|
|
|
|
fields: {},
|
|
|
|
indexes: {},
|
|
|
|
tables: {},
|
2023-09-11 19:35:32 +00:00
|
|
|
lives: {},
|
2023-07-10 08:24:47 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
|
2023-09-26 13:02:53 +00:00
|
|
|
let mut tx = dbs.transaction(Read, Optimistic).await?;
|
2023-07-10 08:24:47 +00:00
|
|
|
for ix in ["uniq_isbn", "idx_author", "ft_title"] {
|
2023-07-18 18:32:38 +00:00
|
|
|
assert_empty_prefix!(&mut tx, surrealdb::key::index::all::new("test", "test", "book", ix));
|
2023-07-10 08:24:47 +00:00
|
|
|
}
|
2023-12-13 13:37:24 +00:00
|
|
|
|
|
|
|
// Every index store cache has been removed
|
2024-05-08 14:26:41 +00:00
|
|
|
assert!(dbs.index_store().is_empty().await);
|
2023-07-10 08:24:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-29 18:47:25 +00:00
|
|
|
|
2024-01-19 14:02:07 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_table_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE TABLE foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::TbNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-03-05 19:28:38 +00:00
|
|
|
async fn should_not_error_when_remove_table_if_exists() -> Result<(), Error> {
|
2024-01-19 14:02:07 +00:00
|
|
|
let sql = "
|
2024-03-05 19:28:38 +00:00
|
|
|
REMOVE TABLE IF EXISTS foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_analyzer_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE ANALYZER foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::AzNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_not_error_when_remove_analyzer_if_exists() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE ANALYZER IF EXISTS foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_database_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE DATABASE foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::DbNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_not_error_when_remove_database_if_exists() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE DATABASE IF EXISTS foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_event_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE EVENT foo ON bar;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::EvNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_not_error_when_remove_event_if_exists() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE EVENT IF EXISTS foo ON bar;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_field_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE FIELD foo ON bar;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::FdNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_not_error_when_remove_field_if_exists() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE FIELD IF EXISTS foo ON bar;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_function_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE FUNCTION fn::foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::FcNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_not_error_when_remove_function_if_exists() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE FUNCTION IF EXISTS fn::foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_index_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE INDEX foo ON bar;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::IxNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_not_error_when_remove_index_if_exists() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE INDEX IF EXISTS foo ON bar;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_namespace_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE NAMESPACE foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::NsNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_not_error_when_remove_namespace_if_exists() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE NAMESPACE IF EXISTS foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_param_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE PARAM $foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::PaNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_not_error_when_remove_param_if_exists() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE PARAM IF EXISTS $foo;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-05-22 13:57:25 +00:00
|
|
|
async fn should_error_when_remove_and_access_does_not_exist() -> Result<(), Error> {
|
2024-03-05 19:28:38 +00:00
|
|
|
let sql = "
|
2024-05-22 13:57:25 +00:00
|
|
|
REMOVE ACCESS foo ON DB;
|
2024-03-05 19:28:38 +00:00
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
2024-05-22 13:57:25 +00:00
|
|
|
assert!(matches!(tmp, Error::DaNotFound { .. }),);
|
2024-03-05 19:28:38 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-05-22 13:57:25 +00:00
|
|
|
async fn should_not_error_when_remove_access_if_exists() -> Result<(), Error> {
|
2024-03-05 19:28:38 +00:00
|
|
|
let sql = "
|
2024-05-22 13:57:25 +00:00
|
|
|
REMOVE ACCESS IF EXISTS foo ON DB;
|
2024-03-05 19:28:38 +00:00
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_error_when_remove_and_user_does_not_exist() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE USER foo ON ROOT;
|
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result.unwrap_err();
|
|
|
|
assert!(matches!(tmp, Error::UserRootNotFound { .. }),);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_not_error_when_remove_user_if_exists() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
REMOVE USER IF EXISTS foo ON ROOT;
|
2024-01-19 14:02:07 +00:00
|
|
|
";
|
|
|
|
let dbs = new_ds().await?;
|
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
|
|
|
assert_eq!(res.len(), 1);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
assert_eq!(tmp, Value::None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-29 18:47:25 +00:00
|
|
|
//
|
|
|
|
// Permissions
|
|
|
|
//
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_ns() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE NAMESPACE NS"),
|
|
|
|
("test", "REMOVE NAMESPACE NS"),
|
|
|
|
("check", "INFO FOR ROOT"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
|
|
|
vec!["{ namespaces: { }, users: { } }"],
|
|
|
|
vec!["{ namespaces: { NS: 'DEFINE NAMESPACE NS' }, users: { } }"],
|
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_db() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE DATABASE DB"),
|
|
|
|
("test", "REMOVE DATABASE DB"),
|
|
|
|
("check", "INFO FOR NS"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-05-22 13:57:25 +00:00
|
|
|
vec!["{ accesses: { }, databases: { }, users: { } }"],
|
|
|
|
vec!["{ accesses: { }, databases: { DB: 'DEFINE DATABASE DB' }, users: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_function() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE FUNCTION fn::greet() {RETURN \"Hello\";}"),
|
|
|
|
("test", "REMOVE FUNCTION fn::greet()"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-05-22 13:57:25 +00:00
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { }, models: { }, params: { }, tables: { }, users: { } }"],
|
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { greet: \"DEFINE FUNCTION fn::greet() { RETURN 'Hello'; } PERMISSIONS FULL\" }, models: { }, params: { }, tables: { }, users: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_analyzer() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE ANALYZER analyzer TOKENIZERS BLANK"),
|
|
|
|
("test", "REMOVE ANALYZER analyzer"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-05-22 13:57:25 +00:00
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { }, models: { }, params: { }, tables: { }, users: { } }"],
|
|
|
|
vec!["{ accesses: { }, analyzers: { analyzer: 'DEFINE ANALYZER analyzer TOKENIZERS BLANK' }, functions: { }, models: { }, params: { }, tables: { }, users: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-05-22 13:57:25 +00:00
|
|
|
async fn permissions_checks_remove_ns_access() {
|
2023-07-29 18:47:25 +00:00
|
|
|
let scenario = HashMap::from([
|
2024-05-22 13:57:25 +00:00
|
|
|
("prepare", "DEFINE ACCESS access ON NS TYPE JWT ALGORITHM HS512 KEY 'secret'"),
|
|
|
|
("test", "REMOVE ACCESS access ON NS"),
|
2023-07-29 18:47:25 +00:00
|
|
|
("check", "INFO FOR NS"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-05-22 13:57:25 +00:00
|
|
|
vec!["{ accesses: { }, databases: { }, users: { } }"],
|
2024-05-28 15:29:11 +00:00
|
|
|
vec!["{ accesses: { access: \"DEFINE ACCESS access ON NAMESPACE TYPE JWT ALGORITHM HS512 KEY '[REDACTED]' WITH ISSUER KEY '[REDACTED]' DURATION 1h\" }, databases: { }, users: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-05-22 13:57:25 +00:00
|
|
|
async fn permissions_checks_remove_db_access() {
|
2023-07-29 18:47:25 +00:00
|
|
|
let scenario = HashMap::from([
|
2024-05-22 13:57:25 +00:00
|
|
|
("prepare", "DEFINE ACCESS access ON DB TYPE JWT ALGORITHM HS512 KEY 'secret'"),
|
|
|
|
("test", "REMOVE ACCESS access ON DB"),
|
2023-07-29 18:47:25 +00:00
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-05-22 13:57:25 +00:00
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { }, models: { }, params: { }, tables: { }, users: { } }"],
|
2024-05-28 15:29:11 +00:00
|
|
|
vec!["{ accesses: { access: \"DEFINE ACCESS access ON DATABASE TYPE JWT ALGORITHM HS512 KEY '[REDACTED]' WITH ISSUER KEY '[REDACTED]' DURATION 1h\" }, analyzers: { }, functions: { }, models: { }, params: { }, tables: { }, users: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_root_user() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE USER user ON ROOT PASSHASH 'secret' ROLES VIEWER"),
|
|
|
|
("test", "REMOVE USER user ON ROOT"),
|
|
|
|
("check", "INFO FOR ROOT"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
|
|
|
vec!["{ namespaces: { }, users: { } }"],
|
|
|
|
vec!["{ namespaces: { }, users: { user: \"DEFINE USER user ON ROOT PASSHASH 'secret' ROLES VIEWER\" } }"],
|
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_ns_user() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE USER user ON NS PASSHASH 'secret' ROLES VIEWER"),
|
|
|
|
("test", "REMOVE USER user ON NS"),
|
|
|
|
("check", "INFO FOR NS"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-05-22 13:57:25 +00:00
|
|
|
vec!["{ accesses: { }, databases: { }, users: { } }"],
|
|
|
|
vec!["{ accesses: { }, databases: { }, users: { user: \"DEFINE USER user ON NAMESPACE PASSHASH 'secret' ROLES VIEWER\" } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_db_user() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE USER user ON DB PASSHASH 'secret' ROLES VIEWER"),
|
|
|
|
("test", "REMOVE USER user ON DB"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-05-22 13:57:25 +00:00
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { }, models: { }, params: { }, tables: { }, users: { } }"],
|
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { }, models: { }, params: { }, tables: { }, users: { user: \"DEFINE USER user ON DATABASE PASSHASH 'secret' ROLES VIEWER\" } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_param() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE PARAM $param VALUE 'foo'"),
|
|
|
|
("test", "REMOVE PARAM $param"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-05-22 13:57:25 +00:00
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { }, models: { }, params: { }, tables: { }, users: { } }"],
|
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { }, models: { }, params: { param: \"DEFINE PARAM $param VALUE 'foo' PERMISSIONS FULL\" }, tables: { }, users: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_table() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE TABLE TB"),
|
|
|
|
("test", "REMOVE TABLE TB"),
|
|
|
|
("check", "INFO FOR DB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2024-05-22 13:57:25 +00:00
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { }, models: { }, params: { }, tables: { }, users: { } }"],
|
|
|
|
vec!["{ accesses: { }, analyzers: { }, functions: { }, models: { }, params: { }, tables: { TB: 'DEFINE TABLE TB TYPE ANY SCHEMALESS PERMISSIONS NONE' }, users: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_event() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE EVENT event ON TABLE TB WHEN true THEN RETURN 'foo'"),
|
|
|
|
("test", "REMOVE EVENT event ON TABLE TB"),
|
|
|
|
("check", "INFO FOR TABLE TB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-09-11 19:35:32 +00:00
|
|
|
vec!["{ events: { }, fields: { }, indexes: { }, lives: { }, tables: { } }"],
|
|
|
|
vec!["{ events: { event: \"DEFINE EVENT event ON TB WHEN true THEN (RETURN 'foo')\" }, fields: { }, indexes: { }, lives: { }, tables: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_field() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE FIELD field ON TABLE TB"),
|
|
|
|
("test", "REMOVE FIELD field ON TABLE TB"),
|
|
|
|
("check", "INFO FOR TABLE TB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-09-11 19:35:32 +00:00
|
|
|
vec!["{ events: { }, fields: { }, indexes: { }, lives: { }, tables: { } }"],
|
2023-12-06 21:30:18 +00:00
|
|
|
vec!["{ events: { }, fields: { field: 'DEFINE FIELD field ON TB PERMISSIONS FULL' }, indexes: { }, lives: { }, tables: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn permissions_checks_remove_index() {
|
|
|
|
let scenario = HashMap::from([
|
|
|
|
("prepare", "DEFINE INDEX index ON TABLE TB FIELDS field"),
|
|
|
|
("test", "REMOVE INDEX index ON TABLE TB"),
|
|
|
|
("check", "INFO FOR TABLE TB"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Define the expected results for the check statement when the test statement succeeded and when it failed
|
|
|
|
let check_results = [
|
2023-09-11 19:35:32 +00:00
|
|
|
vec!["{ events: { }, fields: { }, indexes: { }, lives: { }, tables: { } }"],
|
|
|
|
vec!["{ events: { }, fields: { }, indexes: { index: 'DEFINE INDEX index ON TB FIELDS field' }, lives: { }, tables: { } }"],
|
2023-07-29 18:47:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
// Root level
|
|
|
|
((().into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((().into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
// Namespace level
|
|
|
|
((("NS",).into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS",).into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS",).into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
// Database level
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Owner), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "DB"), true),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Editor), ("OTHER_NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("NS", "OTHER_DB"), false),
|
|
|
|
((("NS", "DB").into(), Role::Viewer), ("OTHER_NS", "DB"), false),
|
|
|
|
];
|
|
|
|
|
|
|
|
let res = iam_check_cases(test_cases.iter(), &scenario, check_results).await;
|
|
|
|
assert!(res.is_ok(), "{}", res.unwrap_err());
|
|
|
|
}
|