From f7cbfb97edd8ea5d01961a538b36aa585869cb98 Mon Sep 17 00:00:00 2001 From: Jussi Hallila Date: Wed, 28 Sep 2022 11:17:55 +0200 Subject: [PATCH] Modify router endpoint to handle singular and collections of request params similarly. Signed-off-by: Jussi Hallila --- .changeset/twelve-melons-notice.md | 5 + .../src/service/router.test.ts | 129 +++++++++++------- .../src/service/router.ts | 19 ++- 3 files changed, 104 insertions(+), 49 deletions(-) create mode 100644 .changeset/twelve-melons-notice.md diff --git a/.changeset/twelve-melons-notice.md b/.changeset/twelve-melons-notice.md new file mode 100644 index 0000000000..621d6325a7 --- /dev/null +++ b/.changeset/twelve-melons-notice.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-tech-insights-backend': patch +--- + +Modify router endpoint to handle singular and collections of request params similarly. diff --git a/plugins/tech-insights-backend/src/service/router.test.ts b/plugins/tech-insights-backend/src/service/router.test.ts index 8513973598..6935338a56 100644 --- a/plugins/tech-insights-backend/src/service/router.test.ts +++ b/plugins/tech-insights-backend/src/service/router.test.ts @@ -87,59 +87,94 @@ describe('Tech Insights router tests', () => { app = express().use(router); }); - - it('should be able to retrieve latest schemas', async () => { - await request(app).get('/fact-schemas').expect(200); - expect(latestSchemasMock).toHaveBeenCalled(); + describe('/fact-schemas', () => { + it('should be able to retrieve latest schemas', async () => { + await request(app).get('/fact-schemas').expect(200); + expect(latestSchemasMock).toHaveBeenCalled(); + }); }); - it('should not contain check endpoints when checker not present', async () => { - await request(app).get('/checks').expect(404); - await request(app).post('/checks/a/a/a').expect(404); + describe('/checks', () => { + it('should not contain check endpoints when checker not present', async () => { + await request(app).get('/checks').expect(404); + await request(app).post('/checks/a/a/a').expect(404); + }); }); - it('should be able to parse id request params for fact retrieval', async () => { - await request(app) - .get('/facts/latest') - .query({ - entity: 'a:a/a', - ids: ['firstId', 'secondId'], - }) - .expect(200); - expect(latestFactsByIdsMock).toHaveBeenCalledWith( - ['firstId', 'secondId'], - 'a:a/a', - ); + describe('/facts/latest', () => { + it('should be able to parse id request params for fact retrieval', async () => { + await request(app) + .get('/facts/latest') + .query({ + entity: 'a:a/a', + ids: ['firstId', 'secondId'], + }) + .expect(200); + expect(latestFactsByIdsMock).toHaveBeenCalledWith( + ['firstId', 'secondId'], + 'a:a/a', + ); + }); + it('should handle singular ids in query params correctly', async () => { + await request(app) + .get('/facts/latest') + .query({ + entity: 'a:a/a', + ids: ['secondId'], + }) + .expect(200); + expect(latestFactsByIdsMock).toHaveBeenCalledWith(['secondId'], 'a:a/a'); + }); }); - it('should be able to parse datetime request params for fact retrieval', async () => { - await request(app) - .get('/facts/range') - .query({ - entity: 'a:a/a', - ids: ['firstId', 'secondId'], - startDatetime: '2021-12-12T12:12:12', - endDatetime: '2022-11-11T11:11:11', - }) - .expect(200); - expect(factsBetweenTimestampsByIdsMock).toHaveBeenCalledWith( - ['firstId', 'secondId'], - 'a:a/a', - DateTime.fromISO('2021-12-12T12:12:12.000+00:00'), - DateTime.fromISO('2022-11-11T11:11:11.000+00:00'), - ); - }); + describe('/facts/range', () => { + it('should be able to parse datetime request params for fact retrieval', async () => { + await request(app) + .get('/facts/range') + .query({ + entity: 'a:a/a', + ids: ['firstId', 'secondId'], + startDatetime: '2021-12-12T12:12:12', + endDatetime: '2022-11-11T11:11:11', + }) + .expect(200); + expect(factsBetweenTimestampsByIdsMock).toHaveBeenCalledWith( + ['firstId', 'secondId'], + 'a:a/a', + DateTime.fromISO('2021-12-12T12:12:12.000+00:00'), + DateTime.fromISO('2022-11-11T11:11:11.000+00:00'), + ); + }); - it('should respond gracefully on parsing errors', async () => { - await request(app) - .get('/facts/range') - .query({ - entity: 'a:a/a', - ids: ['firstId', 'secondId'], - startDatetime: '2021-12-1222T12:12:12', - endDatetime: '2022-1122-11T11:11:11', - }) - .expect(422); - expect(latestFactsByIdsMock).toHaveBeenCalledTimes(0); + it('should respond gracefully on parsing errors', async () => { + await request(app) + .get('/facts/range') + .query({ + entity: 'a:a/a', + ids: ['firstId', 'secondId'], + startDatetime: '2021-12-1222T12:12:12', + endDatetime: '2022-1122-11T11:11:11', + }) + .expect(422); + expect(latestFactsByIdsMock).toHaveBeenCalledTimes(0); + }); + + it('should handle singular ids in query params correctly', async () => { + await request(app) + .get('/facts/range') + .query({ + entity: 'a:a/a', + ids: ['firstId'], + startDatetime: '2021-12-12T12:12:12', + endDatetime: '2022-11-11T11:11:11', + }) + .expect(200); + expect(factsBetweenTimestampsByIdsMock).toHaveBeenCalledWith( + ['firstId'], + 'a:a/a', + DateTime.fromISO('2021-12-12T12:12:12.000+00:00'), + DateTime.fromISO('2022-11-11T11:11:11.000+00:00'), + ); + }); }); }); diff --git a/plugins/tech-insights-backend/src/service/router.ts b/plugins/tech-insights-backend/src/service/router.ts index 4b93bf4d60..a7f48bc3a2 100644 --- a/plugins/tech-insights-backend/src/service/router.ts +++ b/plugins/tech-insights-backend/src/service/router.ts @@ -132,7 +132,15 @@ export async function createRouter< router.get('/facts/latest', async (req, res) => { const { entity } = req.query; const { namespace, kind, name } = parseEntityRef(entity as string); - const ids = req.query.ids as string[]; + + if (!req.query.ids) { + return res + .status(422) + .send({ error: 'Failed to parse ids from request' }); + } + const ids = Array.isArray(req.query.ids) + ? (req.query.ids as string[]) + : ([req.query.ids] as string[]); return res.send( await techInsightsStore.getLatestFactsByIds( ids, @@ -148,7 +156,14 @@ export async function createRouter< const { entity } = req.query; const { namespace, kind, name } = parseEntityRef(entity as string); - const ids = req.query.ids as string[]; + if (!req.query.ids) { + return res + .status(422) + .send({ error: 'Failed to parse ids from request' }); + } + const ids = Array.isArray(req.query.ids) + ? (req.query.ids as string[]) + : ([req.query.ids] as string[]); const startDatetime = DateTime.fromISO(req.query.startDatetime as string); const endDatetime = DateTime.fromISO(req.query.endDatetime as string); if (!startDatetime.isValid || !endDatetime.isValid) {