Fix/chrome pdf preview csp (#572)
* feat: add option to exclude year-end closing entries in SIE export and related reports * delete docs * fix: allow Chrome's PDF viewer in verifikat document preview The /api/documents/:id/inline route shipped with `object-src 'none'` in its CSP, which blocked Chrome's built-in PDF viewer (it renders inline PDFs via an internal <embed>). Users on Chrome saw "Det här innehållet har blockerats" when expanding a PDF attachment in the bookkeeping view; Firefox (PDF.js) and Edge (own viewer) were unaffected, and JPGs worked because <img> isn't subject to object-src. Drops the CSP for this route to the minimum needed for embeddability: `frame-ancestors 'self'`. X-Content-Type-Options: nosniff plus the fixed Content-Type from the handler already block MIME confusion; X-Frame-Options: SAMEORIGIN + frame-ancestors still block clickjacking. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(auth): add webmail deep link to email confirmation screens Mirrors Stripe's signup UX: after asking the user to verify their email, detect their webmail provider from the domain and show a button that opens the inbox in a new tab. Gmail gets a from:<sender> search pre-populated; Outlook/Yahoo/iCloud/Proton open the inbox directly. Unknown / custom domains fall back to the existing copy. Sender address is configurable via NEXT_PUBLIC_BRANDING_AUTH_EMAIL_FROM (default noreply@gnubok.se) so white-label installs can match their Supabase Auth SMTP config. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(auth): unblock first-time password set for BankID users with MFA Supabase rejects updateUser({password}) and mfa.unenroll with "AAL2 session is required" whenever a TOTP factor is enrolled. BankID magic-link logins produce AAL1, and middleware skips MFA enforcement for bankid_linked users, so they had no path to AAL2 — leaving them unable to set a backup password or disable MFA without going through the email-recovery escape hatch. - /api/account/password: branch on app_metadata.has_password. First-time set writes via service.auth.admin.updateUserById (no existing credential to protect, AAL2 guard does not apply). Change-password keeps the user-session updateUser so AAL2 still fires for credential rotation. - /mfa/verify: accept a safeReturnTo query param and route there after successful verify, so step-up flows can land back where they came from. - SecuritySettings: detect the AAL2 error from both change-password and mfa.unenroll and redirect through /mfa/verify?returnTo=/settings/account instead of toasting a dead-end error. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Add tests and rounding utility for öre precision in bokslut calculations - Implemented `roundOre` function for rounding SEK amounts to two decimal places, ensuring consistent monetary calculations. - Introduced `ORE_TOLERANCE` constant for comparing rounded amounts, facilitating invariant checks in financial entries. - Created comprehensive tests for `roundOre`, covering typical cases, edge cases, and idempotency. - Added year-end invariants tests to verify database-level guarantees for closing entries, ensuring they balance to the öre and reject discrepancies. - Developed end-to-end tests for the dispositions chain, validating the correctness of calculations across various scenarios. * fix: update PDF rendering to remove Swish QR code generation and set default to disable Swish visibility * fix: enhance security by rejecting data URIs in safeReturnTo function tests * fix: improve rounding logic in roundOre function and add customer_type migration * fix: add customer_type column to customers and enforce CHECK constraint --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
0087b7be3f
commit
32d9978f1b
@@ -108,6 +108,7 @@ export function makeCompany(overrides: Partial<Company> = {}): Company {
|
||||
name: 'Test Company',
|
||||
org_number: null,
|
||||
entity_type: 'enskild_firma',
|
||||
accounting_framework: 'k2',
|
||||
created_by: 'user-1',
|
||||
team_id,
|
||||
archived_at: null,
|
||||
@@ -536,6 +537,29 @@ export function makeCompanySettings(
|
||||
bookkeeping_locked_through: null,
|
||||
auto_lock_period_days: null,
|
||||
default_voucher_series: 'A',
|
||||
default_voucher_series_per_source_type: {
|
||||
manual: 'A',
|
||||
invoice_created: 'A',
|
||||
invoice_paid: 'A',
|
||||
invoice_cash_payment: 'A',
|
||||
credit_note: 'A',
|
||||
supplier_invoice_registered: 'A',
|
||||
supplier_invoice_paid: 'A',
|
||||
supplier_invoice_cash_payment: 'A',
|
||||
supplier_invoice_privately_paid: 'A',
|
||||
supplier_credit_note: 'A',
|
||||
salary_payment: 'A',
|
||||
bank_transaction: 'A',
|
||||
reminder_fee: 'A',
|
||||
opening_balance: 'A',
|
||||
year_end: 'A',
|
||||
currency_revaluation: 'A',
|
||||
inbox_item: 'A',
|
||||
import: 'A',
|
||||
system: 'A',
|
||||
storno: 'A',
|
||||
correction: 'A',
|
||||
},
|
||||
ore_rounding: true,
|
||||
invoice_show_ocr: true,
|
||||
invoice_show_bankgiro: true,
|
||||
@@ -546,7 +570,15 @@ export function makeCompanySettings(
|
||||
invoice_company_name_position: 'header',
|
||||
invoice_late_fee_text: null,
|
||||
invoice_credit_terms_text: null,
|
||||
invoice_primary_color: '#1a1a1a',
|
||||
invoice_accent_color: '#666666',
|
||||
invoice_font_family: 'Helvetica',
|
||||
invoice_header_text: null,
|
||||
invoice_footer_text: null,
|
||||
send_invoice_reminders: true,
|
||||
reminder_fee_enabled: true,
|
||||
reminder_fee_amount: 60,
|
||||
reminder_interest_rate_override: null,
|
||||
logo_url: null,
|
||||
onboarding_step: 6,
|
||||
onboarding_complete: true,
|
||||
|
||||
@@ -379,3 +379,108 @@ describe('RLS — cross-company isolation', () => {
|
||||
).rejects.toThrow(/row-level security|new row violates/i)
|
||||
})
|
||||
})
|
||||
|
||||
// Asset disposal VAT + jämkning constraints (migration 20260526120300).
|
||||
// The columns are populated by disposeAsset() after the journal entry posts;
|
||||
// these pg tests cover the CHECK constraints directly so future schema changes
|
||||
// can't loosen them without us noticing.
|
||||
describe('assets — disposal VAT + jämkning constraints', () => {
|
||||
it('accepts a disposed_vat_treatment from the allowed enum', async () => {
|
||||
const assetId = await insertAsset({
|
||||
userId: companyA.userId,
|
||||
companyId: companyA.companyId,
|
||||
disposedAt: '2025-12-31',
|
||||
disposedProceeds: 100_000,
|
||||
})
|
||||
await getPool().query(
|
||||
`UPDATE public.assets
|
||||
SET disposed_proceeds_vat = 20000, disposed_vat_treatment = 'standard_25'
|
||||
WHERE id = $1`,
|
||||
[assetId],
|
||||
)
|
||||
const { rows } = await getPool().query(
|
||||
`SELECT disposed_proceeds_vat, disposed_vat_treatment FROM public.assets WHERE id = $1`,
|
||||
[assetId],
|
||||
)
|
||||
expect(Number(rows[0]?.disposed_proceeds_vat)).toBe(20_000)
|
||||
expect(rows[0]?.disposed_vat_treatment).toBe('standard_25')
|
||||
})
|
||||
|
||||
it('rejects a disposed_vat_treatment outside the enum', async () => {
|
||||
const assetId = await insertAsset({
|
||||
userId: companyA.userId,
|
||||
companyId: companyA.companyId,
|
||||
disposedAt: '2025-12-31',
|
||||
disposedProceeds: 100_000,
|
||||
})
|
||||
await expect(
|
||||
getPool().query(
|
||||
`UPDATE public.assets SET disposed_vat_treatment = 'reduced_999' WHERE id = $1`,
|
||||
[assetId],
|
||||
),
|
||||
).rejects.toThrow(/check/i)
|
||||
})
|
||||
|
||||
it('rejects disposed_proceeds_vat > 0 without a disposed_vat_treatment', async () => {
|
||||
const assetId = await insertAsset({
|
||||
userId: companyA.userId,
|
||||
companyId: companyA.companyId,
|
||||
disposedAt: '2025-12-31',
|
||||
disposedProceeds: 100_000,
|
||||
})
|
||||
// Treatment NULL + VAT > 0 must violate the consistency CHECK.
|
||||
await expect(
|
||||
getPool().query(
|
||||
`UPDATE public.assets
|
||||
SET disposed_proceeds_vat = 20000, disposed_vat_treatment = NULL
|
||||
WHERE id = $1`,
|
||||
[assetId],
|
||||
),
|
||||
).rejects.toThrow(/check|consistency/i)
|
||||
})
|
||||
|
||||
it('accepts zero VAT with null treatment (legacy / non-VAT disposal)', async () => {
|
||||
const assetId = await insertAsset({
|
||||
userId: companyA.userId,
|
||||
companyId: companyA.companyId,
|
||||
disposedAt: '2025-12-31',
|
||||
disposedProceeds: 50_000,
|
||||
})
|
||||
// Default values from the migration — should already pass on insert.
|
||||
const { rows } = await getPool().query(
|
||||
`SELECT disposed_proceeds_vat, disposed_vat_treatment FROM public.assets WHERE id = $1`,
|
||||
[assetId],
|
||||
)
|
||||
expect(Number(rows[0]?.disposed_proceeds_vat)).toBe(0)
|
||||
expect(rows[0]?.disposed_vat_treatment).toBeNull()
|
||||
})
|
||||
|
||||
it('persists jämkning audit metadata on the row', async () => {
|
||||
const assetId = await insertAsset({
|
||||
userId: companyA.userId,
|
||||
companyId: companyA.companyId,
|
||||
disposedAt: '2025-12-31',
|
||||
disposedProceeds: 60_000,
|
||||
})
|
||||
await getPool().query(
|
||||
`UPDATE public.assets
|
||||
SET jamkning_amount = 8000,
|
||||
jamkning_remaining_months = 24,
|
||||
jamkning_total_months = 60,
|
||||
jamkning_original_input_vat = 20000
|
||||
WHERE id = $1`,
|
||||
[assetId],
|
||||
)
|
||||
const { rows } = await getPool().query(
|
||||
`SELECT jamkning_amount, jamkning_remaining_months, jamkning_total_months,
|
||||
jamkning_original_input_vat
|
||||
FROM public.assets
|
||||
WHERE id = $1`,
|
||||
[assetId],
|
||||
)
|
||||
expect(Number(rows[0]?.jamkning_amount)).toBe(8_000)
|
||||
expect(rows[0]?.jamkning_remaining_months).toBe(24)
|
||||
expect(rows[0]?.jamkning_total_months).toBe(60)
|
||||
expect(Number(rows[0]?.jamkning_original_input_vat)).toBe(20_000)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
/**
|
||||
* pg-real tests for the ROT/RUT-avdrag schema introduced in
|
||||
* 20260526121700_rot_rut_avdrag.sql.
|
||||
*
|
||||
* Verifies:
|
||||
* - The new columns exist on invoice_items and invoices.
|
||||
* - CHECK constraints behave (deduction_type only 'rot'|'rut'|null,
|
||||
* deduction_amount >= 0, deduction_total >= 0).
|
||||
* - Indexes were created.
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { getPool } from './setup'
|
||||
import { insertAuthUser, insertCompany } from './fixtures'
|
||||
|
||||
describe('ROT/RUT-avdrag schema', () => {
|
||||
it('invoice_items has the new deduction columns', async () => {
|
||||
const result = await getPool().query<{ column_name: string }>(
|
||||
`SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'invoice_items'
|
||||
AND column_name IN (
|
||||
'deduction_type', 'deduction_amount', 'labor_hours',
|
||||
'work_type', 'housing_designation', 'apartment_number'
|
||||
)`,
|
||||
)
|
||||
const found = new Set(result.rows.map((r) => r.column_name))
|
||||
expect(found.has('deduction_type')).toBe(true)
|
||||
expect(found.has('deduction_amount')).toBe(true)
|
||||
expect(found.has('labor_hours')).toBe(true)
|
||||
expect(found.has('work_type')).toBe(true)
|
||||
expect(found.has('housing_designation')).toBe(true)
|
||||
expect(found.has('apartment_number')).toBe(true)
|
||||
})
|
||||
|
||||
it('invoices has the new deduction columns', async () => {
|
||||
const result = await getPool().query<{ column_name: string }>(
|
||||
`SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'invoices'
|
||||
AND column_name IN (
|
||||
'deduction_total',
|
||||
'deduction_personnummer_encrypted',
|
||||
'deduction_personnummer_last4'
|
||||
)`,
|
||||
)
|
||||
const found = new Set(result.rows.map((r) => r.column_name))
|
||||
expect(found.has('deduction_total')).toBe(true)
|
||||
expect(found.has('deduction_personnummer_encrypted')).toBe(true)
|
||||
expect(found.has('deduction_personnummer_last4')).toBe(true)
|
||||
})
|
||||
|
||||
it('deduction_type CHECK rejects values other than rot/rut/null', async () => {
|
||||
// Seed company + customer + draft invoice + insert an invoice item with
|
||||
// a bogus deduction_type. The CHECK should reject.
|
||||
const userId = await insertAuthUser()
|
||||
const companyId = await insertCompany({ createdBy: userId })
|
||||
const customerId = randomUUID()
|
||||
await getPool().query(
|
||||
`INSERT INTO public.customers (id, user_id, company_id, name, customer_type)
|
||||
VALUES ($1, $2, $3, 'Test Cust', 'individual')`,
|
||||
[customerId, userId, companyId],
|
||||
)
|
||||
const invoiceId = randomUUID()
|
||||
await getPool().query(
|
||||
`INSERT INTO public.invoices
|
||||
(id, user_id, company_id, customer_id, invoice_date, due_date,
|
||||
currency, vat_treatment, vat_rate)
|
||||
VALUES ($1, $2, $3, $4, '2026-05-01', '2026-05-31', 'SEK', 'standard_25', 25)`,
|
||||
[invoiceId, userId, companyId, customerId],
|
||||
)
|
||||
|
||||
await expect(
|
||||
getPool().query(
|
||||
`INSERT INTO public.invoice_items
|
||||
(id, invoice_id, sort_order, description, quantity, unit, unit_price,
|
||||
line_total, vat_rate, vat_amount, deduction_type)
|
||||
VALUES ($1, $2, 0, 'X', 1, 'st', 100, 100, 25, 25, 'invalid')`,
|
||||
[randomUUID(), invoiceId],
|
||||
),
|
||||
).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('deduction_amount CHECK rejects negative values', async () => {
|
||||
const userId = await insertAuthUser()
|
||||
const companyId = await insertCompany({ createdBy: userId })
|
||||
const customerId = randomUUID()
|
||||
await getPool().query(
|
||||
`INSERT INTO public.customers (id, user_id, company_id, name, customer_type)
|
||||
VALUES ($1, $2, $3, 'Test Cust', 'individual')`,
|
||||
[customerId, userId, companyId],
|
||||
)
|
||||
const invoiceId = randomUUID()
|
||||
await getPool().query(
|
||||
`INSERT INTO public.invoices
|
||||
(id, user_id, company_id, customer_id, invoice_date, due_date,
|
||||
currency, vat_treatment, vat_rate)
|
||||
VALUES ($1, $2, $3, $4, '2026-05-01', '2026-05-31', 'SEK', 'standard_25', 25)`,
|
||||
[invoiceId, userId, companyId, customerId],
|
||||
)
|
||||
|
||||
await expect(
|
||||
getPool().query(
|
||||
`INSERT INTO public.invoice_items
|
||||
(id, invoice_id, sort_order, description, quantity, unit, unit_price,
|
||||
line_total, vat_rate, vat_amount, deduction_amount)
|
||||
VALUES ($1, $2, 0, 'X', 1, 'st', 100, 100, 25, 25, -100)`,
|
||||
[randomUUID(), invoiceId],
|
||||
),
|
||||
).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('valid ROT row inserts cleanly with full deduction shape', async () => {
|
||||
const userId = await insertAuthUser()
|
||||
const companyId = await insertCompany({ createdBy: userId })
|
||||
const customerId = randomUUID()
|
||||
await getPool().query(
|
||||
`INSERT INTO public.customers (id, user_id, company_id, name, customer_type)
|
||||
VALUES ($1, $2, $3, 'Test Cust', 'individual')`,
|
||||
[customerId, userId, companyId],
|
||||
)
|
||||
const invoiceId = randomUUID()
|
||||
await getPool().query(
|
||||
`INSERT INTO public.invoices
|
||||
(id, user_id, company_id, customer_id, invoice_date, due_date,
|
||||
currency, vat_treatment, vat_rate,
|
||||
deduction_total, deduction_personnummer_last4)
|
||||
VALUES ($1, $2, $3, $4, '2026-05-01', '2026-05-31', 'SEK', 'standard_25', 25,
|
||||
3000, '1234')`,
|
||||
[invoiceId, userId, companyId, customerId],
|
||||
)
|
||||
|
||||
const itemId = randomUUID()
|
||||
await getPool().query(
|
||||
`INSERT INTO public.invoice_items
|
||||
(id, invoice_id, sort_order, description, quantity, unit, unit_price,
|
||||
line_total, vat_rate, vat_amount,
|
||||
deduction_type, deduction_amount, labor_hours, work_type,
|
||||
housing_designation, apartment_number)
|
||||
VALUES ($1, $2, 0, 'Snickeri', 1, 'st', 10000, 10000, 25, 2500,
|
||||
'rot', 3000, 25, 'BYGG', 'Stockholm 1:23', '0301')`,
|
||||
[itemId, invoiceId],
|
||||
)
|
||||
|
||||
const result = await getPool().query<{
|
||||
deduction_type: string
|
||||
deduction_amount: string
|
||||
work_type: string
|
||||
housing_designation: string
|
||||
}>(
|
||||
`SELECT deduction_type, deduction_amount, work_type, housing_designation
|
||||
FROM public.invoice_items WHERE id = $1`,
|
||||
[itemId],
|
||||
)
|
||||
expect(result.rows[0].deduction_type).toBe('rot')
|
||||
expect(Number(result.rows[0].deduction_amount)).toBe(3000)
|
||||
expect(result.rows[0].work_type).toBe('BYGG')
|
||||
expect(result.rows[0].housing_designation).toBe('Stockholm 1:23')
|
||||
})
|
||||
|
||||
it('idx_invoice_items_deduction_type exists', async () => {
|
||||
const result = await getPool().query<{ indexname: string }>(
|
||||
`SELECT indexname FROM pg_indexes
|
||||
WHERE schemaname = 'public' AND tablename = 'invoice_items'
|
||||
AND indexname = 'idx_invoice_items_deduction_type'`,
|
||||
)
|
||||
expect(result.rows).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('idx_invoices_deduction_total exists', async () => {
|
||||
const result = await getPool().query<{ indexname: string }>(
|
||||
`SELECT indexname FROM pg_indexes
|
||||
WHERE schemaname = 'public' AND tablename = 'invoices'
|
||||
AND indexname = 'idx_invoices_deduction_total'`,
|
||||
)
|
||||
expect(result.rows).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user