The minimalistic document archiving platform.
test(intake-emails): added e2e happy path test for email ingestion (#176)
Corentin Thomassetfc973d20fe94
diff --git a/apps/papra-server/package.json b/apps/papra-server/package.jsonindex b69c3c8d..960a5ada 100644--- a/apps/papra-server/package.json+++ b/apps/papra-server/package.json@@ -38,7 +38,7 @@"@hono/node-server": "^1.13.7","@libsql/client": "^0.14.0","@owlrelay/api-sdk": "^0.0.1",- "@owlrelay/webhook": "^0.0.2",+ "@owlrelay/webhook": "^0.0.3","@papra/lecture": "^0.0.4","@paralleldrive/cuid2": "^2.2.2","better-auth": "catalog:",diff --git a/apps/papra-server/src/modules/intake-emails/e2e/ingest-intake-emails.e2e.test.ts b/apps/papra-server/src/modules/intake-emails/e2e/ingest-intake-emails.e2e.test.tsindex f41f64d4..d09d54d1 100644--- a/apps/papra-server/src/modules/intake-emails/e2e/ingest-intake-emails.e2e.test.ts+++ b/apps/papra-server/src/modules/intake-emails/e2e/ingest-intake-emails.e2e.test.ts@@ -1,17 +1,11 @@+import { serializeEmailForWebhook, signBody } from '@owlrelay/webhook';+import { pick } from 'lodash-es';import { describe, expect, test } from 'vitest';import { createInMemoryDatabase } from '../../app/database/database.test-utils';import { createServer } from '../../app/server';import { overrideConfig } from '../../config/config.test-utils';--function buildBody({ attachments = [], from, to }: { attachments?: File[]; from: { address: string; name: string }; to: { address: string; name: string }[] }) {- const formData = new FormData();- formData.append('email', JSON.stringify({ from, to }));- attachments.forEach((attachment) => {- formData.append('attachments[]', attachment);- });-- return formData;-}+import { documentsTable } from '../../documents/documents.table';+import { ORGANIZATION_ROLES } from '../../organizations/organizations.constants';describe('intake-emails e2e', () => {describe('ingest an intake email', () => {@@ -26,12 +20,14 @@ describe('intake-emails e2e', () => {}),});+ const { body } = serializeEmailForWebhook({ email: {+ from: { address: '[email protected]', name: 'Foo' },+ to: [{ address: '[email protected]', name: 'Bar' }],+ } });+const response = await app.request('/api/intake-emails/ingest', {method: 'POST',- body: buildBody({- from: { address: '[email protected]', name: 'Foo' },- to: [{ address: '[email protected]', name: 'Bar' }],- }),+ body,});expect(response.status).to.eql(403);@@ -56,12 +52,14 @@ describe('intake-emails e2e', () => {}),});+ const { body } = serializeEmailForWebhook({ email: {+ from: { address: '[email protected]', name: 'Foo' },+ to: [{ address: '[email protected]', name: 'Bar' }],+ } });+const response = await app.request('/api/intake-emails/ingest', {method: 'POST',- body: buildBody({- from: { address: '[email protected]', name: 'Foo' },- to: [{ address: '[email protected]', name: 'Bar' }],- }),+ body,});expect(response.status).to.eql(400);@@ -85,15 +83,17 @@ describe('intake-emails e2e', () => {}),});+ const { body } = serializeEmailForWebhook({ email: {+ from: { address: '[email protected]', name: 'Foo' },+ to: [{ address: '[email protected]', name: 'Bar' }],+ } });+const response = await app.request('/api/intake-emails/ingest', {method: 'POST',headers: {'X-Signature': 'invalid',},- body: buildBody({- from: { address: '[email protected]', name: 'Foo' },- to: [{ address: '[email protected]', name: 'Bar' }],- }),+ body,});expect(response.status).to.eql(401);@@ -105,5 +105,64 @@ describe('intake-emails e2e', () => {});});});++ test('when the ingestion is enabled and the request signature is valid, the email attachments are added to the organization', async () => {+ const { db } = await createInMemoryDatabase({+ users: [{ id: 'user_1', email: '[email protected]' }],+ organizations: [{ id: 'org_1', name: 'Organization 1' }],+ organizationMembers: [{ id: 'org_member_1', organizationId: 'org_1', userId: 'user_1', role: ORGANIZATION_ROLES.OWNER }],+ intakeEmails: [{ id: 'ie_1', organizationId: 'org_1', emailAddress: '[email protected]', allowedOrigins: ['[email protected]'] }],+ });++ const { app } = createServer({+ db,+ config: overrideConfig({+ intakeEmails: { isEnabled: true, webhookSecret: 'super-secret' },+ }),+ });++ const { body } = serializeEmailForWebhook({ email: {+ from: { address: '[email protected]', name: 'Foo' },+ to: [{ address: '[email protected]', name: 'Bar' }],+ attachments: [{ filename: 'test.txt', mimeType: 'text/plain', content: 'hello world' }],++ // unused fields, but very likely to be present in the payload+ subject: 'Hello world',+ cc: [{ address: '[email protected]', name: 'Cc' }],+ html: '<p>Hello world</p>',+ text: 'Hello world',+ } });+ const bodyResponse = new Response(body);+ const headers = Object.fromEntries(bodyResponse.headers.entries());+ const bodyArrayBuffer = await bodyResponse.arrayBuffer();+ const { signature } = await signBody({ bodyBuffer: bodyArrayBuffer, secret: 'super-secret' });++ headers['X-Signature'] = signature;++ const response = await app.request('/api/intake-emails/ingest', {+ method: 'POST',+ headers,+ body: bodyArrayBuffer,+ });++ expect(response.status).to.eql(202);++ const documents = await db.select().from(documentsTable);++ expect(documents).to.have.length(1);++ const [document] = documents;++ expect(+ pick(document, ['organizationId', 'createdBy', 'mimeType', 'originalName', 'originalSize', 'content']),+ ).to.eql({+ organizationId: 'org_1',+ createdBy: null,+ mimeType: 'text/plain',+ originalName: 'test.txt',+ originalSize: 11,+ content: 'hello world',+ });+ });});});diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.routes.tsindex 689376d6..5b0bc877 100644--- a/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.routes.ts@@ -146,7 +146,7 @@ function setupIngestIntakeEmailRoute({ app, db, config }: RouteDefinitionContextapp.post(INTAKE_EMAILS_INGEST_ROUTE,validateFormData(z.object({- // meta is a JSON string, but it can also be parsed as an object in case of multipart/form-data json section+ // email field is a JSON string'email': z.string().transform(parseJson).pipe(intakeEmailsIngestionMetaSchema),'attachments[]': z.array(z.instanceof(File)).min(1, 'At least one attachment is required').optional(),}), { allowAdditionalFields: true }),diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 8cf95e02..0a80c7ab 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -236,8 +236,8 @@ importers:specifier: ^0.0.1version: 0.0.1'@owlrelay/webhook':- specifier: ^0.0.2- version: 0.0.2+ specifier: ^0.0.3+ version: 0.0.3'@papra/lecture':specifier: ^0.0.4version: 0.0.4@@ -624,13 +624,13 @@ packages:resolution: {integrity: sha512-tbhNuIxNcVb21pInl3ZSjksLCvgdZy9KwJ8brv993QtIVKJBBkYXz4q4ZbAv31GdnC+R90np23L5FbEBlthAEw==}engines: {node: '>=6.9.0'}- '@babel/[email protected]':- resolution: {integrity: sha512-6aQR2zGE/QFi8JpDLjUZEPYOs7+mhKXm86VaKFiLP35JQwQb6bwUE+XbvkH0EptsYhbNBSUGaUBLKqxH1xSgsA==}+ '@babel/[email protected]':+ resolution: {integrity: sha512-WJ/CvmY8Mea8iDXo6a7RK2wbmJITT5fN3BEkRuFlxVyNx8jOKIIhmC4fSkTcPcf8JyavbBwIe6OpiCOBXt/IcA==}engines: {node: '>=6.0.0'}hasBin: true- '@babel/[email protected]':- resolution: {integrity: sha512-WJ/CvmY8Mea8iDXo6a7RK2wbmJITT5fN3BEkRuFlxVyNx8jOKIIhmC4fSkTcPcf8JyavbBwIe6OpiCOBXt/IcA==}+ '@babel/[email protected]':+ resolution: {integrity: sha512-iaepho73/2Pz7w2eMS0Q5f83+0RKI7i4xmiYeBmDzfRVbQtTOG7Ts0S4HzJVsTMGI9keU8rNfuZr8DKfSt7Yyg==}engines: {node: '>=6.0.0'}hasBin: true@@ -652,14 +652,14 @@ packages:resolution: {integrity: sha512-fH+b7Y4p3yqvApJALCPJcwb0/XaOSgtK4pzV6WVjPR5GLFQBRI7pfoX2V2iM48NXvX07NUxxm1Vw98YjqTcU5w==}engines: {node: '>=6.9.0'}- '@babel/[email protected]':- resolution: {integrity: sha512-emqcG3vHrpxUKTrxcblR36dcrcoRDvKmnL/dCL6ZsHaShW80qxCAcNhzQZrpeM765VzEos+xOi4s+r4IXzTwdQ==}- engines: {node: '>=6.9.0'}-'@babel/[email protected]':resolution: {integrity: sha512-vN5p+1kl59GVKMvTHt55NzzmYVxprfJD+ql7U9NFIfKCBkYE55LYtS+WtPlaYOyzydrKI8Nezd+aZextrd+FMA==}engines: {node: '>=6.9.0'}+ '@babel/[email protected]':+ resolution: {integrity: sha512-H45s8fVLYjbhFH62dIJ3WtmJ6RSPt/3DRO0ZcT2SUiYiQyz3BLVb9ADEnLl91m74aQPS3AzzeajZHYOalWe3bg==}+ engines: {node: '>=6.9.0'}+'@bcoe/[email protected]':resolution: {integrity: sha512-6zABk/ECA/QYSCQ1NGiVwwbQerUCZ+TQbp64Q3AgmfNvurHH0j8TtXa1qbShXA6qqkpAj4V5W8pP6mLe1mcMqA==}engines: {node: '>=18'}@@ -2090,8 +2090,8 @@ packages:resolution: {integrity: sha512-/4x/J4ktb9z3Zf3VfX7tjQYYqgn8bMXk7hbYePoGknijk90QRjsfsVjpPjl2fgUs+ZGFeOQqdsgLmpxq2d1NGA==}engines: {node: '>=20.0.0'}- '@owlrelay/[email protected]':- resolution: {integrity: sha512-SmZVHMbkyWgXqXns/ZbnMeK2DnlgfXixeROPzyV75SX6oFpKtL95x3xwKefbwX4EGmFUOSx9dNHfCionTLYyEw==}+ '@owlrelay/[email protected]':+ resolution: {integrity: sha512-LoaRGBRg7c/VSwumRDa62Lo46nf99jCWcxFqIqSAimVgNk5F6dHlR2RAlOXo3Tub/c9FViyudb0+15c9SP7vsA==}engines: {node: '>=20.0.0'}'@pagefind/[email protected]':@@ -5168,6 +5168,9 @@ packages:resolution: {integrity: sha512-p1TRH/edngVEHVbwqWnxUViEmq5znDvyB+Sik5cmuLpGOIfDf/39zLiq3swPF8Vakqn+gvNiOQAZu8djYlQILA==}+ resolution: {integrity: sha512-/ieB+mDe4MrrKMT8z+mQL8klXydZWGR5Dowt4RAGKbJ3kIGEx3X4ljUo+6V73IXtUPWgfOlU5B9MlGxFO5T+cA==}+resolution: {integrity: sha512-X0SNNrZiGU8/e/zAB7sCTtdxWTMSIO73q+xuKgglm2Yvzwlo8UoC5FNySQFCvl84uPaeADkqHUZUkWy4aH4xOA==}engines: {node: ^14.16.0 || >=16.10.0}@@ -6033,6 +6036,12 @@ packages:peerDependencies:typescript: '>=4.8.4'+ resolution: {integrity: sha512-CUgTZL1irw8u29bzrOD/nH85jqyc74D6SshFgujOIA7osm2Rz7dYH77agkx7H4FBNxDq7Cjf+IjaX/8zwFW+ZQ==}+ engines: {node: '>=18.12'}+ peerDependencies:+ typescript: '>=4.8.4'+resolution: {integrity: sha512-d4IxJUXROL5NCa3amvMg6VQW2HVtZYmUTPfvVtO7zJWGYLJ+mry9v2OmYm+z67aniQoQ8/yFNadiEwtNS9qQiw==}@@ -7431,14 +7440,14 @@ snapshots:'@babel/template': 7.25.9'@babel/types': 7.26.3- '@babel/[email protected]':- dependencies:- '@babel/types': 7.26.10-'@babel/[email protected]':dependencies:'@babel/types': 7.26.3+ '@babel/[email protected]':+ dependencies:+ '@babel/types': 7.27.0+'@babel/[email protected](@babel/[email protected])':dependencies:'@babel/core': 7.26.0@@ -7466,12 +7475,12 @@ snapshots:transitivePeerDependencies:- supports-color- '@babel/[email protected]':+ '@babel/[email protected]':dependencies:'@babel/helper-string-parser': 7.25.9'@babel/helper-validator-identifier': 7.25.9- '@babel/[email protected]':+ '@babel/[email protected]':dependencies:'@babel/helper-string-parser': 7.25.9'@babel/helper-validator-identifier': 7.25.9@@ -8538,7 +8547,7 @@ snapshots:dependencies:ofetch: 1.4.1- '@owlrelay/[email protected]':+ '@owlrelay/[email protected]':dependencies:ofetch: 1.4.1@@ -9520,7 +9529,7 @@ snapshots:is-glob: 4.0.3minimatch: 9.0.5semver: 7.7.1- ts-api-utils: 2.0.1([email protected])+ ts-api-utils: 2.1.0([email protected])typescript: 5.7.3transitivePeerDependencies:- supports-color@@ -9534,7 +9543,7 @@ snapshots:is-glob: 4.0.3minimatch: 9.0.5semver: 7.7.1- ts-api-utils: 2.0.1([email protected])+ ts-api-utils: 2.1.0([email protected])typescript: 5.8.2transitivePeerDependencies:- supports-color@@ -9895,7 +9904,7 @@ snapshots:'@vue/[email protected]':dependencies:- '@babel/parser': 7.26.10+ '@babel/parser': 7.27.0'@vue/shared': 3.5.13entities: 4.5.0estree-walker: 2.0.2@@ -9908,7 +9917,7 @@ snapshots:'@vue/[email protected]':dependencies:- '@babel/parser': 7.26.10+ '@babel/parser': 7.27.0'@vue/compiler-core': 3.5.13'@vue/compiler-dom': 3.5.13'@vue/compiler-ssr': 3.5.13@@ -12246,7 +12255,7 @@ snapshots:http-proxy-agent: 7.0.2https-proxy-agent: 7.0.6is-potential-custom-element-name: 1.0.1- nwsapi: 2.2.18+ nwsapi: 2.2.20parse5: 7.2.1rrweb-cssom: 0.8.0saxes: 6.0.0@@ -13057,6 +13066,9 @@ snapshots:+ optional: true+dependencies:citty: 0.1.6@@ -14111,6 +14123,14 @@ snapshots:dependencies:typescript: 5.8.2+ dependencies:+ typescript: 5.7.3++ dependencies:+ typescript: 5.8.2+