Files
wekan/tests/attachmentApiServerWrite.test.cjs
T
Lauri Ojansivu e7505a4fef Attachments: the API's JSON uploads, copies and the site logo are stored again.
The JSON branches of REST /api/attachment/upload, upload-background and
copy, their Meteor-method twins in server/attachmentApi.js, and Admin
Panel's site logo upload created the file with Attachments.insertAsync.
Meteor-Files (ostrio:files 3) has insertAsync only on the client; on the
server it is not a function, so every one of them failed - a REST 500
"Attachments.insertAsync is not a function". Only the multipart branches,
which use addAttachmentFromStream, worked.

All seven store with writeAsync(buffer, { fileName, type, userId, meta },
true) now, as email-to-card and the importers already do. Its
after-upload hook validates the file - type, size and the external
scanner, which these paths skipped - and moves it to the default storage,
or to meta.copyStorage for a copy, so the second deferred moveToStorage
each branch made is removed. The file records who stored it.

tests/attachmentApiServerWrite.test.cjs pins the seven calls and fails on
any server-side Attachments.insertAsync or Avatars.insertAsync.

This was in CHANGELOG TODO Later as needing a maintainer decision; the
site logo upload was found by the new test's tree-wide check.

Thanks to xet7 !
2026-10-27 21:26:30 +02:00

65 lines
3.0 KiB
JavaScript

'use strict';
// The attachment API's JSON branches - REST /api/attachment/upload,
// upload-background and copy, and their Meteor-method twins in
// server/attachmentApi.js - created the file with Attachments.insertAsync.
// Meteor-Files (ostrio:files 3) has insertAsync only on the CLIENT; on the
// server it is not a function, so every one of them failed with a 500. They
// store with writeAsync now, whose after-upload hook validates the file and
// moves it to the default (or copy) storage.
// Run: node tests/attachmentApiServerWrite.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const root = path.join(__dirname, '..');
const read = rel => fs.readFileSync(path.join(root, rel), 'utf8');
let passed = 0;
const test = (name, fn) => { fn(); passed += 1; console.log(' ok -', name); };
const code = src => src.replace(/\/\/[^\n]*/g, '');
test('upload, background and copy store with writeAsync and run the after-upload hook', () => {
for (const file of ['server/routes/attachmentApi.js', 'server/attachmentApi.js']) {
const src = code(read(file));
const writes = src.match(/await Attachments\.writeAsync\(fileBuffer, \{[^}]*meta,\s*\}, true\);/g) || [];
assert.strictEqual(writes.length, 3, `${file}: upload, background and copy`);
for (const w of writes) assert.match(w, /userId/, `${file}: the file records who stored it`);
}
// Admin Panel's site logo upload had the same call.
assert.match(code(read('server/siteLogoUpload.js')),
/await Attachments\.writeAsync\(bytes, \{fileName:`site-\$\{kind\}-logo\$\{type\[1\]\}`, type:type\[0\], userId:this\.userId, meta:/);
});
test('the hook, not a second deferred move, takes the file to its storage', () => {
for (const file of ['server/routes/attachmentApi.js', 'server/attachmentApi.js']) {
const src = code(read(file));
assert.doesNotMatch(src, /moveToStorage\(uploader,/, `${file}: moved twice`);
}
const hook = read('models/attachments.server.js');
assert.match(hook, /let storageDestination = fileObj\.meta\.copyStorage \|\| defaultStorage;/);
});
test('negative: no server file calls insertAsync on a Meteor-Files collection', () => {
const collections = ['Attachments', 'Avatars'];
const offenders = [];
const walk = dir => {
for (const entry of fs.readdirSync(path.join(root, dir), { withFileTypes: true })) {
if (entry.name === 'node_modules' || entry.name.startsWith('.') || entry.name === '_build') continue;
const rel = path.join(dir, entry.name);
if (entry.isDirectory()) { walk(rel); continue; }
if (!/\.(js|cjs|mjs)$/.test(entry.name)) continue;
const serverSide = rel.startsWith('server/') || /\.server\.js$/.test(rel);
if (!serverSide) continue;
const src = code(read(rel));
for (const name of collections) {
if (new RegExp(`\\b${name}\\.insertAsync\\(`).test(src)) offenders.push(`${rel}: ${name}.insertAsync`);
}
}
};
['server', 'models'].forEach(walk);
assert.deepStrictEqual(offenders, []);
});
console.log(`\n${passed} tests passed`);