* feat: import Fortnox voucher attachments * fix: show Fortnox document import follow-up * fix: harden optional Fortnox document import * test: pin optional Fortnox import flow * fix: use browser timer handle type * fix: avoid serializing OAuth resume state
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { FortnoxApiError, FortnoxClient } from '../client';
|
|
|
|
describe('FortnoxClient.getBinary', () => {
|
|
beforeEach(() => {
|
|
vi.stubEnv('UPSTASH_REDIS_REST_URL', '');
|
|
vi.stubEnv('UPSTASH_REDIS_REST_TOKEN', '');
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
it('returns the raw bytes and response content type', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(
|
|
new Response(Uint8Array.from([1, 2, 3]), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/pdf' },
|
|
}),
|
|
);
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
const client = new FortnoxClient('https://fortnox.example.test/3');
|
|
|
|
const result = await client.getBinary('access-token', '/archive/file-1');
|
|
|
|
expect(Array.from(new Uint8Array(result.bytes))).toEqual([1, 2, 3]);
|
|
expect(result.contentType).toBe('application/pdf');
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
'https://fortnox.example.test/3/archive/file-1',
|
|
expect.objectContaining({
|
|
headers: { Authorization: 'Bearer access-token' },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('preserves the HTTP status on binary request failures', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn().mockResolvedValue(
|
|
new Response('missing', { status: 404, statusText: 'Not Found' }),
|
|
),
|
|
);
|
|
const client = new FortnoxClient('https://fortnox.example.test/3');
|
|
|
|
const error = await client
|
|
.getBinary('access-token', '/archive/file-1')
|
|
.catch((caught: unknown) => caught);
|
|
|
|
expect(error).toBeInstanceOf(FortnoxApiError);
|
|
expect(error).toMatchObject({ statusCode: 404, body: 'missing' });
|
|
});
|
|
});
|