Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #574 from sbor23/feat-me
Browse files Browse the repository at this point in the history
feat(employment): add /users/me route
  • Loading branch information
Akanksh Saxena authored Jun 23, 2020
2 parents f6f8946 + 98288aa commit 45491e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
27 changes: 27 additions & 0 deletions timed/employment/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions timed/employment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit 45491e9

Please sign in to comment.