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,7 +215,18 @@ def test_lambda_handler_correctly_flags_empty_file(self):
215215 None ,
216216 )
217217
218- assert_audit_table_entry (file_details , FileStatus .EMPTY )
218+ expected_table_items = [
219+ {
220+ "message_id" : {"S" : file_details .message_id },
221+ "filename" : {"S" : file_details .file_key },
222+ "queue_name" : {"S" : "RAVS_RSV" },
223+ "status" : {"S" : "Not processed - Empty file" },
224+ "error_details" : {"S" : "Initial file validation failed: batch file was empty" },
225+ "timestamp" : {"S" : file_details .created_at_formatted_string },
226+ "expires_at" : {"N" : str (file_details .expires_at )},
227+ }
228+ ]
229+ self .assertEqual (self .get_audit_table_items (), expected_table_items )
219230 self .assert_no_sqs_message ()
220231 self .assert_ack_file_contents (file_details )
221232
@@ -240,8 +251,8 @@ def test_lambda_handler_non_root_file(self):
240251
241252 def test_lambda_invalid_file_key_no_other_files_in_queue (self ):
242253 """
243- Tests that when the file_key is invalid, and there are no other files in the supplier_vaccineType queue :
244- * The file is added to the audit table with a status of 'Processed '
254+ Tests that when the file_key is invalid:
255+ * The file is added to the audit table with a status of 'Not processed - Invalid filename '
245256 * The message is not sent to SQS
246257 * The failure inf_ack file is created
247258 """
@@ -265,7 +276,8 @@ def test_lambda_invalid_file_key_no_other_files_in_queue(self):
265276 "message_id" : {"S" : file_details .message_id },
266277 "filename" : {"S" : file_details .file_key },
267278 "queue_name" : {"S" : "unknown_unknown" },
268- "status" : {"S" : "Processed" },
279+ "status" : {"S" : "Not processed - Invalid filename" },
280+ "error_details" : {"S" : "Initial file validation failed: invalid file key" },
269281 "timestamp" : {"S" : file_details .created_at_formatted_string },
270282 "expires_at" : {"N" : str (file_details .expires_at )},
271283 }
@@ -275,30 +287,72 @@ def test_lambda_invalid_file_key_no_other_files_in_queue(self):
275287 self .assert_ack_file_contents (file_details )
276288 self .assert_no_sqs_message ()
277289
278- def test_lambda_invalid_permissions_other_files_in_queue (self ):
290+ def test_lambda_invalid_permissions (self ):
279291 """
280- Tests that when the file permissions are invalid, and there are other files in the supplier_vaccineType queue :
281- * The file is added to the audit table with a status of 'Processed '
292+ Tests that when the file permissions are invalid:
293+ * The file is added to the audit table with a status of 'Not processed - Unauthorised '
282294 * The message is not sent to SQS
283295 * The failure inf_ack file is created
284296 """
285297 file_details = MockFileDetails .ravs_rsv_1
286298 s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key , Body = MOCK_BATCH_FILE_CONTENT )
287299
288- queued_file_details = MockFileDetails .ravs_rsv_2
289- add_entry_to_table (queued_file_details , FileStatus .QUEUED )
290-
291300 # Mock the supplier permissions with a value which doesn't include the requested Flu permissions
292301 mock_hget = create_mock_hget ({"X8E5B" : "RAVS" }, {})
293302 with ( # noqa: E999
294303 patch ("file_name_processor.uuid4" , return_value = file_details .message_id ), # noqa: E999
295304 patch ("elasticache.redis_client.hget" , side_effect = mock_hget ), # noqa: E999
296305 ): # noqa: E999
297306 lambda_handler (self .make_event ([self .make_record (file_details .file_key )]), None )
298- assert_audit_table_entry (file_details , FileStatus .PROCESSED )
307+
308+ expected_table_items = [
309+ {
310+ "message_id" : {"S" : file_details .message_id },
311+ "filename" : {"S" : file_details .file_key },
312+ "queue_name" : {"S" : "RAVS_RSV" },
313+ "status" : {"S" : "Not processed - Unauthorised" },
314+ "error_details" : {"S" : "Initial file validation failed: RAVS does not have permissions for RSV" },
315+ "timestamp" : {"S" : file_details .created_at_formatted_string },
316+ "expires_at" : {"N" : str (file_details .expires_at )}
317+ }
318+ ]
319+ self .assertEqual (self .get_audit_table_items (), expected_table_items )
299320 self .assert_no_sqs_message ()
300321 self .assert_ack_file_contents (file_details )
301322
323+ def test_lambda_adds_event_to_audit_table_as_failed_when_unexpected_exception_is_caught (self ):
324+ """
325+ Tests that when an unexpected error occurs e.g. sending to SQS (maybe in case of bad deployment):
326+ * The file is added to the audit table with a status of 'Failed' and the reason
327+ * The message is not sent to SQS
328+ * The failure inf_ack file is created
329+ """
330+ test_file_details = MockFileDetails .emis_flu
331+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = test_file_details .file_key , Body = MOCK_BATCH_FILE_CONTENT )
332+
333+ with ( # noqa: E999
334+ patch ("file_name_processor.uuid4" , return_value = test_file_details .message_id ), # noqa: E999
335+ patch ("file_name_processor.make_and_send_sqs_message" , side_effect = UnhandledSqsError (
336+ "Some client error with SQS"
337+ ))
338+ ): # noqa: E999
339+ lambda_handler (self .make_event ([self .make_record (test_file_details .file_key )]), None )
340+
341+ expected_table_items = [
342+ {
343+ "message_id" : {"S" : test_file_details .message_id },
344+ "filename" : {"S" : test_file_details .file_key },
345+ "queue_name" : {"S" : "EMIS_FLU" },
346+ "status" : {"S" : "Failed" },
347+ "error_details" : {"S" : "Some client error with SQS" },
348+ "timestamp" : {"S" : test_file_details .created_at_formatted_string },
349+ "expires_at" : {"N" : str (test_file_details .expires_at )},
350+ }
351+ ]
352+ self .assertEqual (self .get_audit_table_items (), expected_table_items )
353+ self .assert_ack_file_contents (test_file_details )
354+ self .assert_no_sqs_message ()
355+
302356
303357@patch .dict ("os.environ" , MOCK_ENVIRONMENT_DICT )
304358@mock_s3
0 commit comments