feat: define UpdateOAuthAccessToken for out db port and implement it for pg

This commit is contained in:
Aravinth Manivannan 2024-05-12 18:51:10 +05:30
parent f65aa6262d
commit 1621530faa
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 136 additions and 0 deletions

View file

@ -7,9 +7,11 @@ use crate::db::{migrate::RunMigrations, sqlx_postgres::Postgres};
mod delete_oauth_state;
mod errors;
mod get_oauth_access_token;
mod oauth_state_exists;
mod save_oauth_access_token;
mod save_oauth_state;
mod update_oauth_access_token;
#[derive(Clone)]
pub struct DBOutPostgresAdapter {

View file

@ -0,0 +1,95 @@
use super::DBOutPostgresAdapter;
use crate::auth::application::port::out::db::{
errors::OutDBPortResult, update_oauth_access_token::UpdateOAuthAccessToken,
};
use crate::auth::domain::OAuthAccessToken;
#[async_trait::async_trait]
impl UpdateOAuthAccessToken for DBOutPostgresAdapter {
async fn update_oauth_access_token(
&self,
old_oauth_access_token: &OAuthAccessToken,
new_oauth_access_token: &OAuthAccessToken,
) -> OutDBPortResult<()> {
sqlx::query!(
"
UPDATE
oauth_access_token
SET
access_token = $1,
token_type = $2,
expires_in = $3,
refresh_token = $4,
created_at = $5
WHERE
access_token = $6
AND
token_type = $7
AND
expires_in = $8
AND
refresh_token = $9;",
&new_oauth_access_token.access_token,
&new_oauth_access_token.token_type,
new_oauth_access_token.expires_in as i64,
&new_oauth_access_token.refresh_token,
&new_oauth_access_token.created_at,
&old_oauth_access_token.access_token,
&old_oauth_access_token.token_type,
old_oauth_access_token.expires_in as i64,
&old_oauth_access_token.refresh_token,
)
.execute(&self.pool)
.await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::auth::application::port::out::db::{
get_oauth_access_token::GetOAuthAccessToken, save_oauth_access_token::SaveOAuthAccessToken,
};
#[actix_rt::test]
async fn test_postgres_update_oauth_access_token() {
let oauth_provider = "oauthprovitestpostgres";
let username = "test_postgres_save_oauth_access_token";
let old_access_token = OAuthAccessToken {
refresh_token: "asdfasdfarefresh".into(),
..Default::default()
};
let settings = crate::settings::tests::get_settings().await;
let db = super::DBOutPostgresAdapter::new(
sqlx::postgres::PgPool::connect(&settings.database.url)
.await
.unwrap(),
);
db.save_oauth_access_token(username, oauth_provider, &old_access_token)
.await
.unwrap();
let new_access_token = OAuthAccessToken {
refresh_token: "newrefreshtoken".into(),
access_token: "newreaccesstoken".into(),
..Default::default()
};
db.update_oauth_access_token(&old_access_token, &new_access_token)
.await
.unwrap();
assert_eq!(
db.get_oauth_access_token(username, oauth_provider)
.await
.unwrap(),
new_access_token
);
settings.drop_db().await;
}
}

View file

@ -1,5 +1,7 @@
pub mod delete_oauth_state;
pub mod errors;
pub mod get_oauth_access_token;
pub mod oauth_state_exists;
pub mod save_oauth_access_token;
pub mod save_oauth_state;
pub mod update_oauth_access_token;

View file

@ -0,0 +1,37 @@
use mockall::predicate::*;
use mockall::*;
use super::errors::*;
use crate::auth::domain::OAuthAccessToken;
#[automock]
#[async_trait::async_trait]
pub trait UpdateOAuthAccessToken: Send + Sync {
async fn update_oauth_access_token(
&self,
old_oauth_access_token: &OAuthAccessToken,
new_oauth_access_token: &OAuthAccessToken,
) -> OutDBPortResult<()>;
}
#[cfg(test)]
pub mod tests {
use super::*;
use std::sync::Arc;
pub fn mock_update_oauth_access_token(times: Option<usize>) -> Arc<dyn UpdateOAuthAccessToken> {
let mut m = MockUpdateOAuthAccessToken::default();
if let Some(times) = times {
m.expect_update_oauth_access_token()
.times(times)
.returning(move |_, _| Ok(()));
} else {
m.expect_update_oauth_access_token()
.returning(move |_, _| Ok(()));
}
Arc::new(m)
}
}