add tests to confirm password validation

This commit is contained in:
bitfl0wer 2023-04-16 12:26:12 +02:00
parent 40425fc282
commit 062faf8c07
No known key found for this signature in database
GPG Key ID: 84BBB60DF895ABF2
1 changed files with 54 additions and 3 deletions

View File

@ -4,7 +4,7 @@ pub mod schemas {
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub struct RegisterSchema { pub struct RegisterSchema {
username: String, username: String,
@ -19,9 +19,9 @@ pub mod schemas {
promotional_email_opt_in: Option<bool>, promotional_email_opt_in: Option<bool>,
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub struct RegisterSchemaError { pub struct RegisterSchemaError {
message: String, pub message: String,
} }
impl RegisterSchemaError { impl RegisterSchemaError {
@ -110,3 +110,54 @@ pub mod schemas {
login_source: Option<String>, login_source: Option<String>,
} }
} }
#[cfg(test)]
mod schemas_tests {
use super::schemas::*;
#[test]
fn password_too_short() {
assert_eq!(
RegisterSchema::new(
"Test".to_string(),
Some("".to_string()),
true,
None,
None,
None,
None,
None,
None,
None,
),
Err(RegisterSchemaError {
message: "Password must be between 1 and 72 characters.".to_string()
})
);
}
#[test]
fn password_too_long() {
let mut long_pw = String::new();
for _ in 0..73 {
long_pw = long_pw + "a";
}
assert_eq!(
RegisterSchema::new(
"Test".to_string(),
Some(long_pw),
true,
None,
None,
None,
None,
None,
None,
None,
),
Err(RegisterSchemaError {
message: "Password must be between 1 and 72 characters.".to_string()
})
);
}
}