]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/captcha_answer.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / db_schema / src / impls / captcha_answer.rs
index afd18181caf6ab9c90883480678877dba4d30a4e..fe85b78b28015d0385ff0ef1a275d7a6021ef008 100644 (file)
@@ -1,7 +1,7 @@
 use crate::{
-  schema::captcha_answer,
-  source::captcha_answer::CaptchaAnswer,
-  utils::{functions::lower, get_conn, naive_now, DbPool},
+  schema::captcha_answer::dsl::{answer, captcha_answer, uuid},
+  source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer},
+  utils::{functions::lower, get_conn, DbPool},
 };
 use diesel::{
   delete,
@@ -15,34 +15,32 @@ use diesel::{
 use diesel_async::RunQueryDsl;
 
 impl CaptchaAnswer {
-  pub async fn insert(pool: &DbPool, captcha: &CaptchaAnswer) -> Result<Self, Error> {
+  pub async fn insert(pool: &mut DbPool<'_>, captcha: &CaptchaAnswerForm) -> Result<Self, Error> {
     let conn = &mut get_conn(pool).await?;
 
-    insert_into(captcha_answer::table)
+    insert_into(captcha_answer)
       .values(captcha)
       .get_result::<Self>(conn)
       .await
   }
 
-  pub async fn check_captcha(pool: &DbPool, to_check: CaptchaAnswer) -> Result<bool, Error> {
+  pub async fn check_captcha(
+    pool: &mut DbPool<'_>,
+    to_check: CheckCaptchaAnswer,
+  ) -> Result<bool, Error> {
     let conn = &mut get_conn(pool).await?;
 
-    // delete any expired captchas
-    delete(captcha_answer::table.filter(captcha_answer::expires.lt(&naive_now())))
-      .execute(conn)
-      .await?;
-
     // fetch requested captcha
     let captcha_exists = select(exists(
-      captcha_answer::dsl::captcha_answer
-        .filter((captcha_answer::dsl::uuid).eq(to_check.uuid.clone()))
-        .filter(lower(captcha_answer::dsl::answer).eq(to_check.answer.to_lowercase().clone())),
+      captcha_answer
+        .filter((uuid).eq(to_check.uuid))
+        .filter(lower(answer).eq(to_check.answer.to_lowercase().clone())),
     ))
     .get_result::<bool>(conn)
     .await?;
 
     // delete checked captcha
-    delete(captcha_answer::table.filter(captcha_answer::uuid.eq(to_check.uuid.clone())))
+    delete(captcha_answer.filter(uuid.eq(to_check.uuid)))
       .execute(conn)
       .await?;
 
@@ -53,35 +51,31 @@ impl CaptchaAnswer {
 #[cfg(test)]
 mod tests {
   use crate::{
-    source::captcha_answer::CaptchaAnswer,
-    utils::{build_db_pool_for_tests, naive_now},
+    source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer},
+    utils::build_db_pool_for_tests,
   };
-  use chrono::Duration;
   use serial_test::serial;
 
   #[tokio::test]
   #[serial]
   async fn test_captcha_happy_path() {
     let pool = &build_db_pool_for_tests().await;
+    let pool = &mut pool.into();
 
-    let captcha_a_id = "a".to_string();
-
-    let _ = CaptchaAnswer::insert(
+    let inserted = CaptchaAnswer::insert(
       pool,
-      &CaptchaAnswer {
-        uuid: captcha_a_id.clone(),
+      &CaptchaAnswerForm {
         answer: "XYZ".to_string(),
-        expires: naive_now() + Duration::minutes(10),
       },
     )
-    .await;
+    .await
+    .expect("should not fail to insert captcha");
 
     let result = CaptchaAnswer::check_captcha(
       pool,
-      CaptchaAnswer {
-        uuid: captcha_a_id.clone(),
+      CheckCaptchaAnswer {
+        uuid: inserted.uuid,
         answer: "xyz".to_string(),
-        expires: chrono::NaiveDateTime::MIN,
       },
     )
     .await;
@@ -94,35 +88,31 @@ mod tests {
   #[serial]
   async fn test_captcha_repeat_answer_fails() {
     let pool = &build_db_pool_for_tests().await;
+    let pool = &mut pool.into();
 
-    let captcha_a_id = "a".to_string();
-
-    let _ = CaptchaAnswer::insert(
+    let inserted = CaptchaAnswer::insert(
       pool,
-      &CaptchaAnswer {
-        uuid: captcha_a_id.clone(),
+      &CaptchaAnswerForm {
         answer: "XYZ".to_string(),
-        expires: naive_now() + Duration::minutes(10),
       },
     )
-    .await;
+    .await
+    .expect("should not fail to insert captcha");
 
-    let result = CaptchaAnswer::check_captcha(
+    let _result = CaptchaAnswer::check_captcha(
       pool,
-      CaptchaAnswer {
-        uuid: captcha_a_id.clone(),
+      CheckCaptchaAnswer {
+        uuid: inserted.uuid,
         answer: "xyz".to_string(),
-        expires: chrono::NaiveDateTime::MIN,
       },
     )
     .await;
 
     let result_repeat = CaptchaAnswer::check_captcha(
       pool,
-      CaptchaAnswer {
-        uuid: captcha_a_id.clone(),
+      CheckCaptchaAnswer {
+        uuid: inserted.uuid,
         answer: "xyz".to_string(),
-        expires: chrono::NaiveDateTime::MIN,
       },
     )
     .await;
@@ -130,35 +120,4 @@ mod tests {
     assert!(result_repeat.is_ok());
     assert!(!result_repeat.unwrap());
   }
-
-  #[tokio::test]
-  #[serial]
-  async fn test_captcha_expired_fails() {
-    let pool = &build_db_pool_for_tests().await;
-
-    let expired_id = "already_expired".to_string();
-
-    let _ = CaptchaAnswer::insert(
-      pool,
-      &CaptchaAnswer {
-        uuid: expired_id.clone(),
-        answer: "xyz".to_string(),
-        expires: naive_now() - Duration::seconds(1),
-      },
-    )
-    .await;
-
-    let expired_result = CaptchaAnswer::check_captcha(
-      pool,
-      CaptchaAnswer {
-        uuid: expired_id.clone(),
-        answer: "xyz".to_string(),
-        expires: chrono::NaiveDateTime::MIN,
-      },
-    )
-    .await;
-
-    assert!(expired_result.is_ok());
-    assert!(!expired_result.unwrap());
-  }
 }