From a19a4a05dd3e7a9a3e3a7fc8ff8c8d5ba5682dc8 Mon Sep 17 00:00:00 2001 From: Seun Date: Mon, 20 Nov 2023 15:28:29 -0800 Subject: [PATCH 1/2] fix to incorrect error message when wrong refresh token is passed in --- pinterest/utils/refresh_access_token.py | 3 ++- tests/src/pinterest/client/client_test.py | 33 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pinterest/utils/refresh_access_token.py b/pinterest/utils/refresh_access_token.py index 9b313d6..252a1a9 100644 --- a/pinterest/utils/refresh_access_token.py +++ b/pinterest/utils/refresh_access_token.py @@ -50,7 +50,8 @@ def get_new_access_token( body="Authentication error. " + "Kindly check if the following variables are correct: [PINTEREST_ACCESS_TOKEN] or " + "[PINTEREST_APP_ID, PINTEREST_APP_SECRET, PINTEREST_REFRESH_ACCESS_TOKEN]. " + - f"Response from server: {response.body}" + f"Response from server: {response.data}", + http_resp=response ) if response.status != 200: raise SdkException(http_resp=response) diff --git a/tests/src/pinterest/client/client_test.py b/tests/src/pinterest/client/client_test.py index 49e7859..1e90719 100644 --- a/tests/src/pinterest/client/client_test.py +++ b/tests/src/pinterest/client/client_test.py @@ -3,6 +3,7 @@ from unittest.mock import patch import unittest import subprocess +from pinterest.utils.sdk_exceptions import SdkException from pinterest.client import PinterestSDKClient @@ -52,3 +53,35 @@ def test_set_default_refresh_token(self, load_dotenv_mock): self.assertEqual(refresh_token, PINTEREST_REFRESH_ACCESS_TOKEN) self.assertEqual(app_id, PINTEREST_APP_ID) self.assertEqual(app_secret, PINTEREST_APP_SECRET) + + @mock.patch.dict( + os.environ, + { + "PINTEREST_APP_ID": "test_app_id", + "PINTEREST_APP_SECRET": "test_app_secret", + }, + clear=True + ) + @patch('dotenv.load_dotenv') + def test_set_bad_refresh_token(self, load_dotenv_mock): + load_dotenv_mock.return_value = None + refresh_token = 'refresh_token' + app_id = '12345' + app_secret = '123456asdfg' + + from pinterest.config import PINTEREST_REFRESH_ACCESS_TOKEN + from pinterest.config import PINTEREST_APP_ID + from pinterest.config import PINTEREST_APP_SECRET + self.assertNotEqual(refresh_token, PINTEREST_REFRESH_ACCESS_TOKEN) + self.assertNotEqual(app_id, PINTEREST_APP_ID) + self.assertNotEqual(app_secret, PINTEREST_APP_SECRET) + + try: + PinterestSDKClient._get_access_token( + refresh_token=refresh_token, + app_id=app_id, + app_secret=app_secret + ) + self.assertTrue(False) + except SdkException as e: + self.assertTrue(True) From 056bcaf5dd7c28d0333f098d25fb2f015ed9a6fc Mon Sep 17 00:00:00 2001 From: Seun Date: Wed, 29 Nov 2023 17:27:18 -0800 Subject: [PATCH 2/2] integration test --- integration_tests/client/test_client.py | 13 +++++++++++++ tests/src/pinterest/client/client_test.py | 7 ++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/integration_tests/client/test_client.py b/integration_tests/client/test_client.py index 4a791ad..51c9582 100644 --- a/integration_tests/client/test_client.py +++ b/integration_tests/client/test_client.py @@ -12,6 +12,7 @@ from pinterest.organic.boards import Board from pinterest.client import PinterestSDKClient from integration_tests.base_test import BaseTestCase +from pinterest.utils.error_handling import SdkException class ClientTest(BaseTestCase): @@ -56,6 +57,18 @@ def test_good_setup_default_access_token(self): PinterestSDKClient.set_default_access_token(access_token=good_access_token) self.assertIsNotNone(Board.get_all()) + def test_bad_refresh_token(self): + refresh_token = 'refresh_token' + app_id = '12345' + app_secret = '123456asdfg' + with self.assertRaises(SdkException): + PinterestSDKClient._get_access_token( + refresh_token=refresh_token, + app_id=app_id, + app_secret=app_secret + ) + + diff --git a/tests/src/pinterest/client/client_test.py b/tests/src/pinterest/client/client_test.py index 1e90719..f182d59 100644 --- a/tests/src/pinterest/client/client_test.py +++ b/tests/src/pinterest/client/client_test.py @@ -75,13 +75,10 @@ def test_set_bad_refresh_token(self, load_dotenv_mock): self.assertNotEqual(refresh_token, PINTEREST_REFRESH_ACCESS_TOKEN) self.assertNotEqual(app_id, PINTEREST_APP_ID) self.assertNotEqual(app_secret, PINTEREST_APP_SECRET) - - try: + with self.assertRaises(SdkException): PinterestSDKClient._get_access_token( refresh_token=refresh_token, app_id=app_id, app_secret=app_secret ) - self.assertTrue(False) - except SdkException as e: - self.assertTrue(True) +