"""Lambda test module.""" from unittest import mock import pytest import config from src import app as index from src.app import BadRequest from src.app import RetryException mock_identity = 'c48e9989-a79b-48f8-9e56-413f08408d81' @mock.patch('src.app.config.ows_client') def test_handler(ows_client_mock): """Test handler function.""" vendor_uuid = '123456abcdef' request = {'vendor_uuid': vendor_uuid, 'identity_id': mock_identity} subscription_message = 'Successfully edited {} for identity' expected_json_request_body = { 'follow_all_resources': False, 'automatic': True, 'follow_resources': [vendor_uuid], } rejection_message = subscription_message.format( 'email_notification_rejection') approval_message = subscription_message.format( 'email_notification_new_release') expected = {'approval': approval_message, 'rejection': rejection_message} response1 = mock.Mock(status_code=200) response1.json.return_value = {'edit_subscription': rejection_message} response2 = mock.Mock(status_code=200) response2.json.return_value = {'edit_subscription': approval_message} ows_client_mock.put.side_effect = [response1, response2] result = index.handler(request, {}) assert result assert result == expected ows_client_mock.put.assert_has_calls([ mock.call( 'ows-notifications', f'/identity/{mock_identity}/subscriptions/' 'email_notification_rejection', json=expected_json_request_body, headers=config.OWS_REQUEST_HEADERS, ), mock.call( 'ows-notifications', f'/identity/{mock_identity}/subscriptions/' 'email_notification_new_release', json=expected_json_request_body, headers=config.OWS_REQUEST_HEADERS, ), ]) def test_validation(): """Test validation handling.""" request = { 'vendor_uuid': '123456abcdef', } with pytest.raises(BadRequest): index.handler(request, {}) @mock.patch('src.app.config.ows_client') def test_retry(ows_client_mock): """Test retry exception handling.""" request = { 'vendor_uuid': '123456abcdef', 'identity_id': mock_identity } ows_client_mock.put.return_value = mock.Mock(status_code=500) with pytest.raises(RetryException): index.handler(request, {})