diff --git a/timed/employment/tests/test_user.py b/timed/employment/tests/test_user.py index 6efaf22c..79acce98 100644 --- a/timed/employment/tests/test_user.py +++ b/timed/employment/tests/test_user.py @@ -220,3 +220,30 @@ def test_user_attributes(auth_client, project): project.reviewers.add(user) res = auth_client.get(url) assert res.json()["data"]["attributes"]["is-reviewer"] + + +def test_user_me_auth(auth_client): + """Should return the auth_client user.""" + user = auth_client.user + + url = reverse("user-me") + + response = auth_client.get(url) + assert response.status_code == status.HTTP_200_OK + + me_data = response.json()["data"] + assert me_data["id"] == str(user.id) + + # should be the same as user-detail + url = reverse("user-detail", args=[user.id]) + + response = auth_client.get(url) + assert me_data == response.json()["data"] + + +def test_user_me_anonymous(client): + """Non-authenticated client doesn't do anything.""" + url = reverse("user-me") + + response = client.get(url) + assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/timed/employment/views.py b/timed/employment/views.py index 7fd90d02..7c9e5ebc 100644 --- a/timed/employment/views.py +++ b/timed/employment/views.py @@ -4,6 +4,7 @@ from django.contrib.auth import get_user_model from django.db.models import CharField, DateField, IntegerField, Q, Value from django.db.models.functions import Concat +from django.shortcuts import get_object_or_404 from django.utils.translation import ugettext_lazy as _ from rest_framework import exceptions, status from rest_framework.decorators import action @@ -54,6 +55,14 @@ def get_queryset(self): "employments", "supervisees", "supervisors" ) + @action(methods=["get"], detail=False) + def me(self, request, pk=None): + User = get_user_model() + self.object = get_object_or_404(User, pk=request.user.id) + serializer = self.get_serializer(self.object) + + return Response(serializer.data) + @action(methods=["post"], detail=True) def transfer(self, request, pk=None): """