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 };
// 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 {

View File

@ -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(),

View File

@ -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<Self, Error> {
let conn = &mut get_conn(pool).await?;
pub async fn insert(conn: &mut DbConn, captcha: &CaptchaAnswerForm) -> Result<Self, Error> {
insert_into(captcha_answer)
.values(captcha)
.get_result::<Self>(conn)
.await
}
pub async fn check_captcha(pool: &DbPool, to_check: CheckCaptchaAnswer) -> Result<bool, Error> {
let conn = &mut get_conn(pool).await?;
pub async fn check_captcha(
conn: &mut DbConn,
to_check: CheckCaptchaAnswer,
) -> Result<bool, Error> {
// 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(),