From 106c315d47512c43d9182d50b7e48951d582fc75 Mon Sep 17 00:00:00 2001 From: dull b Date: Fri, 30 Jun 2023 18:55:33 +0000 Subject: [PATCH] Use DbConn for CaptchaAnswer methods --- crates/api/src/local_user/get_captcha.rs | 2 +- crates/api_crud/src/user/create.rs | 2 +- crates/db_schema/src/impls/captcha_answer.rs | 29 ++++++++++---------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/crates/api/src/local_user/get_captcha.rs b/crates/api/src/local_user/get_captcha.rs index 8ee785fcd..0d2237d2e 100644 --- a/crates/api/src/local_user/get_captcha.rs +++ b/crates/api/src/local_user/get_captcha.rs @@ -37,7 +37,7 @@ impl Perform for GetCaptcha { let captcha_form: CaptchaAnswerForm = CaptchaAnswerForm { answer }; // Stores the captcha item in the db - let captcha = CaptchaAnswer::insert(context.pool(), &captcha_form).await?; + let captcha = CaptchaAnswer::insert(&mut *context.conn().await?, &captcha_form).await?; Ok(GetCaptchaResponse { ok: Some(CaptchaResponse { diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 9092376b6..d75a3c72d 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -76,7 +76,7 @@ impl PerformCrud for Register { if let Some(captcha_uuid) = &data.captcha_uuid { let uuid = uuid::Uuid::parse_str(captcha_uuid)?; let check = CaptchaAnswer::check_captcha( - context.pool(), + &mut *context.conn().await?, CheckCaptchaAnswer { uuid, answer: data.captcha_answer.clone().unwrap_or_default(), diff --git a/crates/db_schema/src/impls/captcha_answer.rs b/crates/db_schema/src/impls/captcha_answer.rs index de5fac65e..9500d31e5 100644 --- a/crates/db_schema/src/impls/captcha_answer.rs +++ b/crates/db_schema/src/impls/captcha_answer.rs @@ -1,7 +1,7 @@ use crate::{ schema::captcha_answer::dsl::{answer, captcha_answer, uuid}, source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer}, - utils::{functions::lower, get_conn, DbPool}, + utils::{functions::lower, DbConn}, }; use diesel::{ delete, @@ -15,18 +15,17 @@ use diesel::{ use diesel_async::RunQueryDsl; impl CaptchaAnswer { - pub async fn insert(pool: &DbPool, captcha: &CaptchaAnswerForm) -> Result { - let conn = &mut get_conn(pool).await?; - + pub async fn insert(conn: &mut DbConn, captcha: &CaptchaAnswerForm) -> Result { insert_into(captcha_answer) .values(captcha) .get_result::(conn) .await } - pub async fn check_captcha(pool: &DbPool, to_check: CheckCaptchaAnswer) -> Result { - let conn = &mut get_conn(pool).await?; - + pub async fn check_captcha( + conn: &mut DbConn, + to_check: CheckCaptchaAnswer, + ) -> Result { // fetch requested captcha let captcha_exists = select(exists( captcha_answer @@ -49,17 +48,17 @@ impl CaptchaAnswer { mod tests { use crate::{ source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer}, - utils::build_db_pool_for_tests, + utils::build_db_conn_for_tests, }; use serial_test::serial; #[tokio::test] #[serial] async fn test_captcha_happy_path() { - let pool = &build_db_pool_for_tests().await; + let conn = &mut build_db_conn_for_tests().await; let inserted = CaptchaAnswer::insert( - pool, + conn, &CaptchaAnswerForm { answer: "XYZ".to_string(), }, @@ -68,7 +67,7 @@ mod tests { .expect("should not fail to insert captcha"); let result = CaptchaAnswer::check_captcha( - pool, + conn, CheckCaptchaAnswer { uuid: inserted.uuid, answer: "xyz".to_string(), @@ -83,10 +82,10 @@ mod tests { #[tokio::test] #[serial] async fn test_captcha_repeat_answer_fails() { - let pool = &build_db_pool_for_tests().await; + let conn = &mut build_db_conn_for_tests().await; let inserted = CaptchaAnswer::insert( - pool, + conn, &CaptchaAnswerForm { answer: "XYZ".to_string(), }, @@ -95,7 +94,7 @@ mod tests { .expect("should not fail to insert captcha"); let _result = CaptchaAnswer::check_captcha( - pool, + conn, CheckCaptchaAnswer { uuid: inserted.uuid, answer: "xyz".to_string(), @@ -104,7 +103,7 @@ mod tests { .await; let result_repeat = CaptchaAnswer::check_captcha( - pool, + conn, CheckCaptchaAnswer { uuid: inserted.uuid, answer: "xyz".to_string(),