From b4c686bfaafe3283ad6c6e7b46d04c0970c3a3d2 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 21 Oct 2021 10:46:26 +0200 Subject: [PATCH] test: Add auth tests --- userausfall/rest_api/tests/__init__.py | 1 + userausfall/rest_api/tests/auth.py | 68 +++++++++++++++++++++++ userausfall/rest_api/tests/userausfall.py | 13 +++++ userausfall/tests.py | 23 ++++++++ 4 files changed, 105 insertions(+) create mode 100644 userausfall/rest_api/tests/__init__.py create mode 100644 userausfall/rest_api/tests/auth.py create mode 100644 userausfall/rest_api/tests/userausfall.py create mode 100644 userausfall/tests.py diff --git a/userausfall/rest_api/tests/__init__.py b/userausfall/rest_api/tests/__init__.py new file mode 100644 index 0000000..78ced3d --- /dev/null +++ b/userausfall/rest_api/tests/__init__.py @@ -0,0 +1 @@ +from .auth import * # noqa: F401, F403 diff --git a/userausfall/rest_api/tests/auth.py b/userausfall/rest_api/tests/auth.py new file mode 100644 index 0000000..48fb079 --- /dev/null +++ b/userausfall/rest_api/tests/auth.py @@ -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) diff --git a/userausfall/rest_api/tests/userausfall.py b/userausfall/rest_api/tests/userausfall.py new file mode 100644 index 0000000..30fd6ea --- /dev/null +++ b/userausfall/rest_api/tests/userausfall.py @@ -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)}" diff --git a/userausfall/tests.py b/userausfall/tests.py new file mode 100644 index 0000000..ceb2f8b --- /dev/null +++ b/userausfall/tests.py @@ -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)