33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
import { afterAll, describe, expect, it } from 'vitest';
|
|
import {
|
|
deleteObject,
|
|
getObject,
|
|
getPresignedUrl,
|
|
putObject,
|
|
verifyLocalSignature,
|
|
} from '../src/services/storage.js';
|
|
|
|
describe('local storage', () => {
|
|
const key = `tests/${Date.now()}.txt`;
|
|
|
|
afterAll(async () => {
|
|
await deleteObject(key);
|
|
});
|
|
|
|
it('stores an object and returns a one-hour signed URL', async () => {
|
|
await putObject(key, Buffer.from('hello'));
|
|
|
|
expect((await getObject(key)).toString()).toBe('hello');
|
|
|
|
const url = new URL(await getPresignedUrl(key), 'http://localhost');
|
|
const expires = url.searchParams.get('expires');
|
|
const signature = url.searchParams.get('sig');
|
|
|
|
expect(url.pathname).toContain(encodeURIComponent(key));
|
|
expect(Number(expires) - Date.now()).toBeGreaterThan(55 * 60 * 1000);
|
|
expect(Number(expires) - Date.now()).toBeLessThanOrEqual(60 * 60 * 1000);
|
|
expect(verifyLocalSignature(key, expires, signature)).toBe(true);
|
|
expect(verifyLocalSignature(key, String(Date.now() - 1000), signature)).toBe(false);
|
|
});
|
|
});
|