* fix(security): bind api_keys to the caller, lock hash-as-bearer RPCs and provider token tables Security audit 2026-09-01, critical items. - api_keys INSERT requires user_id = auth.uid() again (an admin could forge a key for any co-member and act as them in every company they belong to); SELECT is own-keys-or-admin; a BEFORE trigger freezes the identity and credential columns against user-session UPDATEs. - rotate_mcp_refresh_token and validate_and_increment_api_key become service_role only: they match rows by a presented SHA-256, so a hash readable by co-members was a bearer credential. - validate_and_increment_api_key fails closed when the key's user is no longer a member of the key's company. - provider_consent_tokens and provider_otc: the DELETE policies collapsed to "caller has any team row" (correlated subquery on a non-existent team_members.company_id). All member policies dropped; service_role only, matching every existing code path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): role gates, ownership guards and posting integrity in the database Security audit 2026-09-01, high items at the database layer. - One table-level guard, enforce_company_writer_role(), blocks the read-only viewer role on 55 company-scoped tables including through the 15 membership-only SECURITY DEFINER writers. Keyed on the JWT role claim so it fires inside definer bodies; no-op for service_role and trigger cascades. - company_members user_id/company_id immutable from user sessions; invitations can never grant owner; team_members gains a transition guard (admins keep non-owner role moves); companies team_id and archiving are owner-only and team attachment needs team membership. - Direct statements (current_user = authenticated) can no longer insert posted headers, add lines under posted verifikat, or post a draft with a voucher number the sequence never issued. Sanctioned RPCs run as the definer and are untouched; the engine's own draft-then-post shapes still pass. - create_document_version refuses viewers and foreign storage paths; validate_version_chain needs membership and loses anon EXECUTE; match_documents / match_booking_templates lose anon; cron maintenance RPCs become service_role only; the production-only seed_asset_categories is dropped. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * build: pin tsx as an exact devDependency instead of fetching it with npx at build time prebuild ran "npx tsx" with no lockfile entry, so every Vercel, Docker and CI build downloaded tsx@latest and its transitive tree from the registry with no integrity check, inside the build environment. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): refuse the viewer role on API-key and MCP write paths The v1 wrapper and the MCP company routing checked company membership but never role, and both run as service role, so a read-only viewer holding an API key could post vouchers and change settings through the API. Mutating methods and non-read scopes now return 403 ROLE_READ_ONLY for viewers on v1; MCP write tools refuse viewers the same way. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): stop serving uploaded SVG, XML and HTML as executable content on the app origin Uploads persisted the browser-declared mime type and the inline proxy served it verbatim, sandboxing only text/html; the storage proxy forwarded the uploader's Content-Type. Any writer, or any Peppol sender, could plant a scripted SVG or XHTML that executed on app.gnubok.se. - inline route: allow-list of natively safe types (PDF, raster images) served as before; everything else gets the opaque sandbox CSP. - storage proxy: octet-stream + attachment + sandbox unless the DB mime for the key is on the allow-list. - document-service: the stored mime is the magic-byte validated type. - logo upload: magic-byte validation, SVG refused. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): byrå brand logo upload decides the type by magic bytes and drops SVG Same pattern as the company logo route: the logos bucket is public, so a scripted SVG (or anything declared as an image) must never land there. The upload pickers stop advertising SVG. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): bind Enable Banking, Stripe and WooCommerce callbacks to the initiating user The callbacks resolved the pending row by oauth_state alone, so a victim who completed an attacker-initiated consent had their bank account, merchant account or store attached to the attacker's company. requireFlowInitiator() now requires the cookie session of the user who started the flow: no session redirects to login with the callback URL preserved, a different user is refused and nothing is exchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): guard tenant-controlled outbound fetches and surface the disabled rate limiter WooCommerce and Shopify syncs fetched a member-editable store URL with plain fetch() and redirect following under the service role, and the invoice PDF renderer fetched company_settings.logo_url unguarded. All three go through a new safeFetch() (public-IP validation via url-guard, https only, redirect: 'manual', body size cap) and re-normalise the stored host at use time. checkRateLimit() keeps failing open on hosted but logs one error per process when Upstash is not configured and exports isRateLimiterConfigured(). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): decide the API MFA gate from server-authenticated factors, not the session cookie getAuthenticatorAssuranceLevel() without arguments derives nextLevel from session.user.factors, which comes from the unsigned sb-*-auth-token cookie. Deleting factors from the cookie made an enrolled account look like it had nothing to step up to, on every /api route and in requireAuth. Both gates now read factors from the getUser() result or listFactors() and the level from the verified JWT claim, and fail closed on errors. Page-branch gate hardened the same way. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): bind Fortnox/Visma, Gmail and Skatteverket callbacks to the initiating user The arcim-migration callback exchanged the provider code onto whatever consent the one-time state named, with no check of who completed the flow and no org-number comparison, so a phished Fortnox admin handed their ledger to the attacker's company. provider_otc now records the initiating user (migration 20260902100000); the callback requires that session and, after the exchange, refuses a provider company whose org number differs from the consent's company. The Gmail and Skatteverket callbacks enforce the same initiator check. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): BankID signup confirms the email before linking the identity Signup created an email-confirmed, MFA-exempt account for any address the caller typed and returned a magic link, so an attacker could pre-register a victim's email and keep a permanent BankID login into the account the victim later adopted. The user is now created unconfirmed, the identity carries email_verified_at NULL (migration 20260902101000), bankid_linked is not set until the mailed confirmation is clicked, and BankID login of a pending identity is refused with the confirmation re-sent. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(security): bind MCP OAuth redirect URIs to the consenting user and cap scopes A user-registered redirect URI was allowlisted globally, the consent page named no client, and all scopes were pre-checked, so one phishing link handed an attacker a full-scope key for the victim's company. Registered URIs now resolve only for the registrant or a colleague sharing a company; the consent page shows the client identity and redirect host; non-built-in clients default to read-only pre-checks; scopes are capped by the user's role (viewer: read only) at consent and at /token. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(auth): client follow-ups for BankID confirmation, callback mismatch copy and decision log - register client handles the new confirmation_sent response from BankID signup with the existing inbox screen instead of calling verifyOtp. - BankID login surfaces the email_unconfirmed explanation. - WooCommerce settings map woocommerce_error=wrong_user to its own copy. - Logo help text no longer advertises SVG. - DECISIONS.md records the audit remediation choices. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(mcp-oauth): literal SoD columns in the api_keys insert so the phantom-column scanner resolves them Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * test(logo): type the upload fixtures as Uint8Array<ArrayBuffer> so they are valid BlobParts Fixes the typecheck ratchet on PR #2155 and ratchets the baseline down by the one legacy error the change removed. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
508 lines
19 KiB
TypeScript
508 lines
19 KiB
TypeScript
/**
|
|
* Regression tests for issue #772: "Logotyp kommer inte med på fakturor".
|
|
*
|
|
* Root cause: @react-pdf/renderer's <Image> only decodes JPG/PNG, but the logo
|
|
* upload route and the `logos` bucket accept SVG and WebP. When a logo was an
|
|
* SVG/WebP, @react-pdf silently dropped it (it swallows the decode error in a
|
|
* try/catch), so invoices rendered with no logo and no error.
|
|
*
|
|
* Fix: prepareInvoicePdfRender fetches the stored logo and re-encodes it to a
|
|
* PNG data URL via sharp, so every supported upload format renders. These tests
|
|
* mock `fetch` and exercise the real sharp pipeline.
|
|
*/
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from 'vitest'
|
|
import { readFile } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import sharp from 'sharp'
|
|
import QRCode from 'qrcode'
|
|
import { prepareInvoicePdfRender, buildPaymentLinkQrDataUrl, buildSwishQrDataUrl } from '@/lib/invoices/pdf-render-helpers'
|
|
import { makeCompanySettings, makeInvoice } from '@/tests/helpers'
|
|
|
|
const fontDownloadMock = vi.hoisted(() => vi.fn())
|
|
vi.mock('@/lib/supabase/server', () => ({
|
|
createServiceClient: () => ({
|
|
storage: {
|
|
from: () => ({ download: fontDownloadMock }),
|
|
},
|
|
}),
|
|
}))
|
|
|
|
// The logo fetch runs through the outbound URL guard, which resolves DNS.
|
|
// Stub the validator (same seam the webhook dispatcher tests use): https hosts
|
|
// resolve to a public address, plain http is refused like the real guard does.
|
|
const guard = vi.hoisted(() => ({ validateUrl: vi.fn() }))
|
|
vi.mock('@/lib/webhooks/url-guard', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@/lib/webhooks/url-guard')>()
|
|
return {
|
|
...actual,
|
|
validateWebhookUrl: (...args: unknown[]) => guard.validateUrl(...args),
|
|
}
|
|
})
|
|
|
|
function guardPublicByDefault() {
|
|
guard.validateUrl.mockReset()
|
|
guard.validateUrl.mockImplementation(async (rawUrl: string) => {
|
|
const parsed = new URL(rawUrl)
|
|
if (parsed.protocol !== 'https:') {
|
|
return { ok: false, reason: 'non_https_scheme', detail: `${parsed.protocol} refused` }
|
|
}
|
|
return { ok: true, hostname: parsed.hostname, resolvedAddresses: ['203.0.113.10'] }
|
|
})
|
|
}
|
|
|
|
// The deployment's own storage origin: logos uploaded through the app live
|
|
// here, and it is exempt from the public-address check (a self-hosted NAS
|
|
// install may legitimately serve storage from a private address).
|
|
const STORAGE_ORIGIN = 'https://example.test'
|
|
|
|
const PNG_DATA_URL_PREFIX = 'data:image/png;base64,'
|
|
|
|
const SVG_LOGO = Buffer.from(
|
|
'<svg xmlns="http://www.w3.org/2000/svg" width="120" height="40">' +
|
|
'<rect width="120" height="40" fill="#1a1a1a"/>' +
|
|
'<text x="8" y="26" fill="#fff" font-size="18">ACME</text></svg>',
|
|
)
|
|
|
|
/** Build a one-shot fetch mock that returns the given bytes + content-type. */
|
|
function mockFetchOnce(buf: Buffer, contentType: string) {
|
|
const arrayBuffer = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
|
|
const fn = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
headers: { get: () => contentType },
|
|
arrayBuffer: async () => arrayBuffer,
|
|
})
|
|
vi.stubGlobal('fetch', fn)
|
|
return fn
|
|
}
|
|
|
|
/** A data: URL whose payload decodes to a valid PNG via sharp. */
|
|
async function expectValidEmbeddedPng(logoUrl: string | null | undefined) {
|
|
expect(logoUrl).toMatch(new RegExp(`^${PNG_DATA_URL_PREFIX}`))
|
|
const base64 = (logoUrl as string).slice(PNG_DATA_URL_PREFIX.length)
|
|
const meta = await sharp(Buffer.from(base64, 'base64')).metadata()
|
|
expect(meta.format).toBe('png')
|
|
}
|
|
|
|
describe('prepareInvoicePdfRender: logo resolution (issue #772)', () => {
|
|
beforeEach(() => {
|
|
vi.unstubAllGlobals()
|
|
fontDownloadMock.mockReset()
|
|
guardPublicByDefault()
|
|
vi.stubEnv('NEXT_PUBLIC_SUPABASE_URL', STORAGE_ORIGIN)
|
|
})
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
vi.unstubAllEnvs()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('renders only the payment account matching the invoice currency', async () => {
|
|
const company = makeCompanySettings({
|
|
iban: 'SE0011111111111111111111',
|
|
invoice_payment_accounts: {
|
|
EUR: {
|
|
bank_name: 'EUR Bank',
|
|
clearing_number: null,
|
|
account_number: null,
|
|
bankgiro: null,
|
|
plusgiro: null,
|
|
swish: null,
|
|
iban: 'SE0022222222222222222222',
|
|
bic: 'EURRSESS',
|
|
},
|
|
},
|
|
})
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company, 'EUR')
|
|
|
|
expect(resolved.bank_name).toBe('EUR Bank')
|
|
expect(resolved.iban).toBe('SE0022222222222222222222')
|
|
expect(resolved.bankgiro).toBeNull()
|
|
})
|
|
|
|
it('rejects a foreign-currency PDF without a usable payment account', async () => {
|
|
const company = makeCompanySettings({ invoice_payment_accounts: {} })
|
|
|
|
await expect(prepareInvoicePdfRender(company, 'EUR')).rejects.toMatchObject({
|
|
code: 'INVOICE_SEND_PAYMENT_ACCOUNT_MISSING',
|
|
currency: 'EUR',
|
|
})
|
|
})
|
|
|
|
it('renders non-payable foreign documents without requiring an account or leaking SEK details', async () => {
|
|
const company = makeCompanySettings({
|
|
iban: 'SE0011111111111111111111',
|
|
invoice_payment_accounts: {},
|
|
})
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(
|
|
company,
|
|
'EUR',
|
|
{ paymentAccountRequired: false },
|
|
)
|
|
|
|
expect(resolved.iban).toBeNull()
|
|
expect(resolved.bankgiro).toBeNull()
|
|
})
|
|
|
|
it('embeds an SVG logo as a PNG data URL so @react-pdf can draw it', async () => {
|
|
const fetchMock = mockFetchOnce(SVG_LOGO, 'image/svg+xml')
|
|
const company = makeCompanySettings({
|
|
logo_url: 'https://example.test/svg-logo-1.svg',
|
|
})
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
// Fetched with a timeout signal so a slow logo host can't hang the render,
|
|
// and with redirects disabled so the host can't bounce us elsewhere.
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
'https://example.test/svg-logo-1.svg',
|
|
expect.objectContaining({ signal: expect.any(AbortSignal), redirect: 'manual' }),
|
|
)
|
|
// Our own storage origin skips the DNS guard (it may be private on self-host).
|
|
expect(guard.validateUrl).not.toHaveBeenCalled()
|
|
await expectValidEmbeddedPng(resolved.logo_url)
|
|
})
|
|
|
|
it('embeds a logo from a public https host once the DNS guard passes', async () => {
|
|
const fetchMock = mockFetchOnce(SVG_LOGO, 'image/svg+xml')
|
|
const url = 'https://cdn.example.org/public-logo-1.svg'
|
|
const company = makeCompanySettings({ logo_url: url })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(guard.validateUrl).toHaveBeenCalledWith(url, undefined)
|
|
expect(fetchMock).toHaveBeenCalledWith(url, expect.objectContaining({ redirect: 'manual' }))
|
|
await expectValidEmbeddedPng(resolved.logo_url)
|
|
})
|
|
|
|
it('renders without a logo when the logo host resolves to a private address (SSRF guard)', async () => {
|
|
guard.validateUrl.mockResolvedValue({
|
|
ok: false,
|
|
reason: 'private_address',
|
|
detail: 'Resolved address 10.0.0.9 for intranet.example.org is not publicly routable (private_address).',
|
|
})
|
|
const fetchMock = vi.fn()
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
const company = makeCompanySettings({ logo_url: 'https://intranet.example.org/logo.png' })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
// Refused means refused: no socket, and @react-pdf is not handed the URL either.
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
expect(resolved.logo_url).toBeNull()
|
|
})
|
|
|
|
it('renders without a logo for a plain-http URL off the storage origin', async () => {
|
|
const fetchMock = vi.fn()
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
const company = makeCompanySettings({ logo_url: 'http://cdn.example.org/http-logo.png' })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
expect(resolved.logo_url).toBeNull()
|
|
})
|
|
|
|
it('renders without a logo for a non-http(s) scheme', async () => {
|
|
const fetchMock = vi.fn()
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
const company = makeCompanySettings({ logo_url: 'file:///etc/hostname' })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
expect(guard.validateUrl).not.toHaveBeenCalled()
|
|
expect(resolved.logo_url).toBeNull()
|
|
})
|
|
|
|
it('renders without a logo when the logo host answers with a redirect, even from the storage origin', async () => {
|
|
const cancel = vi.fn(async () => undefined)
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn().mockResolvedValue({
|
|
ok: false,
|
|
status: 302,
|
|
type: 'basic',
|
|
headers: new Headers({ location: 'http://169.254.169.254/latest/meta-data/' }),
|
|
body: { cancel },
|
|
}),
|
|
)
|
|
const company = makeCompanySettings({ logo_url: 'https://example.test/redirecting-logo.png' })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(resolved.logo_url).toBeNull()
|
|
expect(cancel).toHaveBeenCalled()
|
|
})
|
|
|
|
it('drops the logo instead of handing @react-pdf a remote URL when a non-storage host fails', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('network down')))
|
|
const url = 'https://cdn.example.org/flaky-logo.png'
|
|
const company = makeCompanySettings({ logo_url: url })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
// @react-pdf's own fetch follows redirects with no guard; a tenant host
|
|
// that fails us and then redirects it would reopen the hole.
|
|
expect(resolved.logo_url).toBeNull()
|
|
})
|
|
|
|
it('embeds a WebP logo as a PNG data URL', async () => {
|
|
const webp = await sharp(SVG_LOGO).webp().toBuffer()
|
|
mockFetchOnce(webp, 'image/webp')
|
|
const company = makeCompanySettings({
|
|
logo_url: 'https://example.test/webp-logo-1.webp',
|
|
})
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
await expectValidEmbeddedPng(resolved.logo_url)
|
|
})
|
|
|
|
it('re-encodes a PNG logo to an embedded data URL (no remote fetch at render time)', async () => {
|
|
const png = await sharp(SVG_LOGO).png().toBuffer()
|
|
mockFetchOnce(png, 'image/png')
|
|
const company = makeCompanySettings({
|
|
logo_url: 'https://example.test/png-logo-1.png',
|
|
})
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
await expectValidEmbeddedPng(resolved.logo_url)
|
|
})
|
|
|
|
it('falls back to the original URL when the logo fetch is not ok', async () => {
|
|
const fn = vi.fn().mockResolvedValue({
|
|
ok: false,
|
|
headers: { get: () => null },
|
|
arrayBuffer: async () => new ArrayBuffer(0),
|
|
})
|
|
vi.stubGlobal('fetch', fn)
|
|
const url = 'https://example.test/missing-logo.png'
|
|
const company = makeCompanySettings({ logo_url: url })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
// Unchanged: never worse than before (@react-pdf still fetches PNG/JPEG).
|
|
expect(resolved.logo_url).toBe(url)
|
|
})
|
|
|
|
it('falls back to the original URL when the fetch throws', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn().mockRejectedValue(new Error('network down')),
|
|
)
|
|
const url = 'https://example.test/network-error-logo.png'
|
|
const company = makeCompanySettings({ logo_url: url })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(resolved.logo_url).toBe(url)
|
|
})
|
|
|
|
it('falls back to the original URL when the logo exceeds the size cap', async () => {
|
|
// Declared content-length over the cap is rejected before reading the body.
|
|
const fn = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
headers: {
|
|
get: (h: string) =>
|
|
h.toLowerCase() === 'content-length' ? String(11 * 1024 * 1024) : 'image/png',
|
|
},
|
|
arrayBuffer: async () => new ArrayBuffer(0),
|
|
})
|
|
vi.stubGlobal('fetch', fn)
|
|
const url = 'https://example.test/oversized-logo.png'
|
|
const company = makeCompanySettings({ logo_url: url })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(fn).toHaveBeenCalled()
|
|
expect(resolved.logo_url).toBe(url)
|
|
})
|
|
|
|
it('does not fetch when no logo is configured', async () => {
|
|
const fetchMock = vi.fn()
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
const company = makeCompanySettings({ logo_url: null })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
expect(resolved.logo_url).toBeNull()
|
|
})
|
|
|
|
it('passes through an already-embedded data: URL without fetching', async () => {
|
|
const fetchMock = vi.fn()
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
const dataUrl = `${PNG_DATA_URL_PREFIX}iVBORw0KGgo=`
|
|
const company = makeCompanySettings({ logo_url: dataUrl })
|
|
|
|
const { company: resolved } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
expect(resolved.logo_url).toBe(dataUrl)
|
|
})
|
|
|
|
it('still returns branding alongside the resolved company', async () => {
|
|
mockFetchOnce(SVG_LOGO, 'image/svg+xml')
|
|
const company = makeCompanySettings({
|
|
logo_url: 'https://example.test/branding-logo.svg',
|
|
invoice_primary_color: '#c2410c',
|
|
})
|
|
|
|
const { branding } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(branding.primaryColor).toBe('#c2410c')
|
|
})
|
|
})
|
|
|
|
describe('prepareInvoicePdfRender: invoice fonts', () => {
|
|
beforeEach(() => {
|
|
vi.unstubAllGlobals()
|
|
fontDownloadMock.mockReset()
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
vi.unstubAllEnvs()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('registers a bundled font before returning it to the PDF template', async () => {
|
|
const company = makeCompanySettings({ invoice_font_family: 'Source Sans 3' })
|
|
|
|
const { branding } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(branding.fontFamily).toBe('Source Sans 3')
|
|
})
|
|
|
|
it('registers a valid stored custom font under an isolated render family', async () => {
|
|
const bytes = await readFile(
|
|
join(process.cwd(), 'public', 'fonts', 'invoice', 'SourceSans3-Regular.ttf'),
|
|
)
|
|
const arrayBuffer = bytes.buffer.slice(
|
|
bytes.byteOffset,
|
|
bytes.byteOffset + bytes.byteLength,
|
|
)
|
|
fontDownloadMock.mockResolvedValue({
|
|
data: new Blob([arrayBuffer]),
|
|
error: null,
|
|
})
|
|
const company = makeCompanySettings({
|
|
invoice_font_family: 'Custom',
|
|
invoice_custom_font_path: 'company-1/invoice-font-1.ttf',
|
|
})
|
|
|
|
const { branding } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(branding.fontFamily).toMatch(/^InvoiceCustom-[0-9a-f]{12}$/)
|
|
})
|
|
|
|
it('falls back to Helvetica when a custom font cannot be parsed', async () => {
|
|
const corruptedFont = new Uint8Array([0x00, 0x01, 0x00, 0x00, 0xff, 0xff])
|
|
fontDownloadMock.mockResolvedValue({
|
|
data: new Blob([corruptedFont]),
|
|
error: null,
|
|
})
|
|
const company = makeCompanySettings({
|
|
invoice_font_family: 'Custom',
|
|
invoice_custom_font_path: 'company-1/invoice-font-2.ttf',
|
|
})
|
|
|
|
const { branding } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(branding.fontFamily).toBe('Helvetica')
|
|
})
|
|
|
|
it('does not download a custom font from another company path', async () => {
|
|
const company = makeCompanySettings({
|
|
invoice_font_family: 'Custom',
|
|
invoice_custom_font_path: 'company-2/invoice-font-3.ttf',
|
|
})
|
|
|
|
const { branding } = await prepareInvoicePdfRender(company)
|
|
|
|
expect(fontDownloadMock).not.toHaveBeenCalled()
|
|
expect(branding.fontFamily).toBe('Helvetica')
|
|
})
|
|
})
|
|
|
|
describe('buildPaymentLinkQrDataUrl', () => {
|
|
it('encodes the payment link as a PNG QR data URL for a real invoice', async () => {
|
|
const invoice = makeInvoice({ payment_link_url: 'https://buy.stripe.com/test_abc123' })
|
|
const qr = await buildPaymentLinkQrDataUrl(invoice)
|
|
expect(qr).toMatch(new RegExp(`^${PNG_DATA_URL_PREFIX}`))
|
|
})
|
|
|
|
it('returns null when the invoice has no payment link', async () => {
|
|
expect(await buildPaymentLinkQrDataUrl(makeInvoice())).toBeNull()
|
|
expect(await buildPaymentLinkQrDataUrl(makeInvoice({ payment_link_url: ' ' }))).toBeNull()
|
|
})
|
|
|
|
it('returns null for non-payable documents (proforma, delivery note, credit note)', async () => {
|
|
const url = 'https://buy.stripe.com/test_abc123'
|
|
expect(
|
|
await buildPaymentLinkQrDataUrl(makeInvoice({ payment_link_url: url, document_type: 'proforma' })),
|
|
).toBeNull()
|
|
expect(
|
|
await buildPaymentLinkQrDataUrl(makeInvoice({ payment_link_url: url, document_type: 'delivery_note' })),
|
|
).toBeNull()
|
|
expect(
|
|
await buildPaymentLinkQrDataUrl(makeInvoice({ payment_link_url: url, credited_invoice_id: 'inv-orig' })),
|
|
).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('buildSwishQrDataUrl', () => {
|
|
// Spy without replacing the implementation: the payload string is the unit
|
|
// under test (the PNG pixels are qrcode's concern), and the passthrough
|
|
// keeps the returned data URL real. The Swish payload locks the amount
|
|
// (editmask 0), so an amount above "Att betala" makes the customer overpay
|
|
// with no way to correct it in the app.
|
|
let toDataURL: MockInstance
|
|
|
|
beforeEach(() => {
|
|
toDataURL = vi.spyOn(QRCode, 'toDataURL')
|
|
})
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
const company = () => makeCompanySettings({ swish: '1234567890' })
|
|
|
|
it('encodes "Att betala" (total minus ROT/RUT deduction), not the invoice total', async () => {
|
|
const invoice = makeInvoice({ total: 1250, deduction_total: 625 })
|
|
const qr = await buildSwishQrDataUrl(company(), invoice)
|
|
expect(qr).toMatch(new RegExp(`^${PNG_DATA_URL_PREFIX}`))
|
|
expect(toDataURL).toHaveBeenCalledWith('C1234567890;625.00;F-2024001;0', expect.anything())
|
|
})
|
|
|
|
it('applies öresavrundning before the deduction, same order as the PDF totals block', async () => {
|
|
const invoice = makeInvoice({ total: 1250.49, ore_rounding: true, deduction_total: 625 })
|
|
await buildSwishQrDataUrl(company(), invoice)
|
|
expect(toDataURL).toHaveBeenCalledWith('C1234567890;625.00;F-2024001;0', expect.anything())
|
|
})
|
|
|
|
it('renders no QR when the deduction covers the whole invoice (nothing to pay)', async () => {
|
|
const invoice = makeInvoice({ total: 1250, deduction_total: 1250 })
|
|
expect(await buildSwishQrDataUrl(company(), invoice)).toBeNull()
|
|
expect(toDataURL).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('renders no QR for non-payable documents (credit note, proforma, delivery note)', async () => {
|
|
const creditNote = makeInvoice({ total: 1250, deduction_total: 625, credited_invoice_id: 'inv-orig' })
|
|
expect(await buildSwishQrDataUrl(company(), creditNote)).toBeNull()
|
|
expect(await buildSwishQrDataUrl(company(), makeInvoice({ document_type: 'proforma' }))).toBeNull()
|
|
expect(await buildSwishQrDataUrl(company(), makeInvoice({ document_type: 'delivery_note' }))).toBeNull()
|
|
expect(toDataURL).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('keeps the plain total for invoices without a deduction', async () => {
|
|
const invoice = makeInvoice()
|
|
await buildSwishQrDataUrl(company(), invoice)
|
|
expect(toDataURL).toHaveBeenCalledWith('C1234567890;12500.00;F-2024001;0', expect.anything())
|
|
})
|
|
})
|