Skip to content

Commit b94f164

Browse files
committed
SDK-1229: Add first test to test_issuance_details
Implement datetime parser for protobuf times as workaround to old Python not being able to fully parse milli/microsecond timestamps
1 parent 1983f4b commit b94f164

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

yoti_python_sdk/issuance_details.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# -*- coding: utf-8 -*-
22

3+
from datetime import datetime
4+
import re
5+
36

47
class IssuanceDetails(object):
58
def __init__(self, data_entry):
@@ -8,7 +11,20 @@ def __init__(self, data_entry):
811
data_entry.issuing_attributes.expiry_date != ""
912
and data_entry.issuing_attributes.expiry_date is not None
1013
):
11-
self.__expiry_date = data_entry.issuing_attributes.expiry_date
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
1228
else:
1329
self.__expiry_date = None
1430
self.__attributes = data_entry.issuing_attributes.definitions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ChFzb21lSXNzdWFuY2VUb2tlbhIvChgyMDE5LTEwLTE1VDIyOjA0OjA1LjEyM1oSEwoRY29tLnRoaXJkcGFydHkuaWQ=

yoti_python_sdk/tests/share/test_extra_data.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ def test_attribute_issuance_details_should_return_nil_when_no_data_entries():
5454

5555

5656
def test_should_return_first_matching_third_party_attribute():
57-
expiry_date = datetime.now().isoformat()
57+
expiry_date = datetime.now()
5858

5959
thirdparty_attribute1 = create_third_party_test_data(
60-
"tokenValue1", expiry_date, "attributeName1"
60+
"tokenValue1", expiry_date.isoformat(), "attributeName1"
6161
)
6262
thirdparty_attribute2 = create_third_party_test_data(
63-
"tokenValue2", expiry_date, "attributeName2"
63+
"tokenValue2", expiry_date.isoformat(), "attributeName2"
6464
)
6565

6666
parsed_extra_data = ExtraData(
@@ -81,7 +81,7 @@ def test_should_parse_multiple_issuing_attributes():
8181
result = extra_data.attribute_issuance_details
8282
assert result is not None
8383
assert result.token == "someIssuanceToken"
84-
assert result.expiry_date == "2019-10-15T22:04:05.123Z"
84+
assert result.expiry_date == datetime(2019, 10, 15, 22, 4, 5, 123000)
8585
assert result.attributes[0].name == "com.thirdparty.id"
8686
assert result.attributes[1].name == "com.thirdparty.other_id"
8787

@@ -110,8 +110,10 @@ def test_should_handle_no_issuing_attributes():
110110

111111
def test_should_handle_no_issuing_attribute_definitions():
112112
tokenValue = "tokenValue"
113-
expiry_date = datetime.now().isoformat()
114-
thirdparty_attribute = create_third_party_test_data(tokenValue, expiry_date)
113+
expiry_date = datetime.now()
114+
thirdparty_attribute = create_third_party_test_data(
115+
tokenValue, expiry_date.isoformat()
116+
)
115117
extra_data = ExtraData(create_extra_data([thirdparty_attribute]))
116118

117119
result = extra_data.attribute_issuance_details
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os.path
4+
from yoti_python_sdk.tests import file_helper
5+
from yoti_python_sdk.issuance_details import IssuanceDetails
6+
from yoti_python_sdk.protobuf.share_public_api import ThirdPartyAttribute_pb2
7+
from datetime import datetime
8+
9+
FIXTURES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures")
10+
THIRD_PARTY_ATTRIBUTE = os.path.join(FIXTURES_DIR, "testthirdpartyattribute.txt")
11+
12+
13+
def test_should_parse_third_party_attribute_correctly():
14+
thirdparty_attribute_bytes = file_helper.get_file_bytes(THIRD_PARTY_ATTRIBUTE)
15+
16+
proto = ThirdPartyAttribute_pb2.ThirdPartyAttribute()
17+
proto.MergeFromString(thirdparty_attribute_bytes)
18+
19+
issuance_details = IssuanceDetails(proto)
20+
21+
assert issuance_details.attributes[0].name == "com.thirdparty.id"
22+
assert issuance_details.token == "someIssuanceToken"
23+
assert issuance_details.expiry_date == datetime(2019, 10, 15, 22, 4, 5, 123000)

0 commit comments

Comments
 (0)