## Summary - Rename `finance` → **`monetization`** Django app (keep `finance_*` tables via `label = "finance"`) - Add `services/stripe.py` + `services/revenuecat.py`; RevenueCat webhook upserts **subscription + Invoice/Payment** (billing history parity with Stripe) - Mount `/api/monetization/` + keep `/api/finance/` alias - Extend `Source`/`Provider` with `revenuecat`; product→plan mapping via `revenuecat_product_id` / `REVENUECAT_PRODUCT_PLAN_MAP` Closes #68. Companion to [chat_web_app#100](ai_ml_operations/chat_web_app#100). ## Test plan - [x] `manage.py test monetization.tests` (54 OK) - [x] Smoke `chat_backend.tests.test_views_documents` + `test_oauth` - [ ] Deploy: set `REVENUECAT_WEBHOOK_SECRET`; point RC webhook at `/api/finance/webhooks/revenuecat/` - [ ] Map store product IDs on `SubscriptionPlan.revenuecat_product_id` (or env JSON map) - [ ] Sandbox INITIAL_PURCHASE → subscription `source=revenuecat` + invoice in `/finance/invoices/`Reviewed-on: #69
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 monetization.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)
|