Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guard for delete mutation #163

Merged
merged 8 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading