Skip to content

Commit 1983f4b

Browse files
committed
SDK-1229: Remove SimpleNamespace fixtures
Use Protobuf objects for all testing in test_extra_data, without relying on SimpleNamespace mocks.
1 parent ae86d42 commit 1983f4b

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

yoti_python_sdk/issuance_details.py

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

3-
from yoti_python_sdk.protobuf.protobuf import Protobuf
4-
53

64
class IssuanceDetails(object):
75
def __init__(self, data_entry):
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()
6+
self.__token = data_entry.issuance_token.decode()
137
if (
14-
value.issuing_attributes.expiry_date != ""
15-
and value.issuing_attributes.expiry_date is not None
8+
data_entry.issuing_attributes.expiry_date != ""
9+
and data_entry.issuing_attributes.expiry_date is not None
1610
):
17-
self.__expiry_date = value.issuing_attributes.expiry_date
11+
self.__expiry_date = data_entry.issuing_attributes.expiry_date
1812
else:
1913
self.__expiry_date = None
20-
self.__attributes = value.issuing_attributes.definitions
14+
self.__attributes = data_entry.issuing_attributes.definitions
2115

2216
@property
2317
def token(self):

yoti_python_sdk/share/extra_data.py

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

33
from yoti_python_sdk.issuance_details import IssuanceDetails
4+
from yoti_python_sdk.protobuf.share_public_api import ThirdPartyAttribute_pb2
45

56

67
class ExtraData(object):
78
THIRD_PARTY_ATTRIBUTE = 6
89

9-
def __init__(self, data_entries_list):
10+
def __init__(self, proto):
1011
self.__attribute_issuance_details = None
12+
data_entries_list = proto.list
1113

1214
for data_entry in data_entries_list:
1315
if (
1416
data_entry.type == self.THIRD_PARTY_ATTRIBUTE
1517
and self.__attribute_issuance_details is None
1618
):
17-
self.__attribute_issuance_details = IssuanceDetails(data_entry)
19+
attribute = ThirdPartyAttribute_pb2.ThirdPartyAttribute()
20+
attribute.MergeFromString(data_entry.value)
21+
self.__attribute_issuance_details = IssuanceDetails(attribute)
1822

1923
@property
2024
def attribute_issuance_details(self):

yoti_python_sdk/tests/share/test_extra_data.py

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
11
# -*- coding: utf-8 -*-
22

33
from datetime import datetime
4-
from types import SimpleNamespace
54
import os.path
6-
import pytest
75

86
from yoti_python_sdk.share.extra_data import ExtraData
97
from yoti_python_sdk.tests import file_helper
108
from yoti_python_sdk.protobuf import protobuf
119

10+
from yoti_python_sdk.protobuf.share_public_api import IssuingAttributes_pb2
11+
from yoti_python_sdk.protobuf.share_public_api import ThirdPartyAttribute_pb2
12+
from yoti_python_sdk.protobuf.share_public_api import ExtraData_pb2
13+
from yoti_python_sdk.protobuf.share_public_api import DataEntry_pb2
14+
1215
FIXTURES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures")
1316
EXTRADATA = os.path.join(FIXTURES_DIR, "testextradata.txt")
1417

1518

1619
def create_third_party_test_data(token_value, expiry_date, *definitions):
17-
attribute = SimpleNamespace(
18-
issuance_token=bytes(token_value, "utf-8"),
19-
issuing_attributes=SimpleNamespace(
20-
expiry_date=expiry_date,
21-
definitions=[SimpleNamespace(name=s) for s in definitions],
22-
),
23-
)
24-
return SimpleNamespace(type=ExtraData.THIRD_PARTY_ATTRIBUTE, value=attribute)
20+
issuing_attributes = IssuingAttributes_pb2.IssuingAttributes()
21+
issuing_attributes.expiry_date = expiry_date
22+
for s in definitions:
23+
name = IssuingAttributes_pb2.Definition()
24+
name.name = s
25+
issuing_attributes.definitions.extend([name])
26+
27+
attribute = ThirdPartyAttribute_pb2.ThirdPartyAttribute()
28+
attribute.issuance_token = bytes(token_value, "utf-8")
29+
attribute.issuing_attributes.MergeFrom(issuing_attributes)
30+
31+
data_entry = DataEntry_pb2.DataEntry()
32+
data_entry.type = ExtraData.THIRD_PARTY_ATTRIBUTE
33+
data_entry.value = attribute.SerializeToString()
34+
35+
return data_entry
36+
37+
38+
def create_extra_data(data_entry_list):
39+
extra_data = ExtraData_pb2.ExtraData()
40+
extra_data.list.extend(data_entry_list)
41+
return extra_data
2542

2643

2744
def get_extra_data_from_base64(filepath):
2845
extra_data_bytes = file_helper.get_file_bytes(filepath)
2946
protobuf_extra_data = protobuf.Protobuf.extra_data(extra_data_bytes)
30-
return ExtraData(protobuf_extra_data.list)
47+
return ExtraData(protobuf_extra_data)
3148

3249

3350
def test_attribute_issuance_details_should_return_nil_when_no_data_entries():
34-
extra_data = ExtraData([])
51+
extra_data = ExtraData(create_extra_data([]))
3552

3653
assert extra_data.attribute_issuance_details is None
3754

@@ -46,7 +63,9 @@ def test_should_return_first_matching_third_party_attribute():
4663
"tokenValue2", expiry_date, "attributeName2"
4764
)
4865

49-
parsed_extra_data = ExtraData([thirdparty_attribute1, thirdparty_attribute2])
66+
parsed_extra_data = ExtraData(
67+
create_extra_data([thirdparty_attribute1, thirdparty_attribute2])
68+
)
5069

5170
assert parsed_extra_data.attribute_issuance_details.token == "tokenValue1"
5271
assert (
@@ -58,32 +77,32 @@ def test_should_return_first_matching_third_party_attribute():
5877

5978
def test_should_parse_multiple_issuing_attributes():
6079
extra_data = get_extra_data_from_base64(EXTRADATA)
80+
6181
result = extra_data.attribute_issuance_details
6282
assert result is not None
63-
6483
assert result.token == "someIssuanceToken"
6584
assert result.expiry_date == "2019-10-15T22:04:05.123Z"
6685
assert result.attributes[0].name == "com.thirdparty.id"
6786
assert result.attributes[1].name == "com.thirdparty.other_id"
6887

6988

70-
@pytest.mark.parametrize("no_expiry", ["", None])
71-
def test_should_handle_no_expiry_date(no_expiry):
89+
def test_should_handle_no_expiry_date():
7290
tokenValue = "tokenValue"
91+
no_expiry = ""
7392
thirdparty_attribute = create_third_party_test_data(
7493
tokenValue, no_expiry, "attribute.name"
7594
)
76-
extra_data = ExtraData([thirdparty_attribute])
95+
extra_data = ExtraData(create_extra_data([thirdparty_attribute]))
7796

7897
result = extra_data.attribute_issuance_details
79-
8098
assert result.expiry_date is None
8199

82100

83101
def test_should_handle_no_issuing_attributes():
84102
tokenValue = "tokenValue"
85-
thirdparty_attribute = create_third_party_test_data(tokenValue, None)
86-
extra_data = ExtraData([thirdparty_attribute])
103+
thirdparty_attribute = create_third_party_test_data(tokenValue, "")
104+
extra_data = ExtraData(create_extra_data([thirdparty_attribute]))
105+
87106
result = extra_data.attribute_issuance_details
88107
assert result.token == "tokenValue"
89108
assert len(result.attributes) == 0
@@ -93,8 +112,8 @@ def test_should_handle_no_issuing_attribute_definitions():
93112
tokenValue = "tokenValue"
94113
expiry_date = datetime.now().isoformat()
95114
thirdparty_attribute = create_third_party_test_data(tokenValue, expiry_date)
115+
extra_data = ExtraData(create_extra_data([thirdparty_attribute]))
96116

97-
extra_data = ExtraData([thirdparty_attribute])
98117
result = extra_data.attribute_issuance_details
99118
assert result.token == tokenValue
100119
assert result.expiry_date == expiry_date

0 commit comments

Comments
 (0)