Skip to content

Commit

Permalink
Get user profile api (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruuushhh authored Nov 29, 2023
1 parent 8791ea6 commit e626aa5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
19 changes: 19 additions & 0 deletions apps/users/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Tuple

from fyle_integrations_platform_connector import PlatformConnector
from fyle_rest_auth.models import AuthToken

from apps.fyle.helpers import get_cluster_domain
Expand All @@ -20,3 +21,21 @@ def get_cluster_domain_and_refresh_token(user) -> Tuple[str, str]:
cluster_domain = get_cluster_domain(refresh_token)

return cluster_domain, refresh_token


def get_user_profile(request):
"""
Get user profile
"""
refresh_token = AuthToken.objects.get(user__user_id=request.user).refresh_token
cluster_domain, _ = get_cluster_domain_and_refresh_token(request.user)

fyle_credentials = FyleCredential(
cluster_domain=cluster_domain,
refresh_token=refresh_token
)

platform = PlatformConnector(fyle_credentials)
employee_profile = platform.connection.v1beta.spender.my_profile.get()

return employee_profile
3 changes: 2 additions & 1 deletion apps/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"""
from django.urls import path

from apps.users.views import FyleOrgsView
from apps.users.views import FyleOrgsView, UserProfileView

urlpatterns = [
path('profile/', UserProfileView.as_view(), name='user-profile'),
path('orgs/', FyleOrgsView.as_view(), name='fyle-orgs')
]
17 changes: 16 additions & 1 deletion apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_framework.response import Response

from apps.fyle.helpers import get_fyle_orgs
from apps.users.helpers import get_cluster_domain_and_refresh_token
from apps.users.helpers import get_cluster_domain_and_refresh_token, get_user_profile


class FyleOrgsView(generics.ListCreateAPIView):
Expand All @@ -24,3 +24,18 @@ def get(self, request, *args, **kwargs):
data=fyle_orgs,
status=status.HTTP_200_OK
)


class UserProfileView(generics.RetrieveAPIView):

permission_classes = [IsAuthenticated]

def get(self, request, *args, **kwargs):
"""
Get User Details
"""
employee_profile = get_user_profile(request)
return Response(
data=employee_profile,
status=status.HTTP_200_OK
)
17 changes: 16 additions & 1 deletion tests/test_users/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def test_setup():


def test_get_all_orgs_view(api_client, test_connection, create_temp_workspace, add_fyle_credentials, mocker):

"""
Test get all orgs view
"""
access_token = test_connection.access_token
url = reverse('fyle-orgs')

Expand All @@ -24,3 +26,16 @@ def test_get_all_orgs_view(api_client, test_connection, create_temp_workspace, a
response = api_client.get(url)
assert response.status_code == 200
assert response.data == fyle_data['get_all_orgs']


def test_get_user_profile(api_client, test_connection, create_temp_workspace, add_fyle_credentials):
"""
Test get user profile
"""
access_token = test_connection.access_token
url = reverse('user-profile')

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

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

0 comments on commit e626aa5

Please sign in to comment.