feat: db: pg: adapter to delete verification secret

This commit is contained in:
Aravinth Manivannan 2024-05-18 18:27:52 +05:30
parent 1da8f791b4
commit fbb5435d3c
Signed by: realaravinth
GPG key ID: F8F50389936984FF

View file

@ -0,0 +1,76 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use super::DBOutPostgresAdapter;
use crate::identity::application::port::output::db::{delete_verification_secret::*, errors::*};
#[async_trait::async_trait]
impl DeleteVerificationSecretOutDBPort for DBOutPostgresAdapter {
async fn delete_verification_secret(&self, msg: &DeleteSecretMsg) -> OutDBPortResult<()> {
sqlx::query!(
"DELETE FROM
verification_otp
WHERE
username = $1
AND
purpose = $2
AND
secret = $3;",
msg.username,
REGISTRATION_SECRET_PURPOSE,
msg.secret,
)
.execute(&self.pool)
.await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::identity::application::port::output::db::{
create_verification_secret::*, verification_secret_exists::*,
};
#[actix_rt::test]
async fn test_postgres_delete_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(),
);
let msg = VerifySecretExistsMsgBuilder::default()
.username(username.into())
.secret(secret.into())
.build()
.unwrap();
// state doesn't exist
assert!(!db.verification_secret_exists(&msg).await.unwrap());
let create_msg = CreateSecretMsgBuilder::default()
.secret(secret.into())
.username(username.into())
.build()
.unwrap();
db.create_verification_secret(create_msg).await.unwrap();
// state exists
assert!(db.verification_secret_exists(&msg).await.unwrap());
// delete secret
db.delete_verification_secret(&msg.clone().into())
.await
.unwrap();
// state doens't exist
assert!(!db.verification_secret_exists(&msg).await.unwrap());
settings.drop_db().await;
}
}