From 78d24af48cb8148d840ea1506554dbdb67f3e5e1 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 23 Feb 2022 08:08:02 +0530 Subject: [PATCH] feat: convert empty emails to None in register form handler SUMMARY Browsers send empty strings for empty fields in form submissions. Email is optional in registration process but empty field fails validation for email and results in 400 bad request. Converting empty strings to None fixes this. --- src/pages/auth/register.rs | 7 ++++++- src/pages/auth/test.rs | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/pages/auth/register.rs b/src/pages/auth/register.rs index 5c51f23..03fa49e 100644 --- a/src/pages/auth/register.rs +++ b/src/pages/auth/register.rs @@ -71,10 +71,15 @@ pub fn services(cfg: &mut web::ServiceConfig) { #[my_codegen::post(path = "PAGES.auth.register")] pub async fn register_submit( - payload: web::Form, + mut payload: web::Form, data: AppData, db: crate::DB, ) -> PageResult { + if let Some(email) = &payload.email { + if email.is_empty() { + payload.email = None; + } + } data.register(&(**db), &payload) .await .map_err(|e| PageError::new(Register::new(&data.settings, Some(&payload)), e))?; diff --git a/src/pages/auth/test.rs b/src/pages/auth/test.rs index a78daac..71e6b58 100644 --- a/src/pages/auth/test.rs +++ b/src/pages/auth/test.rs @@ -67,6 +67,23 @@ async fn auth_works(data: Arc, db: BoxDB) { assert_eq!(headers.get(header::LOCATION).unwrap(), PAGES.auth.login); let _ = data.delete_user(&db, NAME, PASSWORD).await; + // 1. Register with email == "" // Form request handler converts empty emails to None + let msg = Register { + username: NAME.into(), + password: PASSWORD.into(), + confirm_password: PASSWORD.into(), + email: Some("".into()), + }; + let resp = test::call_service( + &app, + post_request!(&msg, PAGES.auth.register, FORM).to_request(), + ) + .await; + assert_eq!(resp.status(), StatusCode::FOUND); + let headers = resp.headers(); + assert_eq!(headers.get(header::LOCATION).unwrap(), PAGES.auth.login); + let _ = data.delete_user(&db, NAME, PASSWORD).await; + // 1. Register with email let msg = Register { username: NAME.into(),