From 440a88ad2785fe7f38ee8c9a34dcded479be6ce3 Mon Sep 17 00:00:00 2001 From: Bonex Date: Fri, 11 Nov 2022 15:26:29 +0100 Subject: [PATCH] Add failing form validation test cases. --- core/codegen/tests/from_form.rs | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/core/codegen/tests/from_form.rs b/core/codegen/tests/from_form.rs index fc26a558..c2ddf876 100644 --- a/core/codegen/tests/from_form.rs +++ b/core/codegen/tests/from_form.rs @@ -409,6 +409,50 @@ fn form_errors() { })); } +#[test] +fn form_error_return_wrong_field_name() { + fn evaluate_other<'v>(_other: &String, _check: &bool) -> form::Result<'v, ()> { + Err(form::Error::validation(""))? + } + + #[derive(Debug, PartialEq, FromForm)] + struct WhoopsForm { + name: String, + check: bool, + // in the error context this is returned as "name" but should be "other" + // the problem is dependednt on an argument exsiting for evaluate_other + #[field(validate = evaluate_other(&self.check))] + other: String, + } + + let errors = strict::("name=test&check=true&other=").unwrap_err(); + assert!(errors.iter().any(|e| {e.name.as_ref().unwrap() == "other"})); +} + +#[test] +fn form_validate_missing_error() { + fn evaluate<'v>(_value: &String) -> form::Result<'v, ()> { + Err(form::Error::validation(""))? + } + + fn evaluate_with_argument<'v>(_value: &String, _check: &bool) -> form::Result<'v, ()> { + Err(form::Error::validation("lastname failed"))? + } + + #[derive(Debug, PartialEq, FromForm)] + struct WhoopsForm { + #[field(validate = evaluate())] + firstname: String, + check: bool, + // this validator is hardcoded to return an error but it doesnt + #[field(validate = evaluate_with_argument(&self.check))] + lastname: String, + } + + let errors = strict::("firstname=&check=true&lastname=").unwrap_err(); + assert!(errors.iter().any(|e| {e.name.as_ref().unwrap() == "lastname"})); +} + #[test] fn raw_ident_form() { #[derive(Debug, PartialEq, FromForm)]