"""Lambda test module.""" import datetime import os from unittest.mock import patch import boto3 import pytest from moto import mock_aws from slack_sdk import WebClient import config from src.utils import notifications @pytest.fixture() def patch_ses_client(ses_client, monkeypatch): """Patch the global SES client with a mocked one.""" monkeypatch.setattr(notifications, 'ses_client', ses_client) return ses_client @pytest.fixture() def ses_client(aws_credentials): """Return an SES client.""" with mock_aws(): yield boto3.client('ses', region_name='us-east-1') @pytest.fixture() def verify_email_address(ses_client, iam_user): """Verify a sender email address.""" verified_address = ses_client.verify_email_address( EmailAddress=config.EMAIL_SENDER ) return verified_address def test_send_email(patch_ses_client, verify_email_address, iam_user): """Test send_email function.""" os.environ['EMAIL_RECIPIENTS'] = 'test@domain.com' result = notifications.send_email( '1234567890', 'https://jira.com/DEVEX-454', iam_user) assert isinstance(result, str) @patch('src.utils.notifications.WebClient', spec=WebClient) def test_send_slack_message(mock_WebClient, iam_user): """Send a Slack message.""" aws_account = '1234567890' jira_issue_url = 'https://jira.com/DEVEX-454' user = iam_user mock_client = mock_WebClient() mock_client.conversations_list.return_value = { 'ok': 'true', 'channels': [ { 'id': 'C100000', 'name': 'junk', }, { 'id': 'C123456', 'name': 'test-webhooks', }, ] } mock_client.chat_postMessage.return_value = { 'ok': 'true', 'channel': 'C123456', 'ts': '1503435956.000247', } (received_timestamp, channel) = notifications.send_slack_message( aws_account, jira_issue_url, user) mock_client.conversations_list.assert_called_with( exclude_archived='true', types='public_channel', limit=1000 ) mock_client.chat_postMessage.assert_called() assert isinstance(received_timestamp, datetime.datetime) assert channel == 'C123456'