-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: PATCH /integrations
on export errors and token expiry
#442
Open
JustARatherRidiculouslyLongUsername
wants to merge
2
commits into
master
Choose a base branch
from
patch-integrations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import logging | ||
from django.conf import settings | ||
from apps.fyle.helpers import patch_request | ||
from apps.workspaces.models import FyleCredential, XeroCredentials | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.level = logging.INFO | ||
|
||
|
||
def patch_integration_settings(workspace_id: int, errors: int = None, is_token_expired = None): | ||
""" | ||
Patch integration settings | ||
""" | ||
|
||
refresh_token = FyleCredential.objects.get(workspace_id=workspace_id).refresh_token | ||
url = '{}/integrations/'.format(settings.INTEGRATIONS_SETTINGS_API) | ||
payload = { | ||
'tpa_name': 'Fyle Xero Integration', | ||
} | ||
|
||
if errors is not None: | ||
payload['errors_count'] = errors | ||
|
||
if is_token_expired is not None: | ||
payload['is_token_expired'] = is_token_expired | ||
|
||
try: | ||
patch_request(url, payload, refresh_token) | ||
except Exception as error: | ||
logger.error(error, exc_info=True) | ||
|
||
|
||
def invalidate_xero_credentials(workspace_id): | ||
try: | ||
xero_credentials = XeroCredentials.get_active_xero_credentials(workspace_id) | ||
if xero_credentials: | ||
if not xero_credentials.is_expired: | ||
patch_integration_settings(workspace_id, is_token_expired=True) | ||
xero_credentials.refresh_token = None | ||
xero_credentials.is_expired = True | ||
xero_credentials.save() | ||
except XeroCredentials.DoesNotExist as error: | ||
logger.error(f'Xero credentials not found for {workspace_id = }:', ) | ||
logger.error(error, exc_info=True) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from unittest.mock import MagicMock | ||
from xerosdk.exceptions import UnsuccessfulAuthentication | ||
from apps.exceptions import handle_view_exceptions | ||
|
||
|
||
def test_handle_view_exceptions(mocker): | ||
workspace_id = 123 | ||
|
||
mocked_invalidate_call = MagicMock() | ||
mocker.patch('apps.exceptions.invalidate_xero_credentials', side_effect=mocked_invalidate_call) | ||
|
||
@handle_view_exceptions() | ||
def func(*args, **kwargs): | ||
raise UnsuccessfulAuthentication('Invalid Token') | ||
|
||
func(workspace_id=workspace_id) | ||
|
||
args, _ = mocked_invalidate_call.call_args | ||
|
||
assert args[0] == workspace_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
import json | ||
from unittest.mock import MagicMock | ||
import pytest | ||
|
||
from apps.workspaces.actions import patch_integration_settings | ||
|
||
|
||
@pytest.mark.django_db(databases=['default']) | ||
def test_patch_integration_settings(mocker): | ||
|
||
workspace_id = 1 | ||
mocked_patch = MagicMock() | ||
mocker.patch('apps.fyle.helpers.requests.patch', side_effect=mocked_patch) | ||
|
||
# Test patch request with only errors | ||
errors = 7 | ||
patch_integration_settings(workspace_id, errors) | ||
|
||
expected_payload = {'errors_count': errors, 'tpa_name': 'Fyle Xero Integration'} | ||
|
||
_, kwargs = mocked_patch.call_args | ||
actual_payload = json.loads(kwargs['data']) | ||
|
||
assert actual_payload == expected_payload | ||
|
||
# Test patch request with only is_token_expired | ||
is_token_expired = True | ||
patch_integration_settings(workspace_id, is_token_expired=is_token_expired) | ||
|
||
expected_payload = {'is_token_expired': is_token_expired, 'tpa_name': 'Fyle Xero Integration'} | ||
|
||
_, kwargs = mocked_patch.call_args | ||
actual_payload = json.loads(kwargs['data']) | ||
|
||
assert actual_payload == expected_payload | ||
|
||
# Test patch request with errors_count and is_token_expired | ||
is_token_expired = True | ||
errors = 241 | ||
patch_integration_settings(workspace_id, errors=errors, is_token_expired=is_token_expired) | ||
|
||
expected_payload = { | ||
'errors_count': errors, | ||
'is_token_expired': is_token_expired, | ||
'tpa_name': 'Fyle Xero Integration' | ||
} | ||
|
||
_, kwargs = mocked_patch.call_args | ||
actual_payload = json.loads(kwargs['data']) | ||
|
||
assert actual_payload == expected_payload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from unittest.mock import MagicMock | ||
from xerosdk.exceptions import NoPrivilegeError | ||
from apps.xero.exceptions import handle_xero_exceptions | ||
|
||
|
||
def test_handle_view_exceptions(mocker, db): | ||
workspace_id = 1 | ||
task_log_id = 1 | ||
|
||
mocked_invalidate_call = MagicMock() | ||
mocker.patch('apps.xero.exceptions.invalidate_xero_credentials', side_effect=mocked_invalidate_call) | ||
|
||
@handle_xero_exceptions(payment=False) | ||
def func(expense_group_id: int, task_log_id: int, xero_connection): | ||
raise NoPrivilegeError('Invalid Token') | ||
|
||
func(workspace_id, task_log_id, MagicMock()) | ||
|
||
args, _ = mocked_invalidate_call.call_args | ||
|
||
assert args[0] == workspace_id | ||
|
||
@handle_xero_exceptions(payment=True) | ||
def func2(bill, workspace_id: int, task_log): | ||
raise NoPrivilegeError('Invalid Token') | ||
|
||
func2(MagicMock(), workspace_id, MagicMock()) | ||
|
||
args, _ = mocked_invalidate_call.call_args | ||
|
||
assert args[0] == workspace_id |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix error message formatting and improve error handling.
The f-string in the error message has an invalid syntax, and the error handling could be more specific.
Apply this diff to fix the issues:
📝 Committable suggestion