Skip to content

Commit 3a7f79e

Browse files
committed
SDK-1229: Implement test for invalid dates and move date parser
1 parent b94f164 commit 3a7f79e

File tree

3 files changed

+63
-24
lines changed

3 files changed

+63
-24
lines changed

yoti_python_sdk/date_parser.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# -*- coding: utf-8 -*-
22

3-
import datetime
3+
from datetime import datetime, date
4+
import logging
5+
import re
6+
7+
8+
ERROR_PARSING_DATE = "Error parsing date"
49

510

611
def from_iso_format(string):
@@ -9,4 +14,29 @@ def from_iso_format(string):
914
if len(parts) != 3:
1015
raise ValueError
1116

12-
return datetime.date(parts[0], parts[1], parts[2])
17+
return date(parts[0], parts[1], parts[2])
18+
19+
20+
def datetime_with_microsecond(string):
21+
# Python2 does not have a way of parsing date formats.
22+
# TODO Deprecate this once Python2 support is dropped.
23+
time_split = re.split("[^0-9]", string)
24+
parts = len(time_split)
25+
if parts <= 6:
26+
if logging.getLogger().propagate:
27+
logging.warning(ERROR_PARSING_DATE)
28+
return None
29+
30+
year = int(time_split[0]) if parts > 0 else 0
31+
month = int(time_split[1]) if parts > 1 else 0
32+
day = int(time_split[2]) if parts > 2 else 0
33+
hour = int(time_split[3]) if parts > 3 else 0
34+
minute = int(time_split[4]) if parts > 4 else 0
35+
second = int(time_split[5]) if parts > 5 else 0
36+
microsecond = int(float("0." + time_split[6]) * 1e6) if parts > 6 else 0
37+
try:
38+
return datetime(year, month, day, hour, minute, second, microsecond)
39+
except ValueError:
40+
if logging.getLogger().propagate:
41+
logging.warning(ERROR_PARSING_DATE)
42+
return None

yoti_python_sdk/issuance_details.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
# -*- coding: utf-8 -*-
22

3-
from datetime import datetime
4-
import re
3+
from yoti_python_sdk import date_parser
54

65

76
class IssuanceDetails(object):
87
def __init__(self, data_entry):
98
self.__token = data_entry.issuance_token.decode()
10-
if (
11-
data_entry.issuing_attributes.expiry_date != ""
12-
and data_entry.issuing_attributes.expiry_date is not None
13-
):
14-
time_split = re.split("[^0-9]", data_entry.issuing_attributes.expiry_date)
15-
print("!DEBUG! %s" % time_split)
16-
assert len(time_split) >= 7
17-
year = int(time_split[0])
18-
month = int(time_split[1])
19-
day = int(time_split[2])
20-
hour = int(time_split[3])
21-
minute = int(time_split[4])
22-
second = int(time_split[5])
23-
microsecond = int(float("0." + time_split[6]) * 1e6)
24-
self.__expiry_date = datetime(
25-
year, month, day, hour, minute, second, microsecond
26-
)
27-
# TODO: Refactor time parser into own method
28-
else:
29-
self.__expiry_date = None
9+
self.__expiry_date = date_parser.datetime_with_microsecond(
10+
data_entry.issuing_attributes.expiry_date
11+
)
3012
self.__attributes = data_entry.issuing_attributes.definitions
3113

3214
@property

yoti_python_sdk/tests/test_issuance_details.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,28 @@
44
from yoti_python_sdk.tests import file_helper
55
from yoti_python_sdk.issuance_details import IssuanceDetails
66
from yoti_python_sdk.protobuf.share_public_api import ThirdPartyAttribute_pb2
7+
from yoti_python_sdk.protobuf.share_public_api import IssuingAttributes_pb2
78
from datetime import datetime
9+
import pytest
810

911
FIXTURES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures")
1012
THIRD_PARTY_ATTRIBUTE = os.path.join(FIXTURES_DIR, "testthirdpartyattribute.txt")
1113

1214

15+
def create_issuance_test_proto(issuance_token, expiry_date, *definitions):
16+
issuing_attributes = IssuingAttributes_pb2.IssuingAttributes()
17+
issuing_attributes.expiry_date = expiry_date
18+
for s in definitions:
19+
name = IssuingAttributes_pb2.Definition()
20+
name.name = s
21+
issuing_attributes.definitions.extend([name])
22+
23+
attribute = ThirdPartyAttribute_pb2.ThirdPartyAttribute()
24+
attribute.issuance_token = bytes(issuance_token, "utf-8")
25+
attribute.issuing_attributes.MergeFrom(issuing_attributes)
26+
return attribute
27+
28+
1329
def test_should_parse_third_party_attribute_correctly():
1430
thirdparty_attribute_bytes = file_helper.get_file_bytes(THIRD_PARTY_ATTRIBUTE)
1531

@@ -21,3 +37,14 @@ def test_should_parse_third_party_attribute_correctly():
2137
assert issuance_details.attributes[0].name == "com.thirdparty.id"
2238
assert issuance_details.token == "someIssuanceToken"
2339
assert issuance_details.expiry_date == datetime(2019, 10, 15, 22, 4, 5, 123000)
40+
41+
42+
@pytest.mark.parametrize(
43+
"expiry_date", ["2006-13-02T15:04:05.000Z", "", "2006-13-02T15:04:05"]
44+
)
45+
def test_should_return_none_if_error_in_parsing_date(expiry_date):
46+
proto = create_issuance_test_proto("someToken", expiry_date)
47+
48+
issuance_details = IssuanceDetails(proto)
49+
50+
assert issuance_details.expiry_date is None

0 commit comments

Comments
 (0)