Files
accounted/app/api/invoices/[id]/send-payment-confirmation/__tests__/route.test.ts
T
MattssonandClaude Fable 5 0040cadacc feat(invoicing): opt-in invoice email from the company's own sending domain (#1802)
* feat(invoicing): opt-in invoice email from the company's own sending domain

Companies holding the custom_sender_domain capability grant can register
their own domain (Resend sending-only profile), publish DKIM/SPF, and once
verified every invoice email (send, reminders, recurring, payment
confirmation, MCP/v1 sends) leaves as "<name> <faktura@their-domain>"
instead of the platform sender. Reply-To is unchanged.

- New table company_sending_domains (RLS: members read, owner/admin write;
  audit trigger), types, archive-export classification.
- New capability key custom_sender_domain: manually granted per company,
  deliberately outside PAID_CAPABILITIES (never trial-seeded, never written
  by the Stripe sync). Without the grant the settings section is hidden and
  nothing changes.
- Email extension: sending-domain routes (GET/POST/PATCH/DELETE, verify),
  Resend domain lifecycle without orphan adoption, domain.updated handling
  on the delivery webhook, explicit From support in the Resend adapter.
- Core resolveInvoiceSender(): verified + enabled + entitled, else the
  platform sender; never throws.
- Settings -> Invoicing: "Avsändare vid fakturautskick" section (sv/en).
- Unit tests for the resolver, domain helpers, routes, From header; pg-real
  test for RLS and constraints.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(invoicing): harden sending-domain writes, sender fallback, review findings

Skeptic refutations:
- Tenant JWTs could insert/update company_sending_domains with status =
  'verified' and an arbitrary domain through PostgREST (RLS only checked
  membership), then send invoice mail as that domain. New migration
  20260822130000 adds a BEFORE trigger: tenants may only open a pending
  claim and edit sender_local_part/sender_name/enabled; domain and
  verification state are service-role only. claim/verify helpers now take
  a service-role writer for those columns; the route's RLS client still
  does the insert.
- A company domain Resend later rejects made every invoice send fail: the
  Resend adapter retries once as the platform sender when an explicit
  company From is rejected (nothing was sent, so no double send).

Review findings:
- domain.updated webhook: discriminated outcome; DB errors answer 500 so
  Svix retries, unknown domains are acknowledged.
- Display names are RFC 5322-quoted only when they carry specials.
- Sender local part is a strict dot-atom (no trailing/consecutive dots),
  in code and in the CHECK constraint; resend_domain_id index is UNIQUE.
- IME composition guard on the claim input; event bus reset in tests;
  settings section skips its request for non-admins.

Deferred (needs a product call): persisting the effective From address in
the invoice delivery log touches the hardened evidence triggers; recorded
in DECISIONS.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(invoicing): bind sending-domain verification to the claimed domain; fix pg test

Skeptic re-check found a TOCTOU: during the claim's Resend round-trip a
tenant could delete and re-insert its pending row under the same id with a
reserved domain, and the service-role writer updated by id alone. Now:
- the claim's verification-state write filters on (id, company_id, domain,
  resend_domain_id IS NULL) and rolls back on zero rows;
- verify and the domain.updated webhook compare Resend's domain name with
  the row before writing verified;
- resolveInvoiceSender refuses reserved platform domains and non-hostnames
  at send time (reserved-domain logic moved to lib/email/domain-name.ts and
  shared with the claim validator).

pg-real: the case-insensitive uniqueness assertion now expects the
domain_shape CHECK (lowercase enforced) for an uppercase variant and the
unique index for a same-case duplicate.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-23 00:07:30 +02:00

250 lines
8.0 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import {
createMockRequest,
parseJsonResponse,
createMockRouteParams,
createQueuedMockSupabase,
makeInvoice,
makeCustomer,
makeCompanySettings,
} from '@/tests/helpers'
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
vi.mock('@/lib/supabase/server', () => ({
createClient: () => Promise.resolve(mockSupabase),
}))
vi.mock('@/lib/init', () => ({
ensureInitialized: vi.fn(),
}))
vi.mock('@/lib/company/context', () => ({
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
}))
vi.mock('@/lib/auth/require-write', () => ({
requireWritePermission: vi.fn().mockResolvedValue({ ok: true }),
}))
const mockRenderToBuffer = vi.fn()
vi.mock('@react-pdf/renderer', () => ({
renderToBuffer: (...args: unknown[]) => mockRenderToBuffer(...args),
Document: vi.fn(),
Page: vi.fn(),
Text: vi.fn(),
View: vi.fn(),
StyleSheet: { create: (s: unknown) => s },
}))
vi.mock('@/lib/invoices/pdf-template', () => ({
InvoicePDF: vi.fn().mockReturnValue('mock-pdf-element'),
brandingFromCompanySettings: vi.fn().mockReturnValue({}),
SHOW_SWISH_ON_INVOICE: false,
}))
import { InvoicePDF } from '@/lib/invoices/pdf-template'
const mockSendEmail = vi.fn()
const mockIsConfigured = vi.fn()
vi.mock('@/lib/email/invoice-sender', () => ({
resolveInvoiceSender: vi.fn().mockResolvedValue(undefined),
}))
vi.mock('@/lib/email/service', () => ({
getEmailService: () => ({
sendEmail: (...args: unknown[]) => mockSendEmail(...args),
isConfigured: () => mockIsConfigured(),
}),
}))
vi.mock('@/lib/sandbox/guard', () => ({
guardSandbox: vi.fn().mockResolvedValue(null),
isSandboxCompany: vi.fn().mockResolvedValue(false),
sandboxBlockedResponse: vi.fn(),
}))
vi.mock('@/lib/entitlements/has-capability', () => ({
requireCapability: vi.fn().mockResolvedValue(null),
}))
import { POST } from '../route'
describe('POST /api/invoices/[id]/send-payment-confirmation', () => {
const mockUser = { id: 'user-1', email: 'owner@test.se' }
const customer = makeCustomer({ id: 'cust-1', name: 'Kund AB', email: 'kund@test.se', language: 'sv' })
const company = makeCompanySettings({
company_name: 'Acme AB',
email: 'faktura@acme.se',
bankgiro: '123-4567',
})
const paidInvoice = makeInvoice({
id: 'inv-1',
invoice_number: '2026-0042',
status: 'paid',
paid_amount: 12500,
remaining_amount: 0,
paid_at: '2026-08-17T12:00:00+00:00',
customer,
items: [
{
id: 'item-1',
invoice_id: 'inv-1',
sort_order: 0,
description: 'Consulting',
quantity: 10,
unit: 'tim',
unit_price: 1000,
line_total: 10000,
vat_rate: 25,
vat_amount: 2500,
created_at: '2026-06-15T14:30:00Z',
},
],
})
function post(id = 'inv-1') {
return POST(
createMockRequest(`/api/invoices/${id}/send-payment-confirmation`, { method: 'POST' }),
createMockRouteParams({ id }),
)
}
beforeEach(() => {
vi.clearAllMocks()
reset()
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
mockIsConfigured.mockReturnValue(true)
mockRenderToBuffer.mockResolvedValue(Buffer.from('fake-paid-pdf'))
mockSendEmail.mockResolvedValue({ success: true, messageId: 'msg-1', provider: 'resend' })
})
it('returns 401 when not authenticated', async () => {
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
const { status } = await parseJsonResponse(await post())
expect(status).toBe(401)
expect(mockSendEmail).not.toHaveBeenCalled()
})
it('returns 503 when the email service is not configured', async () => {
mockIsConfigured.mockReturnValue(false)
const { status } = await parseJsonResponse(await post())
expect(status).toBe(503)
})
it('returns 404 when the invoice does not exist', async () => {
enqueue({ data: null, error: { message: 'Not found' } })
const { status, body } = await parseJsonResponse(await post('missing'))
expect(status).toBe(404)
expect(body.error.code).toBe('INVOICE_PAID_NOT_FOUND')
})
it.each(['sent', 'partially_paid', 'overdue', 'draft'])(
'returns 409 without rendering or mailing when the invoice is %s',
async (status) => {
enqueue({ data: { ...paidInvoice, status }, error: null })
const response = await parseJsonResponse(await post())
expect(response.status).toBe(409)
expect(response.body.error.code).toBe('INVOICE_PAYMENT_CONFIRMATION_NOT_PAID')
expect(mockRenderToBuffer).not.toHaveBeenCalled()
expect(mockSendEmail).not.toHaveBeenCalled()
},
)
it('returns 400 when the customer has no email', async () => {
enqueue({ data: { ...paidInvoice, customer: { ...customer, email: null } }, error: null })
const { status, body } = await parseJsonResponse(await post())
expect(status).toBe(400)
expect(body.error.code).toBe('INVOICE_SEND_NO_CUSTOMER_EMAIL')
expect(mockSendEmail).not.toHaveBeenCalled()
})
it('returns 404 when company settings are missing', async () => {
enqueue({ data: paidInvoice, error: null })
enqueue({ data: null, error: { message: 'missing' } })
const { status, body } = await parseJsonResponse(await post())
expect(status).toBe(404)
expect(body.error.code).toBe('INVOICE_SEND_COMPANY_SETTINGS_MISSING')
})
it('emails the paid re-render as a betalningsbekräftelse without touching the invoice', async () => {
enqueue({ data: paidInvoice, error: null })
enqueue({ data: company, error: null })
const { status, body } = await parseJsonResponse(await post())
expect(status).toBe(200)
expect(body.success).toBe(true)
expect(body.message).toContain('kund@test.se')
// The row is rendered as stored (status paid): the template stamps BETALD.
expect(InvoicePDF).toHaveBeenCalledWith(
expect.objectContaining({ invoice: expect.objectContaining({ status: 'paid' }) }),
)
expect(mockSendEmail).toHaveBeenCalledTimes(1)
const sent = mockSendEmail.mock.calls[0][0] as {
to: string[]
subject: string
html: string
text: string
replyTo?: string
fromName?: string
attachments: Array<{ filename: string; content: Buffer; contentType?: string }>
}
expect(sent.to).toEqual(['kund@test.se'])
expect(sent.subject).toBe('Betalningsbekräftelse för faktura 2026-0042 från Acme AB')
expect(sent.html).toContain('betald i sin helhet')
expect(sent.text).toContain('Betalt belopp:')
expect(sent.replyTo).toBe('faktura@acme.se')
expect(sent.fromName).toBe('Acme AB')
expect(sent.attachments).toEqual([
{
filename: 'Betalningsbekraftelse-2026-0042.pdf',
content: Buffer.from('fake-paid-pdf'),
contentType: 'application/pdf',
},
])
// No status flip, no delivery-history row, no journal write: the
// confirmation is a resend of a document, not an issuance.
expect(mockSupabase.from).not.toHaveBeenCalledWith('invoice_deliveries')
expect(mockSupabase.from).not.toHaveBeenCalledWith('journal_entries')
expect(mockSupabase.rpc).not.toHaveBeenCalled()
})
it('returns 502 when the provider refuses the email', async () => {
enqueue({ data: paidInvoice, error: null })
enqueue({ data: company, error: null })
mockSendEmail.mockResolvedValue({ success: false, error: 'provider down' })
const { status, body } = await parseJsonResponse(await post())
expect(status).toBe(502)
expect(body.error.code).toBe('INVOICE_SEND_PROVIDER_FAILED')
})
it('returns 500 when the PDF cannot be rendered and sends nothing', async () => {
enqueue({ data: paidInvoice, error: null })
enqueue({ data: company, error: null })
mockRenderToBuffer.mockRejectedValueOnce(new Error('render failed'))
const { status, body } = await parseJsonResponse(await post())
expect(status).toBe(500)
expect(body.error.code).toBe('INVOICE_PDF_RENDER_FAILED')
expect(mockSendEmail).not.toHaveBeenCalled()
})
})