Bug: [FEATURE] Implement `AuthorizationInterface` for `MockDb`



Spin off from https://github.com/juspay/hyperswitch/issues/172. Refer to the parent issue for more information
diff --git a/crates/diesel_models/src/authorization.rs b/crates/diesel_models/src/authorization.rs
index 64fd1c65187..b6f75bbb9b7 100644
--- a/crates/diesel_models/src/authorization.rs
+++ b/crates/diesel_models/src/authorization.rs
@@ -57,6 +57,21 @@ pub struct AuthorizationUpdateInternal {
     pub connector_authorization_id: Option<String>,
 }
 
+impl AuthorizationUpdateInternal {
+    pub fn create_authorization(self, source: Authorization) -> Authorization {
+        Authorization {
+            status: self.status.unwrap_or(source.status),
+            error_code: self.error_code.or(source.error_code),
+            error_message: self.error_message.or(source.error_message),
+            modified_at: self.modified_at.unwrap_or(common_utils::date_time::now()),
+            connector_authorization_id: self
+                .connector_authorization_id
+                .or(source.connector_authorization_id),
+            ..source
+        }
+    }
+}
+
 impl From<AuthorizationUpdate> for AuthorizationUpdateInternal {
     fn from(authorization_child_update: AuthorizationUpdate) -> Self {
         let now = Some(common_utils::date_time::now());
diff --git a/crates/router/src/db/authorization.rs b/crates/router/src/db/authorization.rs
index f24daaf718a..d167d177537 100644
--- a/crates/router/src/db/authorization.rs
+++ b/crates/router/src/db/authorization.rs
@@ -1,3 +1,4 @@
+use diesel_models::authorization::AuthorizationUpdateInternal;
 use error_stack::IntoReport;
 
 use super::{MockDb, Store};
@@ -77,28 +78,71 @@ impl AuthorizationInterface for Store {
 impl AuthorizationInterface for MockDb {
     async fn insert_authorization(
         &self,
-        _authorization: storage::AuthorizationNew,
+        authorization: storage::AuthorizationNew,
     ) -> CustomResult<storage::Authorization, errors::StorageError> {
-        // TODO: Implement function for `MockDb`
-        Err(errors::StorageError::MockDbError)?
+        let mut authorizations = self.authorizations.lock().await;
+        if authorizations.iter().any(|authorization_inner| {
+            authorization_inner.authorization_id == authorization.authorization_id
+        }) {
+            Err(errors::StorageError::DuplicateValue {
+                entity: "authorization_id",
+                key: None,
+            })?
+        }
+        let authorization = storage::Authorization {
+            authorization_id: authorization.authorization_id,
+            merchant_id: authorization.merchant_id,
+            payment_id: authorization.payment_id,
+            amount: authorization.amount,
+            created_at: common_utils::date_time::now(),
+            modified_at: common_utils::date_time::now(),
+            status: authorization.status,
+            error_code: authorization.error_code,
+            error_message: authorization.error_message,
+            connector_authorization_id: authorization.connector_authorization_id,
+            previously_authorized_amount: authorization.previously_authorized_amount,
+        };
+        authorizations.push(authorization.clone());
+        Ok(authorization)
     }
 
     async fn find_all_authorizations_by_merchant_id_payment_id(
         &self,
-        _merchant_id: &str,
-        _payment_id: &str,
+        merchant_id: &str,
+        payment_id: &str,
     ) -> CustomResult<Vec<storage::Authorization>, errors::StorageError> {
-        // TODO: Implement function for `MockDb`
-        Err(errors::StorageError::MockDbError)?
+        let authorizations = self.authorizations.lock().await;
+        let authorizations_found: Vec<storage::Authorization> = authorizations
+            .iter()
+            .filter(|a| a.merchant_id == merchant_id && a.payment_id == payment_id)
+            .cloned()
+            .collect();
+
+        Ok(authorizations_found)
     }
 
     async fn update_authorization_by_merchant_id_authorization_id(
         &self,
-        _merchant_id: String,
-        _authorization_id: String,
-        _authorization: storage::AuthorizationUpdate,
+        merchant_id: String,
+        authorization_id: String,
+        authorization_update: storage::AuthorizationUpdate,
     ) -> CustomResult<storage::Authorization, errors::StorageError> {
-        // TODO: Implement function for `MockDb`
-        Err(errors::StorageError::MockDbError)?
+        let mut authorizations = self.authorizations.lock().await;
+        authorizations
+            .iter_mut()
+            .find(|authorization| authorization.authorization_id == authorization_id && authorization.merchant_id == merchant_id)
+            .map(|authorization| {
+                let authorization_updated =
+                    AuthorizationUpdateInternal::from(authorization_update)
+                        .create_authorization(authorization.clone());
+                *authorization = authorization_updated.clone();
+                authorization_updated
+            })
+            .ok_or(
+                errors::StorageError::ValueNotFound(format!(
+                    "cannot find authorization for authorization_id = {authorization_id} and merchant_id = {merchant_id}"
+                ))
+                .into(),
+            )
     }
 }
diff --git a/crates/storage_impl/src/mock_db.rs b/crates/storage_impl/src/mock_db.rs
index e22d39ce70c..a6ba763bd91 100644
--- a/crates/storage_impl/src/mock_db.rs
+++ b/crates/storage_impl/src/mock_db.rs
@@ -43,6 +43,7 @@ pub struct MockDb {
     pub organizations: Arc<Mutex<Vec<store::organization::Organization>>>,
     pub users: Arc<Mutex<Vec<store::user::User>>>,
     pub user_roles: Arc<Mutex<Vec<store::user_role::UserRole>>>,
+    pub authorizations: Arc<Mutex<Vec<store::authorization::Authorization>>>,
     pub dashboard_metadata: Arc<Mutex<Vec<store::user::dashboard_metadata::DashboardMetadata>>>,
 }
 
@@ -79,6 +80,7 @@ impl MockDb {
             organizations: Default::default(),
             users: Default::default(),
             user_roles: Default::default(),
+            authorizations: Default::default(),
             dashboard_metadata: Default::default(),
         })
     }

## Type of Change
<!-- Put an `x` in the boxes that apply -->

- [ ] Bugfix
- [ ] New feature
- [x] Enhancement
- [ ] Refactoring
- [ ] Dependency updates
- [ ] Documentation
- [ ] CI/CD

## Description
Implement AuthorizationInterface for MockDb


### Additional Changes

- [ ] This PR modifies the API contract
- [ ] This PR modifies the database schema
- [ ] This PR modifies application configuration/environment variables

<!--
Provide links to the files with corresponding changes.

Following are the paths where you can find config files:
1. `config`
2. `crates/router/src/configs`
3. `loadtest/config`
-->


## Motivation and Context
<!--
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.

If you don't have an issue, we'd recommend starting with one first so the PR
can focus on the implementation (unless it is an obvious bug or documentation fix
that will have little conversation).
-->


## How did you test it?
<!--
Did you write an integration/unit/API test to verify the code changes?
Or did you test this change manually (provide relevant screenshots)?
-->


## Checklist
<!-- Put an `x` in the boxes that apply -->

- [ ] I formatted the code `cargo +nightly fmt --all`
- [x] I addressed lints thrown by `cargo clippy`
- [x] I reviewed the submitted code
- [x] I added unit tests for my changes where possible
- [ ] I added a [CHANGELOG](/CHANGELOG.md) entry if applicable
