"""Lambda test module.""" import hashlib import json from unittest.mock import MagicMock from unittest.mock import patch from kafka.errors import UnknownTopicOrPartitionError import pytest from src import app # noqa:I202 from src import consts # noqa:I202 HEADERS = {'content-type': 'application/json'} @patch('src.app.producer') @patch('src.app.uuid') def test_handler_success( uuid_mock, producer_mock, test_event, test_transformed_event): """Test handler function successful.""" expected_topic_name = 'test_topic' expected_uuid = 'uuid-uuid' uuid_mock.uuid4 = MagicMock(return_value=expected_uuid) result = app.handler(test_event, None) expected_data = test_transformed_event expected_id = hashlib.sha256( ('test@test.comtestartist' + consts.SALT).encode() ).hexdigest().encode() expected_headers = [ (consts.ID_HEADER, expected_id), (consts.CORRELATION_ID_HEADER, expected_uuid.encode()) ] expected_body = json.dumps({'response': 'ok'}) actual_topic, actual_data = producer_mock.send.call_args.args actual_headers = producer_mock.send.call_args.kwargs['headers'] actual_data = json.loads(actual_data) producer_mock.send.assert_called() assert actual_topic == expected_topic_name assert actual_data == expected_data assert actual_headers == expected_headers producer_mock.flush.assert_called() assert result == { 'body': expected_body, 'headers': HEADERS, 'statusCode': 200} @patch('src.app.producer') def test_handler_no_body(producer_mock, test_event): """Test handler function fails if the body is empty.""" test_event['body'] = '' result = app.handler(test_event, None) producer_mock.send.assert_not_called() expected_body = json.dumps({'response': consts.ERROR_EMPTY_BODY}) assert result == { 'body': expected_body, 'headers': HEADERS, 'statusCode': 400} @patch('src.app.producer') def test_handler_required_field(producer_mock, test_event_no_required_field): """Test handler function fails if the body is empty.""" result = app.handler(test_event_no_required_field, None) producer_mock.send.assert_not_called() expected_body = json.dumps( {'response': {'email': ['Missing data for required field.']}}) assert result == { 'body': expected_body, 'headers': HEADERS, 'statusCode': 400} @patch('src.app.producer') def test_handler_malformed_json(producer_mock, test_event): """Test handler function fails if the payload is malformed.""" # remove the closing curly bracket from the payload json test_event['body'] = test_event['body'][:-1] result = app.handler(test_event, None) producer_mock.send.assert_not_called() expected_error = "Expecting ',' delimiter: line 1 column 435 (char 434)" expected_body = json.dumps({'response': expected_error}) assert result == { 'body': expected_body, 'headers': HEADERS, 'statusCode': 400} @patch('src.app.producer') def test_handler_producer_failure(producer_mock, test_event): """Test handler function fails because of kafka error.""" producer_mock.send = MagicMock(side_effect=UnknownTopicOrPartitionError) result = app.handler(test_event, None) producer_mock.send.assert_called() expected_error = '[Error 3] UnknownTopicOrPartitionError' expected_body = json.dumps({'response': expected_error}) assert result == { 'body': expected_body, 'headers': HEADERS, 'statusCode': 500} @pytest.mark.parametrize( 'values,same_id', [ ( [ ('test@test.com', 'limp bizkit'), ('test@test.com', 'Limp Bizkit'), ('Test@tesT.com', 'Limp bizkit') ], True ), ( [ ('test@test.com', 'firstname'), ('hello@abc.com', 'othername'), ], False ), ] ) @patch('src.app.producer') def test_generate_salesforce_id(producer_mock, values, same_id): """Test generation of salesforce ID for different values.""" hashes = [] for (email, artist_name) in values: hashes.append(app.generate_salesforce_id(email, artist_name)) if same_id: assert len(set(hashes)) == 1 else: assert len(set(hashes)) == len(hashes)