Use DbConn for CaptchaAnswer methods

This commit is contained in:
dull b 2023-06-30 18:55:33 +00:00
parent cc1a725c8a
commit 106c315d47
3 changed files with 16 additions and 17 deletions

View file

@ -37,7 +37,7 @@ impl Perform for GetCaptcha {
let captcha_form: CaptchaAnswerForm = CaptchaAnswerForm { answer }; let captcha_form: CaptchaAnswerForm = CaptchaAnswerForm { answer };
// Stores the captcha item in the db // 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(GetCaptchaResponse {
ok: Some(CaptchaResponse { ok: Some(CaptchaResponse {

View file

@ -76,7 +76,7 @@ impl PerformCrud for Register {
if let Some(captcha_uuid) = &data.captcha_uuid { if let Some(captcha_uuid) = &data.captcha_uuid {
let uuid = uuid::Uuid::parse_str(captcha_uuid)?; let uuid = uuid::Uuid::parse_str(captcha_uuid)?;
let check = CaptchaAnswer::check_captcha( let check = CaptchaAnswer::check_captcha(
context.pool(), &mut *context.conn().await?,
CheckCaptchaAnswer { CheckCaptchaAnswer {
uuid, uuid,
answer: data.captcha_answer.clone().unwrap_or_default(), answer: data.captcha_answer.clone().unwrap_or_default(),

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
schema::captcha_answer::dsl::{answer, captcha_answer, uuid}, schema::captcha_answer::dsl::{answer, captcha_answer, uuid},
source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer}, source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer},
utils::{functions::lower, get_conn, DbPool}, utils::{functions::lower, DbConn},
}; };
use diesel::{ use diesel::{
delete, delete,
@ -15,18 +15,17 @@ use diesel::{
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
impl CaptchaAnswer { impl CaptchaAnswer {
pub async fn insert(pool: &DbPool, captcha: &CaptchaAnswerForm) -> Result<Self, Error> { pub async fn insert(conn: &mut DbConn, captcha: &CaptchaAnswerForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(captcha_answer) insert_into(captcha_answer)
.values(captcha) .values(captcha)
.get_result::<Self>(conn) .get_result::<Self>(conn)
.await .await
} }
pub async fn check_captcha(pool: &DbPool, to_check: CheckCaptchaAnswer) -> Result<bool, Error> { pub async fn check_captcha(
let conn = &mut get_conn(pool).await?; conn: &mut DbConn,
to_check: CheckCaptchaAnswer,
) -> Result<bool, Error> {
// fetch requested captcha // fetch requested captcha
let captcha_exists = select(exists( let captcha_exists = select(exists(
captcha_answer captcha_answer
@ -49,17 +48,17 @@ impl CaptchaAnswer {
mod tests { mod tests {
use crate::{ use crate::{
source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer}, source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer},
utils::build_db_pool_for_tests, utils::build_db_conn_for_tests,
}; };
use serial_test::serial; use serial_test::serial;
#[tokio::test] #[tokio::test]
#[serial] #[serial]
async fn test_captcha_happy_path() { 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( let inserted = CaptchaAnswer::insert(
pool, conn,
&CaptchaAnswerForm { &CaptchaAnswerForm {
answer: "XYZ".to_string(), answer: "XYZ".to_string(),
}, },
@ -68,7 +67,7 @@ mod tests {
.expect("should not fail to insert captcha"); .expect("should not fail to insert captcha");
let result = CaptchaAnswer::check_captcha( let result = CaptchaAnswer::check_captcha(
pool, conn,
CheckCaptchaAnswer { CheckCaptchaAnswer {
uuid: inserted.uuid, uuid: inserted.uuid,
answer: "xyz".to_string(), answer: "xyz".to_string(),
@ -83,10 +82,10 @@ mod tests {
#[tokio::test] #[tokio::test]
#[serial] #[serial]
async fn test_captcha_repeat_answer_fails() { 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( let inserted = CaptchaAnswer::insert(
pool, conn,
&CaptchaAnswerForm { &CaptchaAnswerForm {
answer: "XYZ".to_string(), answer: "XYZ".to_string(),
}, },
@ -95,7 +94,7 @@ mod tests {
.expect("should not fail to insert captcha"); .expect("should not fail to insert captcha");
let _result = CaptchaAnswer::check_captcha( let _result = CaptchaAnswer::check_captcha(
pool, conn,
CheckCaptchaAnswer { CheckCaptchaAnswer {
uuid: inserted.uuid, uuid: inserted.uuid,
answer: "xyz".to_string(), answer: "xyz".to_string(),
@ -104,7 +103,7 @@ mod tests {
.await; .await;
let result_repeat = CaptchaAnswer::check_captcha( let result_repeat = CaptchaAnswer::check_captcha(
pool, conn,
CheckCaptchaAnswer { CheckCaptchaAnswer {
uuid: inserted.uuid, uuid: inserted.uuid,
answer: "xyz".to_string(), answer: "xyz".to_string(),