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
@@ -216,8 +216,8 @@ def test_lambda_handler_non_root_file(self):
216216
217217 def test_lambda_invalid_file_key_no_other_files_in_queue (self ):
218218 """
219- Tests that when the file_key is invalid, and there are no other files in the supplier_vaccineType queue :
220- * The file is added to the audit table with a status of 'Processed '
219+ Tests that when the file_key is invalid:
220+ * The file is added to the audit table with a status of 'Not processed - Invalid filename '
221221 * The message is not sent to SQS
222222 * The failure inf_ack file is created
223223 """
@@ -241,7 +241,8 @@ def test_lambda_invalid_file_key_no_other_files_in_queue(self):
241241 "message_id" : {"S" : file_details .message_id },
242242 "filename" : {"S" : file_details .file_key },
243243 "queue_name" : {"S" : "unknown_unknown" },
244- "status" : {"S" : "Processed" },
244+ "status" : {"S" : "Not processed - Invalid filename" },
245+ "error_details" : {"S" : "Initial file validation failed: invalid file key" },
245246 "timestamp" : {"S" : file_details .created_at_formatted_string },
246247 "expires_at" : {"N" : str (file_details .expires_at )},
247248 }
@@ -251,30 +252,72 @@ def test_lambda_invalid_file_key_no_other_files_in_queue(self):
251252 self .assert_ack_file_contents (file_details )
252253 self .assert_no_sqs_message ()
253254
254- def test_lambda_invalid_permissions_other_files_in_queue (self ):
255+ def test_lambda_invalid_permissions (self ):
255256 """
256- Tests that when the file permissions are invalid, and there are other files in the supplier_vaccineType queue :
257- * The file is added to the audit table with a status of 'Processed '
257+ Tests that when the file permissions are invalid:
258+ * The file is added to the audit table with a status of 'Not processed - Unauthorised '
258259 * The message is not sent to SQS
259260 * The failure inf_ack file is created
260261 """
261262 file_details = MockFileDetails .ravs_rsv_1
262263 s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key )
263264
264- queued_file_details = MockFileDetails .ravs_rsv_2
265- add_entry_to_table (queued_file_details , FileStatus .QUEUED )
266-
267265 # Mock the supplier permissions with a value which doesn't include the requested Flu permissions
268266 mock_hget = create_mock_hget ({"X8E5B" : "RAVS" }, {})
269267 with ( # noqa: E999
270268 patch ("file_name_processor.uuid4" , return_value = file_details .message_id ), # noqa: E999
271269 patch ("elasticache.redis_client.hget" , side_effect = mock_hget ), # noqa: E999
272270 ): # noqa: E999
273271 lambda_handler (self .make_event ([self .make_record (file_details .file_key )]), None )
274- assert_audit_table_entry (file_details , FileStatus .PROCESSED )
272+
273+ expected_table_items = [
274+ {
275+ "message_id" : {"S" : file_details .message_id },
276+ "filename" : {"S" : file_details .file_key },
277+ "queue_name" : {"S" : "RAVS_RSV" },
278+ "status" : {"S" : "Not processed - Unauthorised" },
279+ "error_details" : {"S" : "Initial file validation failed: RAVS does not have permissions for RSV" },
280+ "timestamp" : {"S" : file_details .created_at_formatted_string },
281+ "expires_at" : {"N" : str (file_details .expires_at )}
282+ }
283+ ]
284+ self .assertEqual (self .get_audit_table_items (), expected_table_items )
275285 self .assert_no_sqs_message ()
276286 self .assert_ack_file_contents (file_details )
277287
288+ def test_lambda_adds_event_to_audit_table_as_failed_when_unexpected_exception_is_caught (self ):
289+ """
290+ Tests that when an unexpected error occurs e.g. sending to SQS (maybe in case of bad deployment):
291+ * The file is added to the audit table with a status of 'Failed' and the reason
292+ * The message is not sent to SQS
293+ * The failure inf_ack file is created
294+ """
295+ test_file_details = MockFileDetails .emis_flu
296+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = test_file_details .file_key )
297+
298+ with ( # noqa: E999
299+ patch ("file_name_processor.uuid4" , return_value = test_file_details .message_id ), # noqa: E999
300+ patch ("file_name_processor.make_and_send_sqs_message" , side_effect = UnhandledSqsError (
301+ "Some client error with SQS"
302+ ))
303+ ): # noqa: E999
304+ lambda_handler (self .make_event ([self .make_record (test_file_details .file_key )]), None )
305+
306+ expected_table_items = [
307+ {
308+ "message_id" : {"S" : test_file_details .message_id },
309+ "filename" : {"S" : test_file_details .file_key },
310+ "queue_name" : {"S" : "EMIS_FLU" },
311+ "status" : {"S" : "Failed" },
312+ "error_details" : {"S" : "Some client error with SQS" },
313+ "timestamp" : {"S" : test_file_details .created_at_formatted_string },
314+ "expires_at" : {"N" : str (test_file_details .expires_at )},
315+ }
316+ ]
317+ self .assertEqual (self .get_audit_table_items (), expected_table_items )
318+ self .assert_ack_file_contents (test_file_details )
319+ self .assert_no_sqs_message ()
320+
278321
279322@patch .dict ("os.environ" , MOCK_ENVIRONMENT_DICT )
280323@mock_s3
0 commit comments