2024-01-09 15:27:03 +00:00
|
|
|
use super::common::{self, Format, Socket, DB, NS, PASS, USER};
|
2024-05-23 13:04:20 +00:00
|
|
|
use assert_fs::TempDir;
|
2024-09-03 13:01:37 +00:00
|
|
|
use http::header::{HeaderMap, HeaderValue};
|
2024-01-09 15:27:03 +00:00
|
|
|
use serde_json::json;
|
2024-02-14 11:12:11 +00:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2024-01-09 15:27:03 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
use test_log::test;
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn ping() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send INFO command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("ping", json!([])).await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn info() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Define a user table
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_query("DEFINE TABLE user PERMISSIONS FULL").await?;
|
2024-05-22 13:57:25 +00:00
|
|
|
// Define a user record access method
|
2024-01-09 15:27:03 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS user ON DATABASE TYPE RECORD
|
2024-01-09 15:27:03 +00:00
|
|
|
SIGNUP ( CREATE user SET user = $user, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE user = $user AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 24h
|
2024-01-09 15:27:03 +00:00
|
|
|
;
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
// Create a user record
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
CREATE user CONTENT {
|
|
|
|
user: 'user',
|
|
|
|
pass: crypto::argon2::generate('pass')
|
|
|
|
};
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.await?;
|
2024-05-22 13:57:25 +00:00
|
|
|
// Sign in as record user
|
|
|
|
socket.send_message_signin("user", "pass", Some(NS), Some(DB), Some("user")).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send INFO command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("info", json!([])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["user"], "user", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn signup() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-05-22 13:57:25 +00:00
|
|
|
// Define a user record access method
|
2024-01-09 15:27:03 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS user ON DATABASE TYPE RECORD
|
2024-01-09 15:27:03 +00:00
|
|
|
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 24h
|
2024-01-09 15:27:03 +00:00
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
// Send SIGNUP command
|
|
|
|
let res = socket
|
2024-02-14 11:12:11 +00:00
|
|
|
.send_request(
|
|
|
|
"signup",
|
|
|
|
json!([{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-02-14 11:12:11 +00:00
|
|
|
"email": "email@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]),
|
2024-01-09 15:27:03 +00:00
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify it returns a token
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_str().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.starts_with("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9"), "result: {res}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn signin() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-05-22 13:57:25 +00:00
|
|
|
// Define a user record access method
|
2024-01-09 15:27:03 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS user ON DATABASE TYPE RECORD
|
2024-01-09 15:27:03 +00:00
|
|
|
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 24h
|
2024-01-09 15:27:03 +00:00
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
// Send SIGNUP command
|
|
|
|
let res = socket
|
2024-02-14 11:12:11 +00:00
|
|
|
.send_request(
|
|
|
|
"signup",
|
|
|
|
json!(
|
|
|
|
[{
|
2024-01-09 15:27:03 +00:00
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-01-09 15:27:03 +00:00
|
|
|
"email": "email@email.com",
|
|
|
|
"pass": "pass",
|
2024-02-14 11:12:11 +00:00
|
|
|
}]
|
|
|
|
),
|
2024-01-09 15:27:03 +00:00
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify it returns a token
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_str().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.starts_with("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9"), "result: {res}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send SIGNIN command
|
|
|
|
let res = socket
|
2024-02-14 11:12:11 +00:00
|
|
|
.send_request(
|
|
|
|
"signin",
|
|
|
|
json!(
|
|
|
|
[{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-02-14 11:12:11 +00:00
|
|
|
"email": "email@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]),
|
2024-01-09 15:27:03 +00:00
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify it returns a token
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_str().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.starts_with("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9"), "result: {res}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn invalidate() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify we have an authenticated session
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("DEFINE NAMESPACE test").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["status"], "OK", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send INVALIDATE command
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_request("invalidate", json!([])).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify we have an invalidated session
|
2024-07-02 20:16:08 +00:00
|
|
|
let res = socket.send_request("query", json!(["DEFINE NAMESPACE test"])).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
assert_eq!(
|
2024-07-02 20:16:08 +00:00
|
|
|
res["error"]["message"],
|
|
|
|
"There was a problem with the database: IAM error: Not enough permissions to perform this action",
|
2024-07-18 12:19:28 +00:00
|
|
|
"result: {res:?}"
|
2024-01-09 15:27:03 +00:00
|
|
|
);
|
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn authenticate() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
let token = socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Disconnect the connection
|
|
|
|
socket.close().await?;
|
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send AUTHENTICATE command
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_request("authenticate", json!([token,])).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify we have an authenticated session
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("DEFINE NAMESPACE test").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["status"], "OK", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn letset() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send LET command
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_request("let", json!(["let_var", "let_value",])).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send SET command
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_request("set", json!(["set_var", "set_value",])).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the variables are set
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM $let_var, $set_var").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["result"], json!(["let_value", "set_value"]), "result: {res:?}");
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn unset() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send LET command
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_request("let", json!(["let_var", "let_value",])).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the variable is set
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM $let_var").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["result"], json!(["let_value"]), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send UNSET command
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_request("unset", json!(["let_var",])).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the variable is unset
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM $let_var").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["result"], json!([null]), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn select() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a test record
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_query("CREATE tester SET name = 'foo', value = 'bar'").await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send SELECT command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("select", json!(["tester",])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn insert() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send INSERT command
|
|
|
|
let res = socket
|
2024-02-14 11:12:11 +00:00
|
|
|
.send_request(
|
|
|
|
"insert",
|
|
|
|
json!([
|
|
|
|
"tester",
|
|
|
|
{
|
|
|
|
"name": "foo",
|
|
|
|
"value": "bar",
|
|
|
|
}
|
|
|
|
]),
|
2024-01-09 15:27:03 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the data was inserted and can be queried
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM tester").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res[0]["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res[0]["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn create() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send CREATE command
|
|
|
|
let res = socket
|
2024-02-14 11:12:11 +00:00
|
|
|
.send_request(
|
|
|
|
"create",
|
|
|
|
json!([
|
|
|
|
"tester",
|
|
|
|
{
|
|
|
|
"value": "bar",
|
|
|
|
}
|
|
|
|
]),
|
2024-01-09 15:27:03 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-09-03 13:01:37 +00:00
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
|
|
|
let res = res["result"].as_object().unwrap();
|
|
|
|
assert_eq!(res["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the data was created
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM tester").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res[0]["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res[0]["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn update() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a test record
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_query("CREATE tester SET name = 'foo'").await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send UPDATE command
|
|
|
|
let res = socket
|
2024-02-14 11:12:11 +00:00
|
|
|
.send_request(
|
|
|
|
"update",
|
|
|
|
json!([
|
|
|
|
"tester",
|
|
|
|
{
|
|
|
|
"value": "bar",
|
|
|
|
}
|
|
|
|
]),
|
2024-01-09 15:27:03 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the data was updated
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM tester").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res[0]["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res[0]["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["name"], json!(null), "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn merge() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a test record
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_query("CREATE tester SET name = 'foo'").await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send UPDATE command
|
|
|
|
let res = socket
|
2024-02-14 11:12:11 +00:00
|
|
|
.send_request(
|
|
|
|
"merge",
|
|
|
|
json!([
|
|
|
|
"tester",
|
|
|
|
{
|
|
|
|
"value": "bar",
|
|
|
|
}
|
|
|
|
]),
|
2024-01-09 15:27:03 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the data was merged
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM tester").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res[0]["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res[0]["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn patch() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a test record
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_query("CREATE tester:id SET name = 'foo'").await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send PATCH command
|
|
|
|
let res = socket
|
2024-02-14 11:12:11 +00:00
|
|
|
.send_request(
|
|
|
|
"patch",
|
|
|
|
json!([
|
|
|
|
"tester:id",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"op": "add",
|
|
|
|
"path": "value",
|
|
|
|
"value": "bar"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"op": "remove",
|
|
|
|
"path": "name",
|
|
|
|
}
|
2024-01-09 15:27:03 +00:00
|
|
|
]
|
2024-02-14 11:12:11 +00:00
|
|
|
]),
|
2024-01-09 15:27:03 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.get("value"), Some(json!("bar")).as_ref(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the data was patched
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM tester").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res[0]["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res[0]["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["name"], json!(null), "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn delete() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a test record
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_query("CREATE tester:id").await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send DELETE command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("delete", json!(["tester"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert_eq!(res[0]["id"], "tester:id", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a test record
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_query("CREATE tester:id").await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send DELETE command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("delete", json!(["tester:id"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["id"], "tester:id", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the data was merged
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM tester").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res[0]["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res[0]["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 0, "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn query() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send QUERY command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res =
|
|
|
|
socket.send_request("query", json!(["CREATE tester; SELECT * FROM tester;",])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 2, "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Verify the data was created
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_message_query("SELECT * FROM tester").await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res[0]["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res[0]["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn version() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send version command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("version", json!([])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_str().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.starts_with("surrealdb-"), "result: {res}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate that the WebSocket is able to process multiple queries concurrently
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn concurrency() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send 5 long-running queries and verify they run concurrently
|
2024-02-14 11:12:11 +00:00
|
|
|
|
|
|
|
let mut futures = Vec::<Pin<Box<dyn Future<Output = _>>>>::new();
|
2024-01-09 15:27:03 +00:00
|
|
|
for i in 0..5 {
|
2024-02-14 11:12:11 +00:00
|
|
|
let future = socket.send_request("query", json!([format!("SLEEP 3s; RETURN {i};")]));
|
|
|
|
futures.push(Box::pin(future))
|
2024-01-09 15:27:03 +00:00
|
|
|
}
|
2024-02-14 11:12:11 +00:00
|
|
|
|
|
|
|
let res =
|
|
|
|
tokio::time::timeout(Duration::from_secs(5), futures::future::join_all(futures)).await;
|
|
|
|
let Ok(res) = res else {
|
|
|
|
panic!("future timed-out");
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = res.into_iter().try_fold(
|
|
|
|
Vec::new(),
|
|
|
|
|mut acc, x| -> Result<_, Box<dyn std::error::Error>> {
|
|
|
|
acc.push(x?);
|
|
|
|
Ok(acc)
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.iter().all(|v| v["error"].is_null()), "Unexpected error received: {res:#?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn live() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send LIVE command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("live", json!(["tester"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let live1 = res["result"].as_str().unwrap();
|
|
|
|
// Send QUERY command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("query", json!(["LIVE SELECT * FROM tester"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert!(res[0]["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let live2 = res[0]["result"].as_str().unwrap();
|
|
|
|
// Create a new test record
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("query", json!(["CREATE tester:id SET name = 'foo'"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Wait some time for all messages to arrive, and then search for the notification message
|
2024-02-14 11:12:11 +00:00
|
|
|
let msgs: Result<_, Box<dyn std::error::Error>> =
|
|
|
|
tokio::time::timeout(Duration::from_secs(1), async {
|
|
|
|
Ok(vec![socket.receive_other_message().await?, socket.receive_other_message().await?])
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
let msgs = msgs?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(msgs.iter().all(|v| v["error"].is_null()), "Unexpected error received: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Check for first live query notifcation
|
|
|
|
let res = msgs.iter().find(|v| common::is_notification_from_lq(v, live1));
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_some(), "Expected to find a notification for LQ id {live1}: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["action"].is_string(), "result: {res:?}");
|
|
|
|
assert_eq!(res["action"], "CREATE", "result: {res:?}");
|
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["id"], "tester:id", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Check for second live query notifcation
|
|
|
|
let res = msgs.iter().find(|v| common::is_notification_from_lq(v, live2));
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_some(), "Expected to find a notification for LQ id {live2}: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["action"], "CREATE", "result: {res:?}");
|
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["id"], "tester:id", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn kill() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send LIVE command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("live", json!(["tester"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let live1 = res["result"].as_str().unwrap();
|
|
|
|
// Send QUERY command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("query", json!(["LIVE SELECT * FROM tester"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert!(res[0]["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let live2 = res[0]["result"].as_str().unwrap();
|
|
|
|
// Create a new test record
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("query", json!(["CREATE tester:one SET name = 'one'"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Wait some time for all messages to arrive, and then search for the notification message
|
2024-02-14 11:12:11 +00:00
|
|
|
let msgs = socket.receive_all_other_messages(2, Duration::from_secs(1)).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(msgs.iter().all(|v| v["error"].is_null()), "Unexpected error received: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Check for first live query notifcation
|
|
|
|
let res = msgs.iter().find(|v| common::is_notification_from_lq(v, live1));
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_some(), "Expected to find a notification for LQ id {live1}: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["action"].is_string(), "result: {res:?}");
|
|
|
|
assert_eq!(res["action"], "CREATE", "result: {res:?}");
|
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["id"], "tester:one", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Check for second live query notifcation
|
|
|
|
let res = msgs.iter().find(|v| common::is_notification_from_lq(v, live2));
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_some(), "Expected to find a notification for LQ id {live2}: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["action"], "CREATE", "result: {res:?}");
|
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["id"], "tester:one", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send KILL command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("kill", json!([live1])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_null(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a new test record
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("query", json!(["CREATE tester:two SET name = 'two'"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Wait some time for all messages to arrive, and then search for the notification message
|
2024-02-14 11:12:11 +00:00
|
|
|
let msgs = socket.receive_all_other_messages(1, Duration::from_secs(1)).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(msgs.iter().all(|v| v["error"].is_null()), "Unexpected error received: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Check for second live query notifcation
|
|
|
|
let res = msgs.iter().find(|v| common::is_notification_from_lq(v, live2));
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_some(), "Expected to find a notification for LQ id {live2}: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["action"], "CREATE", "result: {res:?}");
|
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["id"], "tester:two", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send QUERY command
|
2024-04-05 16:05:22 +00:00
|
|
|
let res = socket.send_request("query", json!([format!("KILL u'{live2}'")])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
|
|
|
assert!(res[0]["result"].is_null(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a new test record
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket.send_request("query", json!(["CREATE tester:tre SET name = 'two'"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Wait some time for all messages to arrive, and then search for the notification message
|
2024-02-14 11:12:11 +00:00
|
|
|
let msgs = socket.receive_all_other_messages(0, Duration::from_secs(1)).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(msgs.iter().all(|v| v["error"].is_null()), "Unexpected error received: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn live_second_connection() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket1 = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket1.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket1.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send LIVE command
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket1.send_request("live", json!(["tester"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let liveid = res["result"].as_str().unwrap();
|
|
|
|
// Connect to WebSocket
|
2024-02-14 11:12:11 +00:00
|
|
|
let mut socket2 = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-02-14 11:12:11 +00:00
|
|
|
socket2.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-02-14 11:12:11 +00:00
|
|
|
socket2.send_message_use(Some(NS), Some(DB)).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a new test record
|
2024-02-14 11:12:11 +00:00
|
|
|
let res = socket2.send_request("query", json!(["CREATE tester:id SET name = 'foo'"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Wait some time for all messages to arrive, and then search for the notification message
|
2024-02-14 11:12:11 +00:00
|
|
|
let msgs = socket1.receive_all_other_messages(1, Duration::from_secs(1)).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(msgs.iter().all(|v| v["error"].is_null()), "Unexpected error received: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Check for live query notifcation
|
|
|
|
let res = msgs.iter().find(|v| common::is_notification_from_lq(v, liveid));
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_some(), "Expected to find a notification for LQ id {liveid}: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["action"].is_string(), "result: {res:?}");
|
|
|
|
assert_eq!(res["action"], "CREATE", "result: {res:?}");
|
|
|
|
assert!(res["result"].is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_object().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res["id"], "tester:id", "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn variable_auth_live_query() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
// Connect to WebSocket
|
2024-03-18 17:54:34 +00:00
|
|
|
let mut socket_permanent = Socket::connect(&addr, SERVER, FORMAT).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Authenticate the connection
|
2024-03-18 17:54:34 +00:00
|
|
|
socket_permanent.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Specify a namespace and database
|
2024-03-18 17:54:34 +00:00
|
|
|
socket_permanent.send_message_use(Some(NS), Some(DB)).await?;
|
2024-05-22 13:57:25 +00:00
|
|
|
// Define a user record access method
|
2024-03-18 17:54:34 +00:00
|
|
|
socket_permanent
|
2024-01-09 15:27:03 +00:00
|
|
|
.send_message_query(
|
|
|
|
r#"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS user ON DATABASE TYPE RECORD
|
2024-01-09 15:27:03 +00:00
|
|
|
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 1s, FOR TOKEN 24h
|
2024-01-09 15:27:03 +00:00
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
// Send SIGNUP command
|
2024-03-18 17:54:34 +00:00
|
|
|
let mut socket_expiring_auth = Socket::connect(&addr, SERVER, FORMAT).await?;
|
|
|
|
|
|
|
|
let res = socket_expiring_auth
|
2024-02-14 11:12:11 +00:00
|
|
|
.send_request(
|
|
|
|
"signup",
|
|
|
|
json!([{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-02-14 11:12:11 +00:00
|
|
|
"email": "email@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]),
|
2024-01-09 15:27:03 +00:00
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-18 17:54:34 +00:00
|
|
|
// Authenticate the connection
|
|
|
|
socket_expiring_auth.send_message_signin(USER, PASS, None, None, None).await?;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Send LIVE command
|
2024-03-18 17:54:34 +00:00
|
|
|
let res = socket_expiring_auth.send_request("live", json!(["tester"])).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Wait 2 seconds for auth to expire
|
2024-03-18 17:54:34 +00:00
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
2024-01-09 15:27:03 +00:00
|
|
|
// Create a new test record
|
2024-03-18 17:54:34 +00:00
|
|
|
let res = socket_permanent
|
|
|
|
.send_request("query", json!(["CREATE tester:id SET name = 'foo'"]))
|
|
|
|
.await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
|
|
|
assert!(res["result"].is_array(), "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
let res = res["result"].as_array().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res.len(), 1, "result: {res:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Wait some time for all messages to arrive, and then search for the notification message
|
2024-03-18 17:54:34 +00:00
|
|
|
let msgs = socket_expiring_auth.receive_all_other_messages(0, Duration::from_secs(1)).await?;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(msgs.iter().all(|v| v["error"].is_null()), "Unexpected error received: {msgs:?}");
|
2024-01-09 15:27:03 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-01-09 15:27:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2024-03-05 15:26:14 +00:00
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_expiration() {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection
|
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
// Define a user record access method
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS user ON DATABASE TYPE RECORD
|
2024-03-05 15:26:14 +00:00
|
|
|
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 1s, FOR TOKEN 1d
|
2024-03-05 15:26:14 +00:00
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
// Create resource that requires a session with the access method to query
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE TABLE test SCHEMALESS
|
2024-05-22 13:57:25 +00:00
|
|
|
PERMISSIONS FOR select, create, update, delete WHERE $access = "user"
|
2024-03-05 15:26:14 +00:00
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
CREATE test:1 SET working = "yes"
|
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
// Send SIGNUP command
|
|
|
|
let res = socket
|
|
|
|
.send_request(
|
|
|
|
"signup",
|
|
|
|
json!(
|
|
|
|
[{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-03-05 15:26:14 +00:00
|
|
|
"email": "email@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Verify it returns a token
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res["result"].as_str().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.starts_with("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9"), "result: {res}");
|
2024-06-05 12:36:41 +00:00
|
|
|
// Authenticate using the token, which expires in a day
|
2024-03-05 15:26:14 +00:00
|
|
|
socket.send_request("authenticate", json!([res,])).await.unwrap();
|
|
|
|
// Check if the session is now authenticated
|
|
|
|
let res = socket.send_message_query("SELECT VALUE working FROM test:1").await.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["result"], json!(["yes"]), "result: {res:?}");
|
2024-06-05 12:36:41 +00:00
|
|
|
// Wait two seconds for the session to expire
|
2024-03-05 15:26:14 +00:00
|
|
|
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
|
|
|
|
// Check that the session has expired and queries fail
|
|
|
|
let res = socket.send_request("query", json!(["SELECT VALUE working FROM test:1",])).await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-03-18 12:30:31 +00:00
|
|
|
assert_eq!(
|
|
|
|
res["error"],
|
|
|
|
json!({"code": -32000, "message": "There was a problem with the database: The session has expired"})
|
|
|
|
);
|
2024-03-05 15:26:14 +00:00
|
|
|
// Sign in again using the same session
|
|
|
|
let res = socket
|
|
|
|
.send_request(
|
|
|
|
"signin",
|
|
|
|
json!(
|
|
|
|
[{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-03-05 15:26:14 +00:00
|
|
|
"email": "email@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Check that the session is now valid again and queries succeed
|
|
|
|
let res = socket.send_message_query("SELECT VALUE working FROM test:1").await.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["result"], json!(["yes"]), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_expiration_operations() {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection
|
|
|
|
// We store the root token to test reauthentication later
|
|
|
|
let root_token = socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
// Define a user record access method
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS user ON DATABASE TYPE RECORD
|
2024-03-05 15:26:14 +00:00
|
|
|
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 1s, FOR TOKEN 1d
|
2024-03-05 15:26:14 +00:00
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
// Create resource that requires a session with the access method to query
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE TABLE test SCHEMALESS
|
2024-05-22 13:57:25 +00:00
|
|
|
PERMISSIONS FOR select, create, update, delete WHERE $access = "user"
|
2024-03-05 15:26:14 +00:00
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
CREATE test:1 SET working = "yes"
|
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
// Send SIGNUP command
|
|
|
|
let res = socket
|
|
|
|
.send_request(
|
|
|
|
"signup",
|
|
|
|
json!(
|
|
|
|
[{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-03-05 15:26:14 +00:00
|
|
|
"email": "email@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Verify it returns a token
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res["result"].as_str().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.starts_with("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9"), "result: {res}");
|
2024-06-05 12:36:41 +00:00
|
|
|
// Authenticate using the token, which expires in a day
|
2024-03-05 15:26:14 +00:00
|
|
|
socket.send_request("authenticate", json!([res,])).await.unwrap();
|
|
|
|
// Check if the session is now authenticated
|
|
|
|
let res = socket.send_message_query("SELECT VALUE working FROM test:1").await.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["result"], json!(["yes"]), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Wait two seconds for the session to expire
|
|
|
|
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
|
|
|
|
// Check if the session is now expired
|
|
|
|
let res = socket.send_request("query", json!(["SELECT VALUE working FROM test:1",])).await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-03-18 12:30:31 +00:00
|
|
|
assert_eq!(
|
|
|
|
res["error"],
|
|
|
|
json!({"code": -32000, "message": "There was a problem with the database: The session has expired"})
|
|
|
|
);
|
2024-03-05 15:26:14 +00:00
|
|
|
// Test operations that SHOULD NOT work with an expired session
|
|
|
|
let operations_ko = vec![
|
|
|
|
socket.send_request("let", json!(["let_var", "let_value",])),
|
|
|
|
socket.send_request("set", json!(["set_var", "set_value",])),
|
|
|
|
socket.send_request("info", json!([])),
|
|
|
|
socket.send_request("select", json!(["tester",])),
|
2024-03-18 12:30:31 +00:00
|
|
|
socket.send_request(
|
|
|
|
"insert",
|
|
|
|
json!([
|
|
|
|
"tester",
|
|
|
|
{
|
|
|
|
"name": "foo",
|
|
|
|
"value": "bar",
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
socket.send_request(
|
|
|
|
"create",
|
|
|
|
json!([
|
|
|
|
"tester",
|
|
|
|
{
|
|
|
|
"value": "bar",
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
socket.send_request(
|
|
|
|
"update",
|
|
|
|
json!([
|
|
|
|
"tester",
|
|
|
|
{
|
|
|
|
"value": "bar",
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
socket.send_request(
|
|
|
|
"merge",
|
|
|
|
json!([
|
|
|
|
"tester",
|
|
|
|
{
|
|
|
|
"value": "bar",
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
socket.send_request(
|
|
|
|
"patch",
|
|
|
|
json!([
|
|
|
|
"tester:id",
|
|
|
|
[
|
2024-03-05 15:26:14 +00:00
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
"op": "add",
|
|
|
|
"path": "value",
|
|
|
|
"value": "bar"
|
|
|
|
},
|
2024-03-05 15:26:14 +00:00
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
"op": "remove",
|
|
|
|
"path": "name",
|
2024-03-05 15:26:14 +00:00
|
|
|
}
|
2024-03-18 12:30:31 +00:00
|
|
|
]
|
|
|
|
]),
|
|
|
|
),
|
2024-03-05 15:26:14 +00:00
|
|
|
socket.send_request("delete", json!(["tester"])),
|
|
|
|
socket.send_request("live", json!(["tester"])),
|
|
|
|
socket.send_request("kill", json!(["tester"])),
|
|
|
|
];
|
|
|
|
// Futures are executed sequentially as some operations rely on the previous state
|
|
|
|
for operation in operations_ko {
|
|
|
|
let res = operation.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-03-18 12:30:31 +00:00
|
|
|
assert_eq!(
|
|
|
|
res["error"],
|
|
|
|
json!({"code": -32000, "message": "There was a problem with the database: The session has expired"})
|
|
|
|
);
|
|
|
|
}
|
2024-03-05 15:26:14 +00:00
|
|
|
|
|
|
|
// Test operations that SHOULD work with an expired session
|
|
|
|
let operations_ok = vec![
|
|
|
|
socket.send_request("use", json!([NS, DB])),
|
|
|
|
socket.send_request("ping", json!([])),
|
|
|
|
socket.send_request("version", json!([])),
|
|
|
|
socket.send_request("invalidate", json!([])),
|
|
|
|
];
|
|
|
|
// Futures are executed sequentially as some operations rely on the previous state
|
|
|
|
for operation in operations_ok {
|
|
|
|
let res = operation.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-18 12:30:31 +00:00
|
|
|
}
|
2024-03-05 15:26:14 +00:00
|
|
|
|
|
|
|
// Test operations that SHOULD work with an expired session
|
|
|
|
// These operations will refresh the session expiration
|
|
|
|
let res = socket
|
2024-03-18 12:30:31 +00:00
|
|
|
.send_request(
|
|
|
|
"signup",
|
|
|
|
json!([{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-03-18 12:30:31 +00:00
|
|
|
"email": "another@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]),
|
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Wait two seconds for the session to expire
|
|
|
|
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
|
|
|
|
// The session must be expired now or we fail the test
|
|
|
|
let res = socket.send_request("query", json!(["SELECT VALUE working FROM test:1",])).await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-03-18 12:30:31 +00:00
|
|
|
assert_eq!(
|
|
|
|
res["error"],
|
|
|
|
json!({"code": -32000, "message": "There was a problem with the database: The session has expired"})
|
|
|
|
);
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = socket
|
|
|
|
.send_request(
|
|
|
|
"signin",
|
|
|
|
json!(
|
|
|
|
[{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-03-05 15:26:14 +00:00
|
|
|
"email": "another@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Wait two seconds for the session to expire
|
|
|
|
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
|
|
|
|
// The session must be expired now or we fail the test
|
|
|
|
let res = socket.send_request("query", json!(["SELECT VALUE working FROM test:1",])).await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-03-18 12:30:31 +00:00
|
|
|
assert_eq!(
|
|
|
|
res["error"],
|
|
|
|
json!({"code": -32000, "message": "There was a problem with the database: The session has expired"})
|
|
|
|
);
|
2024-03-05 15:26:14 +00:00
|
|
|
|
|
|
|
// This needs to be last operation as the session will no longer expire afterwards
|
|
|
|
let res = socket.send_request("authenticate", json!([root_token,])).await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
|
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_reauthentication() {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection and store the root level token
|
|
|
|
let root_token = socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Check that we have root access
|
|
|
|
socket.send_message_query("INFO FOR ROOT").await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
// Define a user record access method
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS user ON DATABASE TYPE RECORD
|
2024-03-05 15:26:14 +00:00
|
|
|
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 1s, FOR TOKEN 24h
|
2024-03-05 15:26:14 +00:00
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
// Create resource that requires a session with the access method to query
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE TABLE test SCHEMALESS
|
2024-05-22 13:57:25 +00:00
|
|
|
PERMISSIONS FOR select, create, update, delete WHERE $access = "user"
|
2024-03-05 15:26:14 +00:00
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
CREATE test:1 SET working = "yes"
|
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
// Send SIGNUP command
|
|
|
|
let res = socket
|
|
|
|
.send_request(
|
|
|
|
"signup",
|
|
|
|
json!(
|
|
|
|
[{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-03-05 15:26:14 +00:00
|
|
|
"email": "email@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Verify it returns a token
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res["result"].as_str().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.starts_with("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9"), "result: {res}");
|
2024-05-22 13:57:25 +00:00
|
|
|
// Authenticate using the token
|
2024-03-05 15:26:14 +00:00
|
|
|
socket.send_request("authenticate", json!([res,])).await.unwrap();
|
|
|
|
// Check that we do not have root access
|
|
|
|
let res = socket.send_message_query("INFO FOR ROOT").await.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["status"], "ERR", "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
assert_eq!(
|
|
|
|
res[0]["result"], "IAM error: Not enough permissions to perform this action",
|
2024-07-18 12:19:28 +00:00
|
|
|
"result: {res:?}"
|
2024-03-05 15:26:14 +00:00
|
|
|
);
|
2024-05-22 13:57:25 +00:00
|
|
|
// Check if the session is authenticated
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = socket.send_message_query("SELECT VALUE working FROM test:1").await.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["result"], json!(["yes"]), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Authenticate using the root token
|
|
|
|
socket.send_request("authenticate", json!([root_token,])).await.unwrap();
|
|
|
|
// Check that we have root access again
|
|
|
|
let res = socket.send_message_query("INFO FOR ROOT").await.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["status"], "OK", "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_reauthentication_expired() {
|
|
|
|
// Setup database server
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection and store the root level token
|
|
|
|
let root_token = socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Check that we have root access
|
|
|
|
socket.send_message_query("INFO FOR ROOT").await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
// Define a user record access method
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
2024-06-05 12:36:41 +00:00
|
|
|
DEFINE ACCESS user ON DATABASE TYPE RECORD
|
2024-03-05 15:26:14 +00:00
|
|
|
SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) )
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) )
|
2024-06-05 12:36:41 +00:00
|
|
|
DURATION FOR SESSION 1s, FOR TOKEN 24h
|
2024-03-05 15:26:14 +00:00
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-05-22 13:57:25 +00:00
|
|
|
// Create resource that requires a session with the access method to query
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE TABLE test SCHEMALESS
|
2024-05-22 13:57:25 +00:00
|
|
|
PERMISSIONS FOR select, create, update, delete WHERE $access = "user"
|
2024-03-05 15:26:14 +00:00
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
CREATE test:1 SET working = "yes"
|
|
|
|
;"#,
|
|
|
|
)
|
2024-03-18 12:30:31 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
// Send SIGNUP command
|
|
|
|
let res = socket
|
|
|
|
.send_request(
|
|
|
|
"signup",
|
|
|
|
json!(
|
|
|
|
[{
|
|
|
|
"ns": NS,
|
|
|
|
"db": DB,
|
2024-05-22 13:57:25 +00:00
|
|
|
"ac": "user",
|
2024-03-05 15:26:14 +00:00
|
|
|
"email": "email@email.com",
|
|
|
|
"pass": "pass",
|
|
|
|
}]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Verify it returns a token
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res["result"].is_string(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res["result"].as_str().unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.starts_with("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9"), "result: {res}");
|
2024-05-22 13:57:25 +00:00
|
|
|
// Authenticate using the token, which will expire soon
|
2024-03-05 15:26:14 +00:00
|
|
|
socket.send_request("authenticate", json!([res,])).await.unwrap();
|
|
|
|
// Wait two seconds for token to expire
|
|
|
|
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
|
|
|
|
// Verify that the session has expired
|
|
|
|
let res = socket.send_request("query", json!(["SELECT VALUE working FROM test:1",])).await;
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_ok(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(res.is_object(), "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
let res = res.as_object().unwrap();
|
2024-03-18 12:30:31 +00:00
|
|
|
assert_eq!(
|
|
|
|
res["error"],
|
|
|
|
json!({"code": -32000, "message": "There was a problem with the database: The session has expired"})
|
|
|
|
);
|
2024-03-05 15:26:14 +00:00
|
|
|
// Authenticate using the root token, which has not expired yet
|
|
|
|
socket.send_request("authenticate", json!([root_token,])).await.unwrap();
|
|
|
|
// Check that we have root access and the session is not expired
|
|
|
|
let res = socket.send_message_query("INFO FOR ROOT").await.unwrap();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(res[0]["status"], "OK", "result: {res:?}");
|
2024-03-05 15:26:14 +00:00
|
|
|
// Test passed
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2024-03-05 15:26:14 +00:00
|
|
|
}
|
2024-03-26 15:27:08 +00:00
|
|
|
|
2024-07-11 19:35:30 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_use_change_database() {
|
|
|
|
// Setup database server
|
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection as a root level system user
|
|
|
|
let _ = socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Check that we have root access
|
|
|
|
socket.send_message_query("INFO FOR ROOT").await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some("original")).await.unwrap();
|
|
|
|
// Define a scope on the original database
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE USER user ON DATABASE PASSWORD "secret" ROLES VIEWER
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// Create resource that requires an authenticated record user to query
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE TABLE user SCHEMALESS
|
|
|
|
PERMISSIONS FOR select, create, update, delete NONE
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
CREATE user:1 CONTENT { name: "original", pass: crypto::argon2::generate("original") }
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// Change to a different database
|
|
|
|
socket.send_message_use(Some(NS), Some("different")).await.unwrap();
|
|
|
|
// Create the same user table with a user record with the same identifier
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE TABLE user SCHEMALESS
|
|
|
|
PERMISSIONS FOR select, create, update, delete NONE
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
CREATE user:1 CONTENT { name: "different", pass: crypto::argon2::generate("different") }
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// Sign in to original database as user
|
|
|
|
let res = socket
|
|
|
|
.send_request(
|
|
|
|
"signin",
|
|
|
|
json!(
|
|
|
|
[{
|
|
|
|
"ns": NS,
|
|
|
|
"db": "original",
|
|
|
|
"user": "user",
|
|
|
|
"pass": "secret",
|
|
|
|
}]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert!(res.is_ok(), "result: {:?}", res);
|
|
|
|
let res = res.unwrap();
|
|
|
|
assert!(res.is_object(), "result: {:?}", res);
|
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {:?}", res);
|
|
|
|
// Verify that the authenticated session corresponds with the original user
|
|
|
|
let res = socket.send_message_query("SELECT VALUE name FROM user:1").await.unwrap();
|
|
|
|
assert_eq!(res[0]["result"], json!(["original"]), "result: {:?}", res);
|
|
|
|
// Swtich to the different database without signing in again
|
|
|
|
socket.send_message_use(Some(NS), Some("different")).await.unwrap();
|
|
|
|
// Verify that the authenticated session is unable to query data
|
|
|
|
let res = socket.send_message_query("SELECT VALUE name FROM user:1").await.unwrap();
|
|
|
|
// The query succeeds but the results does not contain the value with permissions
|
|
|
|
assert_eq!(res[0]["status"], "OK", "result: {:?}", res);
|
|
|
|
assert_eq!(res[0]["result"], json!([]), "result: {:?}", res);
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_use_change_database_scope() {
|
|
|
|
// Setup database server
|
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection as a root level system user
|
|
|
|
let _ = socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Check that we have root access
|
|
|
|
socket.send_message_query("INFO FOR ROOT").await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some("original")).await.unwrap();
|
|
|
|
// Define a user record access method on the original database
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE ACCESS user ON DATABASE TYPE RECORD
|
|
|
|
SIGNIN ( SELECT * FROM user WHERE name = $name AND crypto::argon2::compare(pass, $pass) )
|
|
|
|
DURATION FOR SESSION 24h, FOR TOKEN 24h
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// Create resource that requires an authenticated record user to query
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE TABLE user SCHEMALESS
|
|
|
|
PERMISSIONS FOR select, create, update, delete WHERE id = $auth
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
CREATE user:1 CONTENT { name: "original", pass: crypto::argon2::generate("original") }
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// Change to a different database
|
|
|
|
socket.send_message_use(Some(NS), Some("different")).await.unwrap();
|
|
|
|
// Create the same user table with a user record with the same identifier
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
DEFINE TABLE user SCHEMALESS
|
|
|
|
PERMISSIONS FOR select, create, update, delete WHERE id = $auth
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
r#"
|
|
|
|
CREATE user:1 CONTENT { name: "different", pass: crypto::argon2::generate("different") }
|
|
|
|
;"#,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// Sign in to original database as user
|
|
|
|
let res = socket
|
|
|
|
.send_request(
|
|
|
|
"signin",
|
|
|
|
json!(
|
|
|
|
[{
|
|
|
|
"ns": NS,
|
|
|
|
"db": "original",
|
|
|
|
"ac": "user",
|
|
|
|
"name": "original",
|
|
|
|
"pass": "original",
|
|
|
|
}]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert!(res.is_ok(), "result: {:?}", res);
|
|
|
|
let res = res.unwrap();
|
|
|
|
assert!(res.is_object(), "result: {:?}", res);
|
|
|
|
let res = res.as_object().unwrap();
|
|
|
|
// Verify response contains no error
|
|
|
|
assert!(res.keys().all(|k| ["id", "result"].contains(&k.as_str())), "result: {:?}", res);
|
|
|
|
// Verify that the authenticated session corresponds with the original user
|
|
|
|
let res = socket.send_message_query("SELECT VALUE name FROM $auth").await.unwrap();
|
|
|
|
assert_eq!(res[0]["result"], json!(["original"]), "result: {:?}", res);
|
|
|
|
// Swtich to the different database without signing in again
|
|
|
|
socket.send_message_use(Some(NS), Some("different")).await.unwrap();
|
|
|
|
// Verify that the authenticated session is unable to query data
|
|
|
|
let res = socket.send_message_query("SELECT VALUE name FROM $auth").await.unwrap();
|
|
|
|
// The following statement would be true when the bug was present:
|
|
|
|
// assert_eq!(res[0]["result"], json!(["different"]), "result: {:?}", res);
|
|
|
|
assert_eq!(res[0]["status"], "ERR", "result: {:?}", res);
|
|
|
|
assert_eq!(
|
|
|
|
res[0]["result"], "You don't have permission to change to the different database",
|
|
|
|
"result: {:?}",
|
|
|
|
res
|
|
|
|
);
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
2024-03-26 15:27:08 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn run_functions() {
|
|
|
|
// Setup database server
|
2024-07-02 20:16:08 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_functions().await.unwrap();
|
2024-03-26 15:27:08 +00:00
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection
|
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
|
|
|
// Define function
|
|
|
|
socket
|
|
|
|
.send_message_query("DEFINE FUNCTION fn::foo() {RETURN 'fn::foo called';}")
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
socket
|
|
|
|
.send_message_query(
|
|
|
|
"DEFINE FUNCTION fn::bar($val: string) {RETURN 'fn::bar called with: ' + $val;}",
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// call functions
|
|
|
|
let res = socket.send_message_run("fn::foo", None, vec![]).await.unwrap();
|
|
|
|
assert!(matches!(res, serde_json::Value::String(s) if &s == "fn::foo called"));
|
|
|
|
let res = socket.send_message_run("fn::bar", None, vec![]).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res = socket.send_message_run("fn::bar", None, vec![42.into()]).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res = socket.send_message_run("fn::bar", None, vec!["first".into(), "second".into()]).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res = socket.send_message_run("fn::bar", None, vec!["string_val".into()]).await.unwrap();
|
|
|
|
assert!(matches!(res, serde_json::Value::String(s) if &s == "fn::bar called with: string_val"));
|
|
|
|
|
|
|
|
// normal functions
|
2024-04-18 10:48:12 +00:00
|
|
|
let res = socket.send_message_run("math::abs", None, vec![42.into()]).await.unwrap();
|
2024-03-26 15:27:08 +00:00
|
|
|
assert!(matches!(res, serde_json::Value::Number(n) if n.as_u64() == Some(42)));
|
|
|
|
let res = socket
|
|
|
|
.send_message_run("math::max", None, vec![vec![1, 2, 3, 4, 5, 6].into()])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(matches!(res, serde_json::Value::Number(n) if n.as_u64() == Some(6)));
|
|
|
|
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
2024-04-18 10:48:12 +00:00
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn relate_rpc() {
|
|
|
|
// Setup database server
|
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection
|
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
|
|
|
// create records and relate
|
|
|
|
socket.send_message_query("CREATE foo:a, foo:b").await.unwrap();
|
|
|
|
socket
|
|
|
|
.send_message_relate("foo:a".into(), "bar".into(), "foo:b".into(), Some(json!({"val": 42})))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// test
|
|
|
|
|
|
|
|
let mut res = socket.send_message_query("RETURN foo:a->bar.val").await.unwrap();
|
|
|
|
let expected = json!(42);
|
|
|
|
assert_eq!(res.remove(0)["result"], expected);
|
|
|
|
|
|
|
|
let mut res = socket.send_message_query("RETURN foo:a->bar->foo").await.unwrap();
|
|
|
|
let expected = json!(["foo:b"]);
|
|
|
|
assert_eq!(res.remove(0)["result"], expected);
|
|
|
|
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
2024-05-23 13:04:20 +00:00
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn temporary_directory() {
|
|
|
|
// Setup database server
|
|
|
|
let temp_dir = TempDir::new().unwrap();
|
|
|
|
let (addr, mut server) =
|
|
|
|
common::start_server_with_temporary_directory(temp_dir.to_string_lossy().as_ref())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection
|
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
|
|
|
// create records
|
|
|
|
socket.send_message_query("CREATE test:a, test:b").await.unwrap();
|
|
|
|
// These selects use the memory collector
|
|
|
|
let mut res =
|
|
|
|
socket.send_message_query("SELECT * FROM test ORDER BY id DESC EXPLAIN").await.unwrap();
|
|
|
|
let expected = json!([{"detail": { "table": "test" }, "operation": "Iterate Table" }, { "detail": { "type": "Memory" }, "operation": "Collector" }]);
|
|
|
|
assert_eq!(res.remove(0)["result"], expected);
|
|
|
|
// And return the correct result
|
|
|
|
let mut res = socket.send_message_query("SELECT * FROM test ORDER BY id DESC").await.unwrap();
|
|
|
|
let expected = json!([{"id": "test:b" }, { "id": "test:a" }]);
|
|
|
|
assert_eq!(res.remove(0)["result"], expected);
|
|
|
|
// This one should the file collector
|
|
|
|
let mut res = socket
|
|
|
|
.send_message_query("SELECT * FROM test ORDER BY id DESC TEMPFILES EXPLAIN")
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let expected = json!([{"detail": { "table": "test" }, "operation": "Iterate Table" }, { "detail": { "type": "TempFiles" }, "operation": "Collector" }]);
|
|
|
|
assert_eq!(res.remove(0)["result"], expected);
|
|
|
|
// And return the correct result
|
|
|
|
let mut res =
|
|
|
|
socket.send_message_query("SELECT * FROM test ORDER BY id DESC TEMPFILES").await.unwrap();
|
|
|
|
let expected = json!([{"id": "test:b" }, { "id": "test:a" }]);
|
|
|
|
assert_eq!(res.remove(0)["result"], expected);
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
// Cleanup
|
|
|
|
temp_dir.close().unwrap();
|
|
|
|
}
|
2024-08-22 16:25:08 +00:00
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_id_defined() {
|
|
|
|
// Setup database server
|
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
// We specify a request identifier via a specific SurrealDB header
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert("surreal-id", HeaderValue::from_static("00000000-0000-0000-0000-000000000000"));
|
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect_with_headers(&addr, SERVER, FORMAT, headers).await.unwrap();
|
|
|
|
// Authenticate the connection
|
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
|
|
|
|
|
|
|
let mut res = socket.send_message_query("SELECT VALUE id FROM $session").await.unwrap();
|
|
|
|
let expected = json!(["00000000-0000-0000-0000-000000000000"]);
|
|
|
|
assert_eq!(res.remove(0)["result"], expected);
|
|
|
|
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_id_defined_generic() {
|
|
|
|
// Setup database server
|
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
// We specify a request identifier via a generic header
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert("x-request-id", HeaderValue::from_static("00000000-0000-0000-0000-000000000000"));
|
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect_with_headers(&addr, SERVER, FORMAT, headers).await.unwrap();
|
|
|
|
// Authenticate the connection
|
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
|
|
|
|
|
|
|
let mut res = socket.send_message_query("SELECT VALUE id FROM $session").await.unwrap();
|
|
|
|
let expected = json!(["00000000-0000-0000-0000-000000000000"]);
|
|
|
|
assert_eq!(res.remove(0)["result"], expected);
|
|
|
|
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_id_defined_both() {
|
|
|
|
// Setup database server
|
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
// We specify a request identifier via both headers
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert("surreal-id", HeaderValue::from_static("00000000-0000-0000-0000-000000000000"));
|
|
|
|
headers.insert("x-request-id", HeaderValue::from_static("aaaaaaaa-aaaa-0000-0000-000000000000"));
|
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect_with_headers(&addr, SERVER, FORMAT, headers).await.unwrap();
|
|
|
|
// Authenticate the connection
|
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
|
|
|
|
|
|
|
let mut res = socket.send_message_query("SELECT VALUE id FROM $session").await.unwrap();
|
|
|
|
// The specific header should be used
|
|
|
|
let expected = json!(["00000000-0000-0000-0000-000000000000"]);
|
|
|
|
assert_eq!(res.remove(0)["result"], expected);
|
|
|
|
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
2024-08-26 11:38:57 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_id_invalid() {
|
|
|
|
// Setup database server
|
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
// We specify a request identifier via a specific SurrealDB header
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert("surreal-id", HeaderValue::from_static("123")); // Not a valid UUIDv4
|
|
|
|
// Connect to WebSocket
|
|
|
|
let socket = Socket::connect_with_headers(&addr, SERVER, FORMAT, headers).await;
|
|
|
|
assert!(socket.is_err(), "unexpected success using connecting with invalid id header");
|
|
|
|
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
2024-08-22 16:25:08 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn session_id_undefined() {
|
|
|
|
// Setup database server
|
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
// Connect to WebSocket
|
|
|
|
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
|
|
|
// Authenticate the connection
|
|
|
|
socket.send_message_signin(USER, PASS, None, None, None).await.unwrap();
|
|
|
|
// Specify a namespace and database
|
|
|
|
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
|
|
|
|
|
|
|
let mut res = socket.send_message_query("SELECT VALUE id FROM $session").await.unwrap();
|
|
|
|
// The field is expected to be present even when not provided in the header
|
|
|
|
let unexpected = json!([null]);
|
|
|
|
assert_ne!(res.remove(0)["result"], unexpected);
|
|
|
|
|
|
|
|
// Test passed
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|