Ensure records can be created correctly when using INSERT statement (#2466)

This commit is contained in:
Tobie Morgan Hitchcock 2023-08-20 00:34:41 +01:00 committed by GitHub
parent e9ef8855cf
commit dcc3c4fabb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 2 deletions

View file

@ -17,8 +17,6 @@ impl<'a> Document<'a> {
match self.current.doc.is_some() {
// Run INSERT clause
false => {
// Check if allowed
self.allow(ctx, opt, txn, stm).await?;
// Merge record data
self.merge(ctx, opt, txn, stm).await?;
// Merge fields data

View file

@ -4,6 +4,7 @@ use surrealdb::dbs::Session;
use surrealdb::err::Error;
use surrealdb::iam::Role;
use surrealdb::kvs::Datastore;
use surrealdb::sql::Thing;
use surrealdb::sql::Value;
#[tokio::test]
@ -135,6 +136,64 @@ async fn create_with_id() -> Result<(), Error> {
Ok(())
}
#[tokio::test]
async fn create_or_insert_with_permissions() -> Result<(), Error> {
let sql = "
CREATE user:test;
DEFINE TABLE user SCHEMAFULL PERMISSIONS FULL;
DEFINE TABLE demo SCHEMAFULL PERMISSIONS FOR select, create, update WHERE user = $auth.id;
DEFINE FIELD user ON TABLE demo VALUE $auth.id;
";
let dbs = Datastore::new("memory").await?.with_auth_enabled(true);
let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 4);
//
let tmp = res.remove(0).result;
assert!(tmp.is_ok());
//
let tmp = res.remove(0).result;
assert!(tmp.is_ok());
//
let tmp = res.remove(0).result;
assert!(tmp.is_ok());
//
let tmp = res.remove(0).result;
assert!(tmp.is_ok());
//
let sql = "
CREATE demo SET id = demo:one;
INSERT INTO demo (id) VALUES (demo:two);
";
let ses = Session::for_scope("test", "test", "test", Thing::from(("user", "test")).into());
let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 2);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: demo:one,
user: user:test,
},
]",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: demo:two,
user: user:test,
},
]",
);
assert_eq!(tmp, val);
//
Ok(())
}
#[tokio::test]
async fn create_on_none_values_with_unique_index() -> Result<(), Error> {
let sql = "