feat: db port&pg adapter: get verification otp

This commit is contained in:
Aravinth Manivannan 2024-05-18 19:01:29 +05:30
parent 1565e56737
commit 1be035be98
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 112 additions and 0 deletions

View file

@ -0,0 +1,67 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
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, username: &str) -> OutDBPortResult<String> {
struct Secret {
secret: String,
}
let res = sqlx::query_as!(
Secret,
"SELECT
secret
FROM
verification_otp
WHERE
username = $1
AND
purpose = $2;",
username,
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::*;
#[actix_rt::test]
async fn test_postgres_get_verification_secret() {
let username = "batman";
let secret = "bsdasdf";
let settings = crate::settings::tests::get_settings().await;
let db = super::DBOutPostgresAdapter::new(
sqlx::postgres::PgPool::connect(&settings.database.url)
.await
.unwrap(),
);
assert_eq!(
db.get_verification_secret(username).await.err(),
Some(OutDBPortError::VerificationOTPSecretNotFound)
);
let create_msg = CreateSecretMsgBuilder::default()
.secret(secret.into())
.username(username.into())
.build()
.unwrap();
db.create_verification_secret(create_msg).await.unwrap();
assert_eq!(db.get_verification_secret(username).await.unwrap(), secret);
settings.drop_db().await;
}
}

View file

@ -13,6 +13,7 @@ pub mod create_verification_secret;
pub mod delete_verification_secret;
pub mod email_exists;
mod errors;
pub mod get_verification_secret;
pub mod username_exists;
pub mod verification_secret_exists;

View file

@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use mockall::predicate::*;
use mockall::*;
pub use super::create_verification_secret::REGISTRATION_SECRET_PURPOSE;
use super::errors::*;
#[cfg(test)]
#[allow(unused_imports)]
pub use tests::*;
#[automock]
#[async_trait::async_trait]
pub trait GetVerificationSecretOutDBPort: Send + Sync {
async fn get_verification_secret(&self, username: &str) -> OutDBPortResult<String>;
}
pub type GetVerificationSecretOutDBPortObj = std::sync::Arc<dyn GetVerificationSecretOutDBPort>;
#[cfg(test)]
pub mod tests {
use super::*;
use std::sync::Arc;
pub fn mock_get_verification_secret_db_port(
times: Option<usize>,
returning_secret: String,
) -> GetVerificationSecretOutDBPortObj {
let mut m = MockGetVerificationSecretOutDBPort::new();
if let Some(times) = times {
m.expect_get_verification_secret()
.times(times)
.returning(move |_| Ok(returning_secret.clone()));
} else {
m.expect_get_verification_secret()
.returning(move |_| Ok(returning_secret.clone()));
}
Arc::new(m)
}
}