From d7c6492f2d0d0224951eb1e357eb1484dc225448 Mon Sep 17 00:00:00 2001 From: Quentin Roy Date: Thu, 2 Nov 2023 16:48:07 +0100 Subject: [PATCH] log-server: fix tests, server app, and cli These were broken after a39df7e8737a1004d1cae4beaa67bc3c8b702a63, which changed the admin role to the host role --- packages/log-server/__tests__/app.test.ts | 26 +++++++++++------------ packages/log-server/src/app.ts | 10 +++------ packages/log-server/src/cli.ts | 16 +++++++------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/packages/log-server/__tests__/app.test.ts b/packages/log-server/__tests__/app.test.ts index 893b13f0..27c1c112 100644 --- a/packages/log-server/__tests__/app.test.ts +++ b/packages/log-server/__tests__/app.test.ts @@ -96,43 +96,43 @@ describe('sessions', () => { await api.post('/sessions').send({ role: 'fake' }).expect(400); }); - it('should accept the creation of an admin session if there is no admin password set on the server', async () => { - await api.post('/sessions').send({ role: 'admin' }).expect(201, { - role: 'admin', + it('should accept the creation of an host session if there is no host password set on the server', async () => { + await api.post('/sessions').send({ role: 'host' }).expect(201, { + role: 'host', runs: [], status: 'ok', }); }); - it('should accept the creation of an admin role if the provided password is correct', async () => { + it('should accept the creation of an host role if the provided password is correct', async () => { let app = LogServer({ store: MockStore(), secret: 'secret', - adminPassword: 'admin password', + hostPassword: 'host password', }); let api = request(app); await api .post('/sessions') - .send({ role: 'admin', password: 'admin password' }) + .send({ role: 'host', password: 'host password' }) .expect(201, { - role: 'admin', + role: 'host', runs: [], status: 'ok', }); }); - it('should refuse the creation of an admin role if the provided password is incorrect', async () => { + it('should refuse the creation of an host role if the provided password is incorrect', async () => { let app = LogServer({ store: MockStore(), secret: 'secret', - adminPassword: 'admin password', + hostPassword: 'host password', }); let req = request(app); await req .post('/sessions') - .send({ role: 'admin', password: 'not the admin password' }) + .send({ role: 'host', password: 'not the host password' }) .expect(403, { - message: 'Forbidden role: admin', + message: 'Forbidden role: host', status: 'error', }); }); @@ -589,13 +589,13 @@ describe('logs', () => { secureCookies: false, }); api = request.agent(app); - await api.post('/sessions').send({ role: 'admin' }); + await api.post('/sessions').send({ role: 'host' }); }); afterEach(() => { vi.useRealTimers(); }); - it('should refuse to fetch logs if the client is not logged as an admin', async () => { + it('should refuse to fetch logs if the client is not logged as an host', async () => { await api.delete('/sessions/current').expect(200); await api.post('/sessions').send({ role: 'participant' }).expect(201); await api.get('/experiments/exp/logs').expect(403, { diff --git a/packages/log-server/src/app.ts b/packages/log-server/src/app.ts index 35e3e0fb..3eaebefc 100644 --- a/packages/log-server/src/app.ts +++ b/packages/log-server/src/app.ts @@ -30,14 +30,14 @@ const ctx = zodiosContext( type CreateLogServerOptions = { store: Store; secret: string; - adminPassword?: string; + hostPassword?: string; allowCrossOrigin?: boolean; secureCookies?: boolean; }; export function LogServer({ store, secret, - adminPassword, + hostPassword, allowCrossOrigin = true, secureCookies = allowCrossOrigin, }: CreateLogServerOptions): RequestHandler { @@ -68,11 +68,7 @@ export function LogServer({ return; } const { role, password } = req.body; - if ( - role === 'host' && - adminPassword != null && - password !== adminPassword - ) { + if (role === 'host' && hostPassword != null && password !== hostPassword) { res .status(403) .json({ status: 'error', message: `Forbidden role: ${role}` }); diff --git a/packages/log-server/src/cli.ts b/packages/log-server/src/cli.ts index bb4ef456..997d484f 100644 --- a/packages/log-server/src/cli.ts +++ b/packages/log-server/src/cli.ts @@ -23,7 +23,7 @@ const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); const env = z .object({ SECRET: z.string().optional(), - ADMIN_PASSWORD: z.string().optional(), + HOST_PASSWORD: z.string().optional(), PORT: z.number().default(3000), DB_PATH: z.string().default('./data.sqlite'), LOG_LEVEL: z @@ -50,13 +50,13 @@ type StartParameter = { database: string; port: number; secret?: string; - adminPassword?: string; + hostPassword?: string; }; async function start({ database: dbPath, port, secret, - adminPassword, + hostPassword, }: StartParameter) { if (secret == null) { log.error( @@ -79,7 +79,7 @@ async function start({ } let server = express() .use(cors()) - .use(LogServer({ store, secret, adminPassword })) + .use(LogServer({ store, secret, hostPassword })) .listen(port, () => { log.info(`Listening on port ${port}`); }); @@ -176,11 +176,11 @@ yargs(process.argv.slice(2)) type: 'string', default: env.SECRET, }) - .option('admin-password', { - alias: 'a', - desc: 'Password for the admin user', + .option('host-password', { + alias: 'w', + desc: 'Password for the host user', type: 'string', - default: env.ADMIN_PASSWORD, + default: env.HOST_PASSWORD, }) .help() .alias('help', 'h')