Skip to content

Commit d95537d

Browse files
committed
SDK-856: Allow empty for string content types only
1 parent 4c8b2d5 commit d95537d

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

yoti_python_sdk/anchor.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ def parse_anchors(anchors):
5252
origin_server_certs_item).to_cryptography()
5353

5454
except Exception as exc:
55-
logging.warning(
56-
'Error loading anchor certificate, exception: {0} - {1}'.format(type(exc).__name__, exc))
55+
if logging.getLogger().propagate:
56+
logging.warning(
57+
'Error loading anchor certificate, exception: {0} - {1}'.format(type(exc).__name__, exc))
5758
continue
5859

5960
for i in range(len(crypto_cert.extensions)):
@@ -71,8 +72,9 @@ def parse_anchors(anchors):
7172
parsed_anchors = Anchor.get_values_from_extensions(anc, anchor_type, extensions,
7273
crypto_cert, parsed_anchors)
7374
except Exception as exc:
74-
logging.warning('Error parsing anchor certificate extension, exception: {0} - {1}'.format(
75-
type(exc).__name__, exc))
75+
if logging.getLogger().propagate:
76+
logging.warning('Error parsing anchor certificate extension, exception: {0} - {1}'.format(
77+
type(exc).__name__, exc))
7678
continue
7779

7880
return parsed_anchors

yoti_python_sdk/profile.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ def __init__(self, profile_attributes):
2323

2424
self.attributes[field.name] = Attribute(field.name, value, anchors)
2525

26+
except ValueError as ve:
27+
if logging.getLogger().propagate:
28+
logging.warning(ve)
2629
except Exception as exc:
27-
error = 'Error parsing profile attribute: "{0}"'.format(field.name)
2830
if logging.getLogger().propagate:
29-
logging.warning('error: {0}, exception: {1} - {2}'.format(error, type(exc).__name__, exc))
31+
logging.warning(
32+
'Error parsing profile attribute:{0}, exception: {1} - {2}'.format(field.name, type(exc).__name__, exc))
3033

3134
self.ensure_postal_address()
3235

yoti_python_sdk/protobuf/protobuf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def anchor(data):
4747
def value_based_on_content_type(self, value, content_type=None):
4848
if content_type == self.CT_STRING:
4949
return value.decode('utf-8')
50+
elif value == b'':
51+
raise ValueError("Content type: '{0}' should not have an empty value".format(content_type))
5052
elif content_type == self.CT_DATE:
5153
return value.decode('utf-8')
5254
elif content_type == self.CT_JPEG \

yoti_python_sdk/tests/test_profile.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,42 @@ def test_error_parsing_attribute_has_none_value():
134134
assert profile.get_attribute(int_attribute_name) is None
135135

136136

137+
@pytest.mark.parametrize("content_type", [Protobuf.CT_DATE, Protobuf.CT_INT, Protobuf.CT_JPEG, Protobuf.CT_PNG, Protobuf.CT_JSON, Protobuf.CT_UNDEFINED])
138+
def test_parse_empty_values_returns_none(content_type):
139+
attribute_name = "attribute_name"
140+
141+
attribute_list = create_single_attribute_list(
142+
name=attribute_name,
143+
value=b'',
144+
anchors=None,
145+
content_type=content_type)
146+
147+
# disable logging for the below call: warning logged as value is empty
148+
logger = logging.getLogger()
149+
logger.propagate = False
150+
151+
profile = Profile(attribute_list)
152+
153+
logger.propagate = True
154+
155+
assert profile.get_attribute(attribute_name) is None
156+
157+
158+
@pytest.mark.parametrize("value", [b'', "".encode()])
159+
def test_parse_empty_string_value_returns_attribute(value):
160+
attribute_name = "attribute_name"
161+
162+
attribute_list = create_single_attribute_list(
163+
name=attribute_name,
164+
value=value,
165+
anchors=None,
166+
content_type=Protobuf.CT_STRING)
167+
168+
profile = Profile(attribute_list)
169+
170+
assert profile.get_attribute(attribute_name).value == ""
171+
172+
137173
def test_error_parsing_attribute_does_not_affect_other_attribute():
138174
string_attribute_name = "string_attribute"
139175
int_attribute_name = "int_attribute"

0 commit comments

Comments
 (0)