Repository: akvo/akvo-rsr

Issue:
API endpoint to get current logged-in user.



Discussion:

Resolving patch:
diff --git a/akvo/rest/urls.py b/akvo/rest/urls.py
--- a/akvo/rest/urls.py
+++ b/akvo/rest/urls.py
@@ -145,6 +145,9 @@
     url(r'v1/user/(?P<pk>[0-9]+)/request_organisation/$',
         views.request_organisation,
         name='user_request_organisation'),
+    url(r'v1/me/$',
+        views.current_user,
+        name='current_user'),
     url(r'v1/invite_user/$',
         views.invite_user,
         name='invite_user'),
diff --git a/akvo/rest/views/__init__.py b/akvo/rest/views/__init__.py
--- a/akvo/rest/views/__init__.py
+++ b/akvo/rest/views/__init__.py
@@ -105,7 +105,7 @@
                         typeahead_impact_projects,
                         typeahead_projectupdate)
 from .user import (UserViewSet, change_password, update_details,
-                   request_organisation)
+                   request_organisation, current_user)
 from .user_management import invite_user
 from .user_projects import UserProjectsAccessViewSet
 
@@ -118,6 +118,7 @@
     'BudgetItemViewSet',
     'CategoryViewSet',
     'change_password',
+    'current_user',
     'CountryViewSet',
     'CountryBudgetItemViewSet',
     'CrsAddViewSet',
diff --git a/akvo/rest/views/user.py b/akvo/rest/views/user.py
--- a/akvo/rest/views/user.py
+++ b/akvo/rest/views/user.py
@@ -134,3 +134,22 @@ def request_organisation(request, pk=None):
         return Response(data)
     else:
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
+
+
+@api_view(['GET'])
+@authentication_classes([SessionAuthentication, TastyTokenAuthentication])
+def current_user(request):
+    user = request.user
+
+    if user.is_anonymous():
+        raise PermissionDenied()
+
+    queryset = get_user_model().objects.prefetch_related(
+        'organisations',
+        'organisations__primary_location',
+        'organisations__primary_location__country',
+        'organisations__primary_location__location_target',
+    )
+    instance = queryset.get(pk=user.pk)
+    data = UserSerializer(instance).data
+    return Response(data)


Test patch:
diff --git a/akvo/rsr/tests/rest/test_user.py b/akvo/rsr/tests/rest/test_user.py
--- a/akvo/rsr/tests/rest/test_user.py
+++ b/akvo/rsr/tests/rest/test_user.py
@@ -18,6 +18,7 @@
 from akvo.rsr.forms import PASSWORD_MINIMUM_LENGTH
 from akvo.rsr.models import Employment, Organisation, User
 from akvo.utils import check_auth_groups
+from akvo.rsr.tests.base import BaseTestCase
 
 
 # NOTE: Since these tests actually trigger some integrity errors and we want to
@@ -187,3 +188,22 @@ def _create_user(self, email, password, is_active=True):
         user.save()
 
         return user
+
+
+class CurrentUserTestCase(BaseTestCase):
+    """Tests REST endpoint to get current logged in user data."""
+
+    def test_unauthenticated_request_should_be_forbidden(self):
+        response = self.c.get('/rest/v1/me/?format=json')
+        self.assertEqual(response.status_code, 403)
+
+    def test_should_return_current_logged_in_user_data(self):
+        email = 'test@example.org'
+        password = 'passwd'
+        self.create_user(email, password)
+        self.c.login(username=email, password=password)
+
+        response = self.c.get('/rest/v1/me/?format=json')
+
+        content = json.loads(response.content)
+        self.assertEqual(content['email'], email)