Skip to content

Commit db4cc8f

Browse files
committed
SDK-1229: Add test loading ExtraData from Protobuf fixture file
1 parent a80fa27 commit db4cc8f

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

yoti_python_sdk/issuance_details.py

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

3+
from yoti_python_sdk.protobuf.protobuf import Protobuf
4+
35

46
class IssuanceDetails(object):
57
def __init__(self, data_entry):
6-
self.__token = getattr(data_entry.value, "issuance_token").decode()
7-
self.__expiry_date = data_entry.value.issuing_attributes.expiry_date
8-
self.__attributes = data_entry.value.issuing_attributes.definitions
8+
if isinstance(data_entry.value, bytes):
9+
value = Protobuf.thirdparty_attribute(data_entry.value)
10+
else:
11+
value = data_entry.value
12+
self.__token = value.issuance_token.decode()
13+
self.__expiry_date = value.issuing_attributes.expiry_date
14+
self.__attributes = value.issuing_attributes.definitions
915

1016
@property
1117
def token(self):

yoti_python_sdk/protobuf/protobuf.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from cryptography.fernet import base64
55
from yoti_python_sdk.protobuf.attribute_public_api import Attribute_pb2, List_pb2
66
from yoti_python_sdk.protobuf.common_public_api import EncryptedData_pb2
7+
from yoti_python_sdk.protobuf.share_public_api import ExtraData_pb2
78
from yoti_python_sdk.protobuf.share_public_api import ThirdPartyAttribute_pb2
89

910
class Protobuf(object):
@@ -67,8 +68,12 @@ def multi_value(data):
6768

6869
@staticmethod
6970
def extra_data(data):
70-
decoded_content = base64.b64decode(data)
7171
extra_data = ExtraData_pb2.ExtraData()
72-
attribute.MergeFromString(decoded_content)
72+
extra_data.MergeFromString(data)
7373
return extra_data
7474

75+
@staticmethod
76+
def thirdparty_attribute(data):
77+
thirdparty_attribute = ThirdPartyAttribute_pb2.ThirdPartyAttribute()
78+
thirdparty_attribute.MergeFromString(data)
79+
return thirdparty_attribute
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CmMIBhJfChFzb21lSXNzdWFuY2VUb2tlbhJKChgyMDE5LTEwLTE1VDIyOjA0OjA1LjEyM1oSEwoRY29tLnRoaXJkcGFydHkuaWQSGQoXY29tLnRoaXJkcGFydHkub3RoZXJfaWQ=

yoti_python_sdk/tests/share/test_extra_data.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
from datetime import datetime
44
from types import SimpleNamespace
5+
import os.path
56

67
from yoti_python_sdk.share.extra_data import ExtraData
8+
from yoti_python_sdk.tests import file_helper
9+
from yoti_python_sdk.protobuf import protobuf
10+
11+
FIXTURES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures")
12+
EXTRADATA = os.path.join(FIXTURES_DIR, "testextradata.txt")
713

814

915
def create_third_party_test_data(token_value, expiry_date, *definitions):
@@ -17,14 +23,20 @@ def create_third_party_test_data(token_value, expiry_date, *definitions):
1723
return SimpleNamespace(type=ExtraData.THIRD_PARTY_ATTRIBUTE, value=attribute)
1824

1925

26+
def get_extra_data_from_base64(filepath):
27+
extra_data_bytes = file_helper.get_file_bytes(filepath)
28+
protobuf_extra_data = protobuf.Protobuf.extra_data(extra_data_bytes)
29+
return ExtraData(protobuf_extra_data.list)
30+
31+
2032
def test_attribute_issuance_details_should_return_nil_when_no_data_entries():
2133
extra_data = ExtraData([])
2234

2335
assert extra_data.attribute_issuance_details is None
2436

2537

2638
def test_should_return_first_matching_third_party_attribute():
27-
expiry_date = datetime.now()
39+
expiry_date = datetime.now().isoformat()
2840

2941
thirdparty_attribute1 = create_third_party_test_data(
3042
"tokenValue1", expiry_date, "attributeName1"
@@ -41,3 +53,14 @@ def test_should_return_first_matching_third_party_attribute():
4153
== "attributeName1"
4254
)
4355
assert parsed_extra_data.attribute_issuance_details.expiry_date == expiry_date
56+
57+
58+
def test_should_parse_multiple_issuing_attributes():
59+
extra_data = get_extra_data_from_base64(EXTRADATA)
60+
result = extra_data.attribute_issuance_details
61+
assert result is not None
62+
63+
assert result.token == "someIssuanceToken"
64+
assert result.expiry_date == "2019-10-15T22:04:05.123Z"
65+
assert result.attributes[0].name == "com.thirdparty.id"
66+
assert result.attributes[1].name == "com.thirdparty.other_id"

0 commit comments

Comments
 (0)