2022-10-26 13:00:49 +02:00
|
|
|
import express from 'express';
|
|
|
|
import { conditionalMiddleware } from '../../../../lib/middleware/conditional-middleware';
|
|
|
|
import supertest from 'supertest';
|
|
|
|
|
|
|
|
test('disabled middleware should not block paths that use the same path', async () => {
|
|
|
|
const app = express();
|
|
|
|
const path = '/api/admin/projects';
|
|
|
|
|
|
|
|
app.use(
|
|
|
|
path,
|
|
|
|
conditionalMiddleware(
|
|
|
|
() => false,
|
|
|
|
(req, res) => {
|
2022-11-02 07:34:14 +01:00
|
|
|
res.send({ changeRequest: 'hello' });
|
2022-10-26 13:00:49 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(path, (req, res) => {
|
|
|
|
res.json({ projects: [] });
|
|
|
|
});
|
|
|
|
|
|
|
|
await supertest(app)
|
|
|
|
.get('/api/admin/projects')
|
|
|
|
.expect(200, { projects: [] });
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return 404 when path is not enabled', async () => {
|
|
|
|
const app = express();
|
|
|
|
const path = '/api/admin/projects';
|
|
|
|
|
|
|
|
app.use(
|
2022-11-02 07:34:14 +01:00
|
|
|
`${path}/change-requests`,
|
2022-10-26 13:00:49 +02:00
|
|
|
conditionalMiddleware(
|
|
|
|
() => false,
|
|
|
|
(req, res) => {
|
2022-11-02 07:34:14 +01:00
|
|
|
res.send({ changeRequest: 'hello' });
|
2022-10-26 13:00:49 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(path, (req, res) => {
|
|
|
|
res.json({ projects: [] });
|
|
|
|
});
|
|
|
|
|
2022-11-02 07:34:14 +01:00
|
|
|
await supertest(app).get('/api/admin/projects/change-requests').expect(404);
|
2022-10-26 13:00:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should respect ordering of endpoints', async () => {
|
|
|
|
const app = express();
|
|
|
|
const path = '/api/admin/projects';
|
|
|
|
|
|
|
|
app.use(
|
|
|
|
path,
|
|
|
|
conditionalMiddleware(
|
|
|
|
() => true,
|
|
|
|
(req, res) => {
|
2022-11-02 07:34:14 +01:00
|
|
|
res.json({ name: 'Request changes' });
|
2022-10-26 13:00:49 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(path, (req, res) => {
|
|
|
|
res.json({ projects: [] });
|
|
|
|
});
|
|
|
|
|
|
|
|
await supertest(app)
|
|
|
|
.get('/api/admin/projects')
|
2022-11-02 07:34:14 +01:00
|
|
|
.expect(200, { name: 'Request changes' });
|
2022-10-26 13:00:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('disabled middleware should not block paths that use the same basepath', async () => {
|
|
|
|
const app = express();
|
|
|
|
const path = '/api/admin/projects';
|
|
|
|
|
|
|
|
app.use(
|
2022-11-02 07:34:14 +01:00
|
|
|
`${path}/change-requests`,
|
2022-10-26 13:00:49 +02:00
|
|
|
conditionalMiddleware(
|
|
|
|
() => false,
|
|
|
|
(req, res) => {
|
2022-11-02 07:34:14 +01:00
|
|
|
res.json({ name: 'Request changes' });
|
2022-10-26 13:00:49 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(path, (req, res) => {
|
|
|
|
res.json({ projects: [] });
|
|
|
|
});
|
|
|
|
|
|
|
|
await supertest(app)
|
|
|
|
.get('/api/admin/projects')
|
|
|
|
.expect(200, { projects: [] });
|
|
|
|
});
|