Skip to content

Commit

Permalink
add tests for initial deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
tushar5526 committed May 22, 2024
1 parent 34a05f7 commit 3746f7d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
14 changes: 8 additions & 6 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Check warning on line 425 in server/utils.py

View check run for this annotation

Codecov / codecov/patch

server/utils.py#L424-L425

Added lines #L424 - L425 were not covered by tests

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):
Expand Down
24 changes: 15 additions & 9 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3746f7d

Please sign in to comment.