From 75ab45d8b4d8988d5a52b3c1e0eb6a79d2c786d2 Mon Sep 17 00:00:00 2001 From: LvffY Date: Fri, 8 Apr 2022 19:49:37 +0200 Subject: [PATCH] =?UTF-8?q?[#10582]=20=F0=9F=91=8C=20Re-add=20tests=20and?= =?UTF-8?q?=20code=20to=20throw=20exceptions=20when=20entity=20type=20is?= =?UTF-8?q?=20not=20url.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/backstage/backstage/pull/10584#discussion_r841505986 See https://github.com/backstage/backstage/pull/10584#discussion_r841502848 Signed-off-by: LvffY (cherry picked from commit 8a986150cc1e96b3150516ef36f24bdd89c55bff) --- .../src/service/TodoReaderService.test.ts | 32 +++++++++---------- .../src/service/TodoReaderService.ts | 11 +++++-- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/plugins/todo-backend/src/service/TodoReaderService.test.ts b/plugins/todo-backend/src/service/TodoReaderService.test.ts index b16f0ee669..3806895666 100644 --- a/plugins/todo-backend/src/service/TodoReaderService.test.ts +++ b/plugins/todo-backend/src/service/TodoReaderService.test.ts @@ -326,45 +326,45 @@ describe('TodoReaderService', () => { ); }); - it('should not throw if entity has an invalid location', async () => { + it('should throw if entity has an invalid location', async () => { const todoReader = mockTodoReader([]); const catalogClient = mockCatalogClient({ ...mockEntity, metadata: { ...mockEntity.metadata, annotations: { - ['backstage.io/managed-by-location']: 'file:../info.yaml', + ['backstage.io/managed-by-location']: 'file:../managed-by-location.yaml', }, }, }); const service = new TodoReaderService({ todoReader, catalogClient }); - await expect(service.listTodos({ entity: entityName })).resolves.toEqual({ - items: [], - totalCount: 0, - offset: 0, - limit: 10, - }); + await expect(service.listTodos({ entity: entityName })).rejects.toEqual( + expect.objectContaining({ + name: 'InputError', + message: `Invalid entity location type for component:default/my-component, got 'file' for location ../managed-by-location.yaml`, + }), + ); }); - it('should not throw if entity has an invalid source location', async () => { + it('should throw if entity has an invalid source location', async () => { const todoReader = mockTodoReader([]); const catalogClient = mockCatalogClient({ ...mockEntity, metadata: { ...mockEntity.metadata, annotations: { - ['backstage.io/source-location']: 'file:../info.yaml', + ['backstage.io/source-location']: 'file:../source-location.yaml', }, }, }); const service = new TodoReaderService({ todoReader, catalogClient }); - await expect(service.listTodos({ entity: entityName })).resolves.toEqual({ - items: [], - totalCount: 0, - offset: 0, - limit: 10, - }); + await expect(service.listTodos({ entity: entityName })).rejects.toEqual( + expect.objectContaining({ + name: 'InputError', + message: `Invalid entity location type for component:default/my-component, got 'file' for location ../source-location.yaml`, + }), + ); }); }); diff --git a/plugins/todo-backend/src/service/TodoReaderService.ts b/plugins/todo-backend/src/service/TodoReaderService.ts index 1b3376dbfd..20d633e409 100644 --- a/plugins/todo-backend/src/service/TodoReaderService.ts +++ b/plugins/todo-backend/src/service/TodoReaderService.ts @@ -71,8 +71,15 @@ export class TodoReaderService implements TodoService { `Entity not found, ${stringifyEntityRef(req.entity)}`, ); } - - const url = getEntitySourceLocation(entity).target; + const entitySourceLocation = getEntitySourceLocation(entity) + if (entitySourceLocation.type !== 'url') { + throw new InputError( + `Invalid entity location type for ${stringifyEntityRef( + entity, + )}, got '${entitySourceLocation.type}' for location ${entitySourceLocation.target}`, + ); + } + const url = entitySourceLocation.target; const todos = await this.todoReader.readTodos({ url }); let limit = req.limit ?? this.defaultPageSize;