Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

platform: mock test cache repository #40

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/domain/model/token.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};

//TODO: add getters and setters
#[derive(Debug, Serialize, Deserialize)]
pub struct TokenClaims {
pub sub: String,
Expand Down
112 changes: 112 additions & 0 deletions src/repositories/test_helpers/mock_cache_repository.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#[cfg(test)]
pub mod test_helpers {
use std::{mem, ops::DerefMut, sync::Arc};

use anyhow::anyhow;
use tokio::sync::Mutex;

use crate::domain::{
model::{cache_errors::CacheOperationError, token::TokenDetails, token_uuid::TokenUuid},
repositories::cache_repository::CacheRepository,
};

pub struct MockCacheRepository {
pub save_token_data_result: Arc<Mutex<Result<(), CacheOperationError>>>,
pub verify_active_session_result: Arc<Mutex<Result<(), CacheOperationError>>>,
pub delete_token_result: Arc<Mutex<Result<(), CacheOperationError>>>,
}

impl CacheRepository for MockCacheRepository {
async fn save_token_data(
&self,
_token_details: &TokenDetails,
_max_age: i64,
) -> Result<(), CacheOperationError> {
let mut guard = self.save_token_data_result.lock().await;
let mut result = Err(CacheOperationError::Unknown(anyhow!("substitute error")));
mem::swap(guard.deref_mut(), &mut result);
result
}

async fn verify_active_session(
&self,
_token_details: &TokenDetails,
) -> Result<(), CacheOperationError> {
let mut guard = self.verify_active_session_result.lock().await;
let mut result = Err(CacheOperationError::Unknown(anyhow!("substitute error")));
mem::swap(guard.deref_mut(), &mut result);
result
}

async fn delete_token(&self, _token_uuid: &TokenUuid) -> Result<(), CacheOperationError> {
let mut guard = self.delete_token_result.lock().await;
let mut result = Err(CacheOperationError::Unknown(anyhow!("substitute error")));
mem::swap(guard.deref_mut(), &mut result);
result
}
}

#[tokio::test]
async fn test_cache_repository_success_cases() {
let uuid = uuid::Uuid::new_v4();
let token = TokenDetails {
token: None,
token_uuid: uuid,
user_id: uuid,
expires_in: None,
};
let save_token_data_result = Arc::new(Mutex::new(Ok(())));
let verify_active_session_result = Arc::new(Mutex::new(Ok(())));
let delete_token_result = Arc::new(Mutex::new(Ok(())));

let mock_repo = MockCacheRepository {
save_token_data_result,
verify_active_session_result,
delete_token_result,
};

let result = mock_repo.save_token_data(&token, 10).await;
assert!(result.is_ok());

let result = mock_repo.verify_active_session(&token).await;
assert!(result.is_ok());

let result = mock_repo.delete_token(&TokenUuid::new(uuid)).await;
assert!(result.is_ok());
}

#[tokio::test]
async fn test_cache_repository_failure_cases() {
let uuid = uuid::Uuid::new_v4();
let token = TokenDetails {
token: None,
token_uuid: uuid,
user_id: uuid,
expires_in: None,
};
let save_token_data_result = Arc::new(Mutex::new(Err(CacheOperationError::Unknown(
anyhow!("save token data result error"),
))));
let verify_active_session_result = Arc::new(Mutex::new(Err(CacheOperationError::Unknown(
anyhow!("verify active session result error"),
))));
let delete_token_result = Arc::new(Mutex::new(Err(CacheOperationError::Unknown(anyhow!(
"delete token result error"
)))));

let mock_repo = MockCacheRepository {
save_token_data_result,
verify_active_session_result,
delete_token_result,
};

let result = mock_repo.save_token_data(&token, 10).await;
assert!(result.is_err());

let result = mock_repo.verify_active_session(&token).await;
assert!(result.is_err());

let result = mock_repo.delete_token(&TokenUuid::new(uuid)).await;
assert!(result.is_err());
}
}
Loading