-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UserView.get() to retrieve logged-in user details re #11261
- Loading branch information
1 parent
eec0666
commit 1e796db
Showing
4 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from http import HTTPStatus | ||
|
||
from django.utils.translation import gettext as _ | ||
|
||
from arches.app.utils.betterJSONSerializer import JSONSerializer | ||
from arches.app.utils.response import JSONErrorResponse, JSONResponse | ||
from arches.app.views.api import APIBase | ||
|
||
|
||
class UserView(APIBase): | ||
http_method_names = ["get"] | ||
|
||
def get(self, request): | ||
if not request.user.is_active: | ||
return JSONErrorResponse( | ||
title=_("Login required"), | ||
message=_("This account is no longer active."), | ||
status=HTTPStatus.FORBIDDEN, | ||
) | ||
|
||
# N.B.: SetAnonymousUser middleware provides an anonymous User, | ||
# so don't infer from a 200 OK (or even is_authenticated, if we | ||
# later serialize that) that you have an authenticated user. | ||
fields = {"first_name", "last_name", "username"} | ||
return JSONResponse(JSONSerializer().serialize(request.user, fields=fields)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from http import HTTPStatus | ||
|
||
from django.contrib.auth.models import User | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
# these tests can be run from the command line via | ||
# python manage.py test tests.views.api.test_user --settings="tests.test_settings" | ||
|
||
|
||
class UserAPITests(TestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
cls.visitor = User.objects.create( | ||
username="visitor", | ||
first_name="Esperanza", | ||
last_name="Spalding", | ||
) | ||
cls.visitor.set_password("jazz") | ||
cls.visitor.save() | ||
|
||
def test_logged_in_user(self): | ||
self.client.force_login(self.visitor) | ||
response = self.client.get(reverse("api_user")) | ||
self.assertContains( | ||
response, | ||
'{"first_name": "Esperanza", "last_name": "Spalding", "username": "visitor"}', | ||
status_code=HTTPStatus.OK, | ||
) | ||
self.assertNotContains(response, "password") | ||
|
||
def test_anonymous_user(self): | ||
response = self.client.get(reverse("api_user")) | ||
self.assertContains( | ||
response, | ||
'{"first_name": "", "last_name": "", "username": "anonymous"}', | ||
status_code=HTTPStatus.OK, | ||
) | ||
self.assertNotContains(response, "password") | ||
|
||
def test_inactive_user(self): | ||
self.visitor.is_active = False | ||
self.visitor.save() | ||
|
||
self.client.force_login(self.visitor) | ||
with self.assertLogs("django.request", level="WARNING"): | ||
response = self.client.get(reverse("api_user")) | ||
self.assertContains( | ||
response, | ||
"This account is no longer active.", | ||
status_code=HTTPStatus.FORBIDDEN, | ||
) |