2 schema::captcha_answer,
3 source::captcha_answer::CaptchaAnswer,
4 utils::{functions::lower, get_conn, naive_now, DbPool},
15 use diesel_async::RunQueryDsl;
18 pub async fn insert(pool: &DbPool, captcha: &CaptchaAnswer) -> Result<Self, Error> {
19 let conn = &mut get_conn(pool).await?;
21 insert_into(captcha_answer::table)
23 .get_result::<Self>(conn)
27 pub async fn check_captcha(pool: &DbPool, to_check: CaptchaAnswer) -> Result<bool, Error> {
28 let conn = &mut get_conn(pool).await?;
30 // delete any expired captchas
31 delete(captcha_answer::table.filter(captcha_answer::expires.lt(&naive_now())))
35 // fetch requested captcha
36 let captcha_exists = select(exists(
37 captcha_answer::dsl::captcha_answer
38 .filter((captcha_answer::dsl::uuid).eq(to_check.uuid.clone()))
39 .filter(lower(captcha_answer::dsl::answer).eq(to_check.answer.to_lowercase().clone())),
41 .get_result::<bool>(conn)
44 // delete checked captcha
45 delete(captcha_answer::table.filter(captcha_answer::uuid.eq(to_check.uuid.clone())))
56 source::captcha_answer::CaptchaAnswer,
57 utils::{build_db_pool_for_tests, naive_now},
60 use serial_test::serial;
64 async fn test_captcha_happy_path() {
65 let pool = &build_db_pool_for_tests().await;
67 let captcha_a_id = "a".to_string();
69 let _ = CaptchaAnswer::insert(
72 uuid: captcha_a_id.clone(),
73 answer: "XYZ".to_string(),
74 expires: naive_now() + Duration::minutes(10),
79 let result = CaptchaAnswer::check_captcha(
82 uuid: captcha_a_id.clone(),
83 answer: "xyz".to_string(),
84 expires: chrono::NaiveDateTime::MIN,
89 assert!(result.is_ok());
90 assert!(result.unwrap());
95 async fn test_captcha_repeat_answer_fails() {
96 let pool = &build_db_pool_for_tests().await;
98 let captcha_a_id = "a".to_string();
100 let _ = CaptchaAnswer::insert(
103 uuid: captcha_a_id.clone(),
104 answer: "XYZ".to_string(),
105 expires: naive_now() + Duration::minutes(10),
110 let result = CaptchaAnswer::check_captcha(
113 uuid: captcha_a_id.clone(),
114 answer: "xyz".to_string(),
115 expires: chrono::NaiveDateTime::MIN,
120 let result_repeat = CaptchaAnswer::check_captcha(
123 uuid: captcha_a_id.clone(),
124 answer: "xyz".to_string(),
125 expires: chrono::NaiveDateTime::MIN,
130 assert!(result_repeat.is_ok());
131 assert!(!result_repeat.unwrap());
136 async fn test_captcha_expired_fails() {
137 let pool = &build_db_pool_for_tests().await;
139 let expired_id = "already_expired".to_string();
141 let _ = CaptchaAnswer::insert(
144 uuid: expired_id.clone(),
145 answer: "xyz".to_string(),
146 expires: naive_now() - Duration::seconds(1),
151 let expired_result = CaptchaAnswer::check_captcha(
154 uuid: expired_id.clone(),
155 answer: "xyz".to_string(),
156 expires: chrono::NaiveDateTime::MIN,
161 assert!(expired_result.is_ok());
162 assert!(!expired_result.unwrap());