fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { createQueuedMockSupabase, createMockRequest, parseJsonResponse } from '@/tests/helpers'
|
||||
|
||||
// Exercised through the real withRouteContext wrapper: mock its auth/company
|
||||
// dependencies and inject the Supabase client via requireAuth.
|
||||
const { supabase } = createQueuedMockSupabase()
|
||||
|
||||
const requireAuthMock = vi.fn()
|
||||
vi.mock('@/lib/auth/require-auth', () => ({
|
||||
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/company/context', () => ({
|
||||
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
}))
|
||||
|
||||
// Mock VIES client
|
||||
const mockValidateVatNumber = vi.fn()
|
||||
vi.mock('@/lib/vat/vies-client', () => ({
|
||||
validateVatNumber: (...args: unknown[]) => mockValidateVatNumber(...args),
|
||||
}))
|
||||
|
||||
// Company is not a sandbox, so VIES calls proceed.
|
||||
vi.mock('@/lib/sandbox/guard', () => ({
|
||||
guardSandbox: vi.fn().mockResolvedValue(null),
|
||||
}))
|
||||
|
||||
import { POST } from '../route'
|
||||
|
||||
describe('POST /api/vat/validate', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase, error: null })
|
||||
})
|
||||
|
||||
it('returns 401 when not authenticated', async () => {
|
||||
requireAuthMock.mockResolvedValue({
|
||||
user: null,
|
||||
supabase,
|
||||
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
||||
})
|
||||
|
||||
const req = createMockRequest('/api/vat/validate', {
|
||||
method: 'POST',
|
||||
body: { vat_number: 'DE123456789' },
|
||||
})
|
||||
|
||||
const res = await POST(req, { params: Promise.resolve({}) })
|
||||
const { status, body } = await parseJsonResponse(res)
|
||||
|
||||
expect(status).toBe(401)
|
||||
expect(body).toEqual({ error: 'Unauthorized' })
|
||||
})
|
||||
|
||||
it('returns 400 when vat_number is missing', async () => {
|
||||
const req = createMockRequest('/api/vat/validate', {
|
||||
method: 'POST',
|
||||
body: {},
|
||||
})
|
||||
|
||||
const res = await POST(req, { params: Promise.resolve({}) })
|
||||
const { status } = await parseJsonResponse(res)
|
||||
|
||||
expect(status).toBe(400)
|
||||
})
|
||||
|
||||
it('returns 400 when vat_number is too short', async () => {
|
||||
const req = createMockRequest('/api/vat/validate', {
|
||||
method: 'POST',
|
||||
body: { vat_number: 'DE' },
|
||||
})
|
||||
|
||||
const res = await POST(req, { params: Promise.resolve({}) })
|
||||
const { status } = await parseJsonResponse(res)
|
||||
|
||||
expect(status).toBe(400)
|
||||
})
|
||||
|
||||
it('returns valid result from VIES', async () => {
|
||||
mockValidateVatNumber.mockResolvedValueOnce({
|
||||
valid: true,
|
||||
name: 'Test GmbH',
|
||||
address: 'Berlin',
|
||||
country_code: 'DE',
|
||||
vat_number: 'DE123456789',
|
||||
})
|
||||
|
||||
const req = createMockRequest('/api/vat/validate', {
|
||||
method: 'POST',
|
||||
body: { vat_number: 'DE123456789' },
|
||||
})
|
||||
|
||||
const res = await POST(req, { params: Promise.resolve({}) })
|
||||
const { status, body } = await parseJsonResponse(res)
|
||||
|
||||
expect(status).toBe(200)
|
||||
expect(body).toEqual({
|
||||
valid: true,
|
||||
name: 'Test GmbH',
|
||||
address: 'Berlin',
|
||||
country_code: 'DE',
|
||||
vat_number: 'DE123456789',
|
||||
})
|
||||
})
|
||||
|
||||
it('returns invalid result from VIES', async () => {
|
||||
mockValidateVatNumber.mockResolvedValueOnce({
|
||||
valid: false,
|
||||
country_code: 'DE',
|
||||
vat_number: 'DE000000000',
|
||||
})
|
||||
|
||||
const req = createMockRequest('/api/vat/validate', {
|
||||
method: 'POST',
|
||||
body: { vat_number: 'DE000000000' },
|
||||
})
|
||||
|
||||
const res = await POST(req, { params: Promise.resolve({}) })
|
||||
const { status, body } = await parseJsonResponse(res)
|
||||
|
||||
expect(status).toBe(200)
|
||||
expect(body).toMatchObject({ valid: false })
|
||||
})
|
||||
|
||||
it('updates customer when customer_id provided and valid', async () => {
|
||||
mockValidateVatNumber.mockResolvedValueOnce({
|
||||
valid: true,
|
||||
name: 'Test GmbH',
|
||||
country_code: 'DE',
|
||||
vat_number: 'DE123456789',
|
||||
})
|
||||
|
||||
const req = createMockRequest('/api/vat/validate', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
vat_number: 'DE123456789',
|
||||
customer_id: '550e8400-e29b-41d4-a716-446655440000',
|
||||
},
|
||||
})
|
||||
|
||||
const res = await POST(req, { params: Promise.resolve({}) })
|
||||
const { status, body } = await parseJsonResponse(res)
|
||||
|
||||
expect(status).toBe(200)
|
||||
expect(body).toMatchObject({ valid: true })
|
||||
})
|
||||
|
||||
it('does not update customer when validation fails', async () => {
|
||||
mockValidateVatNumber.mockResolvedValueOnce({
|
||||
valid: false,
|
||||
error: 'Invalid VAT number format',
|
||||
})
|
||||
|
||||
const req = createMockRequest('/api/vat/validate', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
vat_number: 'DE12345',
|
||||
customer_id: '550e8400-e29b-41d4-a716-446655440000',
|
||||
},
|
||||
})
|
||||
|
||||
const res = await POST(req, { params: Promise.resolve({}) })
|
||||
const { status, body } = await parseJsonResponse(res)
|
||||
|
||||
expect(status).toBe(200)
|
||||
expect(body).toMatchObject({ valid: false })
|
||||
})
|
||||
|
||||
it('handles VIES service error gracefully', async () => {
|
||||
mockValidateVatNumber.mockResolvedValueOnce({
|
||||
valid: false,
|
||||
error: 'Could not verify VAT number. Service temporarily unavailable.',
|
||||
})
|
||||
|
||||
const req = createMockRequest('/api/vat/validate', {
|
||||
method: 'POST',
|
||||
body: { vat_number: 'DE123456789' },
|
||||
})
|
||||
|
||||
const res = await POST(req, { params: Promise.resolve({}) })
|
||||
const { status, body } = await parseJsonResponse(res)
|
||||
|
||||
expect(status).toBe(200)
|
||||
expect(body).toMatchObject({ valid: false, error: expect.stringContaining('unavailable') })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,34 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { withRouteContext } from '@/lib/api/with-route-context'
|
||||
import { validateBody } from '@/lib/api/validate'
|
||||
import { ValidateVatNumberSchema } from '@/lib/api/schemas'
|
||||
import { validateVatNumber } from '@/lib/vat/vies-client'
|
||||
import { guardSandbox } from '@/lib/sandbox/guard'
|
||||
|
||||
export const POST = withRouteContext('vat.validate', async (request, { supabase, companyId }) => {
|
||||
// VIES is a live external call to the EU Commission: block in the sandbox
|
||||
// so the demo can't generate background traffic against it.
|
||||
const blocked = await guardSandbox(supabase, companyId)
|
||||
if (blocked) return blocked
|
||||
|
||||
const result = await validateBody(request, ValidateVatNumberSchema)
|
||||
if (!result.success) return result.response
|
||||
const { vat_number, customer_id } = result.data
|
||||
|
||||
const validation = await validateVatNumber(vat_number)
|
||||
|
||||
// Update customer record if customer_id provided and VAT is valid
|
||||
if (customer_id && validation.valid) {
|
||||
await supabase
|
||||
.from('customers')
|
||||
.update({
|
||||
vat_number: validation.vat_number,
|
||||
vat_number_validated: true,
|
||||
vat_number_validated_at: new Date().toISOString(),
|
||||
})
|
||||
.eq('id', customer_id)
|
||||
.eq('company_id', companyId)
|
||||
}
|
||||
|
||||
return NextResponse.json(validation)
|
||||
})
|
||||
Reference in New Issue
Block a user