Skip to content

Commit 99b630b

Browse files
committed
SDK-900: Parse all JSON attributes, disable logging in specific tests
1 parent 16eae0a commit 99b630b

File tree

4 files changed

+70
-19
lines changed

4 files changed

+70
-19
lines changed

yoti_python_sdk/profile.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
import collections
3-
import json
42
import logging
53

64
from yoti_python_sdk import config
@@ -25,12 +23,10 @@ def __init__(self, profile_attributes):
2523

2624
self.attributes[field.name] = Attribute(field.name, value, anchors)
2725

28-
if field.name == config.ATTRIBUTE_STRUCTURED_POSTAL_ADDRESS:
29-
self.try_convert_structured_postal_address_to_dict(field, anchors)
30-
3126
except Exception as exc:
3227
error = 'Error parsing profile attribute: "{0}"'.format(field.name)
33-
logging.warning('error: {0}, exception: {1} - {2}'.format(error, type(exc).__name__, exc))
28+
if logging.getLogger().propagate:
29+
logging.warning('error: {0}, exception: {1} - {2}'.format(error, type(exc).__name__, exc))
3430

3531
self.ensure_postal_address()
3632

@@ -84,15 +80,6 @@ def get_attribute(self, attribute_name):
8480
else:
8581
return None
8682

87-
def try_convert_structured_postal_address_to_dict(self, field, anchors):
88-
decoder = json.JSONDecoder(object_pairs_hook=collections.OrderedDict, strict=False)
89-
value_to_decode = field.value.decode()
90-
91-
self.attributes[config.ATTRIBUTE_STRUCTURED_POSTAL_ADDRESS] = Attribute(
92-
config.ATTRIBUTE_STRUCTURED_POSTAL_ADDRESS,
93-
decoder.decode(value_to_decode),
94-
anchors)
95-
9683
def ensure_postal_address(self):
9784
if config.ATTRIBUTE_POSTAL_ADDRESS not in self.attributes and config.ATTRIBUTE_STRUCTURED_POSTAL_ADDRESS in self.attributes:
9885
structured_postal_address = self.attributes[config.ATTRIBUTE_STRUCTURED_POSTAL_ADDRESS]

yoti_python_sdk/protobuf/protobuf.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4+
import collections
5+
import json
46
import logging
57

68
from cryptography.fernet import base64
@@ -50,12 +52,16 @@ def value_based_on_content_type(self, value, content_type=None):
5052
elif content_type == self.CT_JPEG \
5153
or content_type == self.CT_PNG:
5254
return value
55+
elif content_type == self.CT_JSON:
56+
return self.convert_to_dict(value)
5357
elif content_type == self.CT_INT:
5458
string_value = value.decode('utf-8')
5559
int_value = int(string_value)
5660
return int_value
5761

58-
logging.warning("Unknown type '{0}', attempting to parse it as a String".format(content_type))
62+
if logging.getLogger().propagate:
63+
logging.warning("Unknown type '{0}', attempting to parse it as a String".format(content_type))
64+
5965
return value.decode('utf-8')
6066

6167
def image_uri_based_on_content_type(self, value, content_type=None):
@@ -66,3 +72,10 @@ def image_uri_based_on_content_type(self, value, content_type=None):
6672
data = base64.b64encode(value).decode('utf-8')
6773
return 'data:image/png;base64,{0}'.format(data)
6874
return value
75+
76+
@staticmethod
77+
def convert_to_dict(byte_value):
78+
decoder = json.JSONDecoder(object_pairs_hook=collections.OrderedDict, strict=False)
79+
value_to_decode = byte_value.decode()
80+
81+
return decoder.decode(value_to_decode)

yoti_python_sdk/tests/test_profile.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import collections
33
import json
4+
import logging
45

56
import pytest
67

@@ -122,8 +123,14 @@ def test_error_parsing_attribute_has_none_value():
122123
anchors=None,
123124
content_type=Protobuf.CT_INT)
124125

126+
# disable logging for the below call: warning shown as int is invalid
127+
logger = logging.getLogger()
128+
logger.propagate = False
129+
125130
profile = Profile(attribute_list)
126131

132+
logger.propagate = True
133+
127134
assert profile.get_attribute(int_attribute_name) is None
128135

129136

@@ -146,8 +153,14 @@ def test_error_parsing_attribute_does_not_affect_other_attribute():
146153
anchors=None,
147154
content_type=Protobuf.CT_INT))
148155

156+
# disable logging for the below call: warning shown as int is invalid
157+
logger = logging.getLogger()
158+
logger.propagate = False
159+
149160
profile = Profile(attribute_list)
150161

162+
logger.propagate = True
163+
151164
assert len(profile.attributes) == 1
152165

153166
retrieved_string_attribute = profile.get_attribute(string_attribute_name)
@@ -182,6 +195,33 @@ def test_try_parse_structured_postal_address_uk():
182195
assert actual_structured_postal_address_profile[config.KEY_FORMATTED_ADDRESS] == FORMATTED_ADDRESS_VALUE
183196

184197

198+
def test_other_json_type_is_parsed():
199+
json_attribute_name = "other_json"
200+
key_a = "keyA"
201+
key_b = "keyB"
202+
value_a = "valueA"
203+
value_b = "valueB"
204+
json_value = {key_a: value_a,
205+
key_b: value_b}
206+
207+
encoded_json = json.dumps(json_value).encode()
208+
209+
attribute_list = create_single_attribute_list(
210+
name=json_attribute_name,
211+
value=encoded_json,
212+
anchors=None,
213+
content_type=Protobuf.CT_JSON)
214+
215+
profile = Profile(attribute_list)
216+
217+
retrieved_attribute = profile.get_attribute(json_attribute_name)
218+
219+
assert retrieved_attribute.name == json_attribute_name
220+
assert type(retrieved_attribute.value) is collections.OrderedDict
221+
assert retrieved_attribute.value[key_a] == value_a
222+
assert retrieved_attribute.value[key_b] == value_b
223+
224+
185225
def test_try_parse_structured_postal_address_india():
186226
structured_postal_address = {ADDRESS_FORMAT_KEY: INDIA_FORMAT_VALUE,
187227
CARE_OF_KEY: CARE_OF_VALUE,

yoti_python_sdk/tests/test_protobuf.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import logging
3+
24
import pytest
35

46
from yoti_python_sdk.protobuf import protobuf
@@ -14,9 +16,6 @@ def test_protobuf_value_based_on_content_type(proto):
1416
byte_value = str.encode(string_value)
1517
int_value = int(string_value)
1618

17-
result = proto.value_based_on_content_type(byte_value, proto.CT_UNDEFINED)
18-
assert result == string_value
19-
2019
result = proto.value_based_on_content_type(byte_value, proto.CT_STRING)
2120
assert result == string_value
2221

@@ -32,9 +31,21 @@ def test_protobuf_value_based_on_content_type(proto):
3231
result = proto.value_based_on_content_type(byte_value, proto.CT_INT)
3332
assert result == int_value
3433

34+
# disable logging for the below types: warning shown as type is not recognized
35+
logger = logging.getLogger()
36+
logger.propagate = False
37+
38+
result = proto.value_based_on_content_type(byte_value, proto.CT_UNDEFINED)
39+
assert result == string_value
40+
3541
result = proto.value_based_on_content_type(byte_value)
3642
assert result == string_value
3743

44+
result = proto.value_based_on_content_type(byte_value, 100)
45+
assert result == string_value
46+
47+
logger.propagate = True
48+
3849

3950
def test_protobuf_image_uri_based_on_content_type(proto):
4051
value = b'test string'

0 commit comments

Comments
 (0)