From c123e72ca81dfac23673c4f2d3ed5a3a7776bd58 Mon Sep 17 00:00:00 2001 From: Krzysztof Knop Date: Fri, 29 Dec 2023 12:41:32 +0100 Subject: [PATCH] refactor: create authenticated_api_client fixture --- .../tests/test_api_products.py | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/Dshop/apps/products_catalogue/tests/test_api_products.py b/Dshop/apps/products_catalogue/tests/test_api_products.py index 4b991ee..ce1a98f 100644 --- a/Dshop/apps/products_catalogue/tests/test_api_products.py +++ b/Dshop/apps/products_catalogue/tests/test_api_products.py @@ -36,12 +36,16 @@ def create_inactive_product(): ) -@pytest.mark.django_db -def test_access_protected_resource(api_client, user_instance_token, create_active_product, create_inactive_product): +@pytest.fixture +def authenticated_api_client(api_client, user_instance_token): api_client.credentials(HTTP_AUTHORIZATION=f'Token {user_instance_token.key}') + return api_client + +@pytest.mark.django_db +def test_access_protected_resource(authenticated_api_client, create_active_product, create_inactive_product): url = reverse('products-api-list') - response = api_client.get(url) + response = authenticated_api_client.get(url) assert response.status_code == 200 results = response.data.get('results', []) @@ -65,11 +69,9 @@ def test_access_protected_resource_without_authentication(api_client): @pytest.mark.django_db -def test_product_detail(api_client, user_instance_token, create_active_product): - api_client.credentials(HTTP_AUTHORIZATION=f'Token {user_instance_token.key}') - +def test_product_detail(authenticated_api_client, create_active_product): url = reverse('products-api-detail', kwargs={'pk': create_active_product.id}) - response = api_client.get(url) + response = authenticated_api_client.get(url) assert response.status_code == 200 assert response.data['id'] == create_active_product.id @@ -80,9 +82,7 @@ def test_product_detail(api_client, user_instance_token, create_active_product): @pytest.mark.django_db -def test_create_product(api_client, user_instance_token, create_category): - api_client.credentials(HTTP_AUTHORIZATION=f'Token {user_instance_token.key}') - +def test_create_product(authenticated_api_client, create_category): url = reverse('products-api-list') data = { 'category': create_category.id, # replace with an existing category ID @@ -91,7 +91,7 @@ def test_create_product(api_client, user_instance_token, create_category): 'short_description': 'Test short description', 'full_description': 'Test full description', } - response = api_client.post(url, data, format='json') + response = authenticated_api_client.post(url, data, format='json') assert response.status_code == 201 assert response.data['name'] == "Test Product" @@ -99,12 +99,10 @@ def test_create_product(api_client, user_instance_token, create_category): @pytest.mark.django_db -def test_update_product(api_client, user_instance_token, create_active_product): - api_client.credentials(HTTP_AUTHORIZATION=f'Token {user_instance_token.key}') - +def test_update_product(authenticated_api_client, create_active_product): url = reverse('products-api-detail', kwargs={'pk': create_active_product.id}) data = {'name': 'Updated product name'} - response = api_client.patch(url, data, format='json') + response = authenticated_api_client.patch(url, data, format='json') assert response.status_code == 200 assert response.data['name'] == "Updated product name"