Skip to content
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

test: unit test /integrations patch calls #437

Open
wants to merge 1 commit into
base: patch-integrations-on-token-expiry
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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_patch = MagicMock()
mocker.patch('apps.exceptions.patch_integration_settings', side_effect=mocked_patch)

@handle_view_exceptions()
def func(*args, **kwargs):
raise UnsuccessfulAuthentication('Invalid Token')

func(workspace_id=workspace_id)

args, kwargs = mocked_patch.call_args

assert args[0] == workspace_id
assert kwargs['is_token_expired'] == True
52 changes: 52 additions & 0 deletions tests/test_workspaces/test_actions.py
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
8 changes: 8 additions & 0 deletions tests/test_workspaces/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def test_connect_xero_view_post(mocker, api_client, test_connection):
return_value=xero_data["get_all_organisations"],
)

mocked_patch = mock.MagicMock()
mocker.patch('apps.workspaces.actions.patch_integration_settings', side_effect=mocked_patch)

code = "asdfghj"
url = "/api/workspaces/{}/connect_xero/authorization_code/".format(workspace_id)

Expand All @@ -125,6 +128,11 @@ def test_connect_xero_view_post(mocker, api_client, test_connection):
response = api_client.post(url, data={"code": code})
assert response.status_code == 200

args, kwargs = mocked_patch.call_args

assert args[0] == workspace_id
assert kwargs['is_token_expired'] == False

xero_credentials = XeroCredentials.objects.get(workspace_id=workspace_id)
xero_credentials.delete()

Expand Down
33 changes: 33 additions & 0 deletions tests/test_xero/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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_patch = MagicMock()
mocker.patch('apps.xero.exceptions.patch_integration_settings', side_effect=mocked_patch)

@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, kwargs = mocked_patch.call_args

assert args[0] == workspace_id
assert kwargs['is_token_expired'] == True

@handle_xero_exceptions(payment=True)
def func2(bill, workspace_id: int, task_log):
raise NoPrivilegeError('Invalid Token')

func2(MagicMock(), workspace_id, MagicMock())

args, kwargs = mocked_patch.call_args

assert args[0] == workspace_id
assert kwargs['is_token_expired'] == True
Loading