feat(db): implement EphemeralKeyInterface for MockDb (#1285)

This commit is contained in:
Karthik
2023-06-25 10:58:36 +05:30
committed by GitHub
parent 9cc14b8044
commit 8c93904c3e
3 changed files with 46 additions and 10 deletions

View File

@ -108,6 +108,7 @@ pub struct MockDb {
connector_response: Arc<Mutex<Vec<storage::ConnectorResponse>>>, connector_response: Arc<Mutex<Vec<storage::ConnectorResponse>>>,
redis: Arc<redis_interface::RedisConnectionPool>, redis: Arc<redis_interface::RedisConnectionPool>,
api_keys: Arc<Mutex<Vec<storage::ApiKey>>>, api_keys: Arc<Mutex<Vec<storage::ApiKey>>>,
ephemeral_keys: Arc<Mutex<Vec<storage::EphemeralKey>>>,
cards_info: Arc<Mutex<Vec<storage::CardInfo>>>, cards_info: Arc<Mutex<Vec<storage::CardInfo>>>,
events: Arc<Mutex<Vec<storage::Event>>>, events: Arc<Mutex<Vec<storage::Event>>>,
disputes: Arc<Mutex<Vec<storage::Dispute>>>, disputes: Arc<Mutex<Vec<storage::Dispute>>>,
@ -129,6 +130,7 @@ impl MockDb {
connector_response: Default::default(), connector_response: Default::default(),
redis: Arc::new(crate::connection::redis_connection(redis).await), redis: Arc::new(crate::connection::redis_connection(redis).await),
api_keys: Default::default(), api_keys: Default::default(),
ephemeral_keys: Default::default(),
cards_info: Default::default(), cards_info: Default::default(),
events: Default::default(), events: Default::default(),
disputes: Default::default(), disputes: Default::default(),

View File

@ -1,3 +1,5 @@
use time::ext::NumericalDuration;
use crate::{ use crate::{
core::errors::{self, CustomResult}, core::errors::{self, CustomResult},
db::MockDb, db::MockDb,
@ -125,21 +127,53 @@ mod storage {
impl EphemeralKeyInterface for MockDb { impl EphemeralKeyInterface for MockDb {
async fn create_ephemeral_key( async fn create_ephemeral_key(
&self, &self,
_ek: EphemeralKeyNew, ek: EphemeralKeyNew,
_validity: i64, validity: i64,
) -> CustomResult<EphemeralKey, errors::StorageError> { ) -> CustomResult<EphemeralKey, errors::StorageError> {
Err(errors::StorageError::KVError.into()) let mut ephemeral_keys = self.ephemeral_keys.lock().await;
let created_at = common_utils::date_time::now();
let expires = created_at.saturating_add(validity.hours());
let ephemeral_key = EphemeralKey {
id: ek.id,
merchant_id: ek.merchant_id,
customer_id: ek.customer_id,
created_at: created_at.assume_utc().unix_timestamp(),
expires: expires.assume_utc().unix_timestamp(),
secret: ek.secret,
};
ephemeral_keys.push(ephemeral_key.clone());
Ok(ephemeral_key)
} }
async fn get_ephemeral_key( async fn get_ephemeral_key(
&self, &self,
_key: &str, key: &str,
) -> CustomResult<EphemeralKey, errors::StorageError> { ) -> CustomResult<EphemeralKey, errors::StorageError> {
Err(errors::StorageError::KVError.into()) match self
.ephemeral_keys
.lock()
.await
.iter()
.find(|ephemeral_key| ephemeral_key.secret.eq(key))
{
Some(ephemeral_key) => Ok(ephemeral_key.clone()),
None => Err(
errors::StorageError::ValueNotFound("ephemeral key not found".to_string()).into(),
),
}
} }
async fn delete_ephemeral_key( async fn delete_ephemeral_key(
&self, &self,
_id: &str, id: &str,
) -> CustomResult<EphemeralKey, errors::StorageError> { ) -> CustomResult<EphemeralKey, errors::StorageError> {
Err(errors::StorageError::KVError.into()) let mut ephemeral_keys = self.ephemeral_keys.lock().await;
if let Some(pos) = ephemeral_keys.iter().position(|x| (*x.id).eq(id)) {
let ek = ephemeral_keys.remove(pos);
Ok(ek)
} else {
return Err(
errors::StorageError::ValueNotFound("ephemeral key not found".to_string()).into(),
);
}
} }
} }

View File

@ -27,7 +27,7 @@ pub mod kv;
pub use self::{ pub use self::{
address::*, api_keys::*, cards_info::*, configs::*, connector_response::*, customers::*, address::*, api_keys::*, cards_info::*, configs::*, connector_response::*, customers::*,
dispute::*, events::*, file::*, locker_mock_up::*, mandate::*, merchant_account::*, dispute::*, ephemeral_key::*, events::*, file::*, locker_mock_up::*, mandate::*,
merchant_connector_account::*, payment_attempt::*, payment_intent::*, payment_method::*, merchant_account::*, merchant_connector_account::*, payment_attempt::*, payment_intent::*,
process_tracker::*, refund::*, reverse_lookup::*, payment_method::*, process_tracker::*, refund::*, reverse_lookup::*,
}; };