113 lines
3.5 KiB
Python
113 lines
3.5 KiB
Python
# backend/modules/invoicing/tests/conftest.py
|
|
|
|
import pytest
|
|
from django.contrib.auth import get_user_model
|
|
from modules.invoicing.models import Invoice
|
|
from unittest.mock import MagicMock
|
|
from django.conf import settings
|
|
from core.celery import app as celery_app
|
|
import modules.invoicing.tasks as tasks
|
|
import uuid
|
|
|
|
User = get_user_model()
|
|
|
|
@pytest.fixture
|
|
def user():
|
|
# Dodan username
|
|
return User.objects.create_user(email="test@test.hr", password="password")
|
|
|
|
@pytest.fixture
|
|
def api_client(user):
|
|
from rest_framework.test import APIClient
|
|
client = APIClient()
|
|
client.force_authenticate(user=user)
|
|
return client
|
|
|
|
@pytest.fixture
|
|
def staff_user():
|
|
return User.objects.create_superuser(email='admin@erp.hr', password='password', first_name='Admin', last_name='User')
|
|
|
|
@pytest.fixture
|
|
def regular_user():
|
|
return User.objects.create_user(email='user@erp.hr', password='password', first_name='User', last_name='User')
|
|
|
|
@pytest.fixture
|
|
def serviser_a():
|
|
return User.objects.create_user(email="a@test.hr", password="password", first_name='A', last_name='User')
|
|
|
|
@pytest.fixture
|
|
def serviser_b():
|
|
return User.objects.create_user(email="b@test.hr", password="password", first_name='B', last_name='User')
|
|
|
|
@pytest.fixture
|
|
def test_client():
|
|
from modules.crm.models import Client
|
|
return Client.objects.create(name="Testni Klijent", tax_id="12345678901")
|
|
|
|
@pytest.fixture
|
|
def invoice_a(serviser_a, test_client): # Dodali smo test_client
|
|
from modules.invoicing.utils import generate_invoice_number
|
|
return Invoice.objects.create(
|
|
invoice_number=generate_invoice_number(),
|
|
creator=serviser_a,
|
|
client=test_client, # OVDJE JE BIO PROBLEM (bio je null)
|
|
amount=100.00,
|
|
due_date="2026-12-31"
|
|
)
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def paperless_settings(settings):
|
|
# Postavi privremene vrijednosti za vrijeme trajanja testa
|
|
settings.PAPERLESS_API_URL = "http://mock-paperless.local"
|
|
settings.PAPERLESS_API_TOKEN = "mock-token"
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def force_celery_eager(settings):
|
|
settings.CELERY_TASK_ALWAYS_EAGER = True
|
|
settings.CELERY_TASK_EAGER_PROPAGATES = True
|
|
|
|
# Ovo je ključ: prisilno ažuriraj konfiguraciju Celery aplikacije
|
|
celery_app.conf.update(
|
|
task_always_eager=True,
|
|
task_eager_propagates=True,
|
|
)
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def mock_paperless_gateway(monkeypatch):
|
|
"""
|
|
Koristi monkeypatch za zamjenu PaperlessGateway klase.
|
|
"""
|
|
mock_client = MagicMock()
|
|
mock_client.get_document_metadata.return_value = {
|
|
"correspondent": "INV-2026-TEST",
|
|
"total_amount": "100.00",
|
|
"created": "2026-07-01"
|
|
}
|
|
|
|
# Kreiramo tvornicu koja vraća naš mock
|
|
MockGateway = MagicMock(return_value=mock_client)
|
|
|
|
# Zamijeni klasu unutar tasks modula
|
|
monkeypatch.setattr(tasks, "PaperlessGateway", MockGateway)
|
|
|
|
yield MockGateway
|
|
|
|
@pytest.fixture
|
|
def mock_client(db):
|
|
"""Kreira klijenta kojeg koristi PaperlessGateway u testovima."""
|
|
from modules.crm.models import Client
|
|
return Client.objects.create(name="INV-2026-TEST") # Ime mora odgovarati mocku iz conftest.py
|
|
|
|
@pytest.fixture
|
|
def create_unique_client(db):
|
|
"""Factory funkcija za kreiranje unikatnog klijenta."""
|
|
from modules.crm.models import Client
|
|
def _create_client(name):
|
|
return Client.objects.create(
|
|
name=name,
|
|
tax_id=str(uuid.uuid4()), # Garantira unikatnost
|
|
email=f"{uuid.uuid4()}@test.hr" # Garantira unikatnost
|
|
)
|
|
return _create_client
|
|
|