Skip to content

Commit

Permalink
log-server: fix tests, server app, and cli
Browse files Browse the repository at this point in the history
These were broken after a39df7e, which changed the admin role to the host role
  • Loading branch information
QuentinRoy committed Nov 2, 2023
1 parent 6107507 commit d7c6492
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
26 changes: 13 additions & 13 deletions packages/log-server/__tests__/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});
Expand Down Expand Up @@ -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, {
Expand Down
10 changes: 3 additions & 7 deletions packages/log-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}` });
Expand Down
16 changes: 8 additions & 8 deletions packages/log-server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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}`);
});
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit d7c6492

Please sign in to comment.