Spaces:
Paused
Paused
Download tests/iserv-notes.test.mjs from Luca448/APP-Backend: direct link, hf CLI and curl.
- Browser
- Download file 4.38 kB
-
https://huggingface.co/spaces/Luca448/APP-Backend/resolve/main/tests/iserv-notes.test.mjs
- Command line
-
hf download hf://spaces/Luca448/APP-Backend/tests/iserv-notes.test.mjs
-
curl -L -o iserv-notes.test.mjs https://huggingface.co/spaces/Luca448/APP-Backend/resolve/main/tests/iserv-notes.test.mjs
4.38 kB
| import assert from 'node:assert/strict'; | |
| import test, { after, beforeEach } from 'node:test'; | |
| import express from 'express'; | |
| import { isSafeIServPath, registerIServNotes } from '../src/api/iserv-notes.js'; | |
| const KEY = 'k'.repeat(20); | |
| process.env.NOTES_ACCESS_TOKEN = KEY; | |
| process.env.ISERV_BASE_URL = 'https://schule.example'; | |
| process.env.ISERV_USERNAME = 'user'; | |
| process.env.ISERV_PASSWORD = 'pass'; | |
| let overviewCalls = 0; | |
| let overviewImpl; | |
| let fileImpl; | |
| const app = express(); | |
| registerIServNotes(app, { | |
| overview: async (...args) => { | |
| overviewCalls += 1; | |
| return overviewImpl(...args); | |
| }, | |
| file: async (...args) => fileImpl(...args), | |
| }); | |
| const server = app.listen(0); | |
| const base = `http://127.0.0.1:${server.address().port}/api/notes/iserv`; | |
| const get = (path, key = KEY) => fetch(`${base}${path}`, { headers: key ? { 'x-app-key': key } : {} }); | |
| after(() => server.close()); | |
| beforeEach(() => { | |
| overviewCalls = 0; | |
| overviewImpl = async () => [{ id: 1, url: 'https://schule.example/iserv/exercise/show/1', title: 'Blatt', done: false }]; | |
| fileImpl = async () => ({ contentType: 'application/pdf', buffer: Buffer.from('PDF') }); | |
| }); | |
| test('isSafeIServPath only lets plain /iserv/ paths through', () => { | |
| assert.equal(isSafeIServPath('/iserv/fs/file/exercise/1'), true); | |
| assert.equal(isSafeIServPath('/iserv/fs/download/9?x=1'), true); | |
| for (const bad of ['/etc/passwd', '/iserv/../etc', '//evil.example/iserv/x', 'https://evil.example/iserv/x', '/iserv/a//b', '/iserv/a\\b', '', undefined]) { | |
| assert.equal(isSafeIServPath(bad), false, String(bad)); | |
| } | |
| }); | |
| test('no or short NOTES_ACCESS_TOKEN: closed with 503', async () => { | |
| const saved = process.env.NOTES_ACCESS_TOKEN; | |
| process.env.NOTES_ACCESS_TOKEN = 'short'; | |
| try { | |
| assert.equal((await get('/tasks', 'short')).status, 503); | |
| } finally { | |
| process.env.NOTES_ACCESS_TOKEN = saved; | |
| } | |
| }); | |
| test('missing IServ secrets: closed with 503', async () => { | |
| const saved = process.env.ISERV_PASSWORD; | |
| delete process.env.ISERV_PASSWORD; | |
| try { | |
| assert.equal((await get('/tasks')).status, 503); | |
| } finally { | |
| process.env.ISERV_PASSWORD = saved; | |
| } | |
| }); | |
| test('missing or wrong key is refused on both routes', async () => { | |
| for (const path of ['/tasks', '/file?path=/iserv/x']) { | |
| assert.equal((await get(path, '')).status, 401); | |
| assert.equal((await get(path, 'x'.repeat(20))).status, 401); | |
| } | |
| }); | |
| test('tasks: right key returns the list and a second call comes from the cache', async () => { | |
| const first = await get('/tasks'); | |
| assert.equal(first.status, 200); | |
| assert.equal(first.headers.get('cache-control'), 'no-store'); | |
| const body = await first.json(); | |
| assert.equal(body.tasks[0].title, 'Blatt'); | |
| assert.match(body.fetchedAt, /^\d{4}-\d{2}-\d{2}T/); | |
| await get('/tasks'); | |
| assert.equal(overviewCalls, 1); | |
| }); | |
| test('tasks: a failing fetch gives only the code, never the message', async () => { | |
| const failing = express(); | |
| registerIServNotes(failing, { | |
| overview: async () => { | |
| throw new Error('https://schule.example secret'); | |
| }, | |
| }); | |
| const s = failing.listen(0); | |
| try { | |
| const r = await fetch(`http://127.0.0.1:${s.address().port}/api/notes/iserv/tasks`, { headers: { 'x-app-key': KEY } }); | |
| assert.equal(r.status, 502); | |
| assert.equal(await r.text(), '{"error":"unreachable"}'); | |
| } finally { | |
| s.close(); | |
| } | |
| }); | |
| test('file: unsafe path is 400, safe path streams the bytes with the content type', async () => { | |
| assert.equal((await get('/file?path=/etc/passwd')).status, 400); | |
| const ok = await get('/file?path=%2Fiserv%2Ffs%2Ffile%2Fexercise%2F1'); | |
| assert.equal(ok.status, 200); | |
| assert.equal(ok.headers.get('content-type'), 'application/pdf'); | |
| assert.equal(Buffer.from(await ok.arrayBuffer()).toString(), 'PDF'); | |
| }); | |
| test('file: too_large is 413, refused is 502 with its code', async () => { | |
| fileImpl = async () => { | |
| throw Object.assign(new Error('x'), { code: 'too_large' }); | |
| }; | |
| const big = await get('/file?path=/iserv/fs/file/exercise/1'); | |
| assert.equal(big.status, 413); | |
| assert.deepEqual(await big.json(), { error: 'too_large' }); | |
| fileImpl = async () => { | |
| throw Object.assign(new Error('x'), { code: 'refused' }); | |
| }; | |
| const refused = await get('/file?path=/iserv/fs/file/exercise/1'); | |
| assert.equal(refused.status, 502); | |
| assert.deepEqual(await refused.json(), { error: 'refused' }); | |
| }); | |