From beadfbf0884725a454443cbebf7c32d2bcbf0f32 Mon Sep 17 00:00:00 2001 From: Hrishabh Tiwari <74908943+Hrishabh17@users.noreply.github.com> Date: Mon, 29 Apr 2024 12:23:22 +0530 Subject: [PATCH] Handled UnsuccessfulAuthentication during import (#353) * Handled UnsuccessfulAuthentication during import * Updated sdk version --- apps/mappings/exceptions.py | 17 +++++++++++++---- requirements.txt | 2 +- tests/test_mappings/test_exceptions.py | 19 +++++++++++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/apps/mappings/exceptions.py b/apps/mappings/exceptions.py index 389381f0..680ba76f 100644 --- a/apps/mappings/exceptions.py +++ b/apps/mappings/exceptions.py @@ -8,10 +8,13 @@ WrongParamsError, RetryException ) -from xerosdk.exceptions import InvalidGrant -from xerosdk.exceptions import InvalidTokenError as XeroInvalidTokenError -from xerosdk.exceptions import UnsuccessfulAuthentication -from xerosdk.exceptions import WrongParamsError as XeroWrongParamsError + +from xerosdk.exceptions import ( + InvalidGrant, + InvalidTokenError as XeroInvalidTokenError, + UnsuccessfulAuthentication, + WrongParamsError as XeroWrongParamsError +) from apps.workspaces.models import XeroCredentials from fyle_integrations_imports.models import ImportLog @@ -115,6 +118,12 @@ def new_fn(expense_attribute_instance, *args): error['response'] = exception.__dict__ import_log.status = 'FAILED' + except UnsuccessfulAuthentication as exception: + error["message"] = "Invalid xero tenant ID or xero-tenant-id header missing in workspace_id - {0}".format(workspace_id) + error["alert"] = False + error['response'] = exception.__dict__ + import_log.status = 'FAILED' + except Exception: response = traceback.format_exc() error['message'] = 'Something went wrong' diff --git a/requirements.txt b/requirements.txt index b2e89387..bc495634 100644 --- a/requirements.txt +++ b/requirements.txt @@ -41,7 +41,7 @@ Unidecode==1.1.2 urllib3==1.26.11 wcwidth==0.1.8 wrapt==1.12.1 -xerosdk==0.14.0 +xerosdk==0.14.1 pytest==7.1.2 pytest-cov==3.0.0 pytest-django==4.5.2 diff --git a/tests/test_mappings/test_exceptions.py b/tests/test_mappings/test_exceptions.py index 61e1ae2c..c091d560 100644 --- a/tests/test_mappings/test_exceptions.py +++ b/tests/test_mappings/test_exceptions.py @@ -4,8 +4,11 @@ from apps.xero.utils import XeroConnector from apps.mappings.exceptions import handle_import_exceptions_v2 from fyle.platform.exceptions import InternalServerError, InvalidTokenError, WrongParamsError -from xerosdk.exceptions import InvalidTokenError as XeroInvalidTokenError -from xerosdk.exceptions import WrongParamsError as XeroWrongParamsError +from xerosdk.exceptions import ( + InvalidTokenError as XeroInvalidTokenError, + UnsuccessfulAuthentication, + WrongParamsError as XeroWrongParamsError +) def test_handle_import_exceptions(db, create_temp_workspace, add_xero_credentials, add_fyle_credentials): @@ -96,6 +99,18 @@ def to_be_decoreated(expense_attribute_instance, import_log): assert import_log.error_log['message'] == 'Invalid Token or Xero credentials does not exist workspace_id - 3' assert import_log.error_log['alert'] == False + # Exception + @handle_import_exceptions_v2 + def to_be_decoreated(expense_attribute_instance, import_log): + raise UnsuccessfulAuthentication('Invalid xero tenant ID') + + to_be_decoreated(project, import_log) + + assert import_log.status == 'FAILED' + assert import_log.error_log['task'] == 'Import PROJECT to Fyle and Auto Create Mappings' + assert import_log.error_log['message'] == 'Invalid xero tenant ID or xero-tenant-id header missing in workspace_id - 3' + assert import_log.error_log['alert'] == False + # Exception @handle_import_exceptions_v2 def to_be_decoreated(expense_attribute_instance, import_log):