Skip to content

Commit

Permalink
refactor: create authenticated_api_client fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
knop-k committed Dec 29, 2023
1 parent 2010e6d commit c123e72
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions Dshop/apps/products_catalogue/tests/test_api_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', [])
Expand All @@ -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
Expand All @@ -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
Expand All @@ -91,20 +91,18 @@ 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"
assert response.data['price'] == "19.99"


@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"
Expand Down

0 comments on commit c123e72

Please sign in to comment.