Skip to content

Commit

Permalink
add tests for travelperk views (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
NileshPant1999 authored Mar 14, 2024
1 parent 4cf674e commit 99705ef
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 16 deletions.
2 changes: 2 additions & 0 deletions admin_settings/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,12 @@

TRAVELPERK_CLIENT_ID = os.environ.get('TRAVELPERK_CLIENT_ID')
TRAVELPERK_CLIENT_SECRET = os.environ.get('TRAVELPERK_CLIENT_SECRET')
TRAVELPERK_ENVIRONMENT = os.environ.get('TRAVELPERK_ENVIRONMENT')
TRAVELPERK_AUTH_URL = os.environ.get('TRAVELPERK_AUTH_URL')
TRAVELPERK_TOKEN_URL = os.environ.get('TRAVELPERK_TOKEN_URL')
TRAVELPERK_BASE_URL = os.environ.get('TRAVELPERK_BASE_URL')
TRAVELPERK_REDIRECT_URI = os.environ.get('TRAVELPERK_REDIRECT_URI')
TKWEBHOOKS_SECRET = os.environ.get('TKWEBHOOKS_SECRET')
FYLE_NOTIFICATIONS_EMAIL = os.environ.get('FYLE_NOTIFICATIONS_EMAIL')

FYLE_REFRESH_TOKEN = os.environ.get('FYLE_REFRESH_TOKEN')
1 change: 0 additions & 1 deletion apps/travelperk/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def post(self, request, *args, **kwargs):
try:
org = Org.objects.get(id=kwargs['org_id'])
refresh_token = get_refresh_token_using_auth_code(request.data.get('code'), kwargs['org_id'])

if refresh_token:
travelperk_credential = TravelperkCredential.objects.get(org=org)
travelperk_connection = TravelperkConnector(travelperk_credential, kwargs['org_id'])
Expand Down
2 changes: 2 additions & 0 deletions docker-compose-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ services:
FYLE_REFRESH_TOKEN: dummy
FYLE_TOKEN_URI: https://lolo.fyle.tech
APP_WK_ORIGIN: https://app.workato.com
TRAVELPERK_ENVIRONMENT: sandbox
WK_API_PLATFORM_KEY: ""
WK_PARTNER_EMAIL: ""
WK_JWT_PRIVATE_KEY: ""
WK_API_KEY: ""
WK_CUSTOMER_ID: ""
TKWEBHOOKS_SECRET: '12345'
TRAVELPERK_REDIRECT_URI: https://lolo.fyle.tech
db:
image: "postgres:15"
Expand Down
29 changes: 28 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from tests.fixture import fixture
from apps.orgs.models import Org, FyleCredential
from apps.travelperk.models import TravelPerk
from apps.travelperk.models import TravelPerk, TravelperkProfileMapping, TravelperkAdvancedSetting, TravelperkCredential
from apps.bamboohr.models import BambooHr, BambooHrConfiguration

@pytest.fixture
Expand Down Expand Up @@ -145,3 +145,30 @@ def get_travelperk_id(get_org_id):
)

return travelperk.id

@pytest.fixture()
def get_profile_mappings(get_org_id):
mappings = TravelperkProfileMapping.objects.create(
org=Org.objects.get(id=get_org_id),
profile_name='Dummy Profile',
user_role='BOOKER',
is_import_enabled=False,
source_id='1234',
currency='USD'
)

@pytest.fixture()
def get_advanced_settings(get_org_id):
advanced_settings = TravelperkAdvancedSetting.objects.create(
org=Org.objects.get(id=get_org_id),
default_employee_name='dummy@gmail.com',
default_employee_id='1234',
invoice_lineitem_structure='MULTIPLE',
)

@pytest.fixture()
def add_travelperk_cred(get_org_id):
travelperk_cred = TravelperkCredential.objects.create(
org=Org.objects.get(id=get_org_id),
refresh_token='12312rwer'
)
78 changes: 65 additions & 13 deletions tests/test_travelperk/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,69 @@
fixture = {
'travelperk': {
"id":1,
"folder_id":"1231",
"package_id":"1231",
"is_fyle_connected":"t",
"travelperk_connection_id": None,
"is_travelperk_connected": True,
"onboarding_state": 'CONNECTION',
"is_s3_connected":"dtmmy",
"created_at":"2022-11-29T15:39:49.221955Z",
"updated_at":"2022-11-29T15:41:59.535831Z",
"webhook_subscription_id": "123",
"webhook_enabled": True,
"org":2
'id':1,
'folder_id':'1231',
'package_id':'1231',
'is_fyle_connected':'t',
'travelperk_connection_id': None,
'is_travelperk_connected': True,
'onboarding_state': 'CONNECTION',
'is_s3_connected':'dtmmy',
'created_at':'2022-11-29T15:39:49.221955Z',
'updated_at':'2022-11-29T15:41:59.535831Z',
'webhook_subscription_id': '123',
'webhook_enabled': True,
'org':2
},
'profile_mapping': {
'count': 1,
'next': None,
'previous': None,
'results': [
{
'id': 4,
'profile_name': 'Kamalini Visa Card',
'user_role': 'CARD_HOLDER',
'is_import_enabled': False,
'country': None,
'currency': 'USD',
'source_id': 'b2cca47d-c3bc-4b09-8f86-358a83cae842',
'created_at': '2024-03-12T06:30:59.896680Z',
'updated_at': '2024-03-12T06:31:27.662549Z',
'org': 7
},
]
},
'advanced_settings': {
'default_employee_name': 'ashwin.t@fyle.in',
'default_employee_id': 'usqywo0f3nBY',
'default_category_name': 'Acc. Dep-Leasehold Improvements',
'default_category_id': '228952',
'invoice_lineitem_structure': 'MULTIPLE',
'description_structure': [
'trip_id',
'trip_name',
'traveler_name',
'booker_name',
'merchant_name'
],
'category_mappings': {
'Cars': {
'id': '228952',
'name': 'Acc. Dep-Leasehold Improvements'
},
'Hotels': {
'id': '264337',
'name': 'Elon Baba'
},
'Trains': {
'id': '228955',
'name': 'Sales - Merchandise'
},
'Flights': {
'id': '228953',
'name': 'Customer Deposits'
}
},
'org': 7
}
}
102 changes: 101 additions & 1 deletion tests/test_travelperk/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import pytest
from django.urls import reverse

from unittest.mock import MagicMock

from tests.helper import dict_compare_keys
from .fixtures import fixture
Expand Down Expand Up @@ -36,3 +36,103 @@ def test_travelperk_get_view(api_client, access_token, get_org_id, get_travelper

response = json.loads(response.content)
assert response['message'] != None


@pytest.mark.django_db(databases=['default'])
def test_get_profile_mappings(api_client, access_token, get_org_id, get_travelperk_id, get_profile_mappings):
"""
Test Get of Travelperk
"""
url = reverse('travelperk:profile-mappings',
kwargs={
'org_id': get_org_id,
}
)

api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token))

response = api_client.get(url)
assert response.status_code == 200

response = json.loads(response.content)
assert response['results'][0]['profile_name'] == 'Dummy Profile'
assert dict_compare_keys(response['results'][0], fixture['profile_mapping']['results'][0]) == []


@pytest.mark.django_db(databases=['default'])
def test_get_advanced_settings(api_client, access_token, get_org_id, get_travelperk_id, get_advanced_settings):
"""
Test Get of Travelperk
"""
url = reverse('travelperk:advance-settings-view',
kwargs={
'org_id': get_org_id,
}
)

api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token))

response = api_client.get(url)
assert response.status_code == 200

response = json.loads(response.content)
assert dict_compare_keys(response, fixture['advanced_settings']) == []

@pytest.mark.django_db(databases=['default'])
def test_disconnect_travelperk(mocker, api_client, access_token, get_org_id, get_travelperk_id, add_travelperk_cred):
"""
Test Disconnect Travelperk
"""
url = reverse('travelperk:disconnect-travelperk',
kwargs={
'org_id': get_org_id,
}
)

mock_connector = MagicMock()
mock_connector.delete_webhook_connection.return_value = {'message': 'success'}

mocker.patch(
'apps.travelperk.views.TravelperkConnector',
return_value=mock_connector
)

api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token))

response = api_client.post(url)
assert response.status_code == 200


@pytest.mark.django_db(databases=['default'])
def test_connect_travelperk(mocker, api_client, access_token, get_org_id, get_travelperk_id, add_travelperk_cred):
"""
Test Connect Travelperk
"""

url = reverse('travelperk:connect-travelperk',
kwargs={
'org_id': get_org_id,
}
)

mock_connector = MagicMock()
mock_connector.create_webhook.return_value = {'id': 123}

mocker.patch(
'apps.travelperk.views.TravelperkConnector',
return_value=mock_connector
)

mocker.patch(
'apps.travelperk.views.get_refresh_token_using_auth_code',
return_value={'123e3rwer'}
)

api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token))

response = api_client.post(url)

assert response.status_code == 200

response = json.loads(response.content)
assert response['id'] == 123

0 comments on commit 99705ef

Please sign in to comment.