From 12492abcd2ad1bf3e46cd959e6badec6628d76ce Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Fri, 21 Feb 2025 11:37:28 -0700 Subject: [PATCH 1/3] test: alternative error format --- .../test_error_alternative_format.yaml | 73 +++++++++++++++++++ tests/test_error.py | 28 ++++++- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/cassettes/test_error_alternative_format.yaml diff --git a/tests/cassettes/test_error_alternative_format.yaml b/tests/cassettes/test_error_alternative_format.yaml new file mode 100644 index 00000000..88e68b39 --- /dev/null +++ b/tests/cassettes/test_error_alternative_format.yaml @@ -0,0 +1,73 @@ +interactions: +- request: + body: '{"type": "damage", "email_evidence_attachments": ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAeUlEQVR42mP8//8/AwAI/AL+4Q7AIAAAAABJRU5ErkJggg=="], + "invoice_attachments": ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAeUlEQVR42mP8//8/AwAI/AL+4Q7AIAAAAABJRU5ErkJggg=="], + "supporting_documentation_attachments": ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAeUlEQVR42mP8//8/AwAI/AL+4Q7AIAAAAABJRU5ErkJggg=="], + "description": "Test description", "contact_email": "test@example.com", "tracking_code": + "123"}' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '579' + Content-Type: + - application/json + authorization: + - + user-agent: + - + method: POST + uri: https://api.easypost.com/v2/claims + response: + body: + string: '{"error": {"code": "NOT_FOUND", "errors": ["No eligible insurance found + with provided tracking code."], "message": "The requested resource could not + be found."}}' + headers: + cache-control: + - private, no-cache, no-store + content-length: + - '156' + content-type: + - application/json; charset=utf-8 + expires: + - '0' + pragma: + - no-cache + referrer-policy: + - strict-origin-when-cross-origin + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + x-backend: + - easypost + x-content-type-options: + - nosniff + x-download-options: + - noopen + x-ep-request-uuid: + - 764a527567b8c6c5e2b8e6320004ed95 + x-frame-options: + - SAMEORIGIN + x-node: + - bigweb41nuq + x-permitted-cross-domain-policies: + - none + x-proxied: + - intlb4nuq 51d74985a2 + - extlb1nuq 99aac35317 + x-runtime: + - '0.032715' + x-version-label: + - easypost-202502211712-417f1a0f64-master + x-xss-protection: + - 1; mode=block + status: + code: 404 + message: Not Found +version: 1 diff --git a/tests/test_error.py b/tests/test_error.py index a8f94b73..94df7f0e 100644 --- a/tests/test_error.py +++ b/tests/test_error.py @@ -19,6 +19,28 @@ def test_error(test_client): ) +@pytest.mark.vcr() +def test_error_alternative_format(test_client, basic_claim, full_shipment): + """Tests that we assign properties of an error correctly when returned via the alternative format. + + NOTE: Claims (among other things) uses the alternative errors format. + """ + try: + claim_data = basic_claim + claim_data["tracking_code"] = "123" # Intentionally pass a bad tracking code + + test_client.claim.create(**claim_data) + except ApiError as error: + assert error.http_status == 404 + assert error.code == "NOT_FOUND" + assert error.message == "The requested resource could not be found." + assert error.errors[0] == "No eligible insurance found with provided tracking code." + assert ( + error.http_body + == '{"error": {"code": "NOT_FOUND", "errors": ["No eligible insurance found with provided tracking code."], "message": "The requested resource could not be found."}}' # noqa + ) + + def test_error_no_json(): """Tests if we don't have valid JSON that we don't set the JSON body of an error.""" error = ApiError(message="", http_body="bad json") @@ -55,7 +77,11 @@ def test_error_bad_format_message(): message_data = { "errors": ["Bad format 1", "Bad format 2"], "bad_data": [ - {"first_message": "Bad format 3", "second_message": "Bad format 4", "thrid_message": "Bad format 5"} + { + "first_message": "Bad format 3", + "second_message": "Bad format 4", + "thrid_message": "Bad format 5", + } ], } From 80cfbf8ea9fd2f3bb6b91b61cc4d43ac85ff6f21 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Fri, 21 Feb 2025 11:38:38 -0700 Subject: [PATCH 2/3] fix: unecessary import --- tests/test_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_error.py b/tests/test_error.py index 94df7f0e..99c5ebc4 100644 --- a/tests/test_error.py +++ b/tests/test_error.py @@ -20,7 +20,7 @@ def test_error(test_client): @pytest.mark.vcr() -def test_error_alternative_format(test_client, basic_claim, full_shipment): +def test_error_alternative_format(test_client, basic_claim): """Tests that we assign properties of an error correctly when returned via the alternative format. NOTE: Claims (among other things) uses the alternative errors format. From 0d315d26107157c5a34f5fdc9e076fc64e0b5e62 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Fri, 28 Feb 2025 11:39:37 -0700 Subject: [PATCH 3/3] test: address verification errors --- tests/test_address.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/test_address.py b/tests/test_address.py index 61f69c10..a1ab54ed 100644 --- a/tests/test_address.py +++ b/tests/test_address.py @@ -22,7 +22,7 @@ def test_address_create_verify(incorrect_address, test_client): """ Test creating an address with the `verify` param. """ - # Creating normally (without specifying "verify") will make the address, perform no verifications + # Creating normally (without specifying "verify") will make the address and perform no verifications address = test_client.address.create(**incorrect_address) assert isinstance(address, Address) @@ -33,7 +33,23 @@ def test_address_create_verify(incorrect_address, test_client): address = test_client.address.create(**incorrect_address) assert isinstance(address, Address) + + # Delivery verification assertions assert address.verifications.delivery.success is False + # TODO: details is not deserializing correctly, related to the larger "double EasyPostObject" wrapping issue + # assert address.verifications.delivery.details == {} + assert address.verifications.delivery.errors[0].code == "E.ADDRESS.NOT_FOUND" + assert address.verifications.delivery.errors[0].field == "address" + assert address.verifications.delivery.errors[0].suggestion is None + assert address.verifications.delivery.errors[0].message == "Address not found" + + # Zip4 verification assertions + assert address.verifications.zip4.success is False + assert address.verifications.zip4.details is None + assert address.verifications.zip4.errors[0].code == "E.ADDRESS.NOT_FOUND" + assert address.verifications.zip4.errors[0].field == "address" + assert address.verifications.zip4.errors[0].suggestion is None + assert address.verifications.zip4.errors[0].message == "Address not found" @pytest.mark.vcr()