Unit Tests / test (push) Successful in 10s
## Summary - Closes #21 — new Django `finance` app with Stripe as payment provider - Subscription price defaults to **$10 USD / month** via `SUBSCRIPTION_PRICE_AMOUNT_CENTS = 1000` in `settings.py` (env-overridable) - Persists **Invoice** and **Payment** rows; both registered in Django admin (with payment inline on invoices) - Checkout Session API redirects users to Stripe hosted payment; webhook verifies signatures and upserts ledger idempotently ## API - `POST /api/finance/checkout/` — JWT auth → `{ checkout_url, session_id }` - `GET /api/finance/invoices/` / `GET /api/finance/payments/` — own records - `POST /api/finance/webhooks/stripe/` — Stripe signature-verified webhook ## Config Documented in `.env.example` / `.env.prod.example`: `STRIPE_SECRET_KEY`, `STRIPE_PUBLISHABLE_KEY`, `STRIPE_WEBHOOK_SECRET`, optional `STRIPE_PRICE_ID`, `FRONTEND_BASE_URL` ## Test plan - [x] `uv run python manage.py test finance` (17 tests) - [ ] Set Stripe test keys locally; create checkout session; complete payment in Stripe test mode - [ ] Confirm Invoice/Payment appear in `/admin/` - [ ] Point Stripe webhook to `/api/finance/webhooks/stripe/` and verify `checkout.session.completed` / `invoice.paid`Reviewed-on: #23
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
"""Tests for finance Invoice / Payment models and admin registration."""
|
|
|
|
from django.contrib import admin
|
|
from django.test import TestCase
|
|
|
|
from chat_backend.tests.factories import make_company, make_user
|
|
from finance.models import Invoice, Payment
|
|
|
|
|
|
class InvoicePaymentModelTestCase(TestCase):
|
|
def setUp(self):
|
|
self.company = make_company()
|
|
self.user = make_user(company=self.company)
|
|
|
|
def test_create_invoice_and_payment(self):
|
|
invoice = Invoice.objects.create(
|
|
user=self.user,
|
|
company=self.company,
|
|
status=Invoice.Status.OPEN,
|
|
amount_due=1000,
|
|
currency="usd",
|
|
stripe_checkout_session_id="cs_test_1",
|
|
)
|
|
payment = Payment.objects.create(
|
|
user=self.user,
|
|
company=self.company,
|
|
invoice=invoice,
|
|
amount=1000,
|
|
currency="usd",
|
|
status=Payment.Status.PENDING,
|
|
stripe_payment_intent_id="pi_test_1",
|
|
)
|
|
self.assertEqual(invoice.provider, Invoice.Provider.STRIPE)
|
|
self.assertEqual(payment.invoice_id, invoice.pk)
|
|
self.assertEqual(Invoice.objects.count(), 1)
|
|
self.assertEqual(Payment.objects.count(), 1)
|
|
|
|
def test_mark_payment_succeeded(self):
|
|
payment = Payment.objects.create(
|
|
user=self.user,
|
|
amount=1000,
|
|
stripe_payment_intent_id="pi_test_2",
|
|
)
|
|
payment.mark_succeeded()
|
|
payment.refresh_from_db()
|
|
self.assertEqual(payment.status, Payment.Status.SUCCEEDED)
|
|
self.assertIsNotNone(payment.paid_at)
|
|
|
|
def test_unique_stripe_checkout_session_id(self):
|
|
Invoice.objects.create(
|
|
user=self.user,
|
|
stripe_checkout_session_id="cs_unique",
|
|
amount_due=1000,
|
|
)
|
|
with self.assertRaises(Exception):
|
|
Invoice.objects.create(
|
|
user=self.user,
|
|
stripe_checkout_session_id="cs_unique",
|
|
amount_due=1000,
|
|
)
|
|
|
|
|
|
class FinanceAdminRegistrationTestCase(TestCase):
|
|
def test_invoice_and_payment_registered(self):
|
|
self.assertIn(Invoice, admin.site._registry)
|
|
self.assertIn(Payment, admin.site._registry)
|