[FR] Simple API endpoint to change own user metadata (#4151)

* [FR] Simple API endpoint to change own user metadata
Fixes #3979

* add tests

* fix tests

* add owner api tests

* add debug statments

* run all tests

* disable owner endpoint test for now

* spellfix
This commit is contained in:
Matthias Mair 2023-01-25 03:04:06 +01:00 committed by GitHub
parent 2d3570c923
commit e870b0c9b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View File

@ -10,7 +10,7 @@ from rest_framework.authtoken.models import Token
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
from InvenTree.mixins import ListAPI, RetrieveAPI from InvenTree.mixins import ListAPI, RetrieveAPI, RetrieveUpdateAPI
from InvenTree.serializers import UserSerializer from InvenTree.serializers import UserSerializer
from users.models import Owner, RuleSet, check_user_role from users.models import Owner, RuleSet, check_user_role
from users.serializers import OwnerSerializer from users.serializers import OwnerSerializer
@ -116,6 +116,14 @@ class UserDetail(RetrieveAPI):
permission_classes = (permissions.IsAuthenticated,) permission_classes = (permissions.IsAuthenticated,)
class MeUserDetail(RetrieveUpdateAPI, UserDetail):
"""Detail endpoint for current user."""
def get_object(self):
"""Always return the current user object"""
return self.request.user
class UserList(ListAPI): class UserList(ListAPI):
"""List endpoint for detail on all users.""" """List endpoint for detail on all users."""
@ -170,6 +178,7 @@ user_urls = [
re_path(r'roles/?$', RoleDetails.as_view(), name='api-user-roles'), re_path(r'roles/?$', RoleDetails.as_view(), name='api-user-roles'),
re_path(r'token/?$', GetAuthToken.as_view(), name='api-token'), re_path(r'token/?$', GetAuthToken.as_view(), name='api-token'),
re_path(r'^me/', MeUserDetail.as_view(), name='api-user-me'),
re_path(r'^owner/', include([ re_path(r'^owner/', include([
path('<int:pk>/', OwnerDetail.as_view(), name='api-owner-detail'), path('<int:pk>/', OwnerDetail.as_view(), name='api-owner-detail'),

View File

@ -216,9 +216,26 @@ class OwnerModelTest(InvenTreeTestCase):
self.do_request(reverse('api-owner-list'), {}) self.do_request(reverse('api-owner-list'), {})
# user list with search # user list with search
self.do_request(reverse('api-owner-list'), {'search': 'user'}) self.do_request(reverse('api-owner-list'), {'search': 'user'})
# user detail
# TODO fix this test # # owner detail - user
# self.do_request(reverse('api-owner-detail', kwargs={'pk': self.user.id}), {}) # response = self.do_request(reverse('api-owner-detail', kwargs={'pk': 1}), {})
# self.assertEqual(response['name'], self.username)
# self.assertEqual(response['label'], 'user')
# self.assertEqual(response['owner_id'], self.user.id)
# # owner detail - group
# group = self.user.groups.first()
# response = self.do_request(reverse('api-owner-detail', kwargs={'pk': 2}), {})
# self.assertEqual(response['name'], group.name)
# self.assertEqual(response['label'], 'group')
# self.assertEqual(response['owner_id'], group.pk)
# own user detail
response_detail = self.do_request(reverse('user-detail', kwargs={'pk': self.user.id}), {}, 200)
self.assertEqual(response_detail['username'], self.username)
response_me = self.do_request(reverse('api-user-me'), {}, 200)
self.assertEqual(response_detail, response_me)
def test_token(self): def test_token(self):
"""Test token mechanisms.""" """Test token mechanisms."""
@ -242,3 +259,7 @@ class OwnerModelTest(InvenTreeTestCase):
# token second delete # token second delete
response = self.client.delete(reverse('api-token'), {}, format='json') response = self.client.delete(reverse('api-token'), {}, format='json')
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
# test user is associated with token
response = self.do_request(reverse('api-user-me'), {}, 200)
self.assertEqual(response['username'], self.username)