Skip to content

Commit e174ef0

Browse files
committed
SDK-350: Add source & verifiers filter
1 parent 2072bb3 commit e174ef0

File tree

4 files changed

+88
-34
lines changed

4 files changed

+88
-34
lines changed

yoti_python_sdk/attribute.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from yoti_python_sdk import config
2+
3+
14
class attribute:
25
name = ""
36
value = ""
@@ -16,3 +19,9 @@ def get_value(self):
1619

1720
def get_anchors(self):
1821
return self.anchors
22+
23+
def get_sources(self):
24+
return list(filter(lambda a: a.anchor_type == config.ANCHOR_SOURCE, self.anchors))
25+
26+
def get_verifiers(self):
27+
return list(filter(lambda a: a.anchor_type == config.ANCHOR_VERIFIER, self.anchors))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import binascii
2+
import io
3+
4+
from os.path import abspath, dirname, join
5+
6+
from yoti_python_sdk import anchor
7+
from yoti_python_sdk.protobuf.v1 import protobuf
8+
9+
FIXTURES_DIR = join(dirname(abspath(__file__)), 'fixtures')
10+
ANCHOR_DRIVING_LICENSE = join(FIXTURES_DIR, 'anchor_driving_license.txt')
11+
ANCHOR_PASSPORT = join(FIXTURES_DIR, 'anchor_passport.txt')
12+
ANCHOR_YOTI_ADMIN = join(FIXTURES_DIR, 'anchor_yoti_admin.txt')
13+
14+
15+
def parse_anchor_from_base64_text(file_path):
16+
base64_driving_license_anchor = read_file(file_path)
17+
driving_license_anchor_bytes = binascii.a2b_base64(base64_driving_license_anchor)
18+
19+
protobuf_anchor = protobuf.Protobuf().anchor(driving_license_anchor_bytes)
20+
anchors = list()
21+
anchors.append(protobuf_anchor)
22+
23+
return anchor.Anchor().parse_anchors(anchors)[0]
24+
25+
26+
def get_driving_license_anchor():
27+
return parse_anchor_from_base64_text(ANCHOR_DRIVING_LICENSE)
28+
29+
30+
def get_passport_anchor():
31+
return parse_anchor_from_base64_text(ANCHOR_PASSPORT)
32+
33+
34+
def get_yoti_admin_anchor():
35+
return parse_anchor_from_base64_text(ANCHOR_YOTI_ADMIN)
36+
37+
38+
def read_file(file_path):
39+
with io.open(file_path, mode='r', encoding='utf-8') as file:
40+
return file.read()
Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
# -*- coding: utf-8 -*-
2-
import binascii
32
import datetime
4-
import io
5-
from os.path import abspath, dirname, join
63

7-
from yoti_python_sdk import anchor, config
8-
from yoti_python_sdk.protobuf.v1 import protobuf
9-
10-
FIXTURES_DIR = join(dirname(abspath(__file__)), 'fixtures')
11-
ANCHOR_DRIVING_LICENSE = join(FIXTURES_DIR, 'anchor_driving_license.txt')
12-
ANCHOR_PASSPORT = join(FIXTURES_DIR, 'anchor_passport.txt')
13-
ANCHOR_YOTI_ADMIN = join(FIXTURES_DIR, 'anchor_yoti_admin.txt')
4+
from yoti_python_sdk import config
5+
from yoti_python_sdk.tests import anchor_parser
146

157

168
def test_parse_anchors_driving_license():
17-
parsed_anchor = parse_anchor_from_text(ANCHOR_DRIVING_LICENSE)
9+
parsed_anchor = anchor_parser.get_driving_license_anchor()
1810

1911
assert parsed_anchor.anchor_type == config.ANCHOR_SOURCE
2012
assert parsed_anchor.signed_timestamp == datetime.datetime(2018, 4, 11, 13, 13, 3, 923537)
@@ -23,7 +15,7 @@ def test_parse_anchors_driving_license():
2315

2416

2517
def test_parse_anchors_passport():
26-
parsed_anchor = parse_anchor_from_text(ANCHOR_PASSPORT)
18+
parsed_anchor = anchor_parser.get_passport_anchor()
2719

2820
assert parsed_anchor.anchor_type == config.ANCHOR_SOURCE
2921
assert parsed_anchor.signed_timestamp == datetime.datetime(2018, 4, 12, 14, 14, 32, 835537)
@@ -32,25 +24,9 @@ def test_parse_anchors_passport():
3224

3325

3426
def test_parse_yoti_admin():
35-
parsed_anchor = parse_anchor_from_text(ANCHOR_YOTI_ADMIN)
27+
parsed_anchor = anchor_parser.get_yoti_admin_anchor()
3628

3729
assert parsed_anchor.anchor_type == config.ANCHOR_VERIFIER
3830
assert parsed_anchor.signed_timestamp == datetime.datetime(2018, 4, 11, 13, 13, 4, 95238)
3931
assert parsed_anchor.sub_type == ""
4032
assert parsed_anchor.value == "YOTI_ADMIN"
41-
42-
43-
def parse_anchor_from_text(file_path):
44-
base64_driving_license_anchor = read_file(file_path)
45-
driving_license_anchor_bytes = binascii.a2b_base64(base64_driving_license_anchor)
46-
47-
protobuf_anchor = protobuf.Protobuf().anchor(driving_license_anchor_bytes)
48-
anchors = list()
49-
anchors.append(protobuf_anchor)
50-
51-
return anchor.Anchor().parse_anchors(anchors)[0]
52-
53-
54-
def read_file(file_path):
55-
with io.open(file_path, mode='r', encoding='utf-8') as file:
56-
return file.read()
Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
11
import yoti_python_sdk.attribute
22

3+
from yoti_python_sdk import config
4+
from yoti_python_sdk.tests import anchor_parser
5+
6+
NAME = "name"
7+
VALUE = "value"
8+
39

410
def test_attribute_get_values():
5-
name = "name"
6-
value = "value"
711
parsed_anchors = []
812

9-
attribute = yoti_python_sdk.attribute.attribute(name, value, parsed_anchors)
13+
attribute = yoti_python_sdk.attribute.attribute(NAME, VALUE, parsed_anchors)
1014

11-
assert attribute.get_name() == name
12-
assert attribute.get_value() == value
15+
assert attribute.get_name() == NAME
16+
assert attribute.get_value() == VALUE
1317
assert attribute.get_anchors() == parsed_anchors
18+
19+
20+
def test_attribute_get_sources():
21+
anchors = create_source_and_verifier_anchors()
22+
attribute = yoti_python_sdk.attribute.attribute(NAME, VALUE, anchors)
23+
sources = attribute.get_sources()
24+
25+
assert len(sources) == 1
26+
assert sources[0].anchor_type == config.ANCHOR_SOURCE
27+
28+
29+
def test_attribute_get_verifiers():
30+
anchors = create_source_and_verifier_anchors()
31+
attribute = yoti_python_sdk.attribute.attribute(NAME, VALUE, anchors)
32+
verifiers = attribute.get_verifiers()
33+
34+
assert len(verifiers) == 1
35+
assert verifiers[0].anchor_type == config.ANCHOR_VERIFIER
36+
37+
38+
def create_source_and_verifier_anchors():
39+
passport_anchor = anchor_parser.get_passport_anchor() # source
40+
yoti_admin_anchor = anchor_parser.get_yoti_admin_anchor() # verifier
41+
42+
return [passport_anchor, yoti_admin_anchor]

0 commit comments

Comments
 (0)