test: Add auth tests
This commit is contained in:
parent
a887be1368
commit
b4c686bfaa
4 changed files with 105 additions and 0 deletions
1
userausfall/rest_api/tests/__init__.py
Normal file
1
userausfall/rest_api/tests/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .auth import * # noqa: F401, F403
|
68
userausfall/rest_api/tests/auth.py
Normal file
68
userausfall/rest_api/tests/auth.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
from rest_framework import status
|
||||
|
||||
from userausfall.models import User
|
||||
from userausfall.rest_api.tests.userausfall import UserausfallAPITestCase
|
||||
from userausfall.tests import UserMixin
|
||||
|
||||
|
||||
class UserTestCase(UserMixin, UserausfallAPITestCase):
|
||||
base_url = "/api/auth"
|
||||
|
||||
def test_signup(self):
|
||||
"""
|
||||
Create a new user account (signup)
|
||||
"""
|
||||
url = "/register/"
|
||||
username = "test"
|
||||
password = "test12345"
|
||||
data = {"username": username, "password": password, "passwordConfirm": password}
|
||||
response = self.client.post(self.get_api_url(url), data)
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(User.objects.count(), 1)
|
||||
self.assertEqual(User.objects.get().username, username)
|
||||
|
||||
def test_login(self) -> None:
|
||||
"""
|
||||
Create a session cookie (login)
|
||||
"""
|
||||
url = "/login/"
|
||||
self.create_user()
|
||||
data = {"login": self.username, "password": self.password}
|
||||
response = self.client.post(self.get_api_url(url), data)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertTrue(response.wsgi_request.user.is_authenticated)
|
||||
|
||||
def test_logout(self) -> None:
|
||||
"""
|
||||
Remove the session (logout)
|
||||
"""
|
||||
url = "/logout/"
|
||||
self.authenticate_user()
|
||||
response = self.client.post(self.get_api_url(url))
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertFalse(response.wsgi_request.user.is_authenticated)
|
||||
|
||||
def test_retrieve_user(self) -> None:
|
||||
"""
|
||||
Retrieve data of authenticated user
|
||||
"""
|
||||
url = "/profile/"
|
||||
self.create_user()
|
||||
self.authenticate_user()
|
||||
response = self.client.get(self.get_api_url(url))
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertDictEqual(
|
||||
response.data,
|
||||
{
|
||||
"username": self.username,
|
||||
"id": self.user.id,
|
||||
},
|
||||
)
|
||||
|
||||
def test_create_two_users(self) -> None:
|
||||
"""
|
||||
Create more than one user
|
||||
"""
|
||||
self.create_user()
|
||||
self.create_user()
|
||||
self.assertEqual(User.objects.count(), 2)
|
13
userausfall/rest_api/tests/userausfall.py
Normal file
13
userausfall/rest_api/tests/userausfall.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
from rest_framework.test import APITestCase
|
||||
|
||||
|
||||
class UserausfallAPITestCase(APITestCase):
|
||||
base_url = "/api"
|
||||
|
||||
def get_api_url(self, url, **kwargs):
|
||||
"""
|
||||
Prepend the path to the full url for this test class.
|
||||
:param url: an url fragment
|
||||
:return: the url fragment prepended with the base url
|
||||
"""
|
||||
return f"{self.base_url}{url.format(**kwargs)}"
|
23
userausfall/tests.py
Normal file
23
userausfall/tests.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
from userausfall.models import User
|
||||
|
||||
|
||||
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)
|
||||
|
||||
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)
|
Reference in a new issue