Archived
邮件路由 15 条:账号 CRUD / 探测 / 立即拉取 / 试发 / 入站出站 / 附件原件 / 重新投递;领域错误翻状态码,厂商失败在 body 里说
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TM4kUBvekALu9qqmp7D3wr
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* 邮件路由:zod 校验 → 用例 → 领域错误翻状态码;厂商失败在 200 的 body 里如实说。
|
||||
* 不挂鉴权中间件(归属由路由覆盖测试与注册表测试兜);用例是真的,store 是内存的,provider 是假的。
|
||||
*/
|
||||
import { Hono } from 'hono';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createMailService, createPostalMimeParser } from '@modules/mail';
|
||||
import type { AppEnv } from '../http-env.ts';
|
||||
import { registerMailRoutes } from '../routes/mail.ts';
|
||||
import { account, eml, fakeProvider, fixedClock, memoryAccounts, memoryAttachments, memoryInbound, memoryOutbound, plainSecrets, rawMessage, seqIds } from '../../modules/mail/__tests__/fakes.ts';
|
||||
|
||||
function harness() {
|
||||
const clock = fixedClock();
|
||||
const accounts = memoryAccounts(clock);
|
||||
const imap = fakeProvider('imap-smtp');
|
||||
const mail = createMailService({
|
||||
accounts,
|
||||
inbound: memoryInbound(),
|
||||
outbound: memoryOutbound(),
|
||||
attachments: memoryAttachments(),
|
||||
providers: { 'imap-smtp': imap, wecom: fakeProvider('wecom') },
|
||||
mime: createPostalMimeParser(),
|
||||
secrets: plainSecrets,
|
||||
clock,
|
||||
ids: seqIds('x'),
|
||||
notify: { changed: () => undefined },
|
||||
});
|
||||
const app = new Hono<AppEnv>();
|
||||
registerMailRoutes(app, { mail });
|
||||
const json = (method: string, path: string, body?: unknown) =>
|
||||
app.request(path, { method, headers: { 'content-type': 'application/json' }, ...(body === undefined ? {} : { body: JSON.stringify(body) }) });
|
||||
return { app, mail, accounts, imap, json };
|
||||
}
|
||||
const input = { kind: 'imap-smtp', name: '销售', address: 'sales@we.com', isDefault: true, secret: { password: 'pw-1234' } };
|
||||
|
||||
describe('账号路由', () => {
|
||||
it('列表 / 建 / 取 / 改 / 删;400 / 404 / 409', async () => {
|
||||
const h = harness();
|
||||
expect(await (await h.json('GET', '/api/gov/mail/accounts')).json()).toEqual({ accounts: [] });
|
||||
expect((await h.json('POST', '/api/gov/mail/accounts', { kind: 'imap-smtp' })).status).toBe(400);
|
||||
const created = await h.json('POST', '/api/gov/mail/accounts', input);
|
||||
expect(created.status).toBe(200);
|
||||
const view = (await created.json()) as { id: string; secretBound: boolean };
|
||||
expect(view.secretBound).toBe(true);
|
||||
expect(JSON.stringify(view)).not.toContain('pw-1234');
|
||||
expect((await h.json('POST', '/api/gov/mail/accounts', input)).status).toBe(409);
|
||||
expect((await h.json('GET', `/api/gov/mail/accounts/${view.id}`)).status).toBe(200);
|
||||
expect((await h.json('GET', '/api/gov/mail/accounts/nope')).status).toBe(404);
|
||||
expect((await h.json('PUT', `/api/gov/mail/accounts/${view.id}`, { ...input, displayName: '销售部' })).status).toBe(200);
|
||||
expect(await (await h.json('DELETE', `/api/gov/mail/accounts/${view.id}`)).json()).toEqual({ ok: true });
|
||||
expect((await h.json('DELETE', `/api/gov/mail/accounts/${view.id}`)).status).toBe(404);
|
||||
});
|
||||
|
||||
it('探测:草稿没密码 412;已存账号 200 且厂商失败在 body 里', async () => {
|
||||
const h = harness();
|
||||
expect((await h.json('POST', '/api/gov/mail/accounts/probe', { ...input, secret: undefined })).status).toBe(412);
|
||||
const view = (await (await h.json('POST', '/api/gov/mail/accounts', input)).json()) as { id: string };
|
||||
expect((await h.json('POST', '/api/gov/mail/accounts/probe?accountId=' + view.id, { ...input, secret: undefined })).status).toBe(200);
|
||||
h.imap.probeWith({ kind: 'imap-smtp', receive: { ok: false, error: '535 pw-1234 bad' }, send: { checked: true, ok: true } });
|
||||
const res = await h.json('POST', `/api/gov/mail/accounts/${view.id}/probe`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.json()).toMatchObject({ receive: { ok: false, error: '535 *** bad' } });
|
||||
});
|
||||
|
||||
it('立即拉取:200 outcome;停用 412;租约被占 409', async () => {
|
||||
const h = harness();
|
||||
await h.accounts.save(account());
|
||||
h.imap.queue(rawMessage('uid:v1:1', eml({ messageId: '<m1@a>' })));
|
||||
expect(await (await h.json('POST', '/api/gov/mail/accounts/acc-1/poll')).json()).toMatchObject({ pulled: 1, stored: 1 });
|
||||
await h.accounts.save(account({ enabled: false }));
|
||||
expect((await h.json('POST', '/api/gov/mail/accounts/acc-1/poll')).status).toBe(412);
|
||||
await h.accounts.save(account({ pollingUntil: new Date('2027-01-01T00:00:00Z') }));
|
||||
expect((await h.json('POST', '/api/gov/mail/accounts/acc-1/poll')).status).toBe(409);
|
||||
});
|
||||
|
||||
it('试发:成功与厂商失败都是 200,state 说话;入参错 400', async () => {
|
||||
const h = harness();
|
||||
await h.accounts.save(account());
|
||||
const ok = await h.json('POST', '/api/gov/mail/accounts/acc-1/send-test', { to: 'me@we.com', subject: '试发', text: '正文' });
|
||||
expect(await ok.json()).toMatchObject({ state: 'sent', messageId: '<x-1@we.com>' });
|
||||
h.imap.failSendWith(new Error('535 nope'));
|
||||
expect(await (await h.json('POST', '/api/gov/mail/accounts/acc-1/send-test', { to: 'me@we.com', subject: '试发', text: '正文' })).json()).toMatchObject({ state: 'failed', error: '535 nope' });
|
||||
expect((await h.json('POST', '/api/gov/mail/accounts/acc-1/send-test', { to: 'not-mail', subject: '', text: '' })).status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('入站 / 出站路由', () => {
|
||||
it('列表(limit)/ 详情 / 附件 / 原件 / 重新投递;404 各就各位', async () => {
|
||||
const h = harness();
|
||||
await h.accounts.save(account());
|
||||
h.mail.subscribe('spy', async () => undefined);
|
||||
h.imap.queue(rawMessage('uid:v1:1', eml({ messageId: '<m1@a>', attachment: { name: 'a.txt', contentType: 'text/plain', content: 'hi' } })));
|
||||
await h.json('POST', '/api/gov/mail/accounts/acc-1/poll');
|
||||
const list = (await (await h.json('GET', '/api/gov/mail/inbound?accountId=acc-1&limit=10')).json()) as { mails: Array<{ id: string; deliveries: unknown[] }> };
|
||||
expect(list.mails).toHaveLength(1);
|
||||
expect(list.mails[0]?.deliveries).toHaveLength(1);
|
||||
const id = list.mails[0]!.id;
|
||||
expect((await h.json('GET', `/api/gov/mail/inbound/${id}`)).status).toBe(200);
|
||||
expect((await h.json('GET', '/api/gov/mail/inbound/nope')).status).toBe(404);
|
||||
const att = (await (await h.json('GET', `/api/gov/mail/inbound/${id}/attachments/0`)).json()) as { name: string; dataBase64: string };
|
||||
expect(att.name).toBe('a.txt');
|
||||
expect(Buffer.from(att.dataBase64, 'base64').toString()).toBe('hi');
|
||||
expect((await h.json('GET', `/api/gov/mail/inbound/${id}/attachments/7`)).status).toBe(404);
|
||||
const raw = (await (await h.json('GET', `/api/gov/mail/inbound/${id}/raw`)).json()) as { contentType: string };
|
||||
expect(raw.contentType).toBe('message/rfc822');
|
||||
expect(await (await app_request_redeliver(h, id)).json()).toMatchObject({ deliveries: [{ subscriber: 'spy', state: 'done' }] });
|
||||
expect((await h.json('POST', `/api/gov/mail/inbound/${id}/redeliver`, { subscriber: 'nobody' })).status).toBe(404);
|
||||
expect((await h.json('GET', '/api/gov/mail/inbound?limit=0')).status).toBe(400);
|
||||
expect(await (await h.json('GET', '/api/gov/mail/outbound')).json()).toEqual({ mails: [] });
|
||||
});
|
||||
});
|
||||
|
||||
/** 空 body 的 POST:redeliver 的 body 是可省的 */
|
||||
function app_request_redeliver(h: ReturnType<typeof harness>, id: string): Promise<Response> {
|
||||
return Promise.resolve(h.app.request(`/api/gov/mail/inbound/${id}/redeliver`, { method: 'POST' }));
|
||||
}
|
||||
Reference in New Issue
Block a user