Skip to content

Commit

Permalink
Merge pull request #22 from Naz-iv/tests
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
Naz-iv authored Dec 15, 2023
2 parents 6c33d74 + 3849407 commit 04f2c9a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
17 changes: 12 additions & 5 deletions book_service/tests/test_book_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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="[email protected]", 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",
Expand Down Expand Up @@ -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",
Expand Down
15 changes: 7 additions & 8 deletions customer/tests/test_create_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ def setUp(self):
self.client = APIClient()

def test_create_customer(self):
url = '/api/users/'
url = "/api/users/"
data = {
'email': '[email protected]',
'password': 'testpass',
"email": "[email protected]",
"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'], '[email protected]')
self.assertIn("id", response.data)
self.assertEqual(response.data["email"], "[email protected]")

def test_create_superuser(self):
User = get_user_model()
admin_user = User.objects.create_superuser(
email='[email protected]',
password='adminpass'
email="[email protected]", password="adminpass"
)
self.assertTrue(admin_user.is_superuser)
16 changes: 8 additions & 8 deletions customer/tests/test_manage_and_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@


class ManageCustomerViewTests(TestCase):

def setUp(self):
self.user = get_user_model().objects.create_user(
email="[email protected]",
password="testpassword"
email="[email protected]", 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"])
self.assertEqual(self.user.last_name, new_data["last_name"])

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)

0 comments on commit 04f2c9a

Please sign in to comment.