This commit is contained in:
46
backend/modules/invoicing/tests/test_api.py
Normal file
46
backend/modules/invoicing/tests/test_api.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import uuid
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
from rest_framework.test import APIClient
|
||||
from django.contrib.auth import get_user_model
|
||||
from decimal import Decimal
|
||||
from datetime import date
|
||||
|
||||
from modules.crm.models import Client
|
||||
from modules.invoicing.models import Invoice
|
||||
|
||||
|
||||
class InvoiceAPITests(TestCase):
|
||||
def setUp(self):
|
||||
self.api_client = APIClient()
|
||||
User = get_user_model()
|
||||
# Kreiraj test korisnika (ako treba autentikacija)
|
||||
self.user = User.objects.create_user(
|
||||
username=f"user_{uuid.uuid4().hex[:8]}",
|
||||
email=f"user+{uuid.uuid4().hex[:8]}@example.test",
|
||||
password="pass"
|
||||
)
|
||||
# Kreiraj jedinstvenog klijenta (email s uuid kako bismo izbjegli duplicate key)
|
||||
self.client_obj = Client.objects.create(
|
||||
name="API Test Client",
|
||||
email=f"client+{uuid.uuid4()}@example.test"
|
||||
)
|
||||
# autentikacija ako je endpoint zaštićen
|
||||
self.api_client.force_authenticate(user=self.user)
|
||||
|
||||
def test_api_creates_invoice_with_id(self):
|
||||
url = reverse('invoice-list') # prilagodi ako je druga ruta
|
||||
payload = {
|
||||
"client": self.client_obj.id,
|
||||
"due_date": date.today().isoformat(),
|
||||
"items": [
|
||||
{"description": "Test item", "quantity": "1.00", "unit_price": "10.00"}
|
||||
]
|
||||
}
|
||||
|
||||
response = self.api_client.post(url, payload, format='json')
|
||||
assert response.status_code in (200, 201)
|
||||
# provjeri da se faktura stvorila i da vraća id/kod
|
||||
created = Invoice.objects.filter(client=self.client_obj).first()
|
||||
assert created is not None
|
||||
assert created.amount == Decimal("10.00")
|
||||
Reference in New Issue
Block a user