Skip to content

Commit 1f0fe73

Browse files
committed
SDK-1299: Add test coverage for DocScanClient
1 parent 806fdd6 commit 1f0fe73

10 files changed

Lines changed: 245 additions & 5 deletions

File tree

yoti_python_sdk/docs/exception/doc_scan_exception.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,21 @@ def __init__(self, message, response):
1111
:param response: the http response
1212
:type response: requests.Response
1313
"""
14-
Exception.__init__(self, message)
14+
Exception.__init__(self)
15+
16+
self.__message = message
1517
self.__response = response
1618

19+
@property
20+
def message(self):
21+
"""
22+
Get the specific exception message
23+
24+
:return: the exception message
25+
:rtype: str
26+
"""
27+
return self.__message
28+
1729
@property
1830
def status_code(self):
1931
"""
@@ -43,3 +55,6 @@ def content(self):
4355
:rtype: bytearray or None
4456
"""
4557
return self.__response.content
58+
59+
def __str__(self):
60+
return self.__message

yoti_python_sdk/tests/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import io
3-
from os.path import dirname, join, abspath
3+
from os.path import abspath
4+
from os.path import dirname
5+
from os.path import join
46

57
import pytest
68

yoti_python_sdk/tests/docs/__init__.py

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from os.path import abspath
2+
from os.path import dirname
3+
from os.path import join
4+
5+
import pytest
6+
7+
from yoti_python_sdk.docs.client import DocScanClient
8+
9+
FIXTURES_DIR = join(dirname(abspath(__file__)), "..", "fixtures")
10+
PEM_FILE_PATH = join(FIXTURES_DIR, "sdk-test.pem")
11+
12+
YOTI_CLIENT_SDK_ID = "737204aa-d54e-49a4-8bde-26ddbe6d880c"
13+
14+
15+
@pytest.fixture(scope="module")
16+
def doc_scan_client():
17+
"""
18+
:rtype: DocScanClient
19+
"""
20+
return DocScanClient(YOTI_CLIENT_SDK_ID, PEM_FILE_PATH)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"error": "MALFORMED_REQUEST"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"session_id":"someSessionId"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"session_id": "someSessionId", "client_session_token": "someClientSessionToken", "client_session_token_ttl": 299}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from os.path import abspath
2+
from os.path import dirname
3+
from os.path import join
4+
5+
from yoti_python_sdk.tests.mocks import MockResponse
6+
7+
FIXTURES_DIR = join(dirname(abspath(__file__)), "fixtures")
8+
9+
10+
def mocked_request_successful_session_creation():
11+
with open(FIXTURES_DIR + "/session_create_success.txt", "r") as f:
12+
response = f.read()
13+
return MockResponse(status_code=201, text=response)
14+
15+
16+
def mocked_request_failed_session_creation():
17+
with open(FIXTURES_DIR + "/failed_request.txt", "r") as f:
18+
response = f.read()
19+
return MockResponse(status_code=400, text=response)
20+
21+
22+
def mocked_request_successful_session_retrieval():
23+
with open(FIXTURES_DIR + "/retrieve_session_success.txt", "r") as f:
24+
response = f.read()
25+
return MockResponse(status_code=200, text=response)
26+
27+
28+
def mocked_request_failed_session_retrieval():
29+
return MockResponse(status_code=400, text="")
30+
31+
32+
def mocked_request_media_content():
33+
return MockResponse(
34+
status_code=200,
35+
text=b"someContent",
36+
headers={"Content-Type": "application/json"},
37+
)
38+
39+
40+
def mocked_request_missing_content():
41+
return MockResponse(status_code=404, text="")
42+
43+
44+
def mocked_request_server_error():
45+
return MockResponse(status_code=500, text="")
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import pytest
2+
3+
from yoti_python_sdk.docs.client import DocScanClient # noqa: F401
4+
from yoti_python_sdk.docs.exception import DocScanException
5+
from yoti_python_sdk.docs.session.create.session_spec import SessionSpec
6+
from yoti_python_sdk.docs.session.retrieve.create_session_result import (
7+
CreateSessionResult,
8+
)
9+
from yoti_python_sdk.docs.session.retrieve.get_session_result import GetSessionResult
10+
from yoti_python_sdk.tests.docs.mocks import mocked_request_failed_session_creation
11+
from yoti_python_sdk.tests.docs.mocks import mocked_request_failed_session_retrieval
12+
from yoti_python_sdk.tests.docs.mocks import mocked_request_media_content
13+
from yoti_python_sdk.tests.docs.mocks import mocked_request_missing_content
14+
from yoti_python_sdk.tests.docs.mocks import mocked_request_server_error
15+
from yoti_python_sdk.tests.docs.mocks import mocked_request_successful_session_creation
16+
from yoti_python_sdk.tests.docs.mocks import mocked_request_successful_session_retrieval
17+
18+
try:
19+
from unittest import mock
20+
except ImportError:
21+
import mock
22+
23+
SOME_SESSION_ID = "someSessionId"
24+
SOME_MEDIA_ID = "someMediaId"
25+
26+
27+
@mock.patch(
28+
"yoti_python_sdk.http.SignedRequest.execute",
29+
side_effect=mocked_request_successful_session_creation,
30+
)
31+
def test_should_return_create_session_result(_, doc_scan_client):
32+
"""
33+
:type doc_scan_client: DocScanClient
34+
:return:
35+
:rtype:
36+
"""
37+
session_spec_mock = mock.Mock(spec=SessionSpec)
38+
session_spec_mock.to_json.return_value = {}
39+
40+
create_session_result = doc_scan_client.create_session(session_spec_mock)
41+
42+
assert isinstance(create_session_result, CreateSessionResult)
43+
44+
45+
@mock.patch(
46+
"yoti_python_sdk.http.SignedRequest.execute",
47+
side_effect=mocked_request_failed_session_creation,
48+
)
49+
def test_should_raise_doc_scan_exception_for_session_creation(_, doc_scan_client):
50+
"""
51+
:type doc_scan_client: DocScanClient
52+
"""
53+
session_spec_mock = mock.Mock(spec=SessionSpec)
54+
session_spec_mock.to_json.return_value = {}
55+
56+
with pytest.raises(DocScanException) as ex:
57+
doc_scan_client.create_session(session_spec_mock)
58+
59+
assert "Failed to create session" in str(ex.value)
60+
61+
62+
@mock.patch(
63+
"yoti_python_sdk.http.SignedRequest.execute",
64+
side_effect=mocked_request_successful_session_retrieval,
65+
)
66+
def test_should_return_get_session_result(_, doc_scan_client):
67+
"""
68+
:type doc_scan_client: DocScanClient
69+
"""
70+
session_result = doc_scan_client.get_session(SOME_SESSION_ID)
71+
72+
assert isinstance(session_result, GetSessionResult)
73+
74+
75+
@mock.patch(
76+
"yoti_python_sdk.http.SignedRequest.execute",
77+
side_effect=mocked_request_failed_session_retrieval,
78+
)
79+
def test_should_raise_doc_scan_exception_for_session_retrieval(_, doc_scan_client):
80+
"""
81+
:type doc_scan_client: DocScanClient
82+
"""
83+
with pytest.raises(DocScanException) as ex:
84+
doc_scan_client.get_session(SOME_SESSION_ID)
85+
86+
doc_scan_exception = ex.value # type: DocScanException
87+
assert "Failed to retrieve session" in str(doc_scan_exception)
88+
assert doc_scan_exception.status_code == 400
89+
90+
91+
@mock.patch(
92+
"yoti_python_sdk.http.SignedRequest.execute",
93+
side_effect=mocked_request_server_error,
94+
)
95+
def test_should_raise_exception_for_delete_session(_, doc_scan_client):
96+
"""
97+
:type doc_scan_client: DocScanClient
98+
"""
99+
with pytest.raises(DocScanException) as ex:
100+
doc_scan_client.delete_session(SOME_SESSION_ID)
101+
102+
doc_scan_exception = ex.value # type: DocScanException
103+
assert "Failed to delete session" in str(doc_scan_exception)
104+
assert doc_scan_exception.status_code == 500
105+
106+
107+
@mock.patch(
108+
"yoti_python_sdk.http.SignedRequest.execute",
109+
side_effect=mocked_request_missing_content,
110+
)
111+
def test_should_raise_exception_for_invalid_content(_, doc_scan_client):
112+
"""
113+
:type doc_scan_client: DocScanClient
114+
"""
115+
with pytest.raises(DocScanException) as ex:
116+
doc_scan_client.get_media_content(SOME_SESSION_ID, SOME_MEDIA_ID)
117+
118+
doc_scan_exception = ex.value # type: DocScanException
119+
assert "Failed to retrieve media content" in str(doc_scan_exception)
120+
assert doc_scan_exception.status_code == 404
121+
122+
123+
@mock.patch(
124+
"yoti_python_sdk.http.SignedRequest.execute",
125+
side_effect=mocked_request_media_content,
126+
)
127+
def test_should_return_media_value(_, doc_scan_client):
128+
"""
129+
:type doc_scan_client: DocScanClient
130+
"""
131+
media = doc_scan_client.get_media_content(SOME_SESSION_ID, SOME_MEDIA_ID)
132+
133+
assert media.mime_type == "application/json"
134+
assert media.content == b"someContent"
135+
136+
137+
@mock.patch(
138+
"yoti_python_sdk.http.SignedRequest.execute",
139+
side_effect=mocked_request_missing_content,
140+
)
141+
def test_should_throw_exception_for_delete_media(_, doc_scan_client):
142+
"""
143+
:type doc_scan_client: DocScanClient
144+
"""
145+
with pytest.raises(DocScanException) as ex:
146+
doc_scan_client.delete_media_content(SOME_SESSION_ID, SOME_MEDIA_ID)
147+
148+
doc_scan_exception = ex.value # type: DocScanException
149+
assert "Failed to delete media content" in str(doc_scan_exception)
150+
assert 404 == doc_scan_exception.status_code

yoti_python_sdk/tests/mocks.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import base64
12
from uuid import UUID
2-
from yoti_python_sdk.http import RequestHandler, SignedRequest
3+
4+
from yoti_python_sdk.http import RequestHandler
5+
from yoti_python_sdk.http import SignedRequest
36
from yoti_python_sdk.http import YotiResponse
4-
import base64
57

68

79
class MockResponse(YotiResponse):
8-
def __init__(self, status_code, text, headers={}):
10+
def __init__(self, status_code, text, headers=None):
11+
if headers is None:
12+
headers = dict()
13+
914
super(MockResponse, self).__init__(status_code, text, headers)
1015

1116
@property

0 commit comments

Comments
 (0)