45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
from rest_framework import status
|
|
|
|
from userausfall.models import User
|
|
from userausfall.rest_api.tests.userausfall import get_url, UserausfallAPITestCase
|
|
|
|
|
|
class UserMixin:
|
|
user: User
|
|
password: str
|
|
username: str
|
|
|
|
def create_user(self):
|
|
self.username = f"test{User.objects.count()}"
|
|
self.password = "test12345"
|
|
self.user = User.objects.create_user(self.username, self.password)
|
|
return self.user
|
|
|
|
def ensure_user_exists(self):
|
|
if not hasattr(self, "user"):
|
|
self.create_user()
|
|
|
|
def authenticate_user(self):
|
|
self.ensure_user_exists()
|
|
if hasattr(self.client, "force_authentication"):
|
|
self.client.force_authenticate(user=self.user)
|
|
else:
|
|
self.client.force_login(user=self.user)
|
|
|
|
|
|
class UserTestCase(UserMixin, UserausfallAPITestCase):
|
|
def test_retrieve_user(self):
|
|
"""Retrieve the details of the current user."""
|
|
url = "/users/{pk}/"
|
|
self.authenticate_user()
|
|
response = self.client.get(self.get_api_url(url, pk=self.user.pk))
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
self.assertDictEqual(
|
|
response.data,
|
|
{
|
|
"id": self.user.id,
|
|
"trust_bridge": None,
|
|
"url": get_url(response, "user", self.user),
|
|
},
|
|
)
|