Skip to content

Commit

Permalink
Add guard for delete mutation (#163)
Browse files Browse the repository at this point in the history
* Add guard for delete mutation

* Fixup

* More tests

* More tests

* fmt

* Fix tests

* Fix tests

---------

Co-authored-by: Billy Chan <[email protected]>
  • Loading branch information
YiNNx and billy1624 authored Dec 9, 2024
1 parent 4ccb6df commit 5c2f5c5
Show file tree
Hide file tree
Showing 7 changed files with 1,129 additions and 2 deletions.
169 changes: 169 additions & 0 deletions examples/mysql/tests/guard_mutation_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
use std::collections::BTreeMap;

use async_graphql::{dynamic::*, Response};
use sea_orm::{Database, DatabaseConnection};
use seaography::{Builder, BuilderContext, FnGuard, GuardsConfig};
use seaography_mysql_example::entities::*;

lazy_static::lazy_static! {
static ref CONTEXT : BuilderContext = {
let context = BuilderContext::default();
let mut entity_guards: BTreeMap<String, FnGuard> = BTreeMap::new();
entity_guards.insert("FilmCategory".into(), Box::new(|_ctx| {
seaography::GuardAction::Block(None)
}));
let mut field_guards: BTreeMap<String, FnGuard> = BTreeMap::new();
field_guards.insert("Language.name".into(), Box::new(|_ctx| {
seaography::GuardAction::Block(None)
}));
BuilderContext {
guards: GuardsConfig {
entity_guards,
field_guards,
},
..context
}
};
}

pub fn schema(
database: DatabaseConnection,
depth: Option<usize>,
complexity: Option<usize>,
) -> Result<Schema, SchemaError> {
let mut builder = Builder::new(&CONTEXT, database.clone());
seaography::register_entities!(
builder,
[
actor,
address,
category,
city,
country,
customer,
film,
film_actor,
film_category,
film_text,
inventory,
language,
payment,
rental,
staff,
store,
]
);
builder.register_enumeration::<sea_orm_active_enums::Rating>();
let schema = builder.schema_builder();
let schema = if let Some(depth) = depth {
schema.limit_depth(depth)
} else {
schema
};
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).finish()
}

pub async fn get_schema() -> Schema {
let database = Database::connect("mysql://sea:[email protected]/sakila")
.await
.unwrap();
let schema = schema(database, None, None).unwrap();

schema
}

pub fn assert_eq(a: Response, b: &str) {
assert_eq!(
a.data.into_json().unwrap(),
serde_json::from_str::<serde_json::Value>(b).unwrap()
)
}

#[tokio::test]
async fn entity_guard_mutation() {
let schema = get_schema().await;

assert_eq(
schema
.execute(
r#"
mutation LanguageUpdate {
languageUpdate(
data: { lastUpdate: "2030-01-01 11:11:11 UTC" }
filter: { languageId: { eq: 6 } }
) {
languageId
}
}
"#,
)
.await,
r#"
{
"languageUpdate": [
{
"languageId": 6
}
]
}
"#,
);

let response = schema
.execute(
r#"
mutation FilmCategoryUpdate {
filmCategoryUpdate(
data: { filmId: 1, categoryId: 1, lastUpdate: "2030-01-01 11:11:11 UTC" }
) {
filmId
}
}
"#,
)
.await;

assert_eq!(response.errors.len(), 1);

assert_eq!(response.errors[0].message, "Entity guard triggered.");

let response = schema
.execute(
r#"
mutation FilmCategoryDelete {
filmCategoryDelete(filter: { filmId: { eq: 2 } })
}
"#,
)
.await;

assert_eq!(response.errors.len(), 1);

assert_eq!(response.errors[0].message, "Entity guard triggered.");
}

#[tokio::test]
async fn field_guard_mutation() {
let schema = get_schema().await;

let response = schema
.execute(
r#"
mutation LanguageUpdate {
languageUpdate(data: { name: "Cantonese" }, filter: { languageId: { eq: 6 } }) {
languageId
}
}
"#,
)
.await;

assert_eq!(response.errors.len(), 1);

assert_eq!(response.errors[0].message, "Field guard triggered.");
}
Loading

0 comments on commit 5c2f5c5

Please sign in to comment.