From e21a94b1e80c5ce55ce4a349aa3b2650447562e5 Mon Sep 17 00:00:00 2001 From: "Michael B. Klein" Date: Thu, 19 Dec 2024 01:26:48 +0000 Subject: [PATCH] Add tests for core.secrets --- .github/workflows/test-python.yml | 3 +-- chat/src/core/secrets.py | 3 +-- chat/src/requirements-dev.txt | 1 + chat/test/core/test_secrets.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 chat/test/core/test_secrets.py diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index e24c08e..2b093d4 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -26,8 +26,7 @@ jobs: run: ruff check . - name: Run tests run: | - coverage run --include='src/**/*' -m unittest + coverage run --include='src/**/*' -m pytest coverage report env: - __SKIP_SECRETS__: true AWS_REGION: us-east-1 diff --git a/chat/src/core/secrets.py b/chat/src/core/secrets.py index 9c6173b..ecad562 100644 --- a/chat/src/core/secrets.py +++ b/chat/src/core/secrets.py @@ -2,8 +2,7 @@ import json import os -def load_secrets(): - SecretsPath = os.getenv('SECRETS_PATH') +def load_secrets(SecretsPath=os.getenv('SECRETS_PATH')): EnvironmentMap = [ ['API_TOKEN_SECRET', 'dcapi', 'api_token_secret'], ['OPENSEARCH_ENDPOINT', 'index', 'endpoint'], diff --git a/chat/src/requirements-dev.txt b/chat/src/requirements-dev.txt index 528c4ac..c03376d 100644 --- a/chat/src/requirements-dev.txt +++ b/chat/src/requirements-dev.txt @@ -1,4 +1,5 @@ # Dev/Test Dependencies moto~=5.0 +pytest~=8.3 ruff~=0.2 coverage~=7.3 diff --git a/chat/test/core/test_secrets.py b/chat/test/core/test_secrets.py new file mode 100644 index 0000000..34d7019 --- /dev/null +++ b/chat/test/core/test_secrets.py @@ -0,0 +1,29 @@ +import boto3 +import os +import pytest +from moto import mock_aws +from unittest import TestCase + +from core.secrets import load_secrets + +@mock_aws +@mock_aws +@pytest.mark.filterwarnings("ignore::DeprecationWarning") +class TestSecrets(TestCase): + def setUp(self): + client = boto3.client("secretsmanager", region_name="us-east-1") + client.create_secret( + Name="mock/infrastructure/index", + SecretString='{"endpoint": "https://opensearch-endpoint", "embedding_model": "opensearch-model"}') + client.create_secret( + Name="mock/config/dcapi", + SecretString='{"api_token_secret": "dcapi-token"}') + + def test_load_secrets(self): + self.assertNotEqual('dcapi-token', os.getenv('API_TOKEN_SECRET')) + self.assertNotEqual('https://opensearch-endpoint', os.getenv('OPENSEARCH_ENDPOINT')) + self.assertNotEqual('opensearch-model', os.getenv('OPENSEARCH_MODEL_ID')) + load_secrets('mock') + self.assertEqual('dcapi-token', os.getenv('API_TOKEN_SECRET')) + self.assertEqual('https://opensearch-endpoint', os.getenv('OPENSEARCH_ENDPOINT')) + self.assertEqual('opensearch-model', os.getenv('OPENSEARCH_MODEL_ID'))