From d8ecd274ffff335b6723298f95b5f4a5e7e88a6d Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 16 Oct 2020 07:44:21 +0500 Subject: [PATCH] Confirm password --- src/forms/user.rs | 2 ++ src/tests/forms.rs | 18 ++++++++++++++++++ src/tests/requests.rs | 2 +- templates/users/new.html.hbs | 5 +++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/forms/user.rs b/src/forms/user.rs index 1bcd64c..4aa9908 100644 --- a/src/forms/user.rs +++ b/src/forms/user.rs @@ -12,6 +12,8 @@ pub struct UserSignUp { pub username: String, #[validate(length(min = 8, max = 128), custom = "validate_password")] pub password: String, + #[validate(must_match = "password")] + pub password_confirmation: String, } fn validate_username(value: &String) -> Result<(), ValidationError> { diff --git a/src/tests/forms.rs b/src/tests/forms.rs index 111d25e..6db331c 100644 --- a/src/tests/forms.rs +++ b/src/tests/forms.rs @@ -9,6 +9,7 @@ mod forms { let form = forms::UserSignUp { username: "kotovalexarian".to_string(), password: "q1w2e3r4t5y6".to_string(), + password_confirmation: "q1w2e3r4t5y6".to_string(), }; assert!(matches!(form.validate(), Ok(_))); @@ -19,6 +20,7 @@ mod forms { let form = forms::UserSignUp { username: "".to_string(), password: "q1w2e3r4t5y6".to_string(), + password_confirmation: "q1w2e3r4t5y6".to_string(), }; assert!(matches!(form.validate(), Err(_))); @@ -29,6 +31,7 @@ mod forms { let form = forms::UserSignUp { username: " ".to_string(), password: "q1w2e3r4t5y6".to_string(), + password_confirmation: "q1w2e3r4t5y6".to_string(), }; assert!(matches!(form.validate(), Err(_))); @@ -39,6 +42,7 @@ mod forms { let form = forms::UserSignUp { username: "foo".to_string(), password: "q1w2e3r4t5y6".to_string(), + password_confirmation: "q1w2e3r4t5y6".to_string(), }; assert!(matches!(form.validate(), Err(_))); @@ -49,6 +53,7 @@ mod forms { let form = forms::UserSignUp { username: "kotovalexarian".to_string(), password: "".to_string(), + password_confirmation: "".to_string(), }; assert!(matches!(form.validate(), Err(_))); @@ -59,6 +64,7 @@ mod forms { let form = forms::UserSignUp { username: "kotovalexarian".to_string(), password: " ".to_string(), + password_confirmation: " ".to_string(), }; assert!(matches!(form.validate(), Err(_))); @@ -69,6 +75,18 @@ mod forms { let form = forms::UserSignUp { username: "kotovalexarian".to_string(), password: "1234567".to_string(), + password_confirmation: "1234567".to_string(), + }; + + assert!(matches!(form.validate(), Err(_))); + } + + #[test] + fn user_sign_up_with_invalid_password_confirmation() { + let form = forms::UserSignUp { + username: "kotovalexarian".to_string(), + password: "q1w2e3r4t5y6".to_string(), + password_confirmation: "q1w2e3r4t5y6aaa".to_string(), }; assert!(matches!(form.validate(), Err(_))); diff --git a/src/tests/requests.rs b/src/tests/requests.rs index 0ae535d..8707d78 100644 --- a/src/tests/requests.rs +++ b/src/tests/requests.rs @@ -8,7 +8,7 @@ mod requests { fn client() -> Client { let config = config::Config::default().unwrap(); - let rocket = web::rocket(config); + let rocket = web::rocket(config).unwrap(); Client::new(rocket).unwrap() } diff --git a/templates/users/new.html.hbs b/templates/users/new.html.hbs index 760fc43..9fbf57c 100644 --- a/templates/users/new.html.hbs +++ b/templates/users/new.html.hbs @@ -13,6 +13,11 @@ +
+ + +
+