1010from boto3 import client as boto3_client
1111from moto import mock_s3 , mock_sqs , mock_firehose , mock_dynamodb
1212
13+ from errors import UnhandledSqsError
1314from tests .utils_for_tests .generic_setup_and_teardown import GenericSetUp , GenericTearDown
1415from tests .utils_for_tests .utils_for_filenameprocessor_tests import (
15- add_entry_to_table ,
1616 assert_audit_table_entry ,
1717 create_mock_hget ,
1818 MOCK_ODS_CODE_TO_SUPPLIER
@@ -215,8 +215,8 @@ def test_lambda_handler_non_root_file(self):
215215
216216 def test_lambda_invalid_file_key_no_other_files_in_queue (self ):
217217 """
218- Tests that when the file_key is invalid, and there are no other files in the supplier_vaccineType queue :
219- * The file is added to the audit table with a status of 'Processed '
218+ Tests that when the file_key is invalid:
219+ * The file is added to the audit table with a status of 'Not processed - Invalid filename '
220220 * The message is not sent to SQS
221221 * The failure inf_ack file is created
222222 """
@@ -240,7 +240,8 @@ def test_lambda_invalid_file_key_no_other_files_in_queue(self):
240240 "message_id" : {"S" : file_details .message_id },
241241 "filename" : {"S" : file_details .file_key },
242242 "queue_name" : {"S" : "unknown_unknown" },
243- "status" : {"S" : "Processed" },
243+ "status" : {"S" : "Not processed - Invalid filename" },
244+ "error_details" : {"S" : "Initial file validation failed: invalid file key" },
244245 "timestamp" : {"S" : file_details .created_at_formatted_string },
245246 }
246247 ]
@@ -249,30 +250,70 @@ def test_lambda_invalid_file_key_no_other_files_in_queue(self):
249250 self .assert_ack_file_contents (file_details )
250251 self .assert_no_sqs_message ()
251252
252- def test_lambda_invalid_permissions_other_files_in_queue (self ):
253+ def test_lambda_invalid_permissions (self ):
253254 """
254- Tests that when the file permissions are invalid, and there are other files in the supplier_vaccineType queue :
255- * The file is added to the audit table with a status of 'Processed '
255+ Tests that when the file permissions are invalid:
256+ * The file is added to the audit table with a status of 'Not processed - Unauthorised '
256257 * The message is not sent to SQS
257258 * The failure inf_ack file is created
258259 """
259260 file_details = MockFileDetails .ravs_rsv_1
260261 s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key )
261262
262- queued_file_details = MockFileDetails .ravs_rsv_2
263- add_entry_to_table (queued_file_details , FileStatus .QUEUED )
264-
265263 # Mock the supplier permissions with a value which doesn't include the requested Flu permissions
266264 mock_hget = create_mock_hget ({"X8E5B" : "RAVS" }, {})
267265 with ( # noqa: E999
268266 patch ("file_name_processor.uuid4" , return_value = file_details .message_id ), # noqa: E999
269267 patch ("elasticache.redis_client.hget" , side_effect = mock_hget ), # noqa: E999
270268 ): # noqa: E999
271269 lambda_handler (self .make_event ([self .make_record (file_details .file_key )]), None )
272- assert_audit_table_entry (file_details , FileStatus .PROCESSED )
270+
271+ expected_table_items = [
272+ {
273+ "message_id" : {"S" : file_details .message_id },
274+ "filename" : {"S" : file_details .file_key },
275+ "queue_name" : {"S" : "RAVS_RSV" },
276+ "status" : {"S" : "Not processed - Unauthorised" },
277+ "error_details" : {"S" : "Initial file validation failed: RAVS does not have permissions for RSV" },
278+ "timestamp" : {"S" : file_details .created_at_formatted_string },
279+ }
280+ ]
281+ self .assertEqual (self .get_audit_table_items (), expected_table_items )
273282 self .assert_no_sqs_message ()
274283 self .assert_ack_file_contents (file_details )
275284
285+ def test_lambda_adds_event_to_audit_table_as_failed_when_unexpected_exception_is_caught (self ):
286+ """
287+ Tests that when an unexpected error occurs e.g. sending to SQS (maybe in case of bad deployment):
288+ * The file is added to the audit table with a status of 'Failed' and the reason
289+ * The message is not sent to SQS
290+ * The failure inf_ack file is created
291+ """
292+ test_file_details = MockFileDetails .emis_flu
293+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = test_file_details .file_key )
294+
295+ with ( # noqa: E999
296+ patch ("file_name_processor.uuid4" , return_value = test_file_details .message_id ), # noqa: E999
297+ patch ("file_name_processor.make_and_send_sqs_message" , side_effect = UnhandledSqsError (
298+ "Some client error with SQS"
299+ ))
300+ ): # noqa: E999
301+ lambda_handler (self .make_event ([self .make_record (test_file_details .file_key )]), None )
302+
303+ expected_table_items = [
304+ {
305+ "message_id" : {"S" : test_file_details .message_id },
306+ "filename" : {"S" : test_file_details .file_key },
307+ "queue_name" : {"S" : "EMIS_FLU" },
308+ "status" : {"S" : "Failed" },
309+ "error_details" : {"S" : "Some client error with SQS" },
310+ "timestamp" : {"S" : test_file_details .created_at_formatted_string },
311+ }
312+ ]
313+ self .assertEqual (self .get_audit_table_items (), expected_table_items )
314+ self .assert_ack_file_contents (test_file_details )
315+ self .assert_no_sqs_message ()
316+
276317
277318@patch .dict ("os.environ" , MOCK_ENVIRONMENT_DICT )
278319@mock_s3
0 commit comments