Files
wekan/tests/imapflowMailparserRuntime.test.cjs
T
Lauri Ojansivu 189adaee52 Update imapflow, mailparser, pdfjs-dist, isomorphic-git and pretty-ms, with runtime tests.
imapflow 2.1.2 -> 2.8.0 and mailparser 3.9.31 -> 3.12.2 (email to card):
bug fixes, and mailparser no longer turns a decoded display name into an
address. pdfjs-dist 6.2.108 -> 6.4.299 (attachment text search),
isomorphic-git 1.42.4 -> 1.44.3 (GitHub wiki import), pretty-ms 9 -> 10
(upload time estimate; its breaking changes are in colonNotation, which
WeKan does not use, and the output it shows is unchanged).

Each now has a test that RUNS it the way WeKan calls it, so the next
update that moves an API fails in seconds:
- imapflowMailparserRuntime: the poller's connect, mailbox lock, unseen
  search, fetchOne, simpleParser and \Seen flag against
  server/lib/tests/fakeImapServer.js; a wrong password is refused, and an
  encoded address in a display name stays a name.
- pdfjsTextRuntime: documentGif.js's legacy build, worker, fonts, CMaps
  and getTextContent on a PDF with known text; a non-PDF is refused.
- isomorphicGitRuntime: githubWiki.js's log, recursive readTree and
  readBlob on a two-commit repository; a missing object is an error.

npm audit reports nothing. The e2e run against a build with these:
rules-blocks (147 languages), attachments, both email specs, and the
security specs pass, except two rules-blocks cases that fail the same way
on a build of the commit before this one.

Thanks to developers of dependencies and xet7 !
2026-10-22 06:34:36 +03:00

100 lines
4.3 KiB
JavaScript

'use strict';
// The installed imapflow and mailparser, run the way server/lib/imapInbound.js
// runs them, against the fake IMAP server the Meteor runtime test uses
// (server/lib/tests/fakeImapServer.js) - so a dependency update that changes
// what WeKan's poller calls fails here, in seconds, before a real mailbox.
// imapflow went 2.1.2 -> 2.8.0 and mailparser 3.9.31 -> 3.12.2 in one step.
// Run: node tests/imapflowMailparserRuntime.test.cjs
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { pathToFileURL } = require('url');
const ROOT = path.join(__dirname, '..');
const { ImapFlow } = require('imapflow');
const { simpleParser } = require('mailparser');
let passed = 0;
async function test(name, fn) { await fn(); passed += 1; console.log(' ok -', name); }
const MESSAGE = [
'From: "Ann Example" <ann@example.test>',
'To: cards@example.test',
'Subject: A card from email',
'Message-ID: <one@example.test>',
'Content-Type: text/plain; charset=utf-8',
'',
'The card description.',
'',
].join('\n');
(async () => {
// fakeImapServer.js is an ES module in a CommonJS package: load a copy as .mjs.
const dir = fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'wekan-imap-'));
const copy = path.join(dir, 'fakeImapServer.mjs');
fs.copyFileSync(path.join(ROOT, 'server/lib/tests/fakeImapServer.js'), copy);
const { startFakeImapServer } = await import(pathToFileURL(copy).href);
const imap = await startFakeImapServer({ user: 'cards@example.test', password: 'right-password' });
const client = (password) => new ImapFlow({
host: '127.0.0.1', port: imap.port, secure: false,
auth: { user: 'cards@example.test', pass: password }, logger: false,
});
try {
imap.deliver(MESSAGE);
imap.deliver(MESSAGE.replace('<one@', '<two@').replace('A card from email', 'Second card'));
await test('the poller reads unseen mail, parses it, and marks it read', async () => {
const c = client('right-password');
await c.connect();
try {
const lock = await c.getMailboxLock('INBOX');
try {
assert.strictEqual(String(c.mailbox.uidValidity), '7');
const uids = (await c.search({ seen: false }, { uid: true })).sort((a, b) => a - b);
assert.deepStrictEqual(uids, [1, 2]);
const msg = await c.fetchOne(String(uids[0]), { source: true, size: true }, { uid: true });
assert.ok(Buffer.isBuffer(msg.source) && msg.size > 0);
const parsed = await simpleParser(msg.source, { skipImageLinks: true, skipTextLinks: true });
assert.strictEqual(parsed.subject, 'A card from email');
assert.strictEqual(parsed.from.value[0].address, 'ann@example.test');
assert.strictEqual(parsed.from.value[0].name, 'Ann Example');
assert.strictEqual(parsed.messageId, '<one@example.test>');
assert.match(parsed.text, /The card description\./);
await c.messageFlagsAdd(String(uids[0]), ['\\Seen'], { uid: true });
assert.deepStrictEqual(await c.search({ seen: false }, { uid: true }), [2]);
// The next poll asks from the last UID on.
assert.deepStrictEqual((await c.search({ uid: '2:*' }, { uid: true })), [2]);
} finally {
lock.release();
}
} finally {
await c.logout().catch(() => {});
}
});
await test('a wrong password is refused, and the error says so (negative)', async () => {
const c = client('wrong-password');
await assert.rejects(c.connect(), err => /auth|login|fail/i.test(`${err.message} ${err.responseText || ''} ${err.authenticationFailed}`));
});
await test('a display name that only looks like an address does not become one (negative)', async () => {
// mailparser 3.9.40: "never turn a decoded display name into an address".
const parsed = await simpleParser(Buffer.from([
'From: =?utf-8?Q?attacker=40evil.test?= <ann@example.test>',
'Subject: x', '', 'body', '',
].join('\r\n')));
assert.deepStrictEqual(parsed.from.value.map(v => v.address), ['ann@example.test']);
});
} finally {
await imap.close();
fs.rmSync(dir, { recursive: true, force: true });
}
console.log(`\n${passed} passed`);
})().catch((error) => {
console.error(error);
process.exit(1);
});