2023-04-17 13:27:32 +00:00
|
|
|
#[allow(unused_imports, dead_code)]
|
|
|
|
mod api_integration {
|
2023-08-18 07:17:54 +00:00
|
|
|
use chrono::DateTime;
|
2023-07-29 18:47:25 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2023-04-17 13:27:32 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
|
|
|
use serde_json::json;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::ops::Bound;
|
2023-07-29 18:47:25 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2023-08-25 07:55:22 +00:00
|
|
|
use std::time::Duration;
|
2023-04-19 08:46:37 +00:00
|
|
|
use surrealdb::error::Api as ApiError;
|
|
|
|
use surrealdb::error::Db as DbError;
|
2023-04-17 13:27:32 +00:00
|
|
|
use surrealdb::opt::auth::Database;
|
|
|
|
use surrealdb::opt::auth::Jwt;
|
|
|
|
use surrealdb::opt::auth::Namespace;
|
|
|
|
use surrealdb::opt::auth::Root;
|
|
|
|
use surrealdb::opt::auth::Scope;
|
2023-08-22 09:23:49 +00:00
|
|
|
use surrealdb::opt::Config;
|
2023-04-17 13:27:32 +00:00
|
|
|
use surrealdb::opt::PatchOp;
|
2023-04-28 11:20:57 +00:00
|
|
|
use surrealdb::opt::Resource;
|
2023-04-17 13:27:32 +00:00
|
|
|
use surrealdb::sql::statements::BeginStatement;
|
|
|
|
use surrealdb::sql::statements::CommitStatement;
|
|
|
|
use surrealdb::sql::thing;
|
|
|
|
use surrealdb::sql::Thing;
|
2023-04-28 11:20:57 +00:00
|
|
|
use surrealdb::sql::Value;
|
2023-04-19 08:46:37 +00:00
|
|
|
use surrealdb::Error;
|
2023-04-17 13:27:32 +00:00
|
|
|
use surrealdb::Surreal;
|
2023-06-30 11:43:19 +00:00
|
|
|
use tracing_subscriber::filter::EnvFilter;
|
|
|
|
use tracing_subscriber::fmt;
|
|
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
|
|
use tracing_subscriber::util::SubscriberInitExt;
|
2023-04-17 13:27:32 +00:00
|
|
|
use ulid::Ulid;
|
|
|
|
|
|
|
|
const NS: &str = "test-ns";
|
|
|
|
const ROOT_USER: &str = "root";
|
|
|
|
const ROOT_PASS: &str = "root";
|
2023-08-25 07:55:22 +00:00
|
|
|
const TICK_INTERVAL: Duration = Duration::from_secs(1);
|
2023-07-29 18:47:25 +00:00
|
|
|
// Used to ensure that only one test at a time is setting up the underlaying datastore.
|
|
|
|
// When auth is enabled, multiple tests may try to create the same root user at the same time.
|
|
|
|
static SETUP_MUTEX: Lazy<Arc<Mutex<()>>> = Lazy::new(|| Arc::new(Mutex::new(())));
|
2023-04-17 13:27:32 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct Record<'a> {
|
|
|
|
name: &'a str,
|
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct RecordId {
|
|
|
|
id: Thing,
|
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct RecordName {
|
|
|
|
name: String,
|
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[derive(Debug, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
|
|
|
|
struct RecordBuf {
|
|
|
|
id: Thing,
|
|
|
|
name: String,
|
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
struct AuthParams<'a> {
|
|
|
|
email: &'a str,
|
|
|
|
pass: &'a str,
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 11:43:19 +00:00
|
|
|
fn init_logger() {
|
|
|
|
let test_writer = fmt::layer().with_test_writer();
|
|
|
|
let builder = fmt::Subscriber::builder().with_env_filter(EnvFilter::from_default_env());
|
|
|
|
let subscriber = builder.finish();
|
|
|
|
let _ = subscriber.with(test_writer).try_init();
|
|
|
|
}
|
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[cfg(feature = "protocol-ws")]
|
|
|
|
mod ws {
|
|
|
|
use super::*;
|
|
|
|
use surrealdb::engine::remote::ws::Client;
|
|
|
|
use surrealdb::engine::remote::ws::Ws;
|
|
|
|
|
|
|
|
async fn new_db() -> Surreal<Client> {
|
2023-07-29 18:47:25 +00:00
|
|
|
let _guard = SETUP_MUTEX.lock().unwrap();
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-04-17 13:27:32 +00:00
|
|
|
let db = Surreal::new::<Ws>("127.0.0.1:8000").await.unwrap();
|
|
|
|
db.signin(Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
db
|
|
|
|
}
|
|
|
|
|
|
|
|
include!("api/mod.rs");
|
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[cfg(feature = "protocol-http")]
|
|
|
|
mod http {
|
|
|
|
use super::*;
|
|
|
|
use surrealdb::engine::remote::http::Client;
|
|
|
|
use surrealdb::engine::remote::http::Http;
|
|
|
|
|
|
|
|
async fn new_db() -> Surreal<Client> {
|
2023-07-29 18:47:25 +00:00
|
|
|
let _guard = SETUP_MUTEX.lock().unwrap();
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-04-17 13:27:32 +00:00
|
|
|
let db = Surreal::new::<Http>("127.0.0.1:8000").await.unwrap();
|
|
|
|
db.signin(Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
db
|
|
|
|
}
|
|
|
|
|
|
|
|
include!("api/mod.rs");
|
|
|
|
include!("api/backup.rs");
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[cfg(feature = "kv-mem")]
|
|
|
|
mod mem {
|
|
|
|
use super::*;
|
|
|
|
use surrealdb::engine::any;
|
|
|
|
use surrealdb::engine::local::Db;
|
|
|
|
use surrealdb::engine::local::Mem;
|
2023-07-29 18:47:25 +00:00
|
|
|
use surrealdb::iam;
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
async fn new_db() -> Surreal<Db> {
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-06-09 13:45:07 +00:00
|
|
|
let root = Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
};
|
2023-08-25 07:55:22 +00:00
|
|
|
let config = Config::new().user(root).tick_interval(TICK_INTERVAL);
|
2023-08-22 09:23:49 +00:00
|
|
|
let db = Surreal::new::<Mem>(config).await.unwrap();
|
2023-06-09 13:45:07 +00:00
|
|
|
db.signin(root).await.unwrap();
|
|
|
|
db
|
2023-04-17 13:27:32 +00:00
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn memory_allowed_as_address() {
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-04-17 13:27:32 +00:00
|
|
|
any::connect("memory").await.unwrap();
|
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-06-09 13:45:07 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn signin_first_not_necessary() {
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-06-09 13:45:07 +00:00
|
|
|
let db = Surreal::new::<Mem>(()).await.unwrap();
|
|
|
|
db.use_ns("namespace").use_db("database").await.unwrap();
|
|
|
|
let Some(record): Option<RecordId> = db.create(("item", "foo")).await.unwrap() else {
|
2023-08-24 19:02:44 +00:00
|
|
|
panic!("record not found");
|
|
|
|
};
|
2023-06-09 13:45:07 +00:00
|
|
|
assert_eq!(record.id.to_string(), "item:foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn cant_sign_into_default_root_account() {
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-06-09 13:45:07 +00:00
|
|
|
let db = Surreal::new::<Mem>(()).await.unwrap();
|
2023-08-24 19:02:44 +00:00
|
|
|
let Error::Db(DbError::InvalidAuth) = db
|
|
|
|
.signin(Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap_err()
|
|
|
|
else {
|
|
|
|
panic!("unexpected successful login");
|
|
|
|
};
|
2023-06-09 13:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn credentials_activate_authentication() {
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-08-22 09:23:49 +00:00
|
|
|
let config = Config::new().user(Root {
|
2023-06-09 13:45:07 +00:00
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
2023-08-22 09:23:49 +00:00
|
|
|
});
|
|
|
|
let db = Surreal::new::<Mem>(config).await.unwrap();
|
2023-06-09 13:45:07 +00:00
|
|
|
db.use_ns("namespace").use_db("database").await.unwrap();
|
2023-07-29 18:47:25 +00:00
|
|
|
let res = db.create(Resource::from("item:foo")).await;
|
2023-08-24 19:02:44 +00:00
|
|
|
let Error::Db(DbError::IamError(iam::Error::NotAllowed {
|
|
|
|
actor: _,
|
|
|
|
action: _,
|
|
|
|
resource: _,
|
|
|
|
})) = res.unwrap_err()
|
|
|
|
else {
|
|
|
|
panic!("expected permissions error");
|
|
|
|
};
|
2023-06-09 13:45:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 06:33:38 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn surreal_clone() {
|
|
|
|
use surrealdb::engine::any::Any;
|
|
|
|
|
|
|
|
let db: Surreal<Db> = Surreal::init();
|
|
|
|
db.clone().connect::<Mem>(()).await.unwrap();
|
|
|
|
db.use_ns("test").use_db("test").await.unwrap();
|
|
|
|
|
|
|
|
let db: Surreal<Any> = Surreal::init();
|
|
|
|
db.clone().connect("memory").await.unwrap();
|
|
|
|
db.use_ns("test").use_db("test").await.unwrap();
|
|
|
|
}
|
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
include!("api/mod.rs");
|
|
|
|
include!("api/backup.rs");
|
2022-12-30 23:53:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[cfg(feature = "kv-rocksdb")]
|
|
|
|
mod file {
|
|
|
|
use super::*;
|
|
|
|
use surrealdb::engine::local::Db;
|
|
|
|
use surrealdb::engine::local::File;
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
async fn new_db() -> Surreal<Db> {
|
2023-07-29 18:47:25 +00:00
|
|
|
let _guard = SETUP_MUTEX.lock().unwrap();
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-04-17 13:27:32 +00:00
|
|
|
let path = format!("/tmp/{}.db", Ulid::new());
|
2023-06-09 13:45:07 +00:00
|
|
|
let root = Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
};
|
2023-08-25 07:55:22 +00:00
|
|
|
let config = Config::new().user(root).tick_interval(TICK_INTERVAL);
|
2023-08-22 09:23:49 +00:00
|
|
|
let db = Surreal::new::<File>((path, config)).await.unwrap();
|
2023-06-09 13:45:07 +00:00
|
|
|
db.signin(root).await.unwrap();
|
|
|
|
db
|
2023-04-17 13:27:32 +00:00
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-05-31 12:35:41 +00:00
|
|
|
include!("api/mod.rs");
|
|
|
|
include!("api/backup.rs");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "kv-rocksdb")]
|
|
|
|
mod rocksdb {
|
|
|
|
use super::*;
|
|
|
|
use surrealdb::engine::local::Db;
|
|
|
|
use surrealdb::engine::local::RocksDb;
|
|
|
|
|
|
|
|
async fn new_db() -> Surreal<Db> {
|
2023-07-29 18:47:25 +00:00
|
|
|
let _guard = SETUP_MUTEX.lock().unwrap();
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-05-31 12:35:41 +00:00
|
|
|
let path = format!("/tmp/{}.db", Ulid::new());
|
2023-07-29 18:47:25 +00:00
|
|
|
let root = Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
};
|
2023-08-25 07:55:22 +00:00
|
|
|
let config = Config::new().user(root).tick_interval(TICK_INTERVAL);
|
2023-08-22 09:23:49 +00:00
|
|
|
let db = Surreal::new::<RocksDb>((path, config)).await.unwrap();
|
2023-07-29 18:47:25 +00:00
|
|
|
db.signin(root).await.unwrap();
|
|
|
|
db
|
2023-05-31 12:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
include!("api/mod.rs");
|
|
|
|
include!("api/backup.rs");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "kv-speedb")]
|
|
|
|
mod speedb {
|
|
|
|
use super::*;
|
|
|
|
use surrealdb::engine::local::Db;
|
|
|
|
use surrealdb::engine::local::SpeeDb;
|
|
|
|
|
|
|
|
async fn new_db() -> Surreal<Db> {
|
2023-07-29 18:47:25 +00:00
|
|
|
let _guard = SETUP_MUTEX.lock().unwrap();
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-05-31 12:35:41 +00:00
|
|
|
let path = format!("/tmp/{}.db", Ulid::new());
|
2023-07-29 18:47:25 +00:00
|
|
|
let root = Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
};
|
2023-08-25 07:55:22 +00:00
|
|
|
let config = Config::new().user(root).tick_interval(TICK_INTERVAL);
|
2023-08-22 09:23:49 +00:00
|
|
|
let db = Surreal::new::<SpeeDb>((path, config)).await.unwrap();
|
2023-07-29 18:47:25 +00:00
|
|
|
db.signin(root).await.unwrap();
|
|
|
|
db
|
2023-05-31 12:35:41 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
include!("api/mod.rs");
|
|
|
|
include!("api/backup.rs");
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[cfg(feature = "kv-tikv")]
|
|
|
|
mod tikv {
|
|
|
|
use super::*;
|
|
|
|
use surrealdb::engine::local::Db;
|
|
|
|
use surrealdb::engine::local::TiKv;
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
async fn new_db() -> Surreal<Db> {
|
2023-07-29 18:47:25 +00:00
|
|
|
let _guard = SETUP_MUTEX.lock().unwrap();
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-06-09 13:45:07 +00:00
|
|
|
let root = Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
};
|
2023-08-25 07:55:22 +00:00
|
|
|
let config = Config::new().user(root).tick_interval(TICK_INTERVAL);
|
2023-08-22 09:23:49 +00:00
|
|
|
let db = Surreal::new::<TiKv>(("127.0.0.1:2379", config)).await.unwrap();
|
2023-06-09 13:45:07 +00:00
|
|
|
db.signin(root).await.unwrap();
|
|
|
|
db
|
2023-04-17 13:27:32 +00:00
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
include!("api/mod.rs");
|
|
|
|
include!("api/backup.rs");
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[cfg(feature = "kv-fdb")]
|
|
|
|
mod fdb {
|
|
|
|
use super::*;
|
|
|
|
use surrealdb::engine::local::Db;
|
|
|
|
use surrealdb::engine::local::FDb;
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
async fn new_db() -> Surreal<Db> {
|
2023-07-29 18:47:25 +00:00
|
|
|
let _guard = SETUP_MUTEX.lock().unwrap();
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-06-09 13:45:07 +00:00
|
|
|
let root = Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
};
|
2023-08-25 07:55:22 +00:00
|
|
|
let config = Config::new().user(root).tick_interval(TICK_INTERVAL);
|
2023-08-22 09:23:49 +00:00
|
|
|
let db = Surreal::new::<FDb>(("/etc/foundationdb/fdb.cluster", config)).await.unwrap();
|
2023-06-09 13:45:07 +00:00
|
|
|
db.signin(root).await.unwrap();
|
|
|
|
db
|
2023-04-17 13:27:32 +00:00
|
|
|
}
|
2022-12-30 08:23:19 +00:00
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
include!("api/mod.rs");
|
|
|
|
include!("api/backup.rs");
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 13:27:32 +00:00
|
|
|
#[cfg(feature = "protocol-http")]
|
|
|
|
mod any {
|
|
|
|
use super::*;
|
|
|
|
use surrealdb::engine::any::Any;
|
|
|
|
|
|
|
|
async fn new_db() -> Surreal<Any> {
|
2023-07-29 18:47:25 +00:00
|
|
|
let _guard = SETUP_MUTEX.lock().unwrap();
|
2023-06-30 11:43:19 +00:00
|
|
|
init_logger();
|
2023-04-17 13:27:32 +00:00
|
|
|
let db = surrealdb::engine::any::connect("http://127.0.0.1:8000").await.unwrap();
|
|
|
|
db.signin(Root {
|
|
|
|
username: ROOT_USER,
|
|
|
|
password: ROOT_PASS,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
db
|
|
|
|
}
|
|
|
|
|
|
|
|
include!("api/mod.rs");
|
|
|
|
include!("api/backup.rs");
|
2022-12-30 08:23:19 +00:00
|
|
|
}
|
|
|
|
}
|