Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made changes #10

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions inventory/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
app_name = 'inventory'

urlpatterns = [
path('products', InventoryProductList.as_view(), name='product_list'),
path('products/', InventoryProductCreate.as_view(), name='product_add'),
path('products/', InventoryProductList.as_view(), name='product_list'),
path('products/add/', InventoryProductCreate.as_view(), name='product_add'), #test required to distinguish url not only with slash
path('products/<str:pk>/', InventoryProductDetail.as_view(), name='product_detail'),
path('orders/', OrderListCreate.as_view(), name='order_list_create'),
path('orders/<str:pk>/', OrderDetail.as_view(), name='order_detail'),
Expand Down
1 change: 1 addition & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
_#!/bin/bash
export PYTHONPATH=./
rm -rf db.sqlite3
make migrate
pytest
61 changes: 0 additions & 61 deletions test/test_product.py
Original file line number Diff line number Diff line change
@@ -1,61 +0,0 @@
import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient
from users.models import User
from inventory.models import Product

@pytest.fixture
def api_client():
return APIClient()

@pytest.fixture
def admin_user(db):
return User.objects.create_user(email='[email protected]', username='admin', password='admin123', is_staff=True)

@pytest.fixture
def regular_user(db):
return User.objects.create_user(username='user',email='[email protected]', password='user123')

@pytest.fixture
def product_data():
return {
'name': 'Test Product',
'description': 'Test Description',
'quantity': 10,
'price': 100.0
}

@pytest.mark.django_db
def test_admin_can_create_product(api_client, admin_user, product_data):
url = reverse('product_add')
api_client.force_authenticate(user=admin_user)
response = api_client.post(url, product_data, format='json')
assert response.status_code == status.HTTP_201_CREATED
assert response.data['name'] == product_data['name']
assert response.data['description'] == product_data['description']
assert response.data['quantity'] == product_data['quantity']
assert response.data['price'] == product_data['price']

# @pytest.mark.django_db
# def test_non_admin_cannot_create_product(api_client, regular_user, product_data):
# url = reverse('product_add')
# api_client.force_authenticate(user=regular_user)
# response = api_client.post(url, product_data, format='json')
# assert response.status_code == status.HTTP_403_FORBIDDEN

# @pytest.mark.django_db
# def test_create_product_with_invalid_data(api_client, admin_user, product_data):
# url = reverse('product_add')
# api_client.force_authenticate(user=admin_user)
# invalid_data = product_data.copy()
# invalid_data['price'] = -100 # Invalid price
# response = api_client.post(url, invalid_data, format='json')
# assert response.status_code == status.HTTP_400_BAD_REQUEST
# assert 'price' in response.data

# @pytest.mark.django_db
# def test_unauthenticated_user_cannot_create_product(api_client, product_data):
# url = reverse('product_add')
# response = api_client.post(url, product_data, format='json')
# assert response.status_code == status.HTTP_401_UNAUTHORIZED
Loading