mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
[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:
parent
2d3570c923
commit
e870b0c9b6
@ -10,7 +10,7 @@ from rest_framework.authtoken.models import Token
|
||||
from rest_framework.response import Response
|
||||
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 users.models import Owner, RuleSet, check_user_role
|
||||
from users.serializers import OwnerSerializer
|
||||
@ -116,6 +116,14 @@ class UserDetail(RetrieveAPI):
|
||||
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):
|
||||
"""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'token/?$', GetAuthToken.as_view(), name='api-token'),
|
||||
re_path(r'^me/', MeUserDetail.as_view(), name='api-user-me'),
|
||||
|
||||
re_path(r'^owner/', include([
|
||||
path('<int:pk>/', OwnerDetail.as_view(), name='api-owner-detail'),
|
||||
|
@ -216,9 +216,26 @@ class OwnerModelTest(InvenTreeTestCase):
|
||||
self.do_request(reverse('api-owner-list'), {})
|
||||
# user list with search
|
||||
self.do_request(reverse('api-owner-list'), {'search': 'user'})
|
||||
# user detail
|
||||
# TODO fix this test
|
||||
# self.do_request(reverse('api-owner-detail', kwargs={'pk': self.user.id}), {})
|
||||
|
||||
# # owner detail - user
|
||||
# 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):
|
||||
"""Test token mechanisms."""
|
||||
@ -242,3 +259,7 @@ class OwnerModelTest(InvenTreeTestCase):
|
||||
# token second delete
|
||||
response = self.client.delete(reverse('api-token'), {}, format='json')
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user