* fix(git): pin LF on generated extension registry and vitest snapshots setup:extensions and vitest write these files with LF; with core.autocrlf=true git expects CRLF and flags them as phantom modifications on every dev/build run. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(security): enforce MFA on mcp-oauth consent and gate viewer storno route mcp-oauth/authorize renders an HTML consent page and issues 303 redirects that withRouteContext cannot express, so it kept raw getUser() and thereby skipped the AAL2 gate: a password-only (AAL1) session could approve consent that mints a long-lived, MFA-bypassing API key. Add a route-local requireAal2() step-up on GET and POST; AAL1 sessions redirect to /mfa/verify, BankID users are exempt. Separately, POST /api/reports/vat-declaration/rc-basis-gaps/fix calls correctEntry() (storno of a posted entry) but lacked requireWrite, so viewer-role members could trigger it. Add { requireWrite: true }. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): route transactions endpoints through withRouteContext Migrate the transactions routes off hand-rolled supabase.auth.getUser() onto the MFA-enforcing withRouteContext wrapper; add requireWrite on mutating handlers (book, uncategorize, attach-document, ignore, batch-match, create-from-document). Behavior and response shapes preserved; tests updated to the wrapper mock pattern. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): route SIE import and bank reconciliation through withRouteContext Migrate import/sie and reconciliation/bank routes onto the MFA-enforcing wrapper; requireWrite on mutations (import execute, create-accounts, mappings write verbs, link/unlink/run/mark-opening-balance). Reads (status, unmatched-entries) stay ungated. Response shapes preserved; tests added/updated to the wrapper mock pattern. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): route salary endpoints through withRouteContext Migrate salary employees and runs routes (plus ku, payroll-config, tax-tables) onto the MFA-enforcing wrapper; requireWrite on mutations. Personnummer masking/encryption untouched; file downloads (AGI XML, payslip PDF, payment files) keep their headers. Two payment-file GETs retain requireWrite because they stamp *_file_generated_at and previously gated viewers. Tests added/updated to the wrapper mock pattern. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): route report endpoints through withRouteContext Migrate the read-only report routes (trial balance, balansrapport, resultatrapport, income statement, ledgers, KPI, VAT declaration, salary journal, monthly breakdown, journal register, continuity check, full archive, etc.) onto the MFA-enforcing wrapper. All read-only, no requireWrite. JSON/XLSX/PDF/ZIP response bodies and headers preserved byte-for-byte; tests updated to the wrapper mock pattern. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): route invoices, skatteverket, agent and extension endpoints through withRouteContext Migrate invoices, supplier-invoices, skatteverket tax-payments, and dynamic extension routes onto the MFA-enforcing wrapper with requireWrite on mutations. The two NDJSON streaming agent routes (invoke, onboarding/stream) use requireAuth() directly (the wrapper can't wrap a streaming response) so MFA is still enforced. skatteverket payment-file GET keeps requireWrite (stamps a generated-at field). Response shapes and file headers preserved; tests added/updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): route documents, events, team and account endpoints through withRouteContext Migrate documents, events, kpi/preferences, vat/validate, support/contact onto the MFA-enforcing wrapper with requireWrite on mutations. account/password, team/accept and team/members use requireAuth() directly (user-level or pre-membership flows with no active company context) so MFA is still enforced. events keeps its dual API-key-or-session auth. Document retention guard untouched; tests added/updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): route settings and pending-operations endpoints through withRouteContext Migrate settings (api-keys, oauth-clients, booking-templates, counterparty-templates, logo, company settings) and pending-operations (commit, bulk-commit, reject, edit-before-approve) onto the MFA-enforcing wrapper with requireWrite on mutations. Credential-guarding routes keep their per-user ownership filters. Response shapes preserved; tests added/updated to the wrapper mock pattern. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore(guards): ratchet raw-route-auth baseline 119->1 after A1 migration Lock in the withRouteContext migration so the count cannot regress. The single remaining entry, mcp-oauth/authorize, is a documented exception (HTML consent + redirects, MFA enforced via route-local step-up). Record the campaign and requireWrite decisions in DECISIONS.md. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(vat): add eSKD momsdeklaration file export for "Deklarera via fil" Generate the Skatteverket eSKDUpload v6.0 XML file so users can file VAT by upload instead of typing every ruta into the form. Extract buildFiledAmounts() as the shared whole-krona source of truth (öre truncated per SFL 22 kap 1 §) so the XML file and the manual-filing PDF can never disagree. Adds the /eskd API route, an XML option in the report export menu, and the upload button on the manual-filing card. Strings in sv + en. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(vat): add 'vat_settlement' source type and update related components * fix(booking): adjust search input layout and enable autofocus * fix(vat): support 12-digit org numbers and adjust emission order for eSKD file * fix(migration): add 'vat_settlement' to journal_entries.source_type CHECK --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
452 lines
14 KiB
TypeScript
452 lines
14 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import {
|
|
createMockRequest,
|
|
createMockRouteParams,
|
|
parseJsonResponse,
|
|
createQueuedMockSupabase,
|
|
} from '@/tests/helpers'
|
|
|
|
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
|
|
|
|
// The route runs through the real withRouteContext wrapper: mock its auth,
|
|
// company-resolution and write-permission dependencies (getActiveCompanyId,
|
|
// not requireCompanyId, is what the wrapper calls) and inject the queued
|
|
// Supabase mock via requireAuth so the route's own queries stay in sequence.
|
|
const requireAuthMock = vi.fn()
|
|
vi.mock('@/lib/auth/require-auth', () => ({
|
|
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
|
|
}))
|
|
|
|
vi.mock('@/lib/company/context', () => ({
|
|
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
}))
|
|
|
|
const requireWritePermissionMock = vi.fn()
|
|
vi.mock('@/lib/auth/require-write', () => ({
|
|
requireWritePermission: (...args: unknown[]) => requireWritePermissionMock(...args),
|
|
}))
|
|
|
|
vi.mock('@/lib/init', () => ({ ensureInitialized: vi.fn() }))
|
|
|
|
const mappingMock = vi.fn()
|
|
const accountMappingMock = vi.fn()
|
|
vi.mock('@/lib/bookkeeping/category-mapping', () => ({
|
|
buildMappingResultFromCategory: (...args: unknown[]) => mappingMock(...args),
|
|
getCategoryAccountMapping: (...args: unknown[]) => accountMappingMock(...args),
|
|
}))
|
|
|
|
const buildLinesMock = vi.fn()
|
|
vi.mock('@/lib/bookkeeping/transaction-entries', () => ({
|
|
buildTransactionEntryLines: (...args: unknown[]) => buildLinesMock(...args),
|
|
}))
|
|
|
|
import { PATCH } from '../route'
|
|
|
|
const mockUser = { id: 'user-1' }
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
requireAuthMock.mockResolvedValue({ user: mockUser, supabase: mockSupabase, error: null })
|
|
requireWritePermissionMock.mockResolvedValue({ ok: true })
|
|
mappingMock.mockReturnValue({
|
|
debit_account: '5410',
|
|
credit_account: '1930',
|
|
vat_lines: [],
|
|
})
|
|
accountMappingMock.mockReturnValue({
|
|
debitAccount: '5410',
|
|
creditAccount: '1930',
|
|
vatTreatment: 'standard_25',
|
|
vatDebitAccount: '2641',
|
|
vatCreditAccount: null,
|
|
})
|
|
buildLinesMock.mockReturnValue([])
|
|
})
|
|
|
|
describe('PATCH /api/pending-operations/[id]', () => {
|
|
it('returns 401 when not authenticated', async () => {
|
|
const { NextResponse } = await import('next/server')
|
|
requireAuthMock.mockResolvedValue({
|
|
user: null,
|
|
supabase: mockSupabase,
|
|
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
|
})
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { category: 'expense_software' },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
expect(res.status).toBe(401)
|
|
})
|
|
|
|
it('blocks viewers', async () => {
|
|
const { NextResponse } = await import('next/server')
|
|
requireWritePermissionMock.mockResolvedValue({
|
|
ok: false,
|
|
response: NextResponse.json({ error: 'forbidden' }, { status: 403 }),
|
|
})
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { category: 'expense_software' },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
expect(res.status).toBe(403)
|
|
})
|
|
|
|
it('rejects empty body', async () => {
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', { method: 'PATCH', body: {} }),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
expect(res.status).toBe(400)
|
|
})
|
|
|
|
it('returns 404 when op not visible', async () => {
|
|
enqueue({ data: null })
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-x', {
|
|
method: 'PATCH',
|
|
body: { category: 'expense_software' },
|
|
}),
|
|
createMockRouteParams({ id: 'op-x' }),
|
|
)
|
|
expect(res.status).toBe(404)
|
|
})
|
|
|
|
it('returns 409 when op is no longer pending', async () => {
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'categorize_transaction',
|
|
status: 'committed',
|
|
params: { transaction_id: 'tx-1', category: 'expense_other' },
|
|
preview_data: {},
|
|
title: '',
|
|
},
|
|
})
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { category: 'expense_software' },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
expect(res.status).toBe(409)
|
|
})
|
|
|
|
it('returns 400 when operation_type is not editable', async () => {
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'create_invoice',
|
|
status: 'pending',
|
|
params: {},
|
|
preview_data: {},
|
|
title: '',
|
|
},
|
|
})
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { category: 'expense_software' },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
expect(res.status).toBe(400)
|
|
})
|
|
|
|
it('re-derives accounts and returns updated preview on category change', async () => {
|
|
// Sequence:
|
|
// 1. pending_operations lookup
|
|
// 2. transactions lookup
|
|
// 3. company_settings lookup
|
|
// 4. pending_operations update → returns updated row
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'categorize_transaction',
|
|
status: 'pending',
|
|
params: {
|
|
transaction_id: 'tx-1',
|
|
category: 'expense_other',
|
|
vat_treatment: null,
|
|
},
|
|
preview_data: { debit_account: '6990', credit_account: '1930', amount: 500 },
|
|
title: 'Kategorisera: X',
|
|
},
|
|
})
|
|
enqueue({
|
|
data: {
|
|
id: 'tx-1',
|
|
company_id: 'company-1',
|
|
amount: -500,
|
|
currency: 'SEK',
|
|
date: '2026-05-10',
|
|
},
|
|
})
|
|
enqueue({ data: { entity_type: 'aktiebolag' } })
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
params: { transaction_id: 'tx-1', category: 'expense_software', vat_treatment: null },
|
|
preview_data: {
|
|
debit_account: '5410',
|
|
credit_account: '1930',
|
|
amount: 500,
|
|
currency: 'SEK',
|
|
vat_lines: [],
|
|
category: 'expense_software',
|
|
},
|
|
title: 'Kategorisera: X',
|
|
status: 'pending',
|
|
},
|
|
})
|
|
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { category: 'expense_software' },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
const { status, body } = await parseJsonResponse<{
|
|
data: { preview_data: { category: string; debit_account: string } }
|
|
}>(res)
|
|
expect(status).toBe(200)
|
|
expect(body.data.preview_data.category).toBe('expense_software')
|
|
expect(body.data.preview_data.debit_account).toBe('5410')
|
|
expect(mappingMock).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('re-derives the full journal lines from the new mapping (stale-preview guard)', async () => {
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'categorize_transaction',
|
|
status: 'pending',
|
|
params: { transaction_id: 'tx-1', category: 'expense_other', vat_treatment: null },
|
|
preview_data: {
|
|
debit_account: '6990',
|
|
credit_account: '1930',
|
|
amount: 500,
|
|
// Stale lines from staging — must be replaced, not spread through.
|
|
lines: [{ account_number: '6990', debit_amount: 400, credit_amount: 0 }],
|
|
},
|
|
title: 'Kategorisera: X',
|
|
},
|
|
})
|
|
enqueue({ data: { id: 'tx-1', company_id: 'company-1', amount: -500, currency: 'SEK' } })
|
|
enqueue({ data: { entity_type: 'aktiebolag' } })
|
|
enqueue({ data: { id: 'op-1', params: {}, preview_data: {}, title: '', status: 'pending' } })
|
|
|
|
const mapping = {
|
|
debit_account: '5420',
|
|
credit_account: '1930',
|
|
vat_lines: [
|
|
{ account_number: '2641', debit_amount: 100, credit_amount: 0, description: 'Ingående moms 25%' },
|
|
],
|
|
}
|
|
mappingMock.mockReturnValue(mapping)
|
|
buildLinesMock.mockReturnValue([
|
|
{ account_number: '2641', debit_amount: 100, credit_amount: 0, line_description: 'Ingående moms 25%' },
|
|
{ account_number: '5420', debit_amount: 400, credit_amount: 0, line_description: 'Kostnad' },
|
|
{ account_number: '1930', debit_amount: 0, credit_amount: 500, line_description: 'X' },
|
|
])
|
|
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { category: 'expense_software' },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(buildLinesMock).toHaveBeenCalledTimes(1)
|
|
expect(buildLinesMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ id: 'tx-1' }),
|
|
mapping,
|
|
)
|
|
})
|
|
|
|
it('preserves a staged vat_amount override when the new treatment still carries VAT', async () => {
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'categorize_transaction',
|
|
status: 'pending',
|
|
params: {
|
|
transaction_id: 'tx-1',
|
|
category: 'expense_representation',
|
|
vat_treatment: 'reduced_12',
|
|
vat_amount: 42.43,
|
|
},
|
|
preview_data: {},
|
|
title: '',
|
|
},
|
|
})
|
|
enqueue({ data: { id: 'tx-1', company_id: 'company-1', amount: -415.8, currency: 'SEK' } })
|
|
enqueue({ data: { entity_type: 'enskild_firma' } })
|
|
enqueue({ data: { id: 'op-1', params: {}, preview_data: {}, title: '', status: 'pending' } })
|
|
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { category: 'expense_office' },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
expect(res.status).toBe(200)
|
|
// 6th arg = vat_amount override, carried over from the staged params
|
|
// (vat_treatment persists too, only the category changed)
|
|
expect(mappingMock).toHaveBeenCalledWith(
|
|
'expense_office', expect.anything(), true, 'enskild_firma', 'reduced_12', 42.43,
|
|
)
|
|
})
|
|
|
|
it('drops a stale vat_amount override when the new treatment is VAT-less', async () => {
|
|
accountMappingMock.mockReturnValueOnce({
|
|
debitAccount: '6570',
|
|
creditAccount: '1930',
|
|
vatTreatment: null,
|
|
vatDebitAccount: null,
|
|
vatCreditAccount: null,
|
|
})
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'categorize_transaction',
|
|
status: 'pending',
|
|
params: {
|
|
transaction_id: 'tx-1',
|
|
category: 'expense_representation',
|
|
vat_treatment: 'reduced_12',
|
|
vat_amount: 42.43,
|
|
},
|
|
preview_data: {},
|
|
title: '',
|
|
},
|
|
})
|
|
enqueue({ data: { id: 'tx-1', company_id: 'company-1', amount: -415.8, currency: 'SEK' } })
|
|
enqueue({ data: { entity_type: 'enskild_firma' } })
|
|
enqueue({ data: { id: 'op-1', params: {}, preview_data: {}, title: '', status: 'pending' } })
|
|
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { category: 'expense_bank_fees', vat_treatment: null },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
expect(res.status).toBe(200)
|
|
expect(mappingMock).toHaveBeenCalledWith(
|
|
'expense_bank_fees', expect.anything(), true, 'enskild_firma', undefined, null,
|
|
)
|
|
})
|
|
|
|
it('returns 400 when vat_amount is explicitly set on a VAT-less treatment', async () => {
|
|
accountMappingMock.mockReturnValueOnce({
|
|
debitAccount: '5410',
|
|
creditAccount: '1930',
|
|
vatTreatment: null,
|
|
vatDebitAccount: null,
|
|
vatCreditAccount: null,
|
|
})
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'categorize_transaction',
|
|
status: 'pending',
|
|
params: { transaction_id: 'tx-1', category: 'expense_other' },
|
|
preview_data: {},
|
|
title: '',
|
|
},
|
|
})
|
|
enqueue({ data: { id: 'tx-1', company_id: 'company-1', amount: -500, currency: 'SEK' } })
|
|
enqueue({ data: { entity_type: 'enskild_firma' } })
|
|
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { vat_treatment: 'exempt', vat_amount: 50 },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
expect(res.status).toBe(400)
|
|
expect(mappingMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 400 when the mapping builder rejects the vat_amount', async () => {
|
|
mappingMock.mockImplementationOnce(() => {
|
|
throw new Error('vat_amount 100 exceeds the maximum possible Swedish VAT on 415.8')
|
|
})
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'categorize_transaction',
|
|
status: 'pending',
|
|
params: { transaction_id: 'tx-1', category: 'expense_representation', vat_treatment: 'reduced_12' },
|
|
preview_data: {},
|
|
title: '',
|
|
},
|
|
})
|
|
enqueue({ data: { id: 'tx-1', company_id: 'company-1', amount: -415.8, currency: 'SEK' } })
|
|
enqueue({ data: { entity_type: 'enskild_firma' } })
|
|
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { vat_amount: 100 },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(res)
|
|
expect(status).toBe(400)
|
|
expect(body.error).toMatch(/exceeds the maximum/)
|
|
})
|
|
|
|
it('returns 400 when mapping yields no accounts', async () => {
|
|
enqueue({
|
|
data: {
|
|
id: 'op-1',
|
|
company_id: 'company-1',
|
|
operation_type: 'categorize_transaction',
|
|
status: 'pending',
|
|
params: { transaction_id: 'tx-1', category: 'expense_other' },
|
|
preview_data: {},
|
|
title: '',
|
|
},
|
|
})
|
|
enqueue({ data: { id: 'tx-1', amount: -100, currency: 'SEK' } })
|
|
enqueue({ data: { entity_type: 'enskild_firma' } })
|
|
mappingMock.mockReturnValueOnce({
|
|
debit_account: null,
|
|
credit_account: null,
|
|
vat_lines: [],
|
|
})
|
|
|
|
const res = await PATCH(
|
|
createMockRequest('/api/pending-operations/op-1', {
|
|
method: 'PATCH',
|
|
body: { category: 'private' },
|
|
}),
|
|
createMockRouteParams({ id: 'op-1' }),
|
|
)
|
|
expect(res.status).toBe(400)
|
|
})
|
|
})
|