Skip to content

Commit 01ec7ba

Browse files
committed
SDK-594: Refactor image handling
1 parent a506660 commit 01ec7ba

File tree

5 files changed

+38
-29
lines changed

5 files changed

+38
-29
lines changed

yoti_python_sdk/activity_details.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ def __init__(self, receipt, decrypted_profile=None):
2626
field.content_type
2727
)
2828

29-
self.user_profile[field.name] = value
30-
3129
if field.name == config.ATTRIBUTE_SELFIE:
3230
self.try_parse_selfie_field(field)
31+
value = field.value
32+
33+
self.user_profile[field.name] = value
3334

3435
if field.name.startswith(config.ATTRIBUTE_AGE_OVER) or field.name.startswith(
3536
config.ATTRIBUTE_AGE_UNDER):

yoti_python_sdk/image.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
class Image:
55
def __init__(self, image_bytes, image_content_type):
6-
if image_content_type == Protobuf.CT_JPEG or image_content_type == Protobuf.CT_PNG:
6+
if image_content_type in Image.allowed_types():
77
self.__data = image_bytes
88
self.__content_type = image_content_type
99
else:
1010
raise TypeError("Content type '{0}' is not a supported image type".format(image_content_type))
1111

12+
@staticmethod
13+
def allowed_types():
14+
return [Protobuf.CT_PNG, Protobuf.CT_JPEG]
15+
1216
@property
1317
def data(self):
1418
return self.__data

yoti_python_sdk/profile.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ def __init__(self, profile_attributes):
2020
field.content_type
2121
)
2222

23-
# first condition will be removed in v3.0.0, so selfie also returns an Image object
24-
if field.name != config.ATTRIBUTE_SELFIE and \
25-
(field.content_type == Protobuf.CT_JPEG \
26-
or field.content_type == Protobuf.CT_PNG):
27-
value = Image(field.value, field.content_type)
23+
# this will be removed in v3.0.0, so selfie also returns an Image object
24+
if field.content_type in Image.allowed_types():
25+
if field.name == config.ATTRIBUTE_SELFIE:
26+
value = field.value
2827

2928
anchors = Anchor().parse_anchors(field.anchors)
3029

yoti_python_sdk/protobuf/protobuf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ def anchor(data):
4545
return anchor
4646

4747
def value_based_on_content_type(self, value, content_type=None):
48+
from yoti_python_sdk.image import Image
4849
if content_type == self.CT_STRING:
4950
return value.decode('utf-8')
5051
elif value == b'':
5152
raise ValueError("Content type: '{0}' should not have an empty value".format(content_type))
5253
elif content_type == self.CT_DATE:
5354
return value.decode('utf-8')
54-
elif content_type == self.CT_JPEG \
55-
or content_type == self.CT_PNG:
56-
return value
55+
elif content_type in Image.allowed_types():
56+
return Image(value, content_type)
5757
elif content_type == self.CT_JSON:
5858
return self.convert_to_dict(value)
5959
elif content_type == self.CT_INT:

yoti_python_sdk/tests/test_protobuf.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,27 @@
55

66
from yoti_python_sdk.protobuf import protobuf
77

8+
string_value = "123"
9+
byte_value = str.encode(string_value)
10+
int_value = int(string_value)
11+
812

913
@pytest.fixture(scope='module')
1014
def proto():
1115
return protobuf.Protobuf()
1216

1317

14-
def test_protobuf_value_based_on_content_type(proto):
15-
string_value = "123"
16-
byte_value = str.encode(string_value)
17-
int_value = int(string_value)
18-
19-
result = proto.value_based_on_content_type(byte_value, proto.CT_STRING)
20-
assert result == string_value
21-
22-
result = proto.value_based_on_content_type(byte_value, proto.CT_DATE)
23-
assert result == string_value
18+
@pytest.mark.parametrize(
19+
"content_type, expected_value",
20+
[(proto().CT_STRING, string_value),
21+
(proto().CT_DATE, string_value),
22+
(proto().CT_INT, int_value)])
23+
def test_protobuf_values_based_on_content_type(content_type, expected_value):
24+
result = proto().value_based_on_content_type(byte_value, content_type)
25+
assert result == expected_value
2426

25-
result = proto.value_based_on_content_type(byte_value, proto.CT_JPEG)
26-
assert result == byte_value
27-
28-
result = proto.value_based_on_content_type(byte_value, proto.CT_PNG)
29-
assert result == byte_value
30-
31-
result = proto.value_based_on_content_type(byte_value, proto.CT_INT)
32-
assert result == int_value
3327

28+
def test_warning_protobuf_values_based_on_content_type(proto):
3429
# disable logging for the below types: warning shown as type is not recognized
3530
logger = logging.getLogger()
3631
logger.propagate = False
@@ -47,6 +42,16 @@ def test_protobuf_value_based_on_content_type(proto):
4742
logger.propagate = True
4843

4944

45+
@pytest.mark.parametrize(
46+
"content_type",
47+
(proto().CT_JPEG,
48+
proto().CT_PNG))
49+
def test_image_value_based_on_content_type(proto, content_type):
50+
result = proto.value_based_on_content_type(byte_value, content_type)
51+
assert result.data == byte_value
52+
assert result.content_type == content_type
53+
54+
5055
def test_protobuf_image_uri_based_on_content_type(proto):
5156
value = b'test string'
5257

0 commit comments

Comments
 (0)