From 3746f7dbe91bc45e89294c6d5b7d4d77c311f467 Mon Sep 17 00:00:00 2001 From: Tushar <30565750+tushar5526@users.noreply.github.com> Date: Wed, 22 May 2024 22:22:56 +0530 Subject: [PATCH] add tests for initial deployment --- server/utils.py | 14 ++++++++------ tests/test_utils.py | 24 +++++++++++++++--------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/server/utils.py b/server/utils.py index 6ccec1d..c946efb 100644 --- a/server/utils.py +++ b/server/utils.py @@ -399,7 +399,8 @@ def _read_secrets_from_vault(self, secret_path_url): ) if response.status_code != 200: return None - return response.json() + res_json = response.json() + return res_json["data"]["data"] def _create_env_placeholder(self): # check whether we can copy env vars from the default-dev-secrets path @@ -418,20 +419,21 @@ def _create_env_placeholder(self): break self._write_secrets_to_vault(self._default_secret_url, sample_envs) self._write_secrets_to_vault(self._secret_url, sample_envs) - else: - self._write_secrets_to_vault(self._secret_url, default_response) + return sample_envs + + self._write_secrets_to_vault(self._secret_url, default_response) + return default_response def inject_env_variables(self, project_path): secret_data = self._read_secrets_from_vault(self._secret_url) if not secret_data: logger.info(f"No secrets found in vault for {self._secrets_namespace}") - self._create_env_placeholder() - return + secret_data = self._create_env_placeholder() logger.info(f"Found secrets for {self._secrets_namespace}") with open(os.path.join(project_path, ".env"), "w") as file: - for key, value in secret_data["data"]["data"].items(): + for key, value in secret_data.items(): file.write(f'{key}="{value}"\n') def cleanup_deployment_variables(self): diff --git a/tests/test_utils.py b/tests/test_utils.py index cbb2dc3..8283fb1 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -425,18 +425,24 @@ def test_inject_env_variables_with_no_secrets( mock_response.status_code = 404 mock_requests.get.return_value = mock_response mock_create_env_placeholder = MagicMock() + mock_create_env_placeholder.return_value = {"key": "test"} + with patch.object( secrets_helper_instance, "_create_env_placeholder", mock_create_env_placeholder ): - # Calling the method under test - secrets_helper_instance.inject_env_variables("/path/to/project") - - # Assertions - mock_requests.get.assert_called_once_with( - url="http://vault:8200/v1/kv/data/project_name/branch_name", - headers={"X-Vault-Token": "hvs.randomToken"}, - ) - mock_create_env_placeholder.assert_called_once_with() + mock_open = MagicMock() + mock_os.path.join.return_value = "/path/to/project/.env" + with patch("builtins.open", mock_open): + # Calling the method under test + secrets_helper_instance.inject_env_variables("/path/to/project") + + # Assertions + mock_requests.get.assert_called_once_with( + url="http://vault:8200/v1/kv/data/project_name/branch_name", + headers={"X-Vault-Token": "hvs.randomToken"}, + ) + mock_create_env_placeholder.assert_called_once() + mock_open.assert_called_once_with("/path/to/project/.env", "w") @patch("server.utils.requests.delete", autospec=True)