Files
wekan/tests/shareTarget.test.cjs
T
Lauri Ojansivu 52b0ea0d81 Android: share to WeKan from other apps.
The web app manifest declares a share_target, so on a phone where WeKan
is installed as an app it appears in Android's Share menu. Sharing a web
page, a note or a message opens /share-target, "Add to WeKan": pick a
board and a list, and what was shared becomes a card there - the page's
title or the text's first line as the title, the text and the address in
its description, the address once - and the card opens. Only boards the
member may add cards to are offered, and the server checks the right to
write in that list, on that board, as for any card added.

models/lib/shareTarget.js turns a share into a card and holds the
manifest's declaration; tests/shareTarget.test.cjs covers what browsers,
notes and chat apps share, an empty share, javascript: addresses, the
manifest, the route and the server's checks. Documented in
docs/Platforms/FOSS/OS/Android.md. The four new strings are English
placeholders until the next translation pass.

Thanks to memeplex and xet7 !

Fixes #1293.
2026-10-14 14:55:04 +03:00

56 lines
3.6 KiB
JavaScript

'use strict';
// #1293: an Android share target that makes a card on a chosen board and list.
// Run: node tests/shareTarget.test.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { sharedCard, SHARE_TARGET, MAX_TITLE } = require('../models/lib/shareTarget.js');
const ROOT = path.join(__dirname, '..');
const read = f => fs.readFileSync(path.join(ROOT, f), 'utf8');
// Positive: what browsers, notes and chat apps share.
assert.deepEqual(sharedCard({ title: 'WeKan', url: 'https://wekan.fi/' }), { title: 'WeKan', description: 'https://wekan.fi/' });
assert.deepEqual(sharedCard({ text: 'Buy milk\nand bread' }), { title: 'Buy milk', description: 'Buy milk\nand bread' });
assert.deepEqual(sharedCard({ title: 'Article', text: 'Look at this https://ex.example/a', url: 'https://ex.example/a' }),
{ title: 'Article', description: 'Look at this https://ex.example/a' }, 'the address once');
assert.deepEqual(sharedCard({ url: 'https://ex.example/only' }), { title: 'https://ex.example/only', description: '' });
assert.deepEqual(sharedCard({ title: 'Same', text: 'Same' }), { title: 'Same', description: '' }, 'no repeated title');
console.log(' ok - a title and description from whatever an app shares');
// Negative.
assert.deepEqual(sharedCard({}), { title: '', description: '' });
assert.deepEqual(sharedCard({ title: 5, text: null }), { title: '', description: '' });
assert.equal(sharedCard({ url: 'javascript:alert(1)' }).title, '', 'only http(s) addresses');
assert.equal(sharedCard({ title: 'x'.repeat(5000) }).title.length, MAX_TITLE);
assert.equal(sharedCard({ title: 'a\n\n b' }).title, 'a b', 'one-line title');
console.log(' ok - nothing to share makes no card; bad addresses dropped; bounded');
// The manifest declares it, as the module says.
const manifest = JSON.parse(read('public/site.webmanifest.default'));
assert.deepEqual(manifest.share_target, JSON.parse(JSON.stringify(SHARE_TARGET)));
assert.ok(fs.existsSync(path.join(ROOT, 'public/pwa-service-worker.js')), 'an installable app (service worker)');
// Server: the same right as adding a card in that list.
const server = read('server/shareTarget.js');
assert.match(server, /if \(!this\.userId\) throw new Meteor\.Error\('not-authorized'\);/);
assert.match(server, /Lists\.findOneAsync\(\{ _id: input\.listId, boardId: board\._id, archived: \{ \$ne: true \} \}\)/, 'the list must be on that board');
assert.match(server, /if \(!\(await canWriteInList\(this\.userId, board, list\._id\)\)\) throw new Meteor\.Error\('not-authorized'\);/);
assert.match(server, /if \(!board \|\| board\.archived \|\| !list\) throw/);
assert.match(read('server/imports.js'), /import '\/server\/shareTarget';/);
// Client: the route and the page.
const router = read('config/router.js');
assert.match(router, /FlowRouter\.route\('\/share-target', \{\s*name: 'share-target',\s*triggersEnter: \[ensureSignedInUnlessSandstorm\],/);
const jade = read('client/components/main/shareTarget.jade');
assert.match(jade, /select\.js-share-target-board/);
assert.match(jade, /select\.js-share-target-list\(disabled=noBoard\)/);
assert.match(read('client/components/main/shareTarget.js'), /Meteor\.callAsync\('shareTargetCreateCard', \{ boardId, listId, \.\.\.shared\(\) \}\)/);
const en = JSON.parse(read('imports/i18n/data/en.i18n.json'));
for (const k of ['share-target-title', 'share-target-pick-board', 'share-target-create', 'share-target-empty']) assert.ok(en[k], k);
console.log(' ok - manifest, route, page and a server check of the list\'s write right');
console.log('\nshareTarget: all tests passed');