Files
Webklar-Kundenbereich/test/financeStripeWrite.test.js
T

383 lines
18 KiB
JavaScript

import { test, after, beforeEach } from 'node:test'
import assert from 'node:assert/strict'
import { startAppwriteMock } from './support/appwriteMock.js'
import { startFakeStripe } from './support/fakeStripe.js'
import { useTestEnv } from './support/testEnv.js'
import { schemaKeys } from './support/schema.js'
// Nachbauten starten und die Umgebung setzen, BEVOR config.js (ueber stripeBilling) geladen wird
const aw = await startAppwriteMock({ schema: { subscriptions: schemaKeys('subscriptions') } })
const st = await startFakeStripe()
useTestEnv({ appwriteUrl: aw.url, stripeUrl: st.url })
const { config } = await import('../server/config.js')
const { seedFixtures } = await import('./support/fixtures.js')
const { berlinDate } = await import('../server/services/finance/dates.js')
const { getStripe } = await import('../server/services/stripeBilling.js')
const { invoiceFooter, createProjectInvoice, sendInvoiceNow, voidInvoice, deleteDraft, listInvoices, invalidateFinanceCache } = await import(
'../server/services/finance/stripeFinance.js'
)
after(() => Promise.all([aw.close(), st.close()]))
seedFixtures(aw)
aw.seed('customers', { $id: 'lead1', email: 'lead@example.invalid', name: 'Interessent', customerStatus: 'lead', status: 'active' })
aw.seed('customers', { $id: 'lost1', email: 'lost@example.invalid', name: 'Verloren', customerStatus: 'Lost', status: 'active' })
aw.seed('customers', { $id: 'act1', email: 'act@example.invalid', name: 'Aktiv', companyName: 'Aktiv GmbH', customerStatus: 'active', status: 'active' })
const PAYMENT_NOTE = 'Bitte geben Sie bei Überweisung die Rechnungsnummer als Verwendungszweck an.'
const BASE_ITEMS = [
{ description: 'Website-Anpassung', quantity: 2, unitAmountCents: 5000 },
{ description: 'Domain', quantity: 1, unitAmountCents: 1200 },
]
beforeEach(() => {
invalidateFinanceCache()
st.calls.length = 0
st.invoices.clear()
st.customers.clear()
// Das Abo von c1 kennt die Stripe-Kunden-ID cus_c1 (seedFixtures); der Nachbau soll den Kunden ebenfalls kennen
st.customers.set('cus_c1', { id: 'cus_c1', object: 'customer', name: 'Kunde Eins', email: 'k@example.invalid', metadata: { webklarCustomerId: 'c1' } })
})
const callsTo = (method, path) => st.calls.filter((c) => c.method === method && (path instanceof RegExp ? path.test(c.path) : c.path === path))
const writes = () => st.calls.filter((c) => c.method !== 'GET')
const rejectsWith = (promise, status, message) =>
assert.rejects(promise, (err) => {
assert.equal(err.status, status, `Status ${err.status}: ${err.message}`)
if (message) assert.equal(err.message, message)
return true
})
const aboInvoice = (over = {}) => ({
id: 'in_abo',
object: 'invoice',
number: 'WK-0100',
status: 'open',
customer: 'cus_c1',
subscription: 'sub_h',
created: Math.floor(Date.now() / 1000),
due_date: null,
total: 2000,
amount_remaining: 2000,
amount_paid: 0,
metadata: {},
lines: { object: 'list', data: [], has_more: false },
...over,
})
const projectInvoice = (over = {}) => ({
id: 'in_proj',
object: 'invoice',
number: null,
status: 'draft',
customer: 'cus_c1',
created: Math.floor(Date.now() / 1000),
due_date: null,
total: 6000,
amount_remaining: 6000,
amount_paid: 0,
metadata: { quelle: 'woms' },
lines: { object: 'list', data: [], has_more: false },
...over,
})
test('invoiceFooter: Kleinunternehmer-Hinweis, Bankdaten (falls gesetzt), Verwendungszweck-Satz', () => {
assert.equal(invoiceFooter(), `${config.stripe.invoiceFooter}\n${PAYMENT_NOTE}`)
assert.equal(invoiceFooter(''), `${config.stripe.invoiceFooter}\n${PAYMENT_NOTE}`)
assert.equal(invoiceFooter(' \n '), `${config.stripe.invoiceFooter}\n${PAYMENT_NOTE}`)
assert.equal(
invoiceFooter('IBAN DE00 0000 0000 0000 0000 00\nBIC TESTDEFF'),
`${config.stripe.invoiceFooter}\nIBAN DE00 0000 0000 0000 0000 00\nBIC TESTDEFF\n${PAYMENT_NOTE}`
)
})
test('createProjectInvoice: Entwurf mit Positionen, Rechnungsfuss und Metadaten (nicht gesendet)', async () => {
const result = await createProjectInvoice({
customerId: 'c1',
items: BASE_ITEMS,
memo: 'Leistungen im September',
createdBy: 'kenso@example.invalid',
bankDetailsText: 'IBAN DE00 0000 0000 0000 0000 00',
})
const [create] = callsTo('POST', '/v1/invoices')
assert.ok(create, 'POST /v1/invoices fehlt')
assert.equal(create.body.customer, 'cus_c1')
assert.equal(create.body.collection_method, 'send_invoice')
assert.equal(create.body.days_until_due, '14')
assert.equal(create.body.auto_advance, 'false')
assert.equal(create.body.pending_invoice_items_behavior, 'exclude')
assert.equal(create.body.currency, 'eur')
assert.equal(create.body.description, 'Leistungen im September')
assert.equal(create.body['metadata[quelle]'], 'woms')
assert.equal(create.body['metadata[webklarCustomerId]'], 'c1')
assert.equal(create.body['metadata[erstelltVon]'], 'kenso@example.invalid')
assert.ok(create.body.footer.startsWith(config.stripe.invoiceFooter))
assert.ok(create.body.footer.endsWith(PAYMENT_NOTE))
assert.equal(create.body.footer, `${config.stripe.invoiceFooter}\nIBAN DE00 0000 0000 0000 0000 00\n${PAYMENT_NOTE}`)
// Menge und Einzelpreis jeder Position kommen bei Stripe an: im Aufruf ...
const items = callsTo('POST', '/v1/invoiceitems')
assert.equal(items.length, 2)
assert.deepEqual(items[0].body, {
customer: 'cus_c1',
invoice: result.id,
description: 'Website-Anpassung',
quantity: '2',
unit_amount_decimal: '5000',
currency: 'eur',
})
assert.deepEqual(items[1].body, {
customer: 'cus_c1',
invoice: result.id,
description: 'Domain',
quantity: '1',
unit_amount_decimal: '1200',
currency: 'eur',
})
// ... und auf der Rechnung (Zeilen mit Menge und Einzelpreis, Summe aus Menge mal Einzelpreis)
const lines = st.invoices.get(result.id).lines.data
assert.deepEqual(
lines.map((l) => [l.description, l.quantity, l.pricing.unit_amount_decimal, l.amount]),
[
['Website-Anpassung', 2, '5000', 10000],
['Domain', 1, '1200', 1200],
]
)
assert.equal(result.status, 'entwurf')
assert.equal(result.source, 'projekt')
assert.equal(result.totalCents, 11200)
assert.equal(result.remainingCents, 11200)
assert.equal(result.webklarCustomerId, 'c1')
assert.equal(result.customerName, 'Kunde Eins')
assert.equal(result.number, '')
// kein Finalisieren, kein Senden
assert.equal(callsTo('POST', /finalize|send/).length, 0)
})
test('createProjectInvoice: ohne Vermerk keine Beschreibung, ohne Bankdaten kuerzerer Fuss, Zahlungsziel 0 bleibt 0', async () => {
await createProjectInvoice({ customerId: 'c1', items: [BASE_ITEMS[1]], daysUntilDue: 0, createdBy: 'k' })
const [create] = callsTo('POST', '/v1/invoices')
assert.equal(create.body.days_until_due, '0')
assert.equal('description' in create.body, false)
assert.equal(create.body.footer, `${config.stripe.invoiceFooter}\n${PAYMENT_NOTE}`)
})
test('createProjectInvoice: legt fuer Kunden ohne Stripe-Kunde einen an (Firmenname)', async () => {
const result = await createProjectInvoice({ customerId: 'act1', items: [BASE_ITEMS[1]], createdBy: 'k' })
const created = callsTo('POST', '/v1/customers')
assert.equal(created.length, 1)
assert.equal(created[0].body.name, 'Aktiv GmbH')
assert.equal(result.webklarCustomerId, 'act1')
assert.equal(result.customerName, 'Aktiv GmbH')
})
test('createProjectInvoice: senden=true finalisiert (ohne Auto-Advance) und sendet, Rechnung ist offen mit Nummer', async () => {
const result = await createProjectInvoice({ customerId: 'c1', items: BASE_ITEMS, daysUntilDue: 30, senden: true, createdBy: 'k' })
const order = writes().map((c) => c.path.replace(/in_test_\d+/, ':id'))
assert.deepEqual(order.slice(-4), ['/v1/invoiceitems', '/v1/invoiceitems', '/v1/invoices/:id/finalize', '/v1/invoices/:id/send'])
const [finalize] = callsTo('POST', /\/finalize$/)
assert.equal(finalize.body.auto_advance, 'false')
assert.equal(result.status, 'offen')
assert.match(result.number, /^WK-\d{4}$/)
assert.equal(result.totalCents, 11200)
assert.equal(result.dueDate, berlinDate(new Date(Date.now() + 30 * 86_400_000)))
assert.ok(result.hostedUrl)
})
test('createProjectInvoice: Lead-, verlorene und unbekannte Kunden werden mit 400 abgelehnt, Stripe bleibt unberuehrt', async () => {
await rejectsWith(createProjectInvoice({ customerId: 'lead1', items: BASE_ITEMS, createdBy: 'k' }), 400, 'Rechnungen nur für feste Kunden.')
await rejectsWith(createProjectInvoice({ customerId: 'lost1', items: BASE_ITEMS, createdBy: 'k' }), 400, 'Rechnungen nur für feste Kunden.')
await rejectsWith(createProjectInvoice({ customerId: 'gibtEsNicht', items: BASE_ITEMS, createdBy: 'k' }), 400)
await rejectsWith(createProjectInvoice({ items: BASE_ITEMS, createdBy: 'k' }), 400)
assert.equal(writes().length, 0)
})
test('createProjectInvoice: Eingaben werden geprueft (Positionen, Texte, Mengen, Betraege, Zahlungsziel)', async () => {
const ok = BASE_ITEMS[1]
const bad = async (label, input) => {
try {
await createProjectInvoice({ customerId: 'c1', items: [ok], createdBy: 'k', ...input })
} catch (err) {
assert.equal(err.status, 400, `${label}: Status ${err.status} (${err.message})`)
return
}
assert.fail(`${label}: wurde nicht abgelehnt`)
}
const item = (over) => ({ ...ok, ...over })
// 21 Positionen, keine Positionen, keine Liste
await rejectsWith(createProjectInvoice({ customerId: 'c1', items: Array.from({ length: 21 }, () => ok), createdBy: 'k' }), 400)
await bad('leer', { items: [] })
await bad('keine Liste', { items: undefined })
// Beschreibung
await bad('Beschreibung leer', { items: [item({ description: '' })] })
await bad('Beschreibung nur Leerzeichen', { items: [item({ description: ' ' })] })
await bad('Beschreibung 201 Zeichen', { items: [item({ description: 'x'.repeat(201) })] })
await bad('Beschreibung keine Zeichenkette', { items: [item({ description: 5 })] })
// Menge
for (const quantity of [0, 1000, 1.5, '2', -1, null]) await bad(`Menge ${quantity}`, { items: [item({ quantity })] })
// Einzelpreis
for (const unitAmountCents of [0, 10_000_001, 12.5, '5000', -100, null]) await bad(`Preis ${unitAmountCents}`, { items: [item({ unitAmountCents })] })
// Zahlungsziel
for (const daysUntilDue of [-1, 61, 1.5, '14', null]) await bad(`Ziel ${daysUntilDue}`, { daysUntilDue })
assert.equal(writes().length, 0, 'bei ungueltigen Eingaben darf nichts an Stripe gehen')
})
test('createProjectInvoice: Grenzwerte sind erlaubt (20 Positionen, 200 Zeichen, Menge 999, 10.000.000 Cent, Ziel 60)', async () => {
const items = Array.from({ length: 20 }, (_, i) => ({ description: i === 0 ? 'x'.repeat(200) : `Position ${i}`, quantity: 999, unitAmountCents: 10_000_000 }))
const result = await createProjectInvoice({ customerId: 'c1', items, daysUntilDue: 60, createdBy: 'k' })
assert.equal(callsTo('POST', '/v1/invoiceitems').length, 20)
assert.equal(result.totalCents, 20 * 999 * 10_000_000)
assert.equal(callsTo('POST', '/v1/invoices')[0].body.days_until_due, '60')
})
test('createProjectInvoice: scheitert eine Position, wird der halbfertige Entwurf geloescht', async () => {
st.failNext({ method: 'POST', path: '/v1/invoiceitems', status: 400, message: 'Nachbau: Position abgelehnt', after: 1 })
await assert.rejects(createProjectInvoice({ customerId: 'c1', items: BASE_ITEMS, createdBy: 'k' }), /Position abgelehnt/)
assert.equal(callsTo('DELETE', /^\/v1\/invoices\/in_/).length, 1)
assert.equal(st.invoices.size, 0)
})
test('sendInvoiceNow: Entwurf wird finalisiert (ohne Auto-Advance) und gesendet', async () => {
const draft = await createProjectInvoice({ customerId: 'c1', items: BASE_ITEMS, createdBy: 'k' })
st.calls.length = 0
const sent = await sendInvoiceNow(draft.id)
assert.deepEqual(
writes().map((c) => c.path.replace(draft.id, ':id')),
['/v1/invoices/:id/finalize', '/v1/invoices/:id/send']
)
assert.equal(callsTo('POST', /\/finalize$/)[0].body.auto_advance, 'false')
assert.equal(sent.status, 'offen')
assert.match(sent.number, /^WK-\d{4}$/)
})
test('sendInvoiceNow: nur Entwuerfe (409), nur Projektrechnungen, unbekannte Rechnung 404', async () => {
st.invoices.set('in_open', projectInvoice({ id: 'in_open', status: 'open', number: 'WK-0001' }))
st.invoices.set('in_abo', aboInvoice({ status: 'draft', number: null }))
await rejectsWith(sendInvoiceNow('in_open'), 409, 'Nur Entwürfe können gesendet werden.')
await rejectsWith(sendInvoiceNow('in_abo'), 409, 'Abo-Rechnungen verwaltet Stripe.')
await rejectsWith(sendInvoiceNow('in_gibtEsNicht'), 404, 'Rechnung nicht gefunden.')
assert.equal(writes().length, 0)
})
test('voidInvoice: offene und ueberfaellige Projektrechnungen werden storniert', async () => {
const open = await createProjectInvoice({ customerId: 'c1', items: BASE_ITEMS, senden: true, createdBy: 'k' })
const voided = await voidInvoice(open.id)
assert.equal(voided.status, 'storniert')
assert.equal(callsTo('POST', `/v1/invoices/${open.id}/void`).length, 1)
// ueberfaellig: offen, Faelligkeit vor heute
st.invoices.set('in_late', projectInvoice({ id: 'in_late', status: 'open', number: 'WK-0007', due_date: Math.floor(Date.now() / 1000) - 10 * 86_400 }))
assert.equal((await voidInvoice('in_late')).status, 'storniert')
})
test('voidInvoice: Entwurf, bezahlte und Abo-Rechnungen werden mit 409 abgelehnt, unbekannte mit 404', async () => {
st.invoices.set('in_draft', projectInvoice({ id: 'in_draft' }))
st.invoices.set('in_paid', projectInvoice({ id: 'in_paid', status: 'paid', number: 'WK-0002' }))
st.invoices.set('in_abo', aboInvoice())
await rejectsWith(voidInvoice('in_draft'), 409)
await rejectsWith(voidInvoice('in_paid'), 409)
await rejectsWith(voidInvoice('in_abo'), 409, 'Abo-Rechnungen verwaltet Stripe.')
await rejectsWith(voidInvoice('in_gibtEsNicht'), 404, 'Rechnung nicht gefunden.')
assert.equal(writes().length, 0)
assert.equal(st.invoices.get('in_draft').status, 'draft')
})
test('deleteDraft: loescht einen Projekt-Entwurf bei Stripe', async () => {
const draft = await createProjectInvoice({ customerId: 'c1', items: BASE_ITEMS, createdBy: 'k' })
const result = await deleteDraft(draft.id)
assert.equal(result, undefined)
assert.equal(callsTo('DELETE', `/v1/invoices/${draft.id}`).length, 1)
assert.equal(st.invoices.has(draft.id), false)
})
test('deleteDraft: Abo-Rechnungen und alles ausser Entwuerfen werden mit 409 abgelehnt', async () => {
st.invoices.set('in_abo', aboInvoice())
st.invoices.set('in_abo_draft', aboInvoice({ id: 'in_abo_draft', status: 'draft', number: null }))
st.invoices.set('in_open', projectInvoice({ id: 'in_open', status: 'open', number: 'WK-0003' }))
await rejectsWith(deleteDraft('in_abo'), 409, 'Abo-Rechnungen verwaltet Stripe.')
await rejectsWith(deleteDraft('in_abo_draft'), 409, 'Abo-Rechnungen verwaltet Stripe.')
await rejectsWith(deleteDraft('in_open'), 409)
await rejectsWith(deleteDraft('in_gibtEsNicht'), 404, 'Rechnung nicht gefunden.')
assert.equal(writes().length, 0)
assert.equal(st.invoices.size, 3)
})
test('Schreibzugriffe leeren den Zwischenspeicher: die Liste zeigt neue, gesendete, stornierte und geloeschte Rechnungen sofort', async () => {
assert.deepEqual(await listInvoices(), [])
const draft = await createProjectInvoice({ customerId: 'c1', items: BASE_ITEMS, createdBy: 'k' })
assert.deepEqual((await listInvoices()).map((i) => [i.id, i.status]), [[draft.id, 'entwurf']])
await sendInvoiceNow(draft.id)
assert.equal((await listInvoices())[0].status, 'offen')
await voidInvoice(draft.id)
assert.equal((await listInvoices())[0].status, 'storniert')
const second = await createProjectInvoice({ customerId: 'c1', items: BASE_ITEMS, createdBy: 'k' })
assert.equal((await listInvoices()).length, 2)
await deleteDraft(second.id)
assert.deepEqual((await listInvoices()).map((i) => i.id), [draft.id])
})
test('Stripe-Nachbau: Statusuebergaenge wie Stripe (finalize nur Entwurf, send/void/pay nur offen, loeschen nur Entwurf)', async () => {
const stripe = getStripe()
const status = (id) => st.invoices.get(id).status
const refused = (promise, pattern) => assert.rejects(promise, (err) => err.statusCode === 400 && pattern.test(err.message))
const create = () => stripe.invoices.create({ customer: 'cus_c1', collection_method: 'send_invoice', days_until_due: 7 })
const item = (id) => stripe.invoiceItems.create({ customer: 'cus_c1', invoice: id, description: 'Test', quantity: 3, unit_amount_decimal: '250', currency: 'eur' })
// send_invoice braucht ein Zahlungsziel
await assert.rejects(stripe.invoices.create({ customer: 'cus_c1', collection_method: 'send_invoice' }), (err) => err.statusCode === 400)
const draft = await create()
assert.equal(draft.status, 'draft')
await item(draft.id)
assert.equal(st.invoices.get(draft.id).total, 750)
await refused(stripe.invoices.sendInvoice(draft.id), /open/)
await refused(stripe.invoices.voidInvoice(draft.id), /open/)
await refused(stripe.invoices.pay(draft.id), /open/)
// Metadaten zusammenfuehren, leerer Wert loescht
await stripe.invoices.update(draft.id, { metadata: { a: '1', b: '2' } })
await stripe.invoices.update(draft.id, { metadata: { b: '', c: '3' } })
assert.deepEqual(st.invoices.get(draft.id).metadata, { a: '1', c: '3' })
const open = await stripe.invoices.finalizeInvoice(draft.id, { auto_advance: false })
assert.equal(open.status, 'open')
assert.match(open.number, /^WK-\d{4}$/)
assert.equal(open.auto_advance, false)
await refused(stripe.invoices.finalizeInvoice(draft.id), /draft/)
await refused(stripe.invoices.del(draft.id), /draft/)
await refused(item(draft.id), /draft/)
assert.equal(status(draft.id), 'open')
// Nummern laufen fortlaufend
const second = await create()
assert.equal((await stripe.invoices.finalizeInvoice(second.id)).number, `WK-${String(Number(open.number.slice(3)) + 1).padStart(4, '0')}`)
// bezahlen (ausserhalb von Stripe) und danach nichts mehr
const paid = await stripe.invoices.pay(draft.id, { paid_out_of_band: true })
assert.equal(paid.status, 'paid')
assert.equal(paid.amount_paid, 750)
assert.equal(paid.amount_remaining, 0)
assert.equal(paid.paid_out_of_band, true)
assert.ok(paid.status_transitions.paid_at)
await refused(stripe.invoices.voidInvoice(draft.id), /open/)
await refused(stripe.invoices.pay(draft.id), /open/)
// stornieren nur offen; Entwurf loeschen
await stripe.invoices.voidInvoice(second.id)
assert.equal(status(second.id), 'void')
const third = await create()
const removed = await stripe.invoices.del(third.id)
assert.equal(removed.deleted, true)
assert.equal(st.invoices.has(third.id), false)
})