From 3d3afc828f7453491a19f890b5c90ee5c1c58b31 Mon Sep 17 00:00:00 2001 From: Anna Janiszewska Date: Tue, 29 Oct 2024 15:43:51 +0100 Subject: [PATCH] [FIX] delivery_postlogistics: raise exception when no token received Sometimes due to API error it may happen we don't receive anything back when requesting token. In this case we want to show meaningful message. --- .../postlogistics/web_service.py | 19 +++++++++- .../fixtures/cassettes/test_token_error.yaml | 36 +++++++++++++++++++ .../tests/test_postlogistics.py | 8 +++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 delivery_postlogistics/tests/fixtures/cassettes/test_token_error.yaml diff --git a/delivery_postlogistics/postlogistics/web_service.py b/delivery_postlogistics/postlogistics/web_service.py index af08d7ee27..7eada7feea 100644 --- a/delivery_postlogistics/postlogistics/web_service.py +++ b/delivery_postlogistics/postlogistics/web_service.py @@ -9,11 +9,13 @@ import urllib.parse from datetime import datetime, timedelta from io import BytesIO +from json import JSONDecodeError import requests from PIL import Image from odoo import _, exceptions +from odoo.exceptions import UserError _logger = logging.getLogger(__name__) @@ -450,7 +452,22 @@ def _request_access_token(cls, delivery_carrier): }, timeout=60, ) - return response.json() + + try: + response.raise_for_status() + json_response = response.json() + except ( + JSONDecodeError, + requests.exceptions.HTTPError, + ) as error: + raise UserError( + _( + "Postlogistics service is not accessible at the moment. Error code: %s. " + "Please try again later." % (response.status_code or "None") + ) + ) from error + + return json_response @classmethod def get_access_token(cls, picking_carrier): diff --git a/delivery_postlogistics/tests/fixtures/cassettes/test_token_error.yaml b/delivery_postlogistics/tests/fixtures/cassettes/test_token_error.yaml new file mode 100644 index 0000000000..37bc163407 --- /dev/null +++ b/delivery_postlogistics/tests/fixtures/cassettes/test_token_error.yaml @@ -0,0 +1,36 @@ +interactions: +- request: + body: client_secret=c70e3696ae5146e7fe317434e50a90cd&grant_type=client_credentials&client_id=865783331e39e91e633c3916fe892d92 + headers: + Accept: + - "*/*" + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - "144" + User-Agent: + - python-requests/2.20.0 + content-type: + - application/x-www-form-urlencoded + method: POST + uri: https://wedecint.post.ch/WEDECOAuth/token + response: + body: + string: '' + headers: + Cache-Control: + - no-cache; private + Connection: + - Close + Content-Length: + - "0" + Content-Type: + - text/html; charset=UTF-8 + Date: + - Tue, 13 Oct 2020 10:52:26 GMT + status: + code: 503 + message: Service Unavailable +version: 1 \ No newline at end of file diff --git a/delivery_postlogistics/tests/test_postlogistics.py b/delivery_postlogistics/tests/test_postlogistics.py index bb1b01b75c..c3bc498859 100644 --- a/delivery_postlogistics/tests/test_postlogistics.py +++ b/delivery_postlogistics/tests/test_postlogistics.py @@ -4,6 +4,8 @@ from vcr import VCR +from odoo.exceptions import UserError + from .common import TestPostlogisticsCommon recorder = VCR( @@ -108,3 +110,9 @@ def test_postlogistics_rate_shipment(self): res = self.carrier.postlogistics_rate_shipment(None) self.assertEqual(len(cassette.requests), 2) self.assertEqual(res["price"], 1.0) + + def test_postlogistics_get_token_error(self): + with recorder.use_cassette("test_token_error") as cassette: + with self.assertRaises(UserError): + self.service_class._request_access_token(self.carrier) + self.assertEqual(len(cassette.requests), 1)