199 lines
8.8 KiB
JavaScript
199 lines
8.8 KiB
JavaScript
// Tests for the media files that stand in the note tree beside the notes (the sidebar lists
|
|
// images, sounds, videos and documents with an icon of their type, the way a file explorer does).
|
|
//
|
|
// The repository root installs no dependencies, so the media routes run against a stubbed
|
|
// `require`, the note lists are read out of the real route file the same way, and the sidebar and
|
|
// nested list are read as text like tests/sidebar-entity-entry.test.js reads them.
|
|
//
|
|
// Run with `npm test` from the repository root, or `node --test tests/`.
|
|
|
|
const { test } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const vm = require("node:vm");
|
|
|
|
const read = (...parts) => fs.readFileSync(path.join(__dirname, "..", ...parts), "utf8");
|
|
|
|
function loadRoutes(file, models) {
|
|
const routes = new Map();
|
|
const router = {
|
|
get: (route, ...handlers) => routes.set(`GET ${route}`, handlers),
|
|
post: (route, ...handlers) => routes.set(`POST ${route}`, handlers),
|
|
};
|
|
|
|
const requireStub = (id) => {
|
|
if (id === "express") return { Router: () => router };
|
|
if (id === "path") return path;
|
|
// The upload folder and the file it drops are not what these checks are about.
|
|
if (id === "fs") return { unlink: (file, done) => done() };
|
|
if (id === "../services/storage") return { single: () => (req, res, next) => next() };
|
|
if (models[id]) return models[id];
|
|
throw new Error(`require("${id}") is not stubbed`);
|
|
};
|
|
|
|
const moduleStub = { exports: {} };
|
|
vm.compileFunction(read("backend", "src", "routes", file), ["require", "module", "exports", "__dirname"])(
|
|
requireStub, moduleStub, moduleStub.exports, path.join(__dirname, "..", "backend", "src", "routes")
|
|
);
|
|
|
|
return async function call(route, { body = {}, query = {}, file = null } = {}) {
|
|
const handlers = routes.get(route);
|
|
assert.ok(handlers, `${route} is not mounted`);
|
|
const res = { payload: null, json(payload) { this.payload = payload; } };
|
|
await handlers[handlers.length - 1]({ body, query, file, user: { id: "user-1" } }, res);
|
|
return res.payload;
|
|
};
|
|
}
|
|
|
|
function loadMediaRoutes({ ownsCampaign = true, media = null, folderOk = true } = {}) {
|
|
const deleted = [];
|
|
const api = loadRoutes("media.js", {
|
|
"../models/Campaign": { findOne: () => ({ lean: async () => (ownsCampaign ? { _id: "campaign-1" } : null) }) },
|
|
"../models/Folder": { belongsToSection: async () => folderOk },
|
|
"../models/Media": Object.assign(
|
|
function Media(data) { Object.assign(this, { _id: "media-new", save: async () => {} }, data); },
|
|
{
|
|
findById: async () => media,
|
|
deleteOne: async (filter) => { deleted.push(filter); return { deletedCount: 1 }; },
|
|
}
|
|
),
|
|
});
|
|
return { api, deleted };
|
|
}
|
|
|
|
function seededMedia() {
|
|
return {
|
|
_id: "media-1", campaign: "campaign-1", file: "file-1.png", mimeType: "image/png",
|
|
save: async () => {},
|
|
};
|
|
}
|
|
|
|
// --- Routes ---------------------------------------------------------------------
|
|
|
|
test("an uploaded file becomes a media row of the campaign", async () => {
|
|
const { api } = loadMediaRoutes();
|
|
const response = await api("POST /upload", {
|
|
body: { campaign: "campaign-1" },
|
|
file: { filename: "file-1.png", mimetype: "image/png", originalname: "harbour.png" },
|
|
});
|
|
|
|
assert.equal(response.status, "ok");
|
|
assert.equal(response.media.name, "harbour.png");
|
|
assert.equal(response.media.file, "file-1.png");
|
|
assert.equal(response.media.mimeType, "image/png");
|
|
assert.equal(response.media.folder, null);
|
|
});
|
|
|
|
test("an upload into another campaign is refused", async () => {
|
|
const { api } = loadMediaRoutes({ ownsCampaign: false });
|
|
const response = await api("POST /upload", {
|
|
body: { campaign: "campaign-1" },
|
|
file: { filename: "file-1.png", mimetype: "image/png", originalname: "harbour.png" },
|
|
});
|
|
|
|
assert.equal(response.msg, "unauthorized");
|
|
});
|
|
|
|
test("a media file only stands in a note folder", async () => {
|
|
const { api } = loadMediaRoutes({ folderOk: false });
|
|
const response = await api("POST /upload", {
|
|
body: { campaign: "campaign-1", folder: "actor-folder-1" },
|
|
file: { filename: "file-1.png", mimetype: "image/png", originalname: "harbour.png" },
|
|
});
|
|
|
|
assert.equal(response.msg, "errors.notfound");
|
|
});
|
|
|
|
test("a media row moves into a folder and back to the root", async () => {
|
|
const media = seededMedia();
|
|
const { api } = loadMediaRoutes({ media });
|
|
|
|
const moved = await api("POST /update", { body: { id: "media-1", folder: "folder-1" } });
|
|
assert.equal(moved.status, "ok");
|
|
assert.equal(media.folder, "folder-1");
|
|
|
|
await api("POST /update", { body: { id: "media-1", folder: null } });
|
|
assert.equal(media.folder, null);
|
|
});
|
|
|
|
test("deleting a media file of somebody else's campaign leaves it where it is", async () => {
|
|
const { api, deleted } = loadMediaRoutes({ media: seededMedia(), ownsCampaign: false });
|
|
const response = await api("POST /delete", { body: { id: "media-1" } });
|
|
|
|
assert.equal(response.msg, "unauthorized");
|
|
assert.equal(deleted.length, 0, "the row must not be dropped");
|
|
});
|
|
|
|
test("deleting a media file drops its row", async () => {
|
|
const { api, deleted } = loadMediaRoutes({ media: seededMedia() });
|
|
const response = await api("POST /delete", { body: { id: "media-1" } });
|
|
|
|
assert.equal(response.status, "ok");
|
|
assert.equal(deleted.length, 1);
|
|
});
|
|
|
|
test("a deleted note folder takes its media rows to the root with it", () => {
|
|
const folderRoute = read("backend", "src", "routes", "folder.js");
|
|
assert.match(folderRoute, /Media\.updateMany\(\s*\{ folder: folderId \}/);
|
|
});
|
|
|
|
// --- Both note lists carry the media standing at their level ---------------------
|
|
|
|
test("the note tree's root list carries the media at the root", () => {
|
|
const noteRoute = read("backend", "src", "routes", "note.js");
|
|
assert.match(noteRoute, /Media\.find\(\{ campaign, folder: null \}\)/);
|
|
const list = /router\.get\('\/list'[\s\S]*?res\.json\(\{ status: 'ok', folders, notes: rootNotes, media \}\)/.exec(noteRoute);
|
|
assert.ok(list, "expected /note/list to answer with media");
|
|
});
|
|
|
|
test("a folder's list carries the media standing in that folder", () => {
|
|
const noteRoute = read("backend", "src", "routes", "note.js");
|
|
assert.match(noteRoute, /Media\.find\(\{ campaign, folder \}\)/);
|
|
assert.match(noteRoute, /res\.json\(\{ status: 'ok', subfolders, notes, media \}\)/);
|
|
});
|
|
|
|
// --- The sidebar -----------------------------------------------------------------
|
|
|
|
test("the notes section offers a media upload", () => {
|
|
const sidebar = read("frontend", "app", "components", "partials", "ContentSidebar.vue");
|
|
assert.match(sidebar, /chooseMedia/);
|
|
assert.match(sidebar, /type="file" multiple @change="uploadMedia"/);
|
|
assert.match(sidebar, /Server\(\)\.post\('\/media\/upload'/);
|
|
});
|
|
|
|
test("the note tree passes each level's media down to its rows", () => {
|
|
const sidebar = read("frontend", "app", "components", "partials", "ContentSidebar.vue");
|
|
assert.match(sidebar, /:media="media"/);
|
|
|
|
const nested = read("frontend", "app", "components", "partials", "NestedNoteList.vue");
|
|
assert.match(nested, /:media="getFolderMedia\(folder\._id\)"/);
|
|
assert.match(nested, /v-for="item in media"/);
|
|
});
|
|
|
|
test("every media row leads with the icon of its type", () => {
|
|
const nested = read("frontend", "app", "components", "partials", "NestedNoteList.vue");
|
|
assert.match(nested, /type\.startsWith\('image\/'\)[\s\S]*media-image\.svg/);
|
|
assert.match(nested, /type\.startsWith\('audio\/'\)[\s\S]*music-note\.svg/);
|
|
assert.match(nested, /type\.startsWith\('video\/'\)[\s\S]*media-video\.svg/);
|
|
assert.match(nested, /application\/pdf[\s\S]*page\.svg/);
|
|
});
|
|
|
|
test("a media window is registered and reads the file from /api/public", () => {
|
|
const definitions = read("frontend", "app", "services", "WindowDefinitions.js");
|
|
assert.match(definitions, /media_viewer: \{[\s\S]*MediaViewerWindow\.vue/);
|
|
|
|
const viewer = read("frontend", "app", "components", "windows", "MediaViewerWindow.vue");
|
|
assert.match(viewer, /getBaseUrl\(\)\}\/public\/\$\{media\.file\}/);
|
|
});
|
|
|
|
test("every locale names the upload and the media window", () => {
|
|
for (const locale of ["en", "es", "ca"]) {
|
|
const campaigns = JSON.parse(read("frontend", "i18n", "locales", locale, "campaigns.json"));
|
|
const windows = JSON.parse(read("frontend", "i18n", "locales", locale, "windows.json"));
|
|
assert.ok(campaigns.media?.upload, `${locale} must name the media upload`);
|
|
assert.ok(campaigns.media?.["upload-error"], `${locale} must say an upload failed`);
|
|
assert.ok(windows["media-viewer"], `${locale} must name the media window`);
|
|
}
|
|
});
|