"""Tests for the GitHubConfig class in github_search_text_in_repo.config module.""" # noqa: E501 from unittest.mock import MagicMock, patch from config import GitHubConfig from pydantic import ValidationError from pytest import MonkeyPatch, raises def test_creates_github_config_with_valid_env_vars( monkeypatch: MonkeyPatch, ) -> None: # noqa: E501 """Test that GitHubConfig is created successfully when all required environment variables are set. :param monkeypatch: :return: """ # noqa: E501 monkeypatch.setenv('GITHUB_TOKEN', 'ghp_123456') config = GitHubConfig() assert config.github_token == 'ghp_123456' def test_raises_error_when_missing_required_env_vars( monkeypatch: MonkeyPatch, ) -> None: """Test that a ValidationError is raised when the GITHUB_TOKEN environment variable is missing. :param monkeypatch: :return: """ # noqa: E501 monkeypatch.delenv('GITHUB_TOKEN', raising=False) with raises(ValidationError): GitHubConfig() def test_fetches_token_from_secrets_manager_in_lambda( monkeypatch: MonkeyPatch, ) -> None: """In Lambda, GITHUB_TOKEN is treated as a secret name and token is fetched from Secrets Manager. :param monkeypatch: :return: """ # noqa: E501 monkeypatch.setenv('AWS_LAMBDA_FUNCTION_NAME', 'github-cli') monkeypatch.setenv('GITHUB_TOKEN', 'my/secret/name') mock_client = MagicMock() mock_client.get_github_token.return_value = 'ghp_from_secrets_manager' with patch( 'github_client.secrets_manager.SecretsManagerClient', return_value=mock_client ): config = GitHubConfig() mock_client.get_github_token.assert_called_once_with('my/secret/name') assert config.github_token == 'ghp_from_secrets_manager' def test_does_not_call_secrets_manager_locally( monkeypatch: MonkeyPatch, ) -> None: """Locally (no AWS_LAMBDA_FUNCTION_NAME), Secrets Manager is never called. :param monkeypatch: :return: """ monkeypatch.delenv('AWS_LAMBDA_FUNCTION_NAME', raising=False) monkeypatch.setenv('GITHUB_TOKEN', 'ghp_local_token') with patch('github_client.secrets_manager.SecretsManagerClient') as mock_cls: config = GitHubConfig() mock_cls.assert_not_called() assert config.github_token == 'ghp_local_token'