// SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later use uuid::Uuid; use super::errors::*; use super::DBOutPostgresAdapter; use crate::identity::application::port::output::db::{errors::*, get_verification_secret::*}; #[async_trait::async_trait] impl GetVerificationSecretOutDBPort for DBOutPostgresAdapter { async fn get_verification_secret(&self, user_id: &Uuid) -> OutDBPortResult { struct Secret { secret: String, } let res = sqlx::query_as!( Secret, "SELECT secret FROM verification_otp WHERE user_id = $1 AND purpose = $2;", user_id, REGISTRATION_SECRET_PURPOSE, ) .fetch_one(&self.pool) .await .map_err(|e| map_row_not_found_err(e, OutDBPortError::VerificationOTPSecretNotFound))?; Ok(res.secret) } } #[cfg(test)] mod tests { use super::*; use crate::{ identity::application::port::output::db::create_verification_secret::*, utils::uuid::tests::UUID, }; #[actix_rt::test] async fn test_postgres_get_verification_secret() { let user_id = UUID; let secret = "bsdasdf"; let settings = crate::settings::tests::get_settings().await; settings.create_db().await; let db = super::DBOutPostgresAdapter::new( sqlx::postgres::PgPool::connect(&settings.database.url) .await .unwrap(), ); assert_eq!( db.get_verification_secret(&user_id).await.err(), Some(OutDBPortError::VerificationOTPSecretNotFound) ); let create_msg = CreateSecretMsgBuilder::default() .secret(secret.into()) .user_id(user_id) .build() .unwrap(); db.create_verification_secret(create_msg).await.unwrap(); assert_eq!(db.get_verification_secret(&user_id).await.unwrap(), secret); settings.drop_db().await; } }