215 lines
6.8 KiB
TypeScript
215 lines
6.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest'
|
|
import { createMockRequest, createMockSupabase, parseJsonResponse } from '@/tests/helpers'
|
|
import { eventBus } from '@/lib/events/bus'
|
|
import type { ExtensionContext } from '@/lib/extensions/types'
|
|
|
|
vi.mock('../lib/import-documents', () => {
|
|
class FortnoxDocumentScopesRequiredError extends Error {
|
|
readonly code = 'PROVIDER_DOCUMENT_SCOPES_REQUIRED'
|
|
|
|
constructor() {
|
|
super('Fortnox consent lacks archive/connectfile scope: reconnect required')
|
|
}
|
|
}
|
|
|
|
return {
|
|
FortnoxDocumentScopesRequiredError,
|
|
importProviderDocuments: vi.fn(),
|
|
}
|
|
})
|
|
|
|
vi.mock('../lib/provider-client', () => {
|
|
class ProviderTokenInvalidError extends Error {
|
|
constructor(
|
|
message: string,
|
|
readonly kind: 'credentials' | 'company-not-found' = 'credentials',
|
|
) {
|
|
super(message)
|
|
}
|
|
}
|
|
|
|
return {
|
|
createConsent: vi.fn(),
|
|
getConsent: vi.fn(),
|
|
listConsents: vi.fn(),
|
|
generateOtc: vi.fn(),
|
|
consumeOAuthState: vi.fn(),
|
|
getAuthUrl: vi.fn(),
|
|
exchangeAuthToken: vi.fn(),
|
|
submitProviderToken: vi.fn(),
|
|
acceptConsent: vi.fn(),
|
|
deleteConsent: vi.fn(),
|
|
resolveConsent: vi.fn(),
|
|
fetchCompanyInfoDirect: vi.fn(),
|
|
ProviderTokenInvalidError,
|
|
ProviderCompanyMismatchError: class ProviderCompanyMismatchError extends Error {},
|
|
ConsentNotFoundError: class ConsentNotFoundError extends Error {},
|
|
}
|
|
})
|
|
|
|
import { arcimMigrationExtension } from '../index'
|
|
import {
|
|
FortnoxDocumentScopesRequiredError,
|
|
importProviderDocuments,
|
|
} from '../lib/import-documents'
|
|
import {
|
|
ProviderTokenInvalidError,
|
|
submitProviderToken,
|
|
} from '../lib/provider-client'
|
|
|
|
const route = (arcimMigrationExtension.apiRoutes ?? []).find(
|
|
(candidate) =>
|
|
candidate.method === 'POST' && candidate.path === '/import-documents',
|
|
)!
|
|
|
|
type RouteHandler = (request: Request, ctx?: ExtensionContext) => Promise<Response>
|
|
const handler = route.handler as RouteHandler
|
|
const submitTokenRoute = (arcimMigrationExtension.apiRoutes ?? []).find(
|
|
(candidate) => candidate.method === 'POST' && candidate.path === '/submit-token',
|
|
)!
|
|
const submitTokenHandler = submitTokenRoute.handler as RouteHandler
|
|
|
|
function buildContext(): ExtensionContext {
|
|
const { supabase } = createMockSupabase()
|
|
;(supabase as unknown as { auth: unknown }).auth = {
|
|
getUser: vi.fn().mockResolvedValue({ data: { user: { id: 'user-1' } } }),
|
|
}
|
|
return { supabase, companyId: 'company-1' } as unknown as ExtensionContext
|
|
}
|
|
|
|
function request(dryRun: boolean) {
|
|
return createMockRequest(
|
|
'http://localhost/api/extensions/ext/arcim-migration/import-documents',
|
|
{
|
|
method: 'POST',
|
|
body: { consentId: 'consent-1', dryRun },
|
|
},
|
|
)
|
|
}
|
|
|
|
function submitTokenRequest() {
|
|
return createMockRequest(
|
|
'http://localhost/api/extensions/ext/arcim-migration/submit-token',
|
|
{
|
|
method: 'POST',
|
|
body: {
|
|
consentId: 'consent-1',
|
|
provider: 'bokio',
|
|
apiToken: 'not-a-real-token',
|
|
companyId: '9b408943-7a1e-47ac-85a7-ac52b2c210d3',
|
|
},
|
|
},
|
|
)
|
|
}
|
|
|
|
describe('POST /import-documents', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
eventBus.clear()
|
|
})
|
|
|
|
it('passes dry-run discovery through without storing documents', async () => {
|
|
;(importProviderDocuments as Mock).mockResolvedValue({
|
|
provider: 'fortnox',
|
|
scanned: 4,
|
|
linked: 3,
|
|
skipped: 0,
|
|
unmatched: 1,
|
|
failed: 0,
|
|
dryRun: true,
|
|
unmatchedSamples: [],
|
|
})
|
|
|
|
const response = await handler(request(true), buildContext())
|
|
const { status, body } = await parseJsonResponse<{
|
|
success: boolean
|
|
dryRun: boolean
|
|
result: { scanned: number }
|
|
}>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body).toMatchObject({ success: true, dryRun: true, result: { scanned: 4 } })
|
|
expect(importProviderDocuments).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
companyId: 'company-1',
|
|
consentId: 'consent-1',
|
|
dryRun: true,
|
|
}),
|
|
)
|
|
})
|
|
|
|
it('returns an actionable 403 when Fortnox lacks archive/connectfile scopes', async () => {
|
|
;(importProviderDocuments as Mock).mockRejectedValue(
|
|
new FortnoxDocumentScopesRequiredError(),
|
|
)
|
|
|
|
const response = await handler(request(false), buildContext())
|
|
const { status, body } = await parseJsonResponse<{
|
|
error: { code: string; message: string; message_en?: string }
|
|
}>(response)
|
|
|
|
expect(status).toBe(403)
|
|
expect(body.error.code).toBe('PROVIDER_DOCUMENT_SCOPES_REQUIRED')
|
|
expect(body.error.message).toContain('Koppla om Fortnox')
|
|
expect(body.error.message_en).toContain('Reconnect Fortnox')
|
|
})
|
|
})
|
|
|
|
describe('POST /submit-token Bokio error mapping', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
eventBus.clear()
|
|
})
|
|
|
|
it('reports a 401/403 authentication verdict as rejected integration details', async () => {
|
|
;(submitProviderToken as Mock).mockRejectedValue(
|
|
new ProviderTokenInvalidError('Bokio rejected the integration token (HTTP 403)'),
|
|
)
|
|
|
|
const response = await submitTokenHandler(submitTokenRequest(), buildContext())
|
|
const { status, body } = await parseJsonResponse<{
|
|
error: { code: string; message: string; message_en?: string }
|
|
}>(response)
|
|
|
|
expect(status).toBe(422)
|
|
expect(body.error.code).toBe('PROVIDER_TOKEN_INVALID')
|
|
expect(body.error.message).toContain('avvisade autentiseringen')
|
|
expect(body.error.message_en).toContain('rejected the authentication')
|
|
})
|
|
|
|
it('reports a Bokio 404 as a company-ID failure instead of rejected credentials', async () => {
|
|
;(submitProviderToken as Mock).mockRejectedValue(
|
|
new ProviderTokenInvalidError(
|
|
'Bokio does not know that company id',
|
|
'company-not-found',
|
|
),
|
|
)
|
|
|
|
const response = await submitTokenHandler(submitTokenRequest(), buildContext())
|
|
const { status, body } = await parseJsonResponse<{
|
|
error: { code: string; message: string; message_en?: string }
|
|
}>(response)
|
|
|
|
expect(status).toBe(422)
|
|
expect(body.error.code).toBe('BOKIO_COMPANY_NOT_FOUND')
|
|
expect(body.error.message).toContain('företags-ID')
|
|
expect(body.error.message_en).toContain('company ID')
|
|
})
|
|
|
|
it('keeps an unclassified provider/configuration failure generic', async () => {
|
|
;(submitProviderToken as Mock).mockRejectedValue(
|
|
new Error('Bokio company-information response is missing companyInformation'),
|
|
)
|
|
|
|
const response = await submitTokenHandler(submitTokenRequest(), buildContext())
|
|
const { status, body } = await parseJsonResponse<{
|
|
error: { code: string; message: string; message_en?: string }
|
|
}>(response)
|
|
|
|
expect(status).toBe(500)
|
|
expect(body.error.code).toBe('PROVIDER_TOKEN_SUBMIT_FAILED')
|
|
expect(body.error.message).toContain('kontrollera integrationsuppgifterna')
|
|
expect(body.error.message_en).toContain('verify the integration details')
|
|
})
|
|
})
|