Skip to content

Commit 4f49501

Browse files
committed
Fix timestamp validation for EA and batch
1 parent 6ce912d commit 4f49501

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

lambdas/filenameprocessor/src/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
Exception: 500,
3939
}
4040

41+
# Filename timestamp constants
42+
VALID_TIMESTAMP_LENGTH = 17
43+
VALID_TIMEZONE_OFFSETS = {"00", "01"}
44+
4145

4246
class FileStatus(StrEnum):
4347
"""File status constants"""

lambdas/filenameprocessor/src/file_name_processor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ def handle_extended_attributes_file(
264264
FileStatus.PROCESSING,
265265
)
266266

267-
# TODO: agree the prefix with DPS
268267
dest_file_key = f"{DPS_DESTINATION_PREFIX}/{file_key}"
269268
copy_file_to_external_bucket(
270269
bucket_name,

lambdas/filenameprocessor/src/file_validation.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
from datetime import datetime
44
from re import match
55

6-
from constants import EXTENDED_ATTRIBUTES_FILE_PREFIX, EXTENDED_ATTRIBUTES_VACC_TYPE, VALID_EA_VERSIONS, VALID_VERSIONS
6+
from constants import (
7+
EXTENDED_ATTRIBUTES_FILE_PREFIX,
8+
EXTENDED_ATTRIBUTES_VACC_TYPE,
9+
VALID_EA_VERSIONS,
10+
VALID_TIMESTAMP_LENGTH,
11+
VALID_TIMEZONE_OFFSETS,
12+
VALID_VERSIONS,
13+
)
714
from elasticache import (
815
get_supplier_system_from_cache,
916
get_valid_vaccine_types_from_cache,
@@ -20,20 +27,32 @@ def is_file_in_directory_root(file_key: str) -> bool:
2027

2128
def is_valid_datetime(timestamp: str) -> bool:
2229
"""
23-
Returns a bool to indicate whether the timestamp is a valid datetime in the format 'YYYYmmddTHHMMSSzz'
24-
where 'zz' is a two digit number indicating the timezone
30+
Returns a bool to indicate whether the file timestamp matches the expected NHS file submission format:
31+
- YYYY = Year
32+
- MM = Month (01-12)
33+
- DD = Date (01-31)
34+
- T = fixed value of “T”
35+
- hh = Hours (00-23)
36+
- mm = Minutes (00-59)
37+
- ss = Seconds (00-59)
38+
- time zone offset = 00 for GMT, 01 for BST
39+
40+
e.g. 20220514T10081501
2541
"""
2642
# Check that datetime (excluding timezone) is a valid datetime in the expected format.
27-
if len(timestamp) < 15:
43+
if len(timestamp) != VALID_TIMESTAMP_LENGTH:
2844
return False
2945

30-
# Note that any digits after the seconds (i.e. from the 16th character onwards, usually expected to represent
31-
# timezone), do not need to be validated
3246
try:
3347
datetime.strptime(timestamp[:15], "%Y%m%dT%H%M%S")
3448
except ValueError:
3549
return False
3650

51+
time_zone_offset = timestamp[-2:]
52+
53+
if time_zone_offset not in VALID_TIMEZONE_OFFSETS:
54+
return False
55+
3756
return True
3857

3958

lambdas/filenameprocessor/tests/test_file_key_validation.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ def test_is_valid_datetime(self, _):
4545
"""Tests that is_valid_datetime returns True for valid datetimes, and false otherwise"""
4646
# Test case tuples are structured as (date_time_string, expected_result)
4747
test_cases = [
48-
("20200101T12345600", True), # Valid datetime string with timezone
49-
("20200101T123456", True), # Valid datetime string without timezone
48+
("20200101T12345600", True), # Valid datetime string with timezone offset "00" GMT
49+
("20200101T12345601", True), # Valid datetime string with timezone offset "01" BST
50+
("20200101T123456", False), # Invalid datetime string without timezone
5051
(
5152
"20200101T123456extracharacters",
52-
True,
53-
), # Valid datetime string with additional characters
53+
False,
54+
), # Invalid datetime string with additional characters
5455
("20201301T12345600", False), # Invalid month
5556
("20200100T12345600", False), # Invalid day
5657
("20200230T12345600", False), # Invalid combination of month and day
@@ -124,7 +125,7 @@ def test_validate_extended_attributes_file_key(self, mock_get_redis_client):
124125
),
125126
# Valid extended attributes file key with different organization code
126127
(
127-
"Vaccination_Extended_Attributes_v1_5_YGM41_20221231T23595999.csv",
128+
"Vaccination_Extended_Attributes_v1_5_YGM41_20221231T23595900.csv",
128129
"YGM41",
129130
),
130131
]

0 commit comments

Comments
 (0)