Files
TREK/server/tests/unit/db/journey-share-protection-migration.test.ts
T
Maurice ede109dce8 feat(journey): complete sharing, photo drafts and Studio maps
Connect the Journey backend to the web and mobile UI, including public access and subscriptions, day recordings, photo drafts and per-map framing. Preserve legacy recording responses and cover the new behavior in every locale.
2026-11-30 16:56:18 +01:00

95 lines
4.0 KiB
TypeScript

/**
* `Migration20200101046620_a_journey_link_can_ask_for_a_password_and_run_out`:
* the password, expiry and view-count columns on `journey_share_tokens`.
* Migrate to the step before it with a published journey already there, apply
* just this one, and check that the existing link comes through unprotected,
* never expiring and uncounted, with every flag it had. On Postgres the same
* step runs in the Postgres lane, where PARITY-PG holds the migrated columns to
* the entity.
*/
import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step';
import { describe, expect, it } from 'vitest';
const TARGET = 'Migration20200101046620_a_journey_link_can_ask_for_a_password_and_run_out';
describe('journey_share_tokens protection migration', () => {
it('JSHAREPROT-MIG-001: adds the columns and leaves an existing link open, unexpiring and uncounted', async () => {
const orm = await createMigrationOrm();
try {
const names = await pendingNames(orm);
const idx = names.indexOf(TARGET);
expect(idx).toBeGreaterThan(0);
const previous = names[idx - 1];
if (!previous) throw new Error('no step before the target');
await migrateTo(orm, previous);
await rawExec(orm, "INSERT INTO users (id, username, email, password_hash) VALUES (1, 'owner', 'o@test', 'x')");
await rawExec(
orm,
"INSERT INTO journeys (id, user_id, title, created_at, updated_at) VALUES (5, 1, 'Iceland', 0, 0)",
);
await rawExec(
orm,
"INSERT INTO journey_share_tokens (id, journey_id, token, created_by, share_timeline, share_gallery, share_map, share_plan) VALUES (9, 5, 'tok', 1, 1, 0, 1, 1)",
);
await migrateTo(orm, TARGET);
const columns = await rawQuery<{ name: string; type: string; notnull: number; dflt_value: string | null }>(
orm,
"SELECT name, type, \"notnull\", dflt_value FROM pragma_table_info('journey_share_tokens') WHERE name IN ('password_hash', 'password_version', 'expires_at', 'view_count', 'last_viewed_at') ORDER BY cid",
);
expect(columns).toEqual([
{ name: 'password_hash', type: 'TEXT', notnull: 0, dflt_value: null },
{ name: 'password_version', type: 'INTEGER', notnull: 1, dflt_value: '0' },
{ name: 'expires_at', type: 'DATETIME', notnull: 0, dflt_value: null },
{ name: 'view_count', type: 'INTEGER', notnull: 1, dflt_value: '0' },
{ name: 'last_viewed_at', type: 'DATETIME', notnull: 0, dflt_value: null },
]);
expect(
await rawQuery(
orm,
'SELECT token, share_timeline, share_gallery, share_map, share_plan, password_hash, password_version, expires_at, view_count, last_viewed_at FROM journey_share_tokens WHERE id = 9',
),
).toEqual([
{
token: 'tok',
share_timeline: 1,
share_gallery: 0,
share_map: 1,
share_plan: 1,
password_hash: null,
password_version: 0,
expires_at: null,
view_count: 0,
last_viewed_at: null,
},
]);
} finally {
await orm.close(true);
}
});
it('JSHAREPROT-MIG-002: running it on a database that already has the columns changes nothing', async () => {
const orm = await createMigrationOrm();
try {
const names = await pendingNames(orm);
const previous = names[names.indexOf(TARGET) - 1];
if (!previous) throw new Error('no step before the target');
await migrateTo(orm, previous);
// A development build that added one of them by hand already.
await rawExec(orm, 'ALTER TABLE journey_share_tokens ADD COLUMN view_count INTEGER NOT NULL DEFAULT 0');
await migrateTo(orm, TARGET);
const columns = await rawQuery<{ name: string }>(
orm,
"SELECT name FROM pragma_table_info('journey_share_tokens') WHERE name = 'view_count'",
);
expect(columns).toHaveLength(1);
} finally {
await orm.close(true);
}
});
});