-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Naz-iv/tests
fix tests
- Loading branch information
Showing
3 changed files
with
27 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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="[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", | ||
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) |