"""Test handler add_twilio_logs_to_cw.""" import json from unittest.mock import MagicMock from oto import response import pytest from cloudwatch_logs import api from cloudwatch_logs import config from cloudwatch_logs import handlers # noqa from cloudwatch_logs.logic import logs @pytest.fixture def fixture_client(): """Create an api test client fixture.""" return api.app.test_client() def test_test_write_cloudwatch_log(fixture_client, monkeypatch): """Test test_write_cloudwatch_log with different input params.""" token = 'abcd' expected = response.Response({'foo': 'bar'}) monkeypatch.setattr(logs, 'get_next_token', MagicMock(return_value=token)) monkeypatch.setattr(logs, 'write_cloudwatch_log', MagicMock( return_value=expected)) params = {'user': ['data'], 'To': ['+011234567890'], 'From': ['1234']} result = fixture_client.post('/twilio/logs', data=params) assert result.status_code == 200 response_data = json.loads(result.data.decode('utf-8')) assert response_data == expected.message logs.get_next_token.assert_called_once_with( config.TWILIO_LOG_GROUP, config.TWILIO_LOG_STREAM) logs.write_cloudwatch_log.assert_called_once_with( params, token, config.TWILIO_LOG_GROUP, config.TWILIO_LOG_STREAM)