1
1
# -*- coding: utf-8 -*-
2
2
3
3
from datetime import datetime
4
- from types import SimpleNamespace
5
4
import os .path
6
- import pytest
7
5
8
6
from yoti_python_sdk .share .extra_data import ExtraData
9
7
from yoti_python_sdk .tests import file_helper
10
8
from yoti_python_sdk .protobuf import protobuf
11
9
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
+
12
15
FIXTURES_DIR = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "fixtures" )
13
16
EXTRADATA = os .path .join (FIXTURES_DIR , "testextradata.txt" )
14
17
15
18
16
19
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
25
42
26
43
27
44
def get_extra_data_from_base64 (filepath ):
28
45
extra_data_bytes = file_helper .get_file_bytes (filepath )
29
46
protobuf_extra_data = protobuf .Protobuf .extra_data (extra_data_bytes )
30
- return ExtraData (protobuf_extra_data . list )
47
+ return ExtraData (protobuf_extra_data )
31
48
32
49
33
50
def test_attribute_issuance_details_should_return_nil_when_no_data_entries ():
34
- extra_data = ExtraData ([] )
51
+ extra_data = ExtraData (create_extra_data ([]) )
35
52
36
53
assert extra_data .attribute_issuance_details is None
37
54
@@ -46,7 +63,9 @@ def test_should_return_first_matching_third_party_attribute():
46
63
"tokenValue2" , expiry_date , "attributeName2"
47
64
)
48
65
49
- parsed_extra_data = ExtraData ([thirdparty_attribute1 , thirdparty_attribute2 ])
66
+ parsed_extra_data = ExtraData (
67
+ create_extra_data ([thirdparty_attribute1 , thirdparty_attribute2 ])
68
+ )
50
69
51
70
assert parsed_extra_data .attribute_issuance_details .token == "tokenValue1"
52
71
assert (
@@ -58,32 +77,32 @@ def test_should_return_first_matching_third_party_attribute():
58
77
59
78
def test_should_parse_multiple_issuing_attributes ():
60
79
extra_data = get_extra_data_from_base64 (EXTRADATA )
80
+
61
81
result = extra_data .attribute_issuance_details
62
82
assert result is not None
63
-
64
83
assert result .token == "someIssuanceToken"
65
84
assert result .expiry_date == "2019-10-15T22:04:05.123Z"
66
85
assert result .attributes [0 ].name == "com.thirdparty.id"
67
86
assert result .attributes [1 ].name == "com.thirdparty.other_id"
68
87
69
88
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 ():
72
90
tokenValue = "tokenValue"
91
+ no_expiry = ""
73
92
thirdparty_attribute = create_third_party_test_data (
74
93
tokenValue , no_expiry , "attribute.name"
75
94
)
76
- extra_data = ExtraData ([thirdparty_attribute ])
95
+ extra_data = ExtraData (create_extra_data ( [thirdparty_attribute ]) )
77
96
78
97
result = extra_data .attribute_issuance_details
79
-
80
98
assert result .expiry_date is None
81
99
82
100
83
101
def test_should_handle_no_issuing_attributes ():
84
102
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
+
87
106
result = extra_data .attribute_issuance_details
88
107
assert result .token == "tokenValue"
89
108
assert len (result .attributes ) == 0
@@ -93,8 +112,8 @@ def test_should_handle_no_issuing_attribute_definitions():
93
112
tokenValue = "tokenValue"
94
113
expiry_date = datetime .now ().isoformat ()
95
114
thirdparty_attribute = create_third_party_test_data (tokenValue , expiry_date )
115
+ extra_data = ExtraData (create_extra_data ([thirdparty_attribute ]))
96
116
97
- extra_data = ExtraData ([thirdparty_attribute ])
98
117
result = extra_data .attribute_issuance_details
99
118
assert result .token == tokenValue
100
119
assert result .expiry_date == expiry_date
0 commit comments