from decimal import Decimal from django.contrib.auth import get_user_model from django.db import IntegrityError, transaction from django.test import TestCase from apps.accounts.models import Customer from apps.orders.models import ProcessingOrder from apps.payments.gateways import get_gateway from apps.payments.models import Payment from apps.workflows.models import Workflow, WorkflowVersion User = get_user_model() class PaymentTests(TestCase): def setUp(self): self.user = User.objects.create_user(username="c", password="***") self.customer = Customer.objects.create(user=self.user) wf = Workflow.objects.create(name="W", slug="w-pay") version = WorkflowVersion.objects.create(workflow=wf, version=1) self.order = ProcessingOrder.objects.create(customer=self.customer, workflow_version=version) def test_mock_gateway_verify_returns_success(self): result = get_gateway().verify("AUTH-1", "1000") self.assertEqual(result["status"], "success") self.assertTrue(result["transaction_id"]) def test_duplicate_gateway_transaction_rejected(self): Payment.objects.create( order=self.order, amount=Decimal("1000"), currency="IRR", gateway="mock", transaction_id="TXN-1", status="success", ) with self.assertRaises(IntegrityError): with transaction.atomic(): Payment.objects.create( order=self.order, amount=Decimal("1000"), currency="IRR", gateway="mock", transaction_id="TXN-1", status="success", )