From 3849407e9f27b697b74bae91be435fa656aca44a Mon Sep 17 00:00:00 2001 From: OlehShumov <«shumovoleh@ukr.net»> Date: Fri, 15 Dec 2023 17:21:40 +0200 Subject: [PATCH] fix tests --- book_service/tests/test_book_endpoints.py | 17 ++++++++++++----- customer/tests/test_create_customer.py | 15 +++++++-------- customer/tests/test_manage_and_auth.py | 16 ++++++++-------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/book_service/tests/test_book_endpoints.py b/book_service/tests/test_book_endpoints.py index b958b05..287b5cb 100644 --- a/book_service/tests/test_book_endpoints.py +++ b/book_service/tests/test_book_endpoints.py @@ -4,6 +4,7 @@ from django.urls import reverse from rest_framework import status from rest_framework.test import APIClient +from rest_framework_simplejwt.tokens import RefreshToken from book_service.models import Book from book_service.serializers import BookListSerializer, BookSerializer @@ -39,16 +40,19 @@ def test_book_list_does_not_require_authentication(self): def test_book_create_forbidden(self): response = self.client.post(BOOK_URL, {}) - self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) class AuthenticatedApiTests(TestCase): def setUp(self): self.client = APIClient() self.user = get_user_model().objects.create( - username="user", password="password123" + email="user1@example.com", password="password123" ) - self.client.force_login(self.user) + self.refresh_token = RefreshToken.for_user(self.user) + self.access_token = str(self.refresh_token.access_token) + self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}") + self.book1 = Book.objects.create( title="Inferno", author="Dan Broun", @@ -100,9 +104,12 @@ class AdminApiTests(TestCase): def setUp(self): self.client = APIClient() self.user = get_user_model().objects.create( - username="user", password="password123", is_staff=True + email="user@example.com", password="password123", is_staff=True ) - self.client.force_login(self.user) + self.refresh_token = RefreshToken.for_user(self.user) + self.access_token = str(self.refresh_token.access_token) + self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}") + self.book = Book.objects.create( title="Inferno", author="Dan Broun", diff --git a/customer/tests/test_create_customer.py b/customer/tests/test_create_customer.py index b83ee46..b9d790b 100644 --- a/customer/tests/test_create_customer.py +++ b/customer/tests/test_create_customer.py @@ -9,21 +9,20 @@ def setUp(self): self.client = APIClient() def test_create_customer(self): - url = '/api/users/' + url = "/api/users/" data = { - 'email': 'testuser@example.com', - 'password': 'testpass', + "email": "testuser@example.com", + "password": "testpass", } - response = self.client.post(url, data, format='json') + response = self.client.post(url, data, format="json") self.assertEqual(response.status_code, status.HTTP_201_CREATED) - self.assertIn('id', response.data) - self.assertEqual(response.data['email'], 'testuser@example.com') + self.assertIn("id", response.data) + self.assertEqual(response.data["email"], "testuser@example.com") def test_create_superuser(self): User = get_user_model() admin_user = User.objects.create_superuser( - email='adminuser@example.com', - password='adminpass' + email="adminuser@example.com", password="adminpass" ) self.assertTrue(admin_user.is_superuser) diff --git a/customer/tests/test_manage_and_auth.py b/customer/tests/test_manage_and_auth.py index 25cd8a1..327b18e 100644 --- a/customer/tests/test_manage_and_auth.py +++ b/customer/tests/test_manage_and_auth.py @@ -6,25 +6,23 @@ class ManageCustomerViewTests(TestCase): - def setUp(self): self.user = get_user_model().objects.create_user( - email="test@example.com", - password="testpassword" + email="test@example.com", password="testpassword" ) self.client = APIClient() self.refresh_token = RefreshToken.for_user(self.user) self.access_token = str(self.refresh_token.access_token) - self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {self.access_token}') + self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}") def test_retrieve_customer_profile(self): - response = self.client.get('/api/users/me/') + response = self.client.get("/api/users/me/") self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data['email'], self.user.email) + self.assertEqual(response.data["email"], self.user.email) def test_update_customer_profile(self): new_data = {"first_name": "New", "last_name": "Name"} - response = self.client.patch('/api/users/me/', new_data, format='json') + response = self.client.patch("/api/users/me/", new_data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.user.refresh_from_db() self.assertEqual(self.user.first_name, new_data["first_name"]) @@ -32,5 +30,7 @@ def test_update_customer_profile(self): def test_update_customer_profile_unauthenticated(self): unauthenticated_client = APIClient() - response = unauthenticated_client.patch('/api/users/me/', {"first_name": "New"}, format='json') + response = unauthenticated_client.patch( + "/api/users/me/", {"first_name": "New"}, format="json" + ) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)