vanikam/src/identity/adapters/output/db/postgres/create_verification_secret.rs

57 lines
1.7 KiB
Rust

// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use sqlx::types::time::OffsetDateTime;
use super::DBOutPostgresAdapter;
use crate::identity::application::port::output::db::{create_verification_secret::*, errors::*};
#[async_trait::async_trait]
impl CreateVerificationSecretOutDBPort for DBOutPostgresAdapter {
async fn create_verification_secret(&self, msg: CreateSecretMsg) -> OutDBPortResult<()> {
sqlx::query!(
"INSERT INTO verification_otp (secret, created_at, purpose, username)
VALUES ($1, $2, $3, $4);",
&msg.secret,
OffsetDateTime::now_utc(),
REGISTRATION_SECRET_PURPOSE,
&msg.username,
)
.execute(&self.pool)
.await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[actix_rt::test]
async fn test_postgres_create_verification_secret() {
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(),
);
let msg = CreateSecretMsgBuilder::default()
.secret("secret".into())
.username("username".into())
.build()
.unwrap();
db.create_verification_secret(msg.clone()).await.unwrap();
// duplicate: secret exists
assert_eq!(
db.create_verification_secret(msg).await.err(),
Some(OutDBPortError::DuplicateVerificationOTPSecret)
);
settings.drop_db().await;
}
}