"""Lambda test module.""" from unittest.mock import patch import pytest from src import app from src.constants import service from src.logic import PRODUCT_QUERY @patch('src.logic.gql_connector.execute') def test_handler_success(mock_graphql_conn, request_engine, mock_event, mock_graphql_response): """Test send notification success.""" mock_graphql_conn.return_value = mock_graphql_response request_engine[service.OWS_NOTIFICATIONS].add_spec( 'POST', '/content-review/failure-notify', status=202, response={'foo': 'bar'} ) result = app.handler(mock_event, None) mock_graphql_conn.assert_called_once_with( PRODUCT_QUERY, { 'id': '10101' }) assert result == {'status': 'success'} @patch('src.logic.gql_connector.execute') def test_handler_error(mock_graphql_conn, request_engine, mock_event, mock_graphql_response): """Test send notification failure.""" mock_graphql_conn.return_value = mock_graphql_response request_engine[service.OWS_NOTIFICATIONS].add_spec( 'POST', '/content-review/failure-notify', status=403, response={'foo': 'bar'} ) expected_error_message = ( 'Failed to send content review failure notification for product_id 10101.\n' 'Status code: 403, Error: {"foo": "bar"}' ) with pytest.raises(Exception) as excinfo: app.handler(mock_event, None) mock_graphql_conn.assert_called_once_with( PRODUCT_QUERY, { 'id': '10101' }) assert excinfo.value.args[0] == expected_error_message @patch('src.app.send_failure_notification') def test_handler_does_nothing(mock_send): """Test that the handler does nothing if the skip flag is set.""" actual = app.handler({'product_id': 1, 'skip_failure_notifications': True}, None) assert actual == {'status': 'success'} mock_send.assert_not_called()