"""Tests for the JiraConfig class in jira_query_offboarding_tickets.config.""" from unittest.mock import MagicMock, patch from config import JiraConfig from pydantic import ValidationError from pytest import MonkeyPatch, raises def test_creates_jira_config_with_valid_env_vars( monkeypatch: MonkeyPatch, ) -> None: # noqa: E501 """Test that JiraConfig is created successfully when all required environment variables are set. :param monkeypatch: :return: """ # noqa: E501 monkeypatch.setenv('JIRA_BASE_URL', 'https://example.atlassian.net') monkeypatch.setenv('JIRA_API_TOKEN', 'token_abc123') monkeypatch.setenv('JIRA_USER_EMAIL', 'abc@foo.bar') config = JiraConfig() assert config.jira_base_url == 'https://example.atlassian.net' assert config.jira_api_token == 'token_abc123' assert config.jira_user_email == 'abc@foo.bar' def test_raises_error_when_missing_required_jira_base_env_var( monkeypatch: MonkeyPatch, ) -> None: # noqa: E501 """Test that a ValidationError is raised when the JIRA_BASE_URL environment variable is missing. :param monkeypatch: :return: """ # noqa: E501 monkeypatch.delenv('JIRA_BASE_URL', raising=False) monkeypatch.setenv('JIRA_API_TOKEN', 'token_abc123') monkeypatch.setenv('JIRA_USER_EMAIL', 'abc@foo.bar') with raises(ValidationError): JiraConfig() def test_raises_error_when_missing_required_jira_api_token_env_var( monkeypatch: MonkeyPatch, ) -> None: # noqa: E501 """Test that a ValidationError is raised when the JIRA_API_TOKEN environment variable is missing. :param monkeypatch: :return: """ # noqa: E501 monkeypatch.setenv('JIRA_BASE_URL', 'https://example.atlassian.net') monkeypatch.delenv('JIRA_API_TOKEN', raising=False) monkeypatch.setenv('JIRA_USER_EMAIL', 'abc@foo.bar') with raises(ValidationError): JiraConfig() def test_raises_error_when_missing_required_jira_user_env_var( monkeypatch: MonkeyPatch, ) -> None: # noqa: E501 """Test that a ValidationError is raised when the JIRA_USER_EMAIL environment variable is missing. :param monkeypatch: :return: """ # noqa: E501 monkeypatch.setenv('JIRA_BASE_URL', 'https://example.atlassian.net') monkeypatch.setenv('JIRA_API_TOKEN', 'token_abc123') monkeypatch.delenv('JIRA_USER_EMAIL', raising=False) with raises(ValidationError): JiraConfig() def test_fetches_token_from_secrets_manager_in_lambda( monkeypatch: MonkeyPatch, ) -> None: """In Lambda, JIRA_API_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', 'jira-cli') monkeypatch.setenv('JIRA_BASE_URL', 'https://example.atlassian.net') monkeypatch.setenv('JIRA_API_TOKEN', 'my/secret/name') monkeypatch.setenv('JIRA_USER_EMAIL', 'abc@foo.bar') mock_client = MagicMock() mock_client.get_jira_api_token.return_value = 'token_from_secrets_manager' with patch( 'jira_client.secrets_manager.SecretsManagerClient', return_value=mock_client ): config = JiraConfig() mock_client.get_jira_api_token.assert_called_once_with('my/secret/name') assert config.jira_api_token == 'token_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('JIRA_BASE_URL', 'https://example.atlassian.net') monkeypatch.setenv('JIRA_API_TOKEN', 'local_token') monkeypatch.setenv('JIRA_USER_EMAIL', 'abc@foo.bar') with patch('jira_client.secrets_manager.SecretsManagerClient') as mock_cls: config = JiraConfig() mock_cls.assert_not_called() assert config.jira_api_token == 'local_token'