mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
fix(mail-ingest): refuse an upload while the person's import is off, and start no run in tests
An upload made an address and stored a mail the pipeline then put aside; it now answers 409 instead. The run an upload asks for at once starts only where the crons run, so it can no longer outlive a test's database.
This commit is contained in:
@@ -8,9 +8,7 @@ import type {
|
||||
import type { MailIngestMessagesRepository } from '../../db/repositories/MailIngestMessages.repository';
|
||||
import { utcSuffix } from '../../db/types';
|
||||
import { decodeJson } from '../../utils/json-column';
|
||||
import { logError } from '../audit/audit-log.logger';
|
||||
import { DomainError } from '../common/domain-error';
|
||||
import { withRequestContext } from '../database/request-context';
|
||||
import { MailReadService } from '../llm-parse/mail-read.service';
|
||||
import { MailIngestAddressService } from './mail-ingest-address.service';
|
||||
import { MailIngestIntakeService, type MailIntakeRefusal } from './mail-ingest-intake.service';
|
||||
@@ -18,7 +16,6 @@ import { MailIngestProcessJob } from './mail-ingest-process.job';
|
||||
import { MailIngestSettingsService } from './mail-ingest-settings.service';
|
||||
import { candidateView, messageView } from './mail-ingest-views';
|
||||
import { openMail } from './mail-seal';
|
||||
import { MikroORM } from '@mikro-orm/core';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import type {
|
||||
@@ -60,7 +57,6 @@ export class MailIngestInboxService {
|
||||
private readonly intake: MailIngestIntakeService,
|
||||
private readonly reader: MailReadService,
|
||||
private readonly job: MailIngestProcessJob,
|
||||
private readonly orm: MikroORM,
|
||||
) {}
|
||||
|
||||
/** The settings card. Where the import is off or not part of the plan it says so, and nothing else. */
|
||||
@@ -153,12 +149,17 @@ export class MailIngestInboxService {
|
||||
|
||||
/**
|
||||
* An .eml the person uploaded, read as if they had forwarded it to their
|
||||
* own address. The pipeline is asked to run at once rather than at its next
|
||||
* tick, so the inbox fills while the person still looks at it.
|
||||
* own address. A person who switched their import off uploads nothing: the
|
||||
* pipeline would only put the mail aside. The pipeline is asked to run at
|
||||
* once rather than at its next tick, so the inbox fills while the person
|
||||
* still looks at it.
|
||||
*
|
||||
* @txIndependent the mail is stored by itself; its reading runs on its own afterwards.
|
||||
*/
|
||||
async upload(userId: number, raw: Buffer): Promise<MailIngestUploadResponse> {
|
||||
if ((await this.settings.isAvailable()) && !(await this.settings.prefsOf(userId)).enabled) {
|
||||
throw new DomainError(409, 'Your mail import is switched off');
|
||||
}
|
||||
const result = await this.intake.upload(raw, userId);
|
||||
if (result.status === 'refused') {
|
||||
const [status, error] = UPLOAD_REFUSALS[result.reason];
|
||||
@@ -166,16 +167,10 @@ export class MailIngestInboxService {
|
||||
}
|
||||
const [head] = await this.messages.listHeads([result.messageId]);
|
||||
if (!head) throw new Error(`mail ${result.messageId} vanished after its upload`);
|
||||
if (result.status === 'accepted') this.processSoon();
|
||||
if (result.status === 'accepted') this.job.runSoon();
|
||||
return { message: messageView(head), duplicate: result.status === 'duplicate' };
|
||||
}
|
||||
|
||||
private processSoon(): void {
|
||||
withRequestContext(this.orm, () => this.job.tick()).catch((err: unknown) => {
|
||||
logError(`[mail-ingest] Reading an uploaded mail failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
});
|
||||
}
|
||||
|
||||
private async requireAvailable(): Promise<void> {
|
||||
if (!(await this.settings.isAvailable())) throw new DomainError(404, 'Mail import is not available');
|
||||
}
|
||||
|
||||
@@ -68,4 +68,16 @@ export class MailIngestProcessJob implements OnApplicationBootstrap {
|
||||
await this.cursor.releaseLease('pipeline', '', LEASE_OWNER);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a run now instead of at the next tick, without waiting for it.
|
||||
* Where the crons are off (the test suites) nothing starts, as no tick
|
||||
* would either, so a run never outlives the request that asked for it.
|
||||
*/
|
||||
runSoon(): void {
|
||||
if (!this.registrar.isEnabled()) return;
|
||||
withRequestContext(this.orm, () => this.tick()).catch((err: unknown) => {
|
||||
logError(`[mail-ingest] A run asked for at once failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,16 +174,15 @@ describe('/api/mail-ingest e2e (real guards + snapshot SQLite)', () => {
|
||||
});
|
||||
|
||||
it('MAIL-E2E-003: an uploaded mail waits in the inbox, is added by hand and undone, for its person', async () => {
|
||||
const job = app.get(MailIngestProcessJob);
|
||||
const tick = vi.spyOn(job, 'tick').mockResolvedValueOnce(0);
|
||||
const soon = vi.spyOn(app.get(MailIngestProcessJob), 'runSoon');
|
||||
const upload = await request(server)
|
||||
.post('/api/mail-ingest/upload')
|
||||
.set(as(USER))
|
||||
.attach('file', eml('upload-1'), { filename: 'booking.eml', contentType: 'message/rfc822' });
|
||||
expect(upload.status).toBe(201);
|
||||
expect(upload.body).toMatchObject({ duplicate: false, message: { transport: 'upload', status: 'queued' } });
|
||||
expect(tick).toHaveBeenCalledTimes(1);
|
||||
tick.mockRestore();
|
||||
expect(soon).toHaveBeenCalledTimes(1);
|
||||
soon.mockRestore();
|
||||
expect(await readQueued()).toBe(1);
|
||||
|
||||
const inbox = await request(server).get('/api/mail-ingest/inbox').set(as(USER));
|
||||
|
||||
@@ -518,19 +518,29 @@ describe('what the person sees', () => {
|
||||
|
||||
it('reads an uploaded mail at once, knows one it has, and refuses what it cannot take', async () => {
|
||||
const user = await person();
|
||||
// The reading the upload starts runs on its own; held here, so it cannot outlive the case.
|
||||
const tick = vi.spyOn(job(), 'tick').mockResolvedValue(0);
|
||||
const soon = vi.spyOn(job(), 'runSoon');
|
||||
const tick = vi.spyOn(job(), 'tick');
|
||||
try {
|
||||
const first = await inbox().upload(user.id, eml('upload'));
|
||||
expect(first).toMatchObject({ duplicate: false, message: { transport: 'upload', status: 'queued' } });
|
||||
const again = await inbox().upload(user.id, eml('upload'));
|
||||
expect(again).toMatchObject({ duplicate: true, message: { id: first.message.id } });
|
||||
expect(tick).toHaveBeenCalledTimes(1);
|
||||
expect(soon).toHaveBeenCalledTimes(1);
|
||||
// The crons are off in the suites, so the run asked for at once never starts and cannot outlive the case.
|
||||
expect(tick).not.toHaveBeenCalled();
|
||||
await expect(inbox().upload(user.id, Buffer.from('nothing to see'))).rejects.toThrow('This file is not a mail');
|
||||
} finally {
|
||||
soon.mockRestore();
|
||||
tick.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('takes no upload from a person who switched their import off', async () => {
|
||||
const user = await person();
|
||||
await t.get(MailIngestSettingsService).updatePrefs(user.id, { enabled: false });
|
||||
await expect(inbox().upload(user.id, eml('off'))).rejects.toThrow('Your mail import is switched off');
|
||||
expect(await findRow(orm(), MailIngestMessages, { user: user.id })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('the retention', () => {
|
||||
|
||||
Reference in New Issue
Block a user