Files
wekan/tests/apiImport.test.cjs
T
Lauri Ojansivu 46e30f01e4 Import: Planka, Microsoft To Do, KanbanFlow, Basecamp, Google Tasks and Google Keep from their APIs.
Decided on 2026-10-14. On the import page each of the six asks for what
its service needs - a self-hosted Planka its address, username and
password; the others an access token (Basecamp also its account id) -
lists what that account can import, and fetches the chosen board, list
or project. The credentials go with each call and are used for that
call's requests only; they are never stored or logged, and the page
clears secret fields once a fetch has used them. The fetched JSON fills
the import text box and the import goes on as for a pasted export: the
service's parser (models/lib/<key>ApiFormat.js) into the generalized
importer, with the members step and the loss report. The same JSON can
be pasted, or posted to POST /api/boards/import/<key>.

- server/apiImport.js: apiImportList and apiImportFetch, signed in only,
  off with the Admin Panel's import switch, not in Sandstorm, rate
  limited; credentials checked against the service's own fields
  (models/lib/apiImportServices.js).
- server/lib/apiImportRequest.js: every request through the SSRF guard -
  pinned address, no redirects, bounded in time and size - so a Planka
  address cannot point the server at its own network; an administrator
  can allow such a host with API_IMPORT_ALLOWED_PRIVATE_HOSTS. An error
  names only the origin and the status.
- models/lib/apiImport/<key>.js: each service's client, plain Node with
  the request injected, paging bounded and the token only in a header.
- Each has a docs page under docs/Features/ImportExport/, listed in the
  coverage index and External-Tools.md.

Tests: tests/<key>ApiImport.test.cjs (client with a fake request, every
parsed field, wrong shapes), tests/apiImport.test.cjs (credentials, the
gate, the wiring, the shapes refused), api-import.e2e.js (the form for
every service, fetched JSON imported through the page, refusals incl. a
private address). Live tests, read only, run from build.sh / build.bat ->
Tests -> Live tests with your own accounts (releases/live-tests/README.md);
they were not run here. New strings translated in all locales.

Also: the release risk baseline allows the services' API addresses, and
the guards that count import creators, list the import sources and pin
the platform fetch() calls know the new ones.

Thanks to xet7 !
2026-10-14 22:57:15 +03:00

106 lines
6.9 KiB
JavaScript

'use strict';
// Import from a service's API (decided 2026-10-14): the shared part - the
// catalog and its credential check, the server methods' gate, the request
// through the SSRF guard, the import page's form and the wiring of the six
// services. Each service's own client and parser are tested in
// tests/<key>ApiImport.test.cjs; releases/live-tests/<key>-import.mjs run them
// against the real services from build.sh / build.bat.
// Run: node tests/apiImport.test.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { API_IMPORT_SERVICES, MAX_FIELD_LENGTH, apiImportService, cleanApiImportCredentials } = require('../models/lib/apiImportServices.js');
const ROOT = path.join(__dirname, '..');
const read = f => fs.readFileSync(path.join(ROOT, f), 'utf8');
const KEYS = ['planka', 'mstodo', 'kanbanflow', 'basecamp', 'googletasks', 'googlekeep'];
const PARSERS = { planka: 'parsePlankaApi', mstodo: 'parseMicrosoftTodoApi', kanbanflow: 'parseKanbanflowApi',
basecamp: 'parseBasecampApi', googletasks: 'parseGoogleTasksApi', googlekeep: 'parseGoogleKeepApi' };
(async () => {
assert.deepEqual(API_IMPORT_SERVICES.map(s => s.key), KEYS);
// The credentials a call may carry.
assert.deepEqual(cleanApiImportCredentials('planka', { url: ' https://planka.example.org/ ', username: ' me ', password: ' p w ' }),
{ url: 'https://planka.example.org', username: 'me', password: ' p w ' }, 'a secret is kept exactly, the rest trimmed');
assert.deepEqual(cleanApiImportCredentials('basecamp', { account: '123', token: 't' }), { account: '123', token: 't' });
// Negative: an unknown service or field, a missing one, a non-string, too long, a bad address.
for (const [key, input, why] of [
['trello', { token: 't' }, /Unknown import service/],
['mstodo', null, /required/],
['mstodo', ['t'], /required/],
['mstodo', {}, /token is required/],
['mstodo', { token: 't', url: 'https://x' }, /Unexpected credential field url/],
['mstodo', { token: 42 }, /not valid/],
['mstodo', { token: 'x'.repeat(MAX_FIELD_LENGTH + 1) }, /not valid/],
['planka', { url: 'file:///etc/passwd', username: 'u', password: 'p' }, /url is not valid/],
['planka', { url: 'https://user:pw@planka.example.org', username: 'u', password: 'p' }, /url is not valid/],
['planka', { url: 'not a url', username: 'u', password: 'p' }, /url is not valid/],
]) assert.throws(() => cleanApiImportCredentials(key, input), why, `${key} ${JSON.stringify(input)}`);
assert.equal(apiImportService('nope'), null);
console.log(' ok - the catalog and its credential check');
// The server: login, the import switch, no Sandstorm, rate limited, nothing stored.
const server = read('server/apiImport.js');
assert.match(server, /if \(!userId\) throw new Meteor\.Error\('error-notAuthorized'\);/);
assert.match(server, /await assertImportEnabled\(\);/, 'the Admin Panel switch that disables import also disables this');
assert.match(server, /Meteor\.settings\?\.public\?\.sandstorm\) throw/);
assert.match(server, /cleanApiImportCredentials\(service, credentials\)/);
assert.match(server, /DDPRateLimiter\.addRule\(\{ type: 'method', name: name => \['apiImportList', 'apiImportFetch'\]\.includes\(name\)/);
for (const key of KEYS) assert.match(server, new RegExp(`import \\* as ${key} from '/models/lib/apiImport/${key}';`));
// Negative: credentials never written anywhere or logged.
assert.doesNotMatch(server, /insertAsync|updateAsync|upsertAsync|console\.(log|warn|error)/);
assert.match(read('server/imports.js'), /^import '\/server\/apiImport';$/m);
const request = read('server/lib/apiImportRequest.js');
assert.match(request, /fetchSafe\(url, \{/);
assert.match(request, /maxRedirects: 0, maxResponseBytes: MAX_RESPONSE_BYTES/);
assert.match(request, /redirect: 'error'/, 'an allowed private host is still fetched without redirects');
assert.match(request, /API_IMPORT_ALLOWED_PRIVATE_HOSTS/);
// Negative: an error says only the origin and the status, never the answer.
assert.doesNotMatch(request, /await response\.text\(\)/);
console.log(' ok - the server methods: gated, rate limited, through the SSRF guard');
// The wiring of every service.
const sources = read('models/lib/importSources.js');
const shape = read('models/lib/importSourceShape.js');
const parsers = read('models/lib/externalParsers.js');
const importJs = read('models/import.js');
const en = JSON.parse(read('imports/i18n/data/en.i18n.json'));
for (const key of KEYS) {
assert.match(sources, new RegExp(`\\{ key: '${key}', name: '[^']+', \\.\\.\\.JSON_SOURCE, api: true \\}`), key);
assert.match(shape, new RegExp(`case '${key}'`), key);
assert.match(parsers, new RegExp(` ${key}: ${PARSERS[key]},`), key);
assert.match(importJs, new RegExp(` case '${key}':`), key);
assert.ok(fs.existsSync(path.join(ROOT, `models/lib/apiImport/${key}.js`)), `${key} client`);
assert.ok(fs.existsSync(path.join(ROOT, `tests/${key}ApiImport.test.cjs`)), `${key} test`);
assert.ok(fs.existsSync(path.join(ROOT, `releases/live-tests/${key}-import.mjs`)), `${key} live test`);
assert.equal(typeof en[`import-board-instruction-${key}`], 'string', `${key} instruction`);
}
assert.match(importJs, /creator = new KanboardCreator\(data, importSource\);/);
// The page: the form, and secrets cleared once used.
const page = read('client/components/import/import.js');
assert.match(read('client/components/import/import.jade'), /if apiImport\s*\n\s*\+apiImportFetch\(service=apiImport\)/);
assert.match(page, /tpl\.\$\('\.js-api-import-field\[type="password"\]'\)\.val\(''\);/);
assert.match(read('client/components/import/import.jade'), /type="password" autocomplete="off"/);
for (const key of ['api-import-desc', 'api-import-list', 'api-import-fetch', 'api-import-fetched', 'api-import-failed', 'api-import-token']) {
assert.equal(typeof en[key], 'string', key);
}
console.log(' ok - six services wired: source, shape, parser, import, client, test, live test, instruction');
// A shape the fetch does not return is refused before any parser runs.
const { validateImportSourceShape } = await import('../models/lib/importSourceShape.js');
validateImportSourceShape('planka', { planka: { item: { id: '1' }, included: {} } });
validateImportSourceShape('mstodo', { lists: [] });
validateImportSourceShape('kanbanflow', { board: {}, tasks: [] });
validateImportSourceShape('basecamp', { project: {}, cardTables: [] });
validateImportSourceShape('googlekeep', { notes: [] });
for (const [key, value] of [['planka', {}], ['mstodo', { value: [] }], ['kanbanflow', { tasks: [] }], ['basecamp', { project: {} }],
['googletasks', []], ['googlekeep', { items: [] }]]) {
assert.throws(() => validateImportSourceShape(key, value), /Invalid/, key);
}
console.log(' ok - the import refuses a shape no fetch returns');
console.log('\napiImport: all tests passed');
})().catch(error => { console.error(error); process.exit(1); });