Compare commits
10
Commits
314bdc6296
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a61774a40 | ||
|
|
adf723419d | ||
|
|
f3f4ca2c9f | ||
|
|
c9c40af382 | ||
|
|
ae4f153482 | ||
|
|
1bfcc7a079 | ||
|
|
43c7dccc9f | ||
|
|
c4661d1dc0 | ||
|
|
81397ba61f | ||
|
|
a574e93359 |
No files matched your search
@@ -0,0 +1,106 @@
|
||||
# Tests every push and pull request; deploys pushes to master to the Plesk server.
|
||||
# Setup and what the deploy may touch: docs/deployment.md.
|
||||
#
|
||||
# Same pattern as the calendar frontend (Calendar_Frontend/.gitea/workflows/deploy.yml): build here,
|
||||
# deliver over SSH as the Plesk subscription user, restart Passenger. Unlike there, the app folder
|
||||
# also holds things the deploy must never touch (.env with the secrets, logs/, tmp/), so only the
|
||||
# app's own folders are synced with --delete and nothing is deleted at the top level.
|
||||
name: Test and deploy
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
# One job, not test + deploy: every job starts in a fresh container, so two jobs meant checking out,
|
||||
# setting up Node and running `npm ci` twice (~2 min each, 2026-11-08). The deploy steps only run
|
||||
# for pushes to master; on pull requests the job ends after the tests. The condition is spelled out
|
||||
# on every deploy step rather than stored in an env flag: a flag compared as a string ('true') would
|
||||
# skip the deploy silently - and green - if the runner ever rendered the boolean differently.
|
||||
test-and-deploy:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
DEPLOY_HOST: ${{ vars.DEPLOY_HOST }}
|
||||
DEPLOY_PORT: ${{ vars.DEPLOY_PORT }}
|
||||
DEPLOY_USER: ${{ vars.DEPLOY_USER }}
|
||||
DEPLOY_PATH: ${{ vars.DEPLOY_PATH }}
|
||||
# Absolute on purpose: rsync passes its -e string to ssh without a shell, and ssh expands "~"
|
||||
# from the passwd entry, not $HOME (see the calendar frontend's docs/gotchas.md).
|
||||
SSH_DIR: /tmp/deploy-ssh
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version-file: .nvmrc
|
||||
- run: npm ci
|
||||
- run: npx tsc --noEmit
|
||||
# Unit tests only. The integration tests (npm run test:integration) start their own MariaDB
|
||||
# container and aren't run here.
|
||||
# FEEDBACK_IP_SALT (which the Jenkins job exported) isn't needed: vitest.config.ts sets one.
|
||||
- run: npm test
|
||||
|
||||
- name: Build
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||
run: npm run build
|
||||
# Only runtime dependencies go to the server (no typescript, vitest, ...).
|
||||
- name: Prune dev dependencies
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||
run: npm prune --omit=dev
|
||||
|
||||
- name: SSH setup
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||
env:
|
||||
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
||||
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
||||
run: |
|
||||
test -n "$DEPLOY_HOST" && test -n "$DEPLOY_USER" && test -n "$DEPLOY_PATH"
|
||||
if ! command -v rsync >/dev/null 2>&1 || ! command -v ssh >/dev/null 2>&1; then
|
||||
apt-get update && apt-get install -y --no-install-recommends rsync openssh-client
|
||||
fi
|
||||
install -m 700 -d "$SSH_DIR"
|
||||
printf '%s\n' "$DEPLOY_SSH_KEY" > "$SSH_DIR/key"
|
||||
chmod 600 "$SSH_DIR/key"
|
||||
# Pinned host key, never trust-on-first-use.
|
||||
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$SSH_DIR/known_hosts"
|
||||
|
||||
- name: Upload
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||
run: |
|
||||
rsh="ssh -p ${DEPLOY_PORT:-22} -i $SSH_DIR/key -o UserKnownHostsFile=$SSH_DIR/known_hosts -o IdentitiesOnly=yes -o BatchMode=yes"
|
||||
target="$DEPLOY_USER@$DEPLOY_HOST:$DEPLOY_PATH"
|
||||
# The app's own folders, each mirrored exactly (--delete within the folder only).
|
||||
# src/ is needed at runtime: swagger-jsdoc reads the route docs from src/models/**/*.ts.
|
||||
for dir in dist src public node_modules; do
|
||||
rsync -az --delete -e "$rsh" "$dir/" "$target/$dir/"
|
||||
done
|
||||
# Single files at the top level, without --delete: .env, logs/ and tmp/ live there too.
|
||||
# package.json is what makes Node treat dist/ as ES modules ("type": "module").
|
||||
rsync -az -e "$rsh" package.json package-lock.json "$target/"
|
||||
|
||||
- name: Restart
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||
run: |
|
||||
# Passenger restarts the app on its next request when tmp/restart.txt is touched.
|
||||
ssh -p "${DEPLOY_PORT:-22}" -i "$SSH_DIR/key" -o "UserKnownHostsFile=$SSH_DIR/known_hosts" \
|
||||
-o IdentitiesOnly=yes -o BatchMode=yes "$DEPLOY_USER@$DEPLOY_HOST" \
|
||||
"mkdir -p '$DEPLOY_PATH/tmp' && touch '$DEPLOY_PATH/tmp/restart.txt'"
|
||||
|
||||
# Set DEPLOY_SMOKE_URL to an endpoint that must answer 200 without a session, e.g.
|
||||
# https://api.nachklang.art/calendar/events/public/json (what nachklang.art itself reads).
|
||||
- name: Smoke test
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/master' && vars.DEPLOY_SMOKE_URL != ''
|
||||
env:
|
||||
DEPLOY_SMOKE_URL: ${{ vars.DEPLOY_SMOKE_URL }}
|
||||
run: |
|
||||
# Passenger starts the app on the first request after the restart; give it a moment.
|
||||
for _ in 1 2 3 4 5 6; do
|
||||
if curl -fsS -o /dev/null "$DEPLOY_SMOKE_URL"; then echo "OK: $DEPLOY_SMOKE_URL"; exit 0; fi
|
||||
sleep 5
|
||||
done
|
||||
echo "Smoke test failed: $DEPLOY_SMOKE_URL"
|
||||
exit 1
|
||||
|
||||
- name: Remove key
|
||||
if: always() && github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||
run: rm -f "$SSH_DIR/key"
|
||||
@@ -8,7 +8,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
|
||||
npm run build # Compile TypeScript → dist/
|
||||
npm run start # Build and start (tsc && node ./dist/app.js)
|
||||
npm run debug # Start with DEBUG=* environment variable
|
||||
npm run test # Run the vitest suite once with coverage (lcov + testResults/sonar-report.xml)
|
||||
npm run test # Run the vitest suite once with coverage (text summary + lcov in coverage/)
|
||||
npm run test:watch # vitest in watch mode
|
||||
npm run test:integration # Admin-module tests against a throwaway MariaDB (needs docker or podman)
|
||||
```
|
||||
@@ -18,6 +18,12 @@ Run a single test file:
|
||||
npx vitest run test/some.test.ts
|
||||
```
|
||||
|
||||
**Deploys** run in Gitea Actions (`.gitea/workflows/deploy.yml`, since 2026-11-08; Jenkins is
|
||||
retired): every push/PR runs `tsc` and the unit tests, pushes to master build, prune dev
|
||||
dependencies and rsync `dist/`, `src/`, `public/`, `node_modules/` and `package.json` to Plesk over
|
||||
SSH. `.env`, `logs/` and `tmp/` on the server are never touched; SQL migrations stay manual.
|
||||
Details: `docs/deployment.md`.
|
||||
|
||||
## Architecture
|
||||
|
||||
Express.js REST API in TypeScript with a service-oriented layering. Domains: `Calendar` (events, users), `Feedback` (concert feedback forms, mounted at `/feedback`, backed by its own `FEEDBACK_DB` — see `src/models/feedback/`: public submission flow, admin CRUD, reporting, and a Salesforce newsletter-sync integration), `Tickets` (mounted at `/tickets`), and `Admin` (identity and permissions, mounted at `/admin`, backed by `ADMIN_DB` — see below).
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
USE nachklang_feedback;
|
||||
SOURCE /migrations/feedback/001_init.sql;
|
||||
SOURCE /migrations/feedback/002_add_poster_image_url.sql;
|
||||
SOURCE /migrations/feedback/003_add_submission_client_token.sql;
|
||||
@@ -0,0 +1,65 @@
|
||||
# Calendar iCal export and subscriptions
|
||||
|
||||
How `GET /calendar/events/{calendar}/ical` builds its file, what changed on 2026-09-28, and the
|
||||
endpoint the calendar app uses to hand editors ready-made subscription URLs.
|
||||
|
||||
## The export
|
||||
|
||||
`icalgenerator.service.ts` turns a calendar's `PUBLIC` events into one `VCALENDAR`:
|
||||
|
||||
- Timed events: `DTSTART;TZID=Europe/Berlin:` / `DTEND;TZID=Europe/Berlin:` formatted with the
|
||||
**API process's local** `getHours()` etc. The times are therefore only right if the process runs
|
||||
in Berlin time (`TZ=Europe/Berlin`, or a server in that zone). Nothing enforces this. Inferred
|
||||
from the code; not checked against the production server.
|
||||
- Whole-day events: `VALUE=DATE`, and the stored end date is treated as the *inclusive* last day -
|
||||
the generator adds one day for `DTEND`. The calendar app stores whole-day events as 00:00 to
|
||||
23:59 Berlin time for this reason.
|
||||
- `repeatFrequency` is written verbatim as `RRULE:FREQ=<value>` (no interval, count or end).
|
||||
- `public` needs no credential; the other calendars take `?password=` because a calendar client
|
||||
cannot send the session cookie (see `calendar-auth-migration.md`).
|
||||
|
||||
## RFC 5545 conformance (2026-09-28)
|
||||
|
||||
Before this date the generator wrote text values raw. A line break in a description ended the
|
||||
`DESCRIPTION` property early and the rest was read as a garbage property; unescaped `,` and `;`
|
||||
are invalid in text values. The new calendar frontend has a multi-line description field, so this
|
||||
went from theoretical to certain. Now:
|
||||
|
||||
- `SUMMARY`, `DESCRIPTION` and `LOCATION` go through `escapeText` (`\` `;` `,` and line breaks,
|
||||
backslash first).
|
||||
- `URL` is a URI value and is not escaped; line breaks are stripped from it.
|
||||
- Every content line is folded at 75 octets (UTF-8 aware - a multi-byte character is never split)
|
||||
and the file uses CRLF line endings throughout (`toContentLines`).
|
||||
- `formatDate` no longer shifts the caller's `Date` when it adds the whole-day end's extra day.
|
||||
- The response is `Content-Type: text/calendar; charset=utf-8`. Before, Express inferred
|
||||
`text/html` from the string body; calendar apps coped, but it was wrong.
|
||||
|
||||
Pinned by `test/calendar/icalgenerator.service.test.ts`.
|
||||
|
||||
Not changed, worth knowing: `ORGANIZER` is written as a bare display name (`ORGANIZER:Anna`),
|
||||
while RFC 5545 expects a cal-address (`ORGANIZER;CN=Anna:mailto:…`). Calendar apps have tolerated
|
||||
it so far.
|
||||
|
||||
## `GET /calendar/events/subscriptions`
|
||||
|
||||
For the calendar app's "Abonnieren" dialog. Behind `requireAppAccess('calendar')`; the shared
|
||||
password is **not** accepted here (one calendar's password must not reveal the others).
|
||||
|
||||
Returns `[{calendar, icalUrl}]` in `calendarNames` order, built from `API_BASE_URL`:
|
||||
|
||||
- `public` without a password;
|
||||
- every other calendar with `?password=<its shared password>`, URL-encoded;
|
||||
- a calendar whose credential env var is unset is left out rather than listed with a URL that
|
||||
cannot work.
|
||||
|
||||
`choir` and `birthdays` share `CHOIR_CREDENTIAL`, so rotating it changes both URLs - and breaks
|
||||
every existing subscription to either. The response is sent with `Cache-Control: no-store`
|
||||
because it carries the passwords.
|
||||
|
||||
Signed-in editors can read every event of every calendar anyway, so handing them the passwords
|
||||
exposes nothing they could not already see.
|
||||
|
||||
## Found while doing this
|
||||
|
||||
- Only the admin pool reads `DB_PORT`; `Calendar.db.ts` (and the feedback/tickets pools) always
|
||||
connect to 3306. A local test database for the calendar has to listen there.
|
||||
@@ -0,0 +1,116 @@
|
||||
# Deployment
|
||||
|
||||
The API runs on the Plesk server as a Node.js app (Phusion Passenger) and is deployed by **Gitea
|
||||
Actions** (`.gitea/workflows/deploy.yml`), which replaced the Jenkins job on 2026-11-08. Same
|
||||
pattern as the calendar frontend; the reusable how-to (server facts, one-time setup, gotchas) is in
|
||||
`Calendar_Frontend/docs/deployment.md` and in Patrick's PKM ("Jenkins to Gitea Actions for a Plesk
|
||||
Node.js App").
|
||||
|
||||
```
|
||||
push to master ──▶ Gitea ──▶ runner (container on the server)
|
||||
└─ one job: npm ci, tsc --noEmit, npm test (unit tests)
|
||||
└─ (master only) build, prune dev deps ─ rsync over SSH ─▶
|
||||
plesk-nachklang@nachklang.art:<app root> + tmp/restart.txt
|
||||
```
|
||||
|
||||
Pull requests run the same job up to the tests; the deploy steps only run for pushes to master. It
|
||||
used to be two jobs, which ran `npm ci` twice (~2 min each); merged into one on 2026-11-08.
|
||||
|
||||
## Plesk (as configured, 2026-11-08)
|
||||
|
||||
*Websites & Domains → api.nachklang.art → Node.js*:
|
||||
|
||||
| Setting | Value |
|
||||
|---|---|
|
||||
| Node.js version | 26.13.0 |
|
||||
| Application root | `/api.nachklang.art` (= `/var/www/vhosts/nachklang.art/api.nachklang.art`) |
|
||||
| Document root | `/api.nachklang.art/public` |
|
||||
| Application startup file | `dist/app.js` |
|
||||
| Application mode | production |
|
||||
| Environment variable | `PORT=443` (Passenger takes over the port; `app.ts` exits without `PORT`) |
|
||||
|
||||
## What the deploy touches - and what it must not
|
||||
|
||||
The app root holds more than the build, so the workflow syncs **only the app's own parts**:
|
||||
|
||||
| Path | How | Why it's needed at runtime |
|
||||
|---|---|---|
|
||||
| `dist/` | mirrored (`--delete` inside) | the compiled app |
|
||||
| `src/` | mirrored | swagger-jsdoc reads the route docs from `src/models/**/*.router.ts` / `*.interface.ts` for `/docs` |
|
||||
| `public/` | mirrored | Passenger's document root |
|
||||
| `node_modules/` | mirrored, production dependencies only (`npm prune --omit=dev`) | runtime dependencies |
|
||||
| `package.json`, `package-lock.json` | copied, no delete | `"type": "module"` makes Node load `dist/` as ES modules |
|
||||
|
||||
**Never touched** (nothing is deleted at the top level): `.env` (the secrets; `dotenv` reads it from
|
||||
the app root), `logs/` (Winston writes there, relative to the app root), `tmp/` (Passenger's
|
||||
`restart.txt`), and anything else in that folder.
|
||||
|
||||
The Jenkins job copied `dist/`, `src/` and `node_modules/` with `cp` (never deleting, so stale files
|
||||
piled up) and never `package.json` or `public/`; the first rsync deploy cleans that up.
|
||||
|
||||
**SQL migrations are not part of the deploy.** Files under `sql/` are still applied by hand, before
|
||||
the deploy that needs them.
|
||||
|
||||
## Gitea settings
|
||||
|
||||
Shared by all Nachklang repos, on the organisation (*Nachklang → Settings → Actions*): secrets
|
||||
`DEPLOY_SSH_KEY`, `DEPLOY_KNOWN_HOSTS`; variables `DEPLOY_HOST=nachklang.art`, `DEPLOY_PORT=42069`,
|
||||
`DEPLOY_USER=plesk-nachklang`.
|
||||
|
||||
On this repository (*API → Settings → Actions → Variables*):
|
||||
|
||||
| Variable | Value |
|
||||
|---|---|
|
||||
| `DEPLOY_PATH` | `/var/www/vhosts/nachklang.art/api.nachklang.art` |
|
||||
| `DEPLOY_SMOKE_URL` | `https://api.nachklang.art/calendar/events/public/json` |
|
||||
|
||||
The smoke test only needs an endpoint that answers 200 without a session; the public calendar feed
|
||||
is the one nachklang.art itself depends on.
|
||||
|
||||
## Switchover (done once)
|
||||
|
||||
1. Pause the Jenkins API job (done 2026-11-08).
|
||||
1a. **Fix file ownership** (as root): Jenkins deployed as **root**, so `dist/`, `src/` and
|
||||
`node_modules/` were `root:root`, and the first Gitea deploy failed with rsync "Operation not
|
||||
permitted" / "Permission denied" - the subscription user can't change root's files. Fixed with
|
||||
`chown -R plesk-nachklang:psacln dist src node_modules` in the app root (the group of the files
|
||||
the app creates itself, e.g. `.env`, `logs/`); then re-run the failed job.
|
||||
Check: `find . ! -user plesk-nachklang` prints nothing.
|
||||
2. Set `DEPLOY_PATH` and `DEPLOY_SMOKE_URL`.
|
||||
3. Open a PR → green run → merge → watch the deploy steps. Unlike the calendar there is no gap:
|
||||
the folder keeps working while files are replaced, and Passenger restarts on the next request.
|
||||
4. Check `https://api.nachklang.art/docs` and the calendar/feedback/tickets apps.
|
||||
|
||||
Rollback: revert the commit on master; the workflow deploys the previous state.
|
||||
|
||||
## Checked locally (2026-11-08)
|
||||
|
||||
- `actionlint`: no findings.
|
||||
- In a clean `node:26` container (26.13.0, no `.env`): `npm ci`, `tsc --noEmit`, all 233 unit tests,
|
||||
build, `npm prune --omit=dev`.
|
||||
- The pruned bundle, laid out like the server folder with a test `.env`, started with
|
||||
`node dist/app.js` against a throwaway MariaDB: public JSON 200, iCal `text/calendar`, `/docs` 200
|
||||
including the subscriptions route (so `src/` is read), `/admin/me` 401, `logs/` created in the app
|
||||
root.
|
||||
- The workflow's upload step against a throwaway SSH server with a folder like the real one: `.env`,
|
||||
`logs/`, `tmp/` and an unrelated top-level file kept; a stale package in `node_modules/` and a stale
|
||||
file in `dist/` removed; `package.json` updated.
|
||||
|
||||
Not checked: the workflow on the real runner.
|
||||
|
||||
## Notes
|
||||
|
||||
- There was **no `package.json` on the server** before the first rsync deploy (Jenkins never copied
|
||||
it); `dist/app.js` still ran as ES modules, probably because Node ≥ 22 detects ES module syntax on
|
||||
its own [inference, not checked]. The deploy now ships `package.json`, so it doesn't depend on that.
|
||||
- A failed upload leaves new and old files mixed in `dist/`, but the running app is unaffected:
|
||||
the restart step is skipped, so Passenger keeps the old code in memory until the next deploy (or an
|
||||
unplanned restart - fix and re-run soon).
|
||||
|
||||
- `bcrypt` was removed on 2026-11-08: nothing imported it any more (passwords moved to better-auth,
|
||||
which hashes with its own scrypt). It was the only production dependency with a native build step.
|
||||
- `FEEDBACK_IP_SALT`, which the Jenkins test step exported, isn't needed in CI: `vitest.config.ts`
|
||||
sets one for the test run.
|
||||
- SonarQube isn't used any more; `sonar-project.properties`, the `vitest-sonar-reporter` and its
|
||||
`testResults/sonar-report.xml` were removed on 2026-11-08 (the Jenkins job ran the tests only to
|
||||
feed Sonar's coverage).
|
||||
Generated
+6
-757
File diff suppressed because it is too large.
Load diff
+1
-4
@@ -23,7 +23,6 @@
|
||||
"@better-auth/passkey": "^1.7.2",
|
||||
"app-root-path": "^3.0.0",
|
||||
"axios": "^1.20.0",
|
||||
"bcrypt": "^5.0.1",
|
||||
"better-auth": "^1.7.2",
|
||||
"cors": "^2.8.5",
|
||||
"debug": "^4.3.1",
|
||||
@@ -41,7 +40,6 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/app-root-path": "^1.2.4",
|
||||
"@types/bcrypt": "^3.0.1",
|
||||
"@types/cors": "^2.8.19",
|
||||
"@types/debug": "^4.1.5",
|
||||
"@types/express": "^4.17.15",
|
||||
@@ -56,8 +54,7 @@
|
||||
"source-map-support": "^0.5.19",
|
||||
"supertest": "^7.2.2",
|
||||
"typescript": "^5.9.3",
|
||||
"vitest": "^5.0.0",
|
||||
"vitest-sonar-reporter": "^3.0.0"
|
||||
"vitest": "^5.0.0"
|
||||
},
|
||||
"overrides": {
|
||||
"better-auth": {
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
sonar.projectKey=nk-api
|
||||
sonar.sources=src
|
||||
sonar.tests=test
|
||||
sonar.language=ts
|
||||
sonar.javascript.lcov.reportPaths=coverage/lcov.info
|
||||
sonar.testExecutionReportPaths=testResults/sonar-report.xml
|
||||
@@ -0,0 +1,17 @@
|
||||
-- Nachklang e.V. Feedback module — makes submitting idempotent per form visit.
|
||||
-- Apply manually against the FEEDBACK_DB database, after 002_add_poster_image_url.sql:
|
||||
-- mysql -h <DB_HOST> -u <DB_USER> -p <FEEDBACK_DB> < 003_add_submission_client_token.sql
|
||||
--
|
||||
-- The form generates one UUID per visit and resends it on every retry. When
|
||||
-- a submission saved but its response never reached the phone, the visitor
|
||||
-- sees an error and taps "absenden" again; the API now finds the stored token
|
||||
-- and answers with the original submission instead of saving a copy.
|
||||
--
|
||||
-- Unique per event, not globally: one visit can switch to another concert's
|
||||
-- form in the same tab, and that event's submission must still save.
|
||||
--
|
||||
-- NULL for every row written before this migration and for any client that
|
||||
-- sends no token; a UNIQUE index allows any number of NULLs.
|
||||
ALTER TABLE submissions
|
||||
ADD COLUMN client_token CHAR(36) NULL AFTER ip_hash,
|
||||
ADD UNIQUE KEY uq_sub_event_client_token (event_id, client_token);
|
||||
@@ -21,7 +21,13 @@ dotenv.config();
|
||||
* test/calendar/credentials.service.test.ts.
|
||||
*/
|
||||
|
||||
const credentialFor = (calendarName: string): string | undefined => {
|
||||
/**
|
||||
* The shared password for a calendar, or undefined for `public` (which needs
|
||||
* none), an unknown calendar, or one whose credential is not configured.
|
||||
* Exported for the subscriptions endpoint, which hands signed-in editors the
|
||||
* ready-made iCal URLs - see GET /calendar/events/subscriptions.
|
||||
*/
|
||||
export const credentialFor = (calendarName: string): string | undefined => {
|
||||
switch (calendarName) {
|
||||
case 'members':
|
||||
return process.env.MEMBER_CREDENTIAL;
|
||||
|
||||
@@ -8,6 +8,7 @@ import * as EventService from './events.service.js';
|
||||
import * as iCalService from './icalgenerator.service.js';
|
||||
import * as CredentialService from './credentials.service.js';
|
||||
import {requireAppAccess, resolveAccess, AdminAccess} from '../../admin/admin.middleware.js';
|
||||
import {API_BASE_URL} from '../../admin/admin.config.js';
|
||||
import {Guid} from 'guid-typescript';
|
||||
import logger from '../../../middleware/logger.js';
|
||||
|
||||
@@ -423,7 +424,12 @@ eventsRouter.get('/:calendar/ical', async (req: Request, res: Response) => {
|
||||
let file = await iCalService.convertToIcal(events);
|
||||
|
||||
// Send the ical file back
|
||||
res.set({'Content-Disposition': 'attachment; filename=' + fileName + '.ics'});
|
||||
// text/calendar, not the text/html that send() infers from a string - calendar apps
|
||||
// coped with the wrong type, but it was wrong (RFC 5545 §8.1).
|
||||
res.set({
|
||||
'Content-Type': 'text/calendar; charset=utf-8',
|
||||
'Content-Disposition': 'attachment; filename=' + fileName + '.ics'
|
||||
});
|
||||
res.status(200).send(file);
|
||||
} catch (e: any) {
|
||||
let errorGuid = Guid.create().toString();
|
||||
@@ -436,6 +442,59 @@ eventsRouter.get('/:calendar/ical', async (req: Request, res: Response) => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /calendar/events/subscriptions:
|
||||
* get:
|
||||
* summary: The iCal subscription URL of every calendar
|
||||
* description: >
|
||||
* Ready-to-use iCal URLs for each calendar, with the calendar's shared password
|
||||
* already in the query string where it needs one - for the calendar app's
|
||||
* "Abonnieren" dialog. A calendar whose password is not configured is left out
|
||||
* rather than listed with a URL that cannot work. Requires a signed-in account
|
||||
* with the calendar permission: those editors can read every event anyway, so the
|
||||
* passwords tell them nothing new. Never cached.
|
||||
* tags:
|
||||
* - calendar
|
||||
* security:
|
||||
* - AdminSessionCookie: []
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Success
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: array
|
||||
* items:
|
||||
* type: object
|
||||
* properties:
|
||||
* calendar:
|
||||
* type: string
|
||||
* example: members
|
||||
* icalUrl:
|
||||
* type: string
|
||||
* example: https://api.nachklang.art/calendar/events/members/ical?password=…
|
||||
* 401:
|
||||
* description: Unauthorized - not signed in
|
||||
* 403:
|
||||
* description: Forbidden - the account lacks the calendar permission
|
||||
*/
|
||||
eventsRouter.get('/subscriptions', requireCalendarAccess, (req: Request, res: Response) => {
|
||||
const base = API_BASE_URL.replace(/\/+$/, '');
|
||||
const subscriptions = Array.from(calendarNames.keys()).flatMap((calendar) => {
|
||||
const url = `${base}/calendar/events/${calendar}/ical`;
|
||||
if (calendar === 'public') {
|
||||
return [{calendar, icalUrl: url}];
|
||||
}
|
||||
const password = CredentialService.credentialFor(calendar);
|
||||
return password ? [{calendar, icalUrl: `${url}?password=${encodeURIComponent(password)}`}] : [];
|
||||
});
|
||||
|
||||
// These URLs carry the shared passwords; keep them out of every cache on the way.
|
||||
res.set('Cache-Control', 'no-store');
|
||||
res.status(200).send(subscriptions);
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /calendar/events:
|
||||
|
||||
@@ -13,7 +13,7 @@ export const convertToIcal = async (events: Event[]): Promise<string> => {
|
||||
addEventToFile(ical, event);
|
||||
}
|
||||
|
||||
return serializeIcalFile(ical);
|
||||
return toContentLines(serializeIcalFile(ical));
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
@@ -116,9 +116,11 @@ const addEventToFile = (ical: iCalFile, event: Event) => {
|
||||
* @param event
|
||||
*/
|
||||
const createIcalEvent = (event: Event): iCalEvent => {
|
||||
let description = event.description ? event.description + '\n' : '';
|
||||
let location = event.location ? event.location + '\n' : '';
|
||||
let url = event.url ? event.url + '\n' : '';
|
||||
// Free text is escaped (see escapeText); the URL is a URI value, which has
|
||||
// no escapes - it only must not break the line.
|
||||
let description = event.description ? escapeText(event.description) + '\n' : '';
|
||||
let location = event.location ? escapeText(event.location) + '\n' : '';
|
||||
let url = event.url ? event.url.replace(/[\r\n]+/g, '') + '\n' : '';
|
||||
|
||||
return {
|
||||
header: 'BEGIN:VEVENT\n',
|
||||
@@ -128,7 +130,7 @@ const createIcalEvent = (event: Event): iCalEvent => {
|
||||
start: formatDate(event.startDateTime, event.wholeDay) + '\n',
|
||||
end: formatDate(event.endDateTime, event.wholeDay, true) + '\n',
|
||||
repeatFrequency: event.repeatFrequency ? event.repeatFrequency + '\n' : '',
|
||||
summary: event.name + '\n',
|
||||
summary: escapeText(event.name) + '\n',
|
||||
description: description,
|
||||
location: location,
|
||||
url: url,
|
||||
@@ -143,8 +145,11 @@ const createIcalEvent = (event: Event): iCalEvent => {
|
||||
* @param wholeDayFormat
|
||||
* @param isEndDate
|
||||
*/
|
||||
const formatDate = (date: Date, wholeDayFormat: boolean = false, isEndDate: boolean = false): string => {
|
||||
const formatDate = (input: Date, wholeDayFormat: boolean = false, isEndDate: boolean = false): string => {
|
||||
let returnString = '';
|
||||
// A copy: this used to shift the caller's Date in place, so the event object
|
||||
// came out of the export with its end a day later than it went in.
|
||||
const date = new Date(input);
|
||||
|
||||
// We need to do this for whole day events as otherwise the event ends one day too early
|
||||
if(wholeDayFormat && isEndDate) date.setDate(date.getDate() + 1)
|
||||
@@ -184,6 +189,58 @@ export interface iCalEvent {
|
||||
footer: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* RFC 5545 §3.3.11 TEXT escaping. Without it a description with a line break
|
||||
* ended the DESCRIPTION property early and the rest of the text was read as a
|
||||
* new (garbage) property, and an unescaped `,` or `;` is invalid in the value.
|
||||
* The backslash goes first, or the escapes added after it would be doubled.
|
||||
*/
|
||||
export const escapeText = (value: string): string =>
|
||||
value
|
||||
.replace(/\\/g, '\\\\')
|
||||
.replace(/;/g, '\\;')
|
||||
.replace(/,/g, '\\,')
|
||||
.replace(/\r\n|\r|\n/g, '\\n');
|
||||
|
||||
const MAX_LINE_OCTETS = 75;
|
||||
|
||||
/**
|
||||
* RFC 5545 §3.1 line folding: no content line longer than 75 octets, a longer
|
||||
* one continues on the next line after a single leading space. Counts UTF-8
|
||||
* octets, not characters, and never splits inside a character - "ü" is two
|
||||
* octets and a fold between them corrupts it.
|
||||
*/
|
||||
export const foldLine = (line: string): string => {
|
||||
const parts: string[] = [];
|
||||
let current = '';
|
||||
let octets = 0;
|
||||
for (const char of line) {
|
||||
const size = Buffer.byteLength(char, 'utf8');
|
||||
// A continuation line's leading space counts towards its 75.
|
||||
const limit = parts.length === 0 ? MAX_LINE_OCTETS : MAX_LINE_OCTETS - 1;
|
||||
if (octets + size > limit) {
|
||||
parts.push(current);
|
||||
current = '';
|
||||
octets = 0;
|
||||
}
|
||||
current += char;
|
||||
octets += size;
|
||||
}
|
||||
parts.push(current);
|
||||
return parts.join('\r\n ');
|
||||
};
|
||||
|
||||
/**
|
||||
* The generator builds the file with bare `\n` line ends; the format wants
|
||||
* every content line folded and terminated by CRLF (RFC 5545 §3.1).
|
||||
*/
|
||||
const toContentLines = (ical: string): string =>
|
||||
ical
|
||||
.split('\n')
|
||||
.filter((line) => line !== '')
|
||||
.map(foldLine)
|
||||
.join('\r\n') + '\r\n';
|
||||
|
||||
/**
|
||||
* Checks if a given string is null, undefined or blank
|
||||
* @param str The string to check
|
||||
|
||||
@@ -56,6 +56,10 @@
|
||||
* website:
|
||||
* type: string
|
||||
* description: Honeypot field. Must stay empty; a real visitor never fills it in.
|
||||
* clientToken:
|
||||
* type: string
|
||||
* format: uuid
|
||||
* description: Generated once per form and resent on every retry. A retry whose token is already stored for this event returns the original submissionId instead of saving a duplicate. Optional; a value that isn't a UUID is ignored.
|
||||
* SubmissionResponse:
|
||||
* type: object
|
||||
* properties:
|
||||
@@ -95,4 +99,5 @@ export interface SubmissionRequestBody {
|
||||
guestBook?: GuestBookInput;
|
||||
newsletter?: NewsletterInput;
|
||||
website?: string;
|
||||
clientToken?: string;
|
||||
}
|
||||
@@ -11,6 +11,8 @@ const CONSENT_TEXT_VERSION = '2026-08-02';
|
||||
|
||||
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
|
||||
const CLIENT_TOKEN_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
// A real setlist tops out around a few dozen songs and a handful of
|
||||
// questions, so a legitimate submission never comes close to this. Caps
|
||||
// total generated rows regardless of how large the client's answers/ratings
|
||||
@@ -136,6 +138,33 @@ export const validateNewsletter = (input?: NewsletterInput): ValidatedNewsletter
|
||||
return {firstName, lastName, email};
|
||||
};
|
||||
|
||||
/**
|
||||
* The form generates one UUID per visit and resends it on every retry, so a
|
||||
* request whose response was lost (the row saved, but the phone never heard
|
||||
* back) can be told apart from a genuinely new submission. Anything that
|
||||
* isn't a UUID is treated as "no token" rather than rejected - an old cached
|
||||
* copy of the form must still be able to submit.
|
||||
*/
|
||||
export const normalizeClientToken = (token: unknown): string | null => {
|
||||
return typeof token === 'string' && CLIENT_TOKEN_RE.test(token) ? token.toLowerCase() : null;
|
||||
};
|
||||
|
||||
// Scoped to the event: the token is per form visit, and one visit can move
|
||||
// on to another concert's form, so a match for a different event is not a
|
||||
// retry of this one.
|
||||
const findSubmissionIdByClientToken = async (clientToken: string, slug: string): Promise<number | null> => {
|
||||
const conn = await NachklangFeedbackDB.getConnection();
|
||||
try {
|
||||
const rows = await conn.query(
|
||||
'SELECT s.submission_id FROM submissions s JOIN events e ON e.event_id = s.event_id WHERE s.client_token = ? AND e.slug = ?',
|
||||
[clientToken, slug]
|
||||
);
|
||||
return rows.length > 0 ? rows[0].submission_id : null;
|
||||
} finally {
|
||||
await conn.end();
|
||||
}
|
||||
};
|
||||
|
||||
export type SubmitResult =
|
||||
| { status: 'OK'; submissionId: number; newsletterDropped: boolean }
|
||||
| { status: 'NOT_FOUND' }
|
||||
@@ -146,9 +175,29 @@ export type SubmitResult =
|
||||
* Validates and persists one feedback submission. Re-checks event
|
||||
* eligibility (the window may have closed between page load and submit),
|
||||
* validates every answer against the event's live questions/songs, then
|
||||
* inserts everything in a single transaction.
|
||||
* inserts everything in a single transaction. A retry carrying an already
|
||||
* stored client token gets the original submission back instead of a copy.
|
||||
*/
|
||||
export const submitFeedback = async (slug: string, body: SubmissionRequestBody, ipHash: string | null): Promise<SubmitResult> => {
|
||||
const clientToken = normalizeClientToken(body.clientToken);
|
||||
const newsletter = validateNewsletter(body.newsletter);
|
||||
// body.newsletter is only sent at all when the visitor had the opt-in
|
||||
// checkbox on (see FeedbackForm.tsx), so a present-but-invalid block
|
||||
// (e.g. a mistyped email) is distinguishable from "didn't opt in" - the
|
||||
// rest of the submission still saves, but the client can tell the
|
||||
// visitor their newsletter signup specifically didn't go through.
|
||||
const newsletterDropped = !!body.newsletter && !newsletter;
|
||||
|
||||
// Checked before eligibility, so a retry still succeeds if the feedback
|
||||
// window closed in the seconds after the original saved.
|
||||
if (clientToken) {
|
||||
const existingId = await findSubmissionIdByClientToken(clientToken, slug);
|
||||
if (existingId !== null) {
|
||||
logger.info('Feedback submission retry answered with the stored submission', {submissionId: existingId});
|
||||
return {status: 'OK', submissionId: existingId, newsletterDropped};
|
||||
}
|
||||
}
|
||||
|
||||
const lookup = await getEventConfigBySlug(slug);
|
||||
if (lookup.status === 'NOT_FOUND') return {status: 'NOT_FOUND'};
|
||||
if (lookup.status === 'CLOSED') return {status: 'CLOSED'};
|
||||
@@ -159,13 +208,6 @@ export const submitFeedback = async (slug: string, body: SubmissionRequestBody,
|
||||
|
||||
const answerRows = validateAnswers(body.answers || [], questionsById, songTitleById);
|
||||
const guestBook = validateGuestBook(body.guestBook);
|
||||
const newsletter = validateNewsletter(body.newsletter);
|
||||
// body.newsletter is only sent at all when the visitor had the opt-in
|
||||
// checkbox on (see FeedbackForm.tsx), so a present-but-invalid block
|
||||
// (e.g. a mistyped email) is distinguishable from "didn't opt in" - the
|
||||
// rest of the submission still saves, but the client can tell the
|
||||
// visitor their newsletter signup specifically didn't go through.
|
||||
const newsletterDropped = !!body.newsletter && !newsletter;
|
||||
|
||||
if (answerRows.length === 0 && !guestBook && !newsletter) {
|
||||
return {status: 'EMPTY'};
|
||||
@@ -175,8 +217,8 @@ export const submitFeedback = async (slug: string, body: SubmissionRequestBody,
|
||||
try {
|
||||
await conn.beginTransaction();
|
||||
|
||||
const subQuery = 'INSERT INTO submissions (event_id, ip_hash, has_guestbook, has_newsletter) VALUES (?,?,?,?) RETURNING submission_id';
|
||||
const subRes = await conn.query(subQuery, [eventId, ipHash, guestBook ? 1 : 0, newsletter ? 1 : 0]);
|
||||
const subQuery = 'INSERT INTO submissions (event_id, ip_hash, has_guestbook, has_newsletter, client_token) VALUES (?,?,?,?,?) RETURNING submission_id';
|
||||
const subRes = await conn.query(subQuery, [eventId, ipHash, guestBook ? 1 : 0, newsletter ? 1 : 0, clientToken]);
|
||||
const submissionId = subRes[0].submission_id;
|
||||
|
||||
for (const row of answerRows) {
|
||||
@@ -220,8 +262,16 @@ export const submitFeedback = async (slug: string, body: SubmissionRequestBody,
|
||||
}
|
||||
|
||||
return {status: 'OK', submissionId, newsletterDropped};
|
||||
} catch (err) {
|
||||
} catch (err: any) {
|
||||
await conn.rollback();
|
||||
// Two copies of the same retry both got past the lookup above; the
|
||||
// unique index on (event_id, client_token) let only one of them commit.
|
||||
if (clientToken && err?.code === 'ER_DUP_ENTRY') {
|
||||
const existingId = await findSubmissionIdByClientToken(clientToken, slug);
|
||||
if (existingId !== null) {
|
||||
return {status: 'OK', submissionId: existingId, newsletterDropped};
|
||||
}
|
||||
}
|
||||
throw err;
|
||||
} finally {
|
||||
await conn.end();
|
||||
|
||||
@@ -68,6 +68,8 @@ const validEvent = {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
process.env.MEMBER_CREDENTIAL = 'member-secret';
|
||||
process.env.CHOIR_CREDENTIAL = 'choir & more';
|
||||
process.env.MANAGEMENT_CREDENTIAL = '';
|
||||
(EventService.getAllEvents as any).mockResolvedValue([]);
|
||||
(EventService.getAllEventsAdmin as any).mockResolvedValue([]);
|
||||
(EventService.getNextUpcomingEvent as any).mockResolvedValue({eventId: 1, name: 'Konzert'});
|
||||
@@ -164,6 +166,13 @@ describe('reading', () => {
|
||||
expect(auth.api.getSession).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('serves the iCal export as text/calendar', async () => {
|
||||
const res = await request(app).get('/calendar/events/public/ical').expect(200);
|
||||
|
||||
expect(res.headers['content-type']).toBe('text/calendar; charset=utf-8');
|
||||
expect(res.headers['content-disposition']).toBe('attachment; filename=Nachklang_calendar.ics');
|
||||
});
|
||||
|
||||
it('keeps the shared password working on the iCal export', async () => {
|
||||
(EventService.getAllEvents as any).mockResolvedValue([]);
|
||||
|
||||
@@ -222,3 +231,43 @@ describe('writing', () => {
|
||||
.expect(401);
|
||||
});
|
||||
});
|
||||
|
||||
describe('subscriptions', () => {
|
||||
it('is not for anyone signed out, or signed in without the calendar permission', async () => {
|
||||
await request(app).get('/calendar/events/subscriptions').expect(401);
|
||||
|
||||
signedInAs(['tickets']);
|
||||
await request(app).get('/calendar/events/subscriptions').expect(403);
|
||||
});
|
||||
|
||||
it('refuses the shared password as a way in', async () => {
|
||||
// Otherwise one calendar's password would unlock all the others.
|
||||
await request(app).get('/calendar/events/subscriptions').query({password: 'member-secret'}).expect(401);
|
||||
});
|
||||
|
||||
it('lists a ready-made iCal URL per configured calendar, never cached', async () => {
|
||||
signedInAs(['calendar']);
|
||||
|
||||
const res = await request(app).get('/calendar/events/subscriptions').expect(200);
|
||||
|
||||
expect(res.headers['cache-control']).toBe('no-store');
|
||||
expect(res.body).toEqual([
|
||||
{calendar: 'public', icalUrl: 'http://localhost:3000/calendar/events/public/ical'},
|
||||
{calendar: 'members', icalUrl: 'http://localhost:3000/calendar/events/members/ical?password=member-secret'},
|
||||
// URL-encoded, so a password with & or spaces survives.
|
||||
{calendar: 'choir', icalUrl: 'http://localhost:3000/calendar/events/choir/ical?password=choir%20%26%20more'},
|
||||
// management is missing: its credential is unset, and a URL without one could never work.
|
||||
{calendar: 'birthdays', icalUrl: 'http://localhost:3000/calendar/events/birthdays/ical?password=choir%20%26%20more'}
|
||||
]);
|
||||
});
|
||||
|
||||
it('hands out URLs that actually open the calendar', async () => {
|
||||
signedInAs(['calendar']);
|
||||
const res = await request(app).get('/calendar/events/subscriptions').expect(200);
|
||||
const members = res.body.find((s: {calendar: string}) => s.calendar === 'members');
|
||||
|
||||
signedOut();
|
||||
const path = new URL(members.icalUrl);
|
||||
await request(app).get(path.pathname + path.search).expect(200);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,111 @@
|
||||
import {describe, expect, it} from 'vitest';
|
||||
|
||||
import {convertToIcal, escapeText, foldLine} from '../../src/models/calendar/events/icalgenerator.service.js';
|
||||
import {Event} from '../../src/models/calendar/events/event.interface.js';
|
||||
|
||||
/**
|
||||
* The iCal export is read by every subscribed calendar app, so a malformed
|
||||
* file is an outage nobody on our side sees. These pin RFC 5545's text
|
||||
* escaping and line folding, which the generator did not do before: a line
|
||||
* break in a description ended the DESCRIPTION property early, and the new
|
||||
* calendar frontend's multi-line description field would have produced
|
||||
* exactly that.
|
||||
*/
|
||||
|
||||
const event = (overrides: Partial<Event> = {}): Event => ({
|
||||
eventId: 1,
|
||||
calendarId: 1,
|
||||
uuid: 'uuid-1',
|
||||
name: 'Konzert',
|
||||
description: '',
|
||||
startDateTime: new Date('2026-10-03T17:00:00Z'),
|
||||
endDateTime: new Date('2026-10-03T19:00:00Z'),
|
||||
createdDate: new Date('2026-01-01T12:00:00Z'),
|
||||
location: '',
|
||||
createdBy: 'Anna',
|
||||
url: '',
|
||||
wholeDay: false,
|
||||
repeatFrequency: '',
|
||||
...overrides
|
||||
});
|
||||
|
||||
/** RFC 5545 unfolding: a CRLF followed by one space or tab joins the lines. */
|
||||
const unfold = (ical: string): string[] => ical.replace(/\r\n[ \t]/g, '').split('\r\n').filter(Boolean);
|
||||
|
||||
describe('escapeText', () => {
|
||||
it('escapes backslash, semicolon, comma and line breaks', () => {
|
||||
expect(escapeText('a\\b;c,d')).toBe('a\\\\b\\;c\\,d');
|
||||
expect(escapeText('Zeile 1\nZeile 2\r\nZeile 3\rZeile 4')).toBe('Zeile 1\\nZeile 2\\nZeile 3\\nZeile 4');
|
||||
});
|
||||
|
||||
it('escapes the backslash first, so the others are not doubled', () => {
|
||||
expect(escapeText(';')).toBe('\\;');
|
||||
});
|
||||
});
|
||||
|
||||
describe('foldLine', () => {
|
||||
it('leaves a short line alone', () => {
|
||||
expect(foldLine('SUMMARY:Konzert')).toBe('SUMMARY:Konzert');
|
||||
});
|
||||
|
||||
it('keeps every physical line within 75 octets and unfolds back to the original', () => {
|
||||
const line = 'DESCRIPTION:' + 'Probe im Gemeindehaus Süd – bitte Noten mitbringen. '.repeat(6);
|
||||
const folded = foldLine(line);
|
||||
for (const physical of folded.split('\r\n')) {
|
||||
expect(Buffer.byteLength(physical, 'utf8')).toBeLessThanOrEqual(75);
|
||||
}
|
||||
expect(folded.replace(/\r\n /g, '')).toBe(line);
|
||||
});
|
||||
|
||||
it('never splits a multi-byte character', () => {
|
||||
const line = 'SUMMARY:' + 'ü'.repeat(80);
|
||||
for (const physical of foldLine(line).split('\r\n')) {
|
||||
expect(physical.replace(/^ /, '')).toMatch(/^(SUMMARY:)?ü*$/);
|
||||
expect(Buffer.byteLength(physical, 'utf8')).toBeLessThanOrEqual(75);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertToIcal', () => {
|
||||
it('terminates every line with CRLF', async () => {
|
||||
const ical = await convertToIcal([event()]);
|
||||
expect(ical.endsWith('END:VCALENDAR\r\n')).toBe(true);
|
||||
expect(ical.replace(/\r\n/g, '')).not.toContain('\n');
|
||||
});
|
||||
|
||||
it('keeps a multi-line description inside one DESCRIPTION property', async () => {
|
||||
const ical = await convertToIcal([event({description: 'Einlass 18:30\nBeginn 19:00; Eintritt frei, Spenden willkommen'})]);
|
||||
const lines = unfold(ical);
|
||||
expect(lines).toContain('DESCRIPTION:Einlass 18:30\\nBeginn 19:00\\; Eintritt frei\\, Spenden willkommen');
|
||||
// Nothing leaked out as a line of its own.
|
||||
expect(lines.some((l) => l.startsWith('Beginn'))).toBe(false);
|
||||
});
|
||||
|
||||
it('escapes the title and the location too', async () => {
|
||||
const lines = unfold(await convertToIcal([event({name: 'Konzert, Teil 2', location: 'Kirche; Karlsruhe'})]));
|
||||
expect(lines).toContain('SUMMARY:Konzert\\, Teil 2');
|
||||
expect(lines).toContain('LOCATION:Kirche\\; Karlsruhe');
|
||||
});
|
||||
|
||||
it('does not escape the URL, but keeps it on one line', async () => {
|
||||
const lines = unfold(await convertToIcal([event({url: 'https://nachklang.art/konzert?a=1,2'})]));
|
||||
expect(lines).toContain('URL:https://nachklang.art/konzert?a=1,2');
|
||||
});
|
||||
|
||||
it('still writes the yearly rule for birthdays', async () => {
|
||||
const lines = unfold(await convertToIcal([event({repeatFrequency: 'YEARLY', wholeDay: true})]));
|
||||
expect(lines).toContain('RRULE:FREQ=YEARLY');
|
||||
});
|
||||
|
||||
it('leaves the event\'s own dates untouched', async () => {
|
||||
// formatDate used to add the whole-day end's extra day to the caller's Date in place.
|
||||
const whole = event({
|
||||
wholeDay: true,
|
||||
startDateTime: new Date('2026-10-03T00:00:00'),
|
||||
endDateTime: new Date('2026-10-03T23:59:00')
|
||||
});
|
||||
const endBefore = whole.endDateTime.getTime();
|
||||
await convertToIcal([whole]);
|
||||
expect(whole.endDateTime.getTime()).toBe(endBefore);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,143 @@
|
||||
import {vi, describe, it, expect, beforeEach, type Mock} from 'vitest';
|
||||
vi.mock('../../src/models/feedback/Feedback.db.js', () => ({
|
||||
NachklangFeedbackDB: {getConnection: vi.fn()}
|
||||
}));
|
||||
vi.mock('../../src/models/feedback/public/events.public.service.js', () => ({
|
||||
getEventConfigBySlug: vi.fn()
|
||||
}));
|
||||
vi.mock('../../src/models/feedback/integrations/salesforce.service.js', () => ({
|
||||
syncNewsletterSignup: vi.fn().mockResolvedValue(undefined)
|
||||
}));
|
||||
vi.mock('../../src/middleware/logger.js', () => ({
|
||||
__esModule: true,
|
||||
default: {info: vi.fn(), error: vi.fn()}
|
||||
}));
|
||||
|
||||
import {NachklangFeedbackDB} from '../../src/models/feedback/Feedback.db.js';
|
||||
import {getEventConfigBySlug} from '../../src/models/feedback/public/events.public.service.js';
|
||||
import {submitFeedback} from '../../src/models/feedback/public/submissions.service.js';
|
||||
|
||||
const mockGetConnection = NachklangFeedbackDB.getConnection as Mock;
|
||||
const mockGetEventConfigBySlug = getEventConfigBySlug as Mock;
|
||||
|
||||
const TOKEN = '3f2b8c1e-4d5a-4b6c-9e7f-0a1b2c3d4e5f';
|
||||
const BODY = {answers: [{eventQuestionId: 26, text: 'Danke!'}], clientToken: TOKEN};
|
||||
|
||||
const SLUG = 'sommerkonzert-2026';
|
||||
const LOOKUP_SQL = 'SELECT s.submission_id FROM submissions s';
|
||||
|
||||
// One fake connection serves every query; `existingId` is what a lookup by
|
||||
// client_token finds when it was stored for `existingSlug`, and
|
||||
// `insertError` makes the submissions INSERT throw.
|
||||
const makeConn = ({existingId = null, existingSlug = SLUG, insertError = null}: {existingId?: number | null; existingSlug?: string; insertError?: any} = {}) => ({
|
||||
query: vi.fn(async (sql: string, params: any[] = []) => {
|
||||
if (sql.startsWith(LOOKUP_SQL)) {
|
||||
return existingId === null || params[1] !== existingSlug ? [] : [{submission_id: existingId}];
|
||||
}
|
||||
if (sql.startsWith('INSERT INTO submissions')) {
|
||||
if (insertError) throw insertError;
|
||||
return [{submission_id: 90}];
|
||||
}
|
||||
return [];
|
||||
}),
|
||||
beginTransaction: vi.fn().mockResolvedValue(undefined),
|
||||
commit: vi.fn().mockResolvedValue(undefined),
|
||||
rollback: vi.fn().mockResolvedValue(undefined),
|
||||
end: vi.fn().mockResolvedValue(undefined)
|
||||
});
|
||||
|
||||
const allQueries = (conn: ReturnType<typeof makeConn>): [string, any[]][] => conn.query.mock.calls as any;
|
||||
|
||||
describe('submitFeedback - client token idempotency', () => {
|
||||
beforeEach(() => {
|
||||
mockGetConnection.mockReset();
|
||||
mockGetEventConfigBySlug.mockReset();
|
||||
mockGetEventConfigBySlug.mockResolvedValue({
|
||||
status: 'OK',
|
||||
eventId: 22,
|
||||
event: {
|
||||
questions: [{eventQuestionId: 26, questionId: 4, type: 'FREE_TEXT', label: 'Sonstiges', position: 0}],
|
||||
songs: []
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('returns the already-saved submission for a repeated token without inserting again', async () => {
|
||||
const conn = makeConn({existingId: 82});
|
||||
mockGetConnection.mockResolvedValue(conn);
|
||||
|
||||
const result = await submitFeedback('sommerkonzert-2026', BODY, 'hash');
|
||||
|
||||
expect(result).toEqual({status: 'OK', submissionId: 82, newsletterDropped: false});
|
||||
expect(allQueries(conn).some(([sql]) => sql.startsWith('INSERT'))).toBe(false);
|
||||
});
|
||||
|
||||
it('still answers a retry with the saved submission after the feedback window closed', async () => {
|
||||
mockGetConnection.mockResolvedValue(makeConn({existingId: 82}));
|
||||
mockGetEventConfigBySlug.mockResolvedValue({status: 'CLOSED'});
|
||||
|
||||
const result = await submitFeedback('sommerkonzert-2026', BODY, 'hash');
|
||||
|
||||
expect(result).toEqual({status: 'OK', submissionId: 82, newsletterDropped: false});
|
||||
});
|
||||
|
||||
it('stores a new token, lowercased, on the submissions row', async () => {
|
||||
const conn = makeConn();
|
||||
mockGetConnection.mockResolvedValue(conn);
|
||||
|
||||
const result = await submitFeedback('sommerkonzert-2026', {...BODY, clientToken: TOKEN.toUpperCase()}, 'hash');
|
||||
|
||||
expect(result).toEqual({status: 'OK', submissionId: 90, newsletterDropped: false});
|
||||
const insert = allQueries(conn).find(([sql]) => sql.startsWith('INSERT INTO submissions'))!;
|
||||
expect(insert[1]).toContain(TOKEN);
|
||||
});
|
||||
|
||||
it('saves a new submission when the token was only ever used for a different event', async () => {
|
||||
// Same browser tab, two concerts: the token must not make the second
|
||||
// event's feedback come back as the first event's submission.
|
||||
const conn = makeConn({existingId: 82, existingSlug: 'winterkonzert-2026'});
|
||||
mockGetConnection.mockResolvedValue(conn);
|
||||
|
||||
const result = await submitFeedback(SLUG, BODY, 'hash');
|
||||
|
||||
expect(result).toEqual({status: 'OK', submissionId: 90, newsletterDropped: false});
|
||||
expect(allQueries(conn).find(([sql]) => sql.startsWith(LOOKUP_SQL))![1]).toEqual([TOKEN, SLUG]);
|
||||
});
|
||||
|
||||
it('ignores a malformed token: no lookup, and the row is stored without one', async () => {
|
||||
const conn = makeConn();
|
||||
mockGetConnection.mockResolvedValue(conn);
|
||||
|
||||
await submitFeedback('sommerkonzert-2026', {...BODY, clientToken: 'not-a-uuid'}, 'hash');
|
||||
|
||||
expect(allQueries(conn).some(([sql]) => sql.startsWith(LOOKUP_SQL))).toBe(false);
|
||||
const insert = allQueries(conn).find(([sql]) => sql.startsWith('INSERT INTO submissions'))!;
|
||||
expect(insert[1]).not.toContain('not-a-uuid');
|
||||
expect(insert[1][insert[1].length - 1]).toBeNull();
|
||||
});
|
||||
|
||||
it('resolves a concurrent duplicate that loses the race on the unique index to the winning submission', async () => {
|
||||
const dupError = Object.assign(new Error('Duplicate entry'), {code: 'ER_DUP_ENTRY', errno: 1062});
|
||||
const racingConn = makeConn({insertError: dupError});
|
||||
// First lookup (before the INSERT) finds nothing; the one after the
|
||||
// duplicate-key error finds the row the other request committed.
|
||||
const lookupAfterRace = makeConn({existingId: 83});
|
||||
mockGetConnection
|
||||
.mockResolvedValueOnce(makeConn())
|
||||
.mockResolvedValueOnce(racingConn)
|
||||
.mockResolvedValueOnce(lookupAfterRace);
|
||||
|
||||
const result = await submitFeedback('sommerkonzert-2026', BODY, 'hash');
|
||||
|
||||
expect(result).toEqual({status: 'OK', submissionId: 83, newsletterDropped: false});
|
||||
expect(racingConn.rollback).toHaveBeenCalled();
|
||||
expect(racingConn.commit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rethrows a duplicate-key error when the request carried no token', async () => {
|
||||
const dupError = Object.assign(new Error('Duplicate entry'), {code: 'ER_DUP_ENTRY', errno: 1062});
|
||||
mockGetConnection.mockResolvedValue(makeConn({insertError: dupError}));
|
||||
|
||||
await expect(submitFeedback('sommerkonzert-2026', {answers: BODY.answers}, 'hash')).rejects.toThrow('Duplicate entry');
|
||||
});
|
||||
});
|
||||
@@ -14,10 +14,6 @@ export default defineConfig({
|
||||
env: {
|
||||
FEEDBACK_IP_SALT: 'vitest-salt'
|
||||
},
|
||||
reporters: [
|
||||
'default',
|
||||
['vitest-sonar-reporter', {outputFile: 'testResults/sonar-report.xml'}]
|
||||
],
|
||||
coverage: {
|
||||
provider: 'v8',
|
||||
reporter: ['text-summary', 'lcov'],
|
||||
|
||||
Reference in new issue
Block a user