Coverage for modules\erp\tests.py: 100%
49 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-06 17:01 +0800
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-06 17:01 +0800
1from decimal import Decimal
2from datetime import date, timedelta
3from unittest.mock import patch
5from django.test import Client, TestCase
7from core.auth import create_access_token
8from modules.authentication.models import Organization, User
9from modules.erp.models import Product, Shipment
10from modules.erp.services import ERPService
13class ERPInvoiceEmailTests(TestCase):
14 def setUp(self):
15 self.organization = Organization.objects.create(name='Invoice Org', slug='invoice-org')
16 self.product = Product.objects.create(
17 tenant_id=self.organization.id,
18 sku='SKU-001',
19 name='Consulting Service',
20 cost_price=Decimal('100.00'),
21 selling_price=Decimal('150.00'),
22 tax_rate=Decimal('12.00'),
23 )
25 @patch('modules.erp.services.send_invoice_email')
26 def test_create_invoice_sends_email_to_customer(self, mock_send):
27 class Payload:
28 customer_name = 'Acme Corp'
29 customer_email = 'billing@acme.test'
30 deal_id = None
31 issue_date = date.today()
32 due_date = date.today() + timedelta(days=30)
33 currency = 'PHP'
34 notes = 'Thank you for your business.'
35 terms = 'Net 30'
36 lines = [
37 type('Line', (), {
38 'product_id': self.product.id,
39 'description': 'Consulting service',
40 'quantity': Decimal('1.00'),
41 'unit_price': Decimal('150.00'),
42 'tax_rate': Decimal('12.00'),
43 })()
44 ]
46 with self.captureOnCommitCallbacks(execute=True):
47 invoice = ERPService.create_invoice(self.organization.id, Payload())
49 self.assertEqual(invoice.customer_email, 'billing@acme.test')
50 mock_send.assert_called_once_with(invoice, 'billing@acme.test')
53class ShipmentAPITests(TestCase):
54 def setUp(self):
55 self.client = Client()
56 self.user = User.objects.create_user(email='broker@example.com', password='password')
57 self.first_org = Organization.objects.create(name='First Broker', slug='first-broker')
58 self.second_org = Organization.objects.create(name='Second Broker', slug='second-broker')
60 def auth_headers(self, organization):
61 token = create_access_token(str(self.user.id), str(organization.id))
62 return {'HTTP_AUTHORIZATION': f'Bearer {token}'}
64 def test_create_and_list_are_tenant_scoped(self):
65 payload = {
66 'customer_name': 'Acme Imports',
67 'shipment_type': 'IMPORT',
68 'origin': 'Manila',
69 'destination': 'Cebu',
70 'declared_value': '1000.00',
71 }
72 response = self.client.post('/api/erp/shipments', payload, content_type='application/json', **self.auth_headers(self.first_org))
73 self.assertEqual(response.status_code, 201)
74 self.assertEqual(response.json()['reference_number'], 'SHP-00001')
75 self.assertEqual(Shipment.objects.filter(tenant_id=self.first_org.id).count(), 1)
77 other_response = self.client.get('/api/erp/shipments', **self.auth_headers(self.second_org))
78 self.assertEqual(other_response.status_code, 200)
79 self.assertEqual(other_response.json(), [])
81 first_response = self.client.get('/api/erp/shipments', **self.auth_headers(self.first_org))
82 self.assertEqual(first_response.status_code, 200)
83 self.assertEqual(len(first_response.json()), 1)