|
| 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 |
0 commit comments