Files
wekan/tests/icalExport.test.cjs
T
Lauri Ojansivu a49e9d70aa Add a per-board iCal (.ics) export feed, answering #2836.
#808 added the in-app Calendar view; #2836 ("CalDAV or iCal Support") asked
specifically for calendar EXPORT/SYNC via iCal or CalDAV, which did not exist
- only a one-way .ics IMPORT (icsImport.js, #6323) was in the codebase, with
a comment pointing to a separate external project for export.

Adds a new GET /api/boards/:boardId/calendar.ics route (models/export.js)
that streams a subscribable, read-only iCalendar feed of a board's cards:
one VEVENT per Received / Start-End span / Due date, the same four dates the
Calendar view already draws. Public boards need no token; private boards
authenticate the same way every other export route does (?authToken=, or a
logged-in session), and reuse Exporter.canExport()/_scopedCardSelector() so
the feed honours the same board-visibility and swimlane/list scoping as the
JSON/CSV/PDF exports. The ICS text itself is built by a new pure, dependency-
free module, models/lib/icalExport.js (mirrors icsImport.js's style), so it
is unit-testable without Meteor and reusable elsewhere.

The board Export popup gets a new "iCal" group with one link, built through
the existing exportUrl()/EXPORT_FORMAT_GROUPS table so it carries the same
authToken/scope handling as every other export format.

Deliberately EXPORT only, one-way, read-only: full CalDAV is a stateful,
two-way sync PROTOCOL with its own server (PROPFIND/REPORT, ETags,
collections), which is a much larger feature than a dates feed. Every
calendar client that can "subscribe to a URL" - Google Calendar, Apple
Calendar, Thunderbird, Outlook - reads a plain .ics feed like this one
directly; CalDAV is not needed for that.

Thanks to xet7 !

Closes #2836,
2026-09-10 20:35:37 +03:00

124 lines
5.1 KiB
JavaScript

'use strict';
(async () => {
// Plain-Node unit test (no Meteor) for the iCal EXPORT feed generator
// (models/lib/icalExport.js), added for GitHub issue #2836 ("CalDAV or iCal
// Support"). #808 added the in-app Calendar VIEW; this is the separate
// EXPORT/subscribe direction the issue actually asked for - a per-board .ics
// feed a real calendar app can subscribe to. Run: node tests/icalExport.test.cjs
const assert = require('assert');
const { cardsToIcs, escapeText, formatDateUtc, foldLine } = await import('../models/lib/icalExport.js');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
test('formatDateUtc produces a valid RFC 5545 UTC DATE-TIME', () => {
const s = formatDateUtc(new Date(Date.UTC(2026, 8, 10, 12, 0, 0)));
assert.strictEqual(s, '20260910T120000Z');
});
test('formatDateUtc returns null for an invalid date', () => {
assert.strictEqual(formatDateUtc(new Date('not-a-date')), null);
});
test('escapeText escapes backslash, semicolon, comma and newlines per RFC 5545 3.3.11', () => {
assert.strictEqual(escapeText('a;b,c\\d\ne'), 'a\\;b\\,c\\\\d\\ne');
});
test('escapeText handles null/undefined as empty string', () => {
assert.strictEqual(escapeText(null), '');
assert.strictEqual(escapeText(undefined), '');
});
test('foldLine leaves short lines alone and folds long ones at 75 octets with a leading space', () => {
const short = 'SUMMARY:short';
assert.strictEqual(foldLine(short), short);
const long = 'SUMMARY:' + 'x'.repeat(100);
const folded = foldLine(long);
assert.ok(folded.includes('\r\n '));
// Rejoining the fold (removing the CRLF + single leading space) must
// reconstruct the original content line exactly.
const rejoined = folded.split('\r\n ').join('');
assert.strictEqual(rejoined, long);
});
test('cardsToIcs produces a valid VCALENDAR wrapper with the board name', () => {
const ics = cardsToIcs([], { calendarName: 'WeKan - My Board', now: new Date(Date.UTC(2026, 0, 1)) });
assert.match(ics, /^BEGIN:VCALENDAR\r\n/);
assert.match(ics, /VERSION:2\.0\r\n/);
assert.match(ics, /X-WR-CALNAME:WeKan - My Board\r\n/);
assert.match(ics, /END:VCALENDAR\r\n$/);
});
test('a card with a due date produces one VEVENT with the correct DTSTART/DTEND', () => {
const dueAt = new Date(Date.UTC(2026, 5, 15, 9, 0, 0));
const ics = cardsToIcs([
{ _id: 'card1', title: 'Ship the release', dueAt, url: 'https://example.com/b/x/y/card1' },
], { now: new Date(Date.UTC(2026, 0, 1)) });
assert.match(ics, /BEGIN:VEVENT/);
assert.match(ics, /UID:card1-due@wekan/);
assert.match(ics, /DTSTART:20260615T090000Z/);
assert.match(ics, /DTEND:20260615T090000Z/);
assert.match(ics, /SUMMARY:Ship the release \(due\)/);
assert.match(ics, /URL:https:\/\/example\.com\/b\/x\/y\/card1/);
assert.match(ics, /END:VEVENT/);
});
test('a card with start and end dates produces a spanning VEVENT distinct from its due event', () => {
const startAt = new Date(Date.UTC(2026, 5, 10));
const endAt = new Date(Date.UTC(2026, 5, 12));
const dueAt = new Date(Date.UTC(2026, 5, 12));
const ics = cardsToIcs([{ _id: 'card2', title: 'Sprint', startAt, endAt, dueAt }]);
assert.match(ics, /UID:card2-span@wekan/);
assert.match(ics, /DTSTART:20260610T000000Z/);
assert.match(ics, /DTEND:20260612T000000Z/);
assert.match(ics, /UID:card2-due@wekan/);
// Two distinct VEVENT blocks for the one card (span + due), not one merged event.
assert.strictEqual((ics.match(/BEGIN:VEVENT/g) || []).length, 2);
});
test('a card with a received date produces its own VEVENT (matches the Calendar view\'s four date types)', () => {
const receivedAt = new Date(Date.UTC(2026, 5, 1));
const ics = cardsToIcs([{ _id: 'card3', title: 'Intake', receivedAt }]);
assert.match(ics, /UID:card3-received@wekan/);
assert.match(ics, /SUMMARY:Intake \(received\)/);
});
test('a card with no dates at all contributes no VEVENT', () => {
const ics = cardsToIcs([{ _id: 'card4', title: 'No dates' }]);
assert.strictEqual((ics.match(/BEGIN:VEVENT/g) || []).length, 0);
});
test('special characters in a title/description are escaped, not left to break the VEVENT', () => {
const dueAt = new Date(Date.UTC(2026, 5, 15));
const ics = cardsToIcs([{
_id: 'card5',
title: 'Fix bug; urgent, please',
description: 'line one\nline two',
dueAt,
}]);
assert.match(ics, /SUMMARY:Fix bug\\; urgent\\, please \(due\)/);
assert.match(ics, /DESCRIPTION:line one\\nline two/);
});
// --- negative test: the server route this feeds must exist and be wired to
// the same generator, so the fix is not just a standalone unused module. ----
test('the server export route serves the feed via cardsToIcs, scoped and access-checked', () => {
const fs = require('fs');
const path = require('path');
const exportJs = fs.readFileSync(path.join(__dirname, '..', 'models/export.js'), 'utf8');
assert.match(exportJs, /\/api\/boards\/:boardId\/calendar\.ics/);
assert.match(exportJs, /cardsToIcs/);
assert.match(exportJs, /exporter\.canExport\(user\)/);
assert.match(exportJs, /text\/calendar/);
});
console.log(`\n${passed} tests passed`);
})().catch(e => { console.error(e); process.exit(1); });