"""Lambda test module.""" import json from unittest.mock import MagicMock from unittest.mock import patch from src import app as index url = 'https://s3.us-east-1.amazonaws.com/dev-orcd-podcast-output-assets/{}'.format( 'owspodcasttranscribe_edfab972_8c56_4955_857f_e1162f900f22.wav.json' ) transaction_job_return_value = { 'TranscriptionJob': { 'Transcript': { 'TranscriptFileUri': url }, 'Media': { 'MediaFileUri': 'https://dev-orcd-asset-transcoder-input.s3.amazonaws.com/2b8a5127_5fe69a2023e9.wav' } }, } mock_items_with_speaker = [ { 'type': 'pronunciation', 'speaker_label': 'spk_0', 'end_time': '1.0', 'start_time': '0.2', 'alternatives': [{'content': 'I'}] }, { 'type': 'pronunciation', 'speaker_label': 'spk_0', 'end_time': '2.0', 'start_time': '1.0', 'alternatives': [{'content': 'walk'}] }, { 'type': 'pronunciation', 'speaker_label': 'spk_0', 'end_time': '3.0', 'start_time': '2.0', 'alternatives': [{'content': 'a'}] }, { 'type': 'pronunciation', 'speaker_label': 'spk_1', 'end_time': '4.0', 'start_time': '3.0', 'alternatives': [{'content': 'lonely'}] }, { 'type': 'pronunciation', 'speaker_label': 'spk_1', 'end_time': '5.0', 'start_time': '4.0', 'alternatives': [{'content': 'road'}] }, {'type': 'punctuation', 'alternatives': [{'content': ','}]} ] mock_items_without_speaker = [ {'type': 'pronunciation', 'end_time': '1.0', 'start_time': '0.2', 'alternatives': [{'content': 'I'}]}, {'type': 'pronunciation', 'end_time': '2.0', 'start_time': '1.0', 'alternatives': [{'content': 'walk'}]}, {'type': 'pronunciation', 'end_time': '3.0', 'start_time': '2.0', 'alternatives': [{'content': 'a'}]}, {'type': 'pronunciation', 'end_time': '4.0', 'start_time': '3.0', 'alternatives': [{'content': 'lonely'}]}, {'type': 'pronunciation', 'end_time': '5.0', 'start_time': '4.0', 'alternatives': [{'content': 'road'}]}, {'type': 'punctuation', 'alternatives': [{'content': ','}]} ] @patch('src.common.sentry.capture_message') @patch('owsrequest.request.process') @patch('src.app.s3_client') @patch('src.app.transcribe') def test_transcribe_has_speakers_success(transcribe, s3_client, process, capture_message): """Test handler function when aws transcribe fails.""" transcribe.get_transcription_job.return_value = transaction_job_return_value process.return_value.status_code = 200 body_mock = MagicMock() body_mock.read.return_value = json.dumps({ 'results': { 'items': mock_items_with_speaker, 'speaker_labels': { 'segments': [ {'end_time': '3.0', 'start_time': '0.0', 'speaker_label': 'spk_0'}, {'end_time': '5.0', 'start_time': '3.0', 'speaker_label': 'spk_1'} ] } } }) s3_client.get_object.return_value = {'Body': body_mock} s3_client.head_object.return_value = {'Metadata': {'object_type': 'episode', 'object_id': 12}} index.handler({ 'detail': { 'TranscriptionJobName': 'owspodcasttranscribe_key', 'TranscriptionJobStatus': 'COMPLETED', } }, {}) process.assert_called_with( application='lambda-podcast-transcribe', environment='test', method='PUT', service_name='ows-podcast', path='/episode/12/transcript', json={'transcript': [ { 'speaker_label': 'Speaker 1', 'items': [ {'end_time': '1.0', 'start_time': '0.2', 'content': 'I', 'speaker_label': 'spk_0'}, {'end_time': '2.0', 'start_time': '1.0', 'content': 'walk', 'speaker_label': 'spk_0'}, {'end_time': '3.0', 'start_time': '2.0', 'content': 'a', 'speaker_label': 'spk_0'} ] }, { 'speaker_label': 'Speaker 2', 'items': [ {'end_time': '4.0', 'start_time': '3.0', 'content': 'lonely', 'speaker_label': 'spk_1'}, {'end_time': '5.0', 'start_time': '4.0', 'content': 'road,', 'speaker_label': 'spk_1'}, ] } ]} ) @patch('src.common.sentry.capture_message') @patch('owsrequest.request.process') @patch('src.app.s3_client') @patch('src.app.transcribe') def test_transcribe_no_speakers_success(transcribe, s3_client, process, capture_message): """Test handler function when aws transcribe fails.""" transcribe.get_transcription_job.return_value = transaction_job_return_value process.return_value.status_code = 200 body_mock = MagicMock() body_mock.read.return_value = json.dumps({ 'results': {'items': mock_items_without_speaker} }) s3_client.get_object.return_value = {'Body': body_mock} s3_client.head_object.return_value = {'Metadata': {'object_type': 'episode', 'object_id': 12}} index.handler({ 'detail': { 'TranscriptionJobName': 'owspodcasttranscribe_key', 'TranscriptionJobStatus': 'COMPLETED', } }, {}) process.assert_called_with( application='lambda-podcast-transcribe', environment='test', method='PUT', service_name='ows-podcast', path='/episode/12/transcript', json={'transcript': [ { 'speaker_label': 'Speaker 1', 'items': [ {'end_time': '1.0', 'start_time': '0.2', 'content': 'I', 'speaker_label': None}, {'end_time': '2.0', 'start_time': '1.0', 'content': 'walk', 'speaker_label': None}, {'end_time': '3.0', 'start_time': '2.0', 'content': 'a', 'speaker_label': None}, {'end_time': '4.0', 'start_time': '3.0', 'content': 'lonely', 'speaker_label': None}, {'end_time': '5.0', 'start_time': '4.0', 'content': 'road,', 'speaker_label': None}, ] } ]} ) @patch('src.common.sentry.capture_message') @patch('owsrequest.request.process') @patch('config.logger.exception') @patch('src.app.s3_client') @patch('src.app.transcribe') def test_transcribe_failure(transcribe, s3_client, log_exception, process, capture_message): """Test handler function when aws transcribe fails.""" transcribe.get_transcription_job.return_value = transaction_job_return_value s3_client.head_object.return_value = {'Metadata': {'object_type': 'episode', 'object_id': 12}} index.handler({ 'detail': { 'TranscriptionJobName': 'owspodcasttranscribe_key', 'TranscriptionJobStatus': 'FAILURE', 'FailureReason': 'CAW' } }, {}) log_exception.assert_called_with('CAW') process.assert_called_with( application='lambda-podcast-transcribe', environment='test', method='PUT', service_name='ows-podcast', path='/episode/12/transcript', json={'transcript': ''} ) @patch('owsrequest.request.process') @patch('src.app.s3_client') @patch('src.app.transcribe') @patch('src.common.sentry.capture_message') def test_transcribe_update_fail(capture_message, transcribe, s3_client, process): """Test handler function when ows-podcast update fails.""" transcribe.get_transcription_job.return_value = transaction_job_return_value s3_client.head_object.return_value = {'Metadata': {'object_type': 'episode', 'object_id': 12}} process.return_value.status_code = 400 body_mock = MagicMock() body_mock.read.return_value = json.dumps({'results': {'items': mock_items_without_speaker}}) s3_client.get_object.return_value = {'Body': body_mock} index.handler({ 'detail': { 'TranscriptionJobName': 'owspodcasttranscribe_key', 'TranscriptionJobStatus': 'COMPLETED', } }, {}) capture_message.assert_called_with('ows-podcast error, code 400') @patch('src.common.sentry.capture_message') @patch('owsrequest.request.process') @patch('src.app.s3_client') @patch('src.app.transcribe') def test_transcribe_metadata_bad_object_type(transcribe, s3_client, process, capture_message): """Test handler function when ows-podcast metadata wrong type.""" transcribe.get_transcription_job.return_value = transaction_job_return_value s3_client.head_object.return_value = {'Metadata': {'object_type': 'cow', 'object_id': 12}} index.handler({ 'detail': { 'TranscriptionJobName': 'owspodcasttranscribe_key', 'TranscriptionJobStatus': 'COMPLETED', } }, {}) process.assert_not_called() @patch('owsrequest.request.process') @patch('src.app.s3_client') @patch('src.app.transcribe') def test_transcribe_metadata_bad_object_id(transcribe, s3_client, process): """Test handler function when ows-podcast metadata has no object id type.""" transcribe.get_transcription_job.return_value = transaction_job_return_value s3_client.head_object.return_value = {'Metadata': {'object_type': 'episode'}} index.handler({ 'detail': { 'TranscriptionJobName': 'owspodcasttranscribe_key', 'TranscriptionJobStatus': 'COMPLETED', } }, {}) process.assert_not_called()