|
| 1 | +import json |
| 2 | + |
| 3 | +from yoti_python_sdk.doc_scan.exception import DocScanException |
| 4 | +from yoti_python_sdk.tests.mocks import MockResponse |
| 5 | + |
| 6 | + |
| 7 | +def test_return_message(): |
| 8 | + response = MockResponse(status_code=400, text="some response") |
| 9 | + exception = DocScanException("some error", response) |
| 10 | + |
| 11 | + assert exception.message == "some error" |
| 12 | + |
| 13 | + |
| 14 | +def test_return_only_message_when_html_response(): |
| 15 | + response = MockResponse( |
| 16 | + status_code=400, |
| 17 | + text="<html>some html</html>", |
| 18 | + headers={"Content-Type": "text/html"}, |
| 19 | + ) |
| 20 | + exception = DocScanException("some error", response) |
| 21 | + |
| 22 | + assert exception.message == "some error" |
| 23 | + |
| 24 | + |
| 25 | +def test_return_only_message_when_json_response_has_no_message_property(): |
| 26 | + response = MockResponse( |
| 27 | + status_code=400, |
| 28 | + text=json.dumps({}), |
| 29 | + headers={"Content-Type": "application/json"}, |
| 30 | + ) |
| 31 | + exception = DocScanException("some error", response) |
| 32 | + |
| 33 | + assert exception.message == "some error" |
| 34 | + |
| 35 | + |
| 36 | +def test_return_formatted_response_code_and_message(): |
| 37 | + response = MockResponse( |
| 38 | + status_code=400, |
| 39 | + text=json.dumps({"code": "SOME_CODE", "message": "some message"}), |
| 40 | + headers={"Content-Type": "application/json"}, |
| 41 | + ) |
| 42 | + exception = DocScanException("some error", response) |
| 43 | + |
| 44 | + assert exception.message == "some error - SOME_CODE - some message" |
| 45 | + |
| 46 | + |
| 47 | +def test_return_formatted_response_code_message_and_errors(): |
| 48 | + response = MockResponse( |
| 49 | + status_code=400, |
| 50 | + text=json.dumps( |
| 51 | + { |
| 52 | + "code": "SOME_CODE", |
| 53 | + "message": "some message", |
| 54 | + "errors": [ |
| 55 | + {"property": "some.property", "message": "some message"}, |
| 56 | + { |
| 57 | + "property": "some.other.property", |
| 58 | + "message": "some other message", |
| 59 | + }, |
| 60 | + ], |
| 61 | + } |
| 62 | + ), |
| 63 | + headers={"Content-Type": "application/json"}, |
| 64 | + ) |
| 65 | + exception = DocScanException("some error", response) |
| 66 | + |
| 67 | + assert ( |
| 68 | + exception.message |
| 69 | + == 'some error - SOME_CODE - some message: some.property "some message", some.other.property "some other message"' |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def test_excludes_errors_without_property_or_message(): |
| 74 | + response = MockResponse( |
| 75 | + status_code=400, |
| 76 | + text=json.dumps( |
| 77 | + { |
| 78 | + "code": "SOME_CODE", |
| 79 | + "message": "some message", |
| 80 | + "errors": [ |
| 81 | + {"message": "some message"}, |
| 82 | + {"property": "some.other.property"}, |
| 83 | + ], |
| 84 | + } |
| 85 | + ), |
| 86 | + headers={"Content-Type": "application/json"}, |
| 87 | + ) |
| 88 | + exception = DocScanException("some error", response) |
| 89 | + |
| 90 | + assert exception.message == "some error - SOME_CODE - some message" |
0 commit comments