Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

chore(server): add integration tests for S3 storage providers (#1181)

Corentin Thomassetb65e728e0c83
diff --git a/.gitignore b/.gitignore
index 5ad57319..4b285bf4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,6 +25,7 @@ logs
.env
.env.*
!.env.example
+!.env.*.example
.wrangler
coverage
@@ -46,4 +47,4 @@ ingestion
.eslintcache
.claude
.opencode
-AGENTS.md
\ No newline at end of file
+AGENTS.md
diff --git a/apps/papra-server/.env.test.example b/apps/papra-server/.env.test.example
new file mode 100644
index 00000000..d492db6b
--- /dev/null
+++ b/apps/papra-server/.env.test.example
@@ -0,0 +1,9 @@
+TEST_R2_ENDPOINT=
+TEST_R2_BUCKET_NAME=
+TEST_R2_ACCESS_KEY_ID=
+TEST_R2_SECRET_ACCESS_KEY=
+
+TEST_B2_ENDPOINT=
+TEST_B2_BUCKET_NAME=
+TEST_B2_ACCESS_KEY_ID=
+TEST_B2_SECRET_ACCESS_KEY=
diff --git a/apps/papra-server/src/modules/documents/storage/drivers/drivers.test-suite.ts b/apps/papra-server/src/modules/documents/storage/drivers/drivers.test-suite.ts
index 010dc8e5..00aaef2c 100644
--- a/apps/papra-server/src/modules/documents/storage/drivers/drivers.test-suite.ts
+++ b/apps/papra-server/src/modules/documents/storage/drivers/drivers.test-suite.ts
@@ -1,5 +1,6 @@
import type { StorageDriver, StorageServices } from './drivers.models';
import { Buffer } from 'node:buffer';
+import { randomUUID } from 'node:crypto';
import { describe, expect, test } from 'vitest';
import {
collectReadableStreamToString,
@@ -76,32 +77,36 @@ export function runDriverTestSuites({
const { storageServices } = resource;
+ // Use a unique key per run so suites that share a real backend (e.g. a live R2/B2 bucket)
+ // don't collide. With isolated localstack containers this just adds harmless entropy.
+ const storageKey = `files/${randomUUID()}.txt`;
+
// Save the file
const storageContext = await storageServices.saveFile({
fileName: 'test.txt',
mimeType: 'text/plain',
- storageKey: 'files/test.txt',
+ storageKey,
fileStream: createReadableStream({ content: 'Hello, world!' }),
});
// Retrieve the file
const { fileStream } = await storageServices.getFileStream({
...storageContext,
- storageKey: 'files/test.txt',
+ storageKey,
});
expect(await collectReadableStreamToString({ stream: fileStream })).to.eql(
'Hello, world!',
);
// Check that the file exists
- expect(await storageServices.fileExists({ storageKey: 'files/test.txt' })).to.eql(true);
+ expect(await storageServices.fileExists({ storageKey })).to.eql(true);
// Try to save another file with the same storage key and expect an error
await expect(
storageServices.saveFile({
fileName: 'test.txt',
mimeType: 'text/plain',
- storageKey: 'files/test.txt',
+ storageKey,
fileStream: createReadableStream({ content: 'Lorem ipsum' }),
}),
).rejects.toThrow(createFileAlreadyExistsInStorageError());
@@ -109,25 +114,25 @@ export function runDriverTestSuites({
// Ensure that the original file is still intact after the failed attempt to overwrite it
const { fileStream: fileStreamAfterError } = await storageServices.getFileStream({
...storageContext,
- storageKey: 'files/test.txt',
+ storageKey,
});
expect(await collectReadableStreamToString({ stream: fileStreamAfterError })).to.eql(
'Hello, world!',
);
// Delete the file
- await storageServices.deleteFile({ storageKey: 'files/test.txt' });
- await expect(
- storageServices.getFileStream({ storageKey: 'files/test.txt' }),
- ).rejects.toThrow(createFileNotFoundError());
+ await storageServices.deleteFile({ storageKey });
+ await expect(storageServices.getFileStream({ storageKey })).rejects.toThrow(
+ createFileNotFoundError(),
+ );
// Check that the file no longer exists
- expect(await storageServices.fileExists({ storageKey: 'files/test.txt' })).to.eql(false);
+ expect(await storageServices.fileExists({ storageKey })).to.eql(false);
// Try to delete the file again
- await expect(
- storageServices.deleteFile({ storageKey: 'files/test.txt' }),
- ).rejects.toThrow(createFileNotFoundError());
+ await expect(storageServices.deleteFile({ storageKey })).rejects.toThrow(
+ createFileNotFoundError(),
+ );
},
);
});
diff --git a/apps/papra-server/src/modules/documents/storage/drivers/s3/integrations-tests/b2.storage-driver.int.test.ts b/apps/papra-server/src/modules/documents/storage/drivers/s3/integrations-tests/b2.storage-driver.int.test.ts
new file mode 100644
index 00000000..7332380c
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/storage/drivers/s3/integrations-tests/b2.storage-driver.int.test.ts
@@ -0,0 +1,37 @@
+import type { DocumentStorageConfig } from '../../../documents.storage.types';
+import { describe } from 'vitest';
+import { runDriverTestSuites } from '../../drivers.test-suite';
+import { s3StorageDriverFactory } from '../s3.storage-driver';
+
+const endpoint = process.env.TEST_B2_ENDPOINT;
+const bucketName = process.env.TEST_B2_BUCKET_NAME;
+const accessKeyId = process.env.TEST_B2_ACCESS_KEY_ID;
+const secretAccessKey = process.env.TEST_B2_SECRET_ACCESS_KEY;
+
+describe('s3 storage-driver', () => {
+ describe.skipIf(!endpoint)('b2 bucket', () => {
+ runDriverTestSuites({
+ createDriver: async () => {
+ const driver = s3StorageDriverFactory({
+ documentStorageConfig: {
+ drivers: {
+ s3: {
+ accessKeyId,
+ secretAccessKey,
+ bucketName,
+ region: 'auto',
+ endpoint,
+ forcePathStyle: true,
+ },
+ },
+ } as DocumentStorageConfig,
+ });
+
+ return {
+ driver,
+ [Symbol.asyncDispose]: async () => {},
+ };
+ },
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/documents/storage/drivers/s3/integrations-tests/r2.storage-driver.int.test.ts b/apps/papra-server/src/modules/documents/storage/drivers/s3/integrations-tests/r2.storage-driver.int.test.ts
new file mode 100644
index 00000000..c0062dae
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/storage/drivers/s3/integrations-tests/r2.storage-driver.int.test.ts
@@ -0,0 +1,37 @@
+import type { DocumentStorageConfig } from '../../../documents.storage.types';
+import { describe } from 'vitest';
+import { runDriverTestSuites } from '../../drivers.test-suite';
+import { s3StorageDriverFactory } from '../s3.storage-driver';
+
+const endpoint = process.env.TEST_R2_ENDPOINT;
+const bucketName = process.env.TEST_R2_BUCKET_NAME;
+const accessKeyId = process.env.TEST_R2_ACCESS_KEY_ID;
+const secretAccessKey = process.env.TEST_R2_SECRET_ACCESS_KEY;
+
+describe('s3 storage-driver', () => {
+ describe.skipIf(!endpoint)('r2 bucket', () => {
+ runDriverTestSuites({
+ createDriver: async () => {
+ const driver = s3StorageDriverFactory({
+ documentStorageConfig: {
+ drivers: {
+ s3: {
+ accessKeyId,
+ secretAccessKey,
+ bucketName,
+ region: 'auto',
+ endpoint,
+ forcePathStyle: true,
+ },
+ },
+ } as DocumentStorageConfig,
+ });
+
+ return {
+ driver,
+ [Symbol.asyncDispose]: async () => {},
+ };
+ },
+ });
+ });
+});
diff --git a/apps/papra-server/vitest.config.ts b/apps/papra-server/vitest.config.ts
index 0c241c1b..b819aaa7 100644
--- a/apps/papra-server/vitest.config.ts
+++ b/apps/papra-server/vitest.config.ts
@@ -1,10 +1,14 @@
import { defineConfig } from 'vitest/config';
+import { loadEnv } from 'vite';
+
+const loadedEnv = loadEnv('test', process.cwd(), 'TEST_');
export default defineConfig({
test: {
isolate: false,
env: {
TZ: 'UTC',
+ ...loadedEnv,
},
},
});