Skip to content

Commit 369b757

Browse files
committed
improved unit test
1 parent 233e091 commit 369b757

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

backend/tests/test_log_structure_wrapper.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ def mock_success_function(_event, _context):
2323
def mock_function_raises(_event, _context):
2424
raise ValueError("Test error")
2525

26+
def extract_all_call_args_for_logger(self, mock_logger) -> list:
27+
"""Extracts all arguments for logger.*."""
28+
return (
29+
[ args[0] for args, _ in mock_logger.info.call_args_list ]
30+
+ [ args[0] for args, _ in mock_logger.warning.call_args_list ]
31+
+ [ args[0] for args, _ in mock_logger.error.call_args_list ]
32+
)
33+
2634
def test_successful_execution(self, mock_logger, mock_firehose_logger):
2735
# Arrange
2836
test_correlation = "test_correlation"
@@ -65,7 +73,8 @@ def test_successful_execution(self, mock_logger, mock_firehose_logger):
6573
self.assertEqual(logged_info['local_id'], '12345^http://test')
6674
self.assertEqual(logged_info['vaccine_type'], 'FLU')
6775

68-
def test_successful_execution_logger(self, mock_logger, mock_firehose_logger):
76+
def test_successful_execution_pii(self, mock_logger, mock_firehose_logger):
77+
"""Pass personally identifiable information in an event, and ensure that it is not logged anywhere."""
6978
# Arrange
7079
test_correlation = "test_correlation"
7180
test_request = "test_request"
@@ -83,15 +92,17 @@ def test_successful_execution_logger(self, mock_logger, mock_firehose_logger):
8392
},
8493
'path': test_actual_path,
8594
'requestContext': {'resourcePath': test_resource_path},
86-
'body': "{\"identifier\": [{\"system\": \"http://test\", \"value\": \"12345\"}], \"protocolApplied\": [{\"targetDisease\": [{\"coding\": [{\"system\": \"http://snomed.info/sct\", \"code\": \"840539006\", \"display\": \"Disease caused by severe acute respiratory syndrome coronavirus 2\"}]}]}]}"
95+
'body': "{\"identifier\": [{\"system\": \"http://test\", \"value\": \"12345\"}], \"contained\": [{\"resourceType\": \"Patient\", \"id\": \"Pat1\", \"identifier\": [{\"system\": \"https://fhir.nhs.uk/Id/nhs-number\", \"value\": \"9693632109\"}]}], \"protocolApplied\": [{\"targetDisease\": [{\"coding\": [{\"system\": \"http://snomed.info/sct\", \"code\": \"840539006\", \"display\": \"Disease caused by severe acute respiratory syndrome coronavirus 2\"}]}]}]}"
8796
}
8897

8998
# Act
9099
result = wrapped_function(event, {})
91100

92101
# Assert
93102
self.assertEqual(result, "Success")
94-
self.assertEqual(mock_logger.info.call_count, 2)
103+
104+
for logger_info in self.extract_all_call_args_for_logger(mock_logger):
105+
self.assertNotIn("9693632109", str(logger_info))
95106

96107
def test_exception_handling(self, mock_logger, mock_firehose_logger):
97108
# Arrange

0 commit comments

Comments
 (0)