From 19f0b667b1a8dc25378a33b358574a4801314041 Mon Sep 17 00:00:00 2001 From: pedr Date: Tue, 4 Jun 2024 05:55:57 -0300 Subject: [PATCH] Server: Fixes #10118: Missing record validation before trying to add item to user (#10471) --- packages/server/src/models/UserItemModel.ts | 3 +++ .../server/src/models/UserItemModels.test.ts | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 packages/server/src/models/UserItemModels.test.ts diff --git a/packages/server/src/models/UserItemModel.ts b/packages/server/src/models/UserItemModel.ts index 4d417b9f3d9..6a22a5fe98a 100644 --- a/packages/server/src/models/UserItemModel.ts +++ b/packages/server/src/models/UserItemModel.ts @@ -114,6 +114,9 @@ export default class UserItemModel extends BaseModel { public async add(userId: Uuid, itemId: Uuid, options: SaveOptions = {}): Promise { const item = await this.models().item().load(itemId, { fields: ['id', 'name'] }); + if (!item) { + throw new ErrorNotFound(`No such item: ${itemId}`); + } await this.addMulti(userId, [item], options); } diff --git a/packages/server/src/models/UserItemModels.test.ts b/packages/server/src/models/UserItemModels.test.ts new file mode 100644 index 00000000000..ab30a13fd05 --- /dev/null +++ b/packages/server/src/models/UserItemModels.test.ts @@ -0,0 +1,23 @@ +import { beforeAllDb, afterAllTests, beforeEachDb, models } from '../utils/testing/testUtils'; + +describe('UserItemModel', () => { + + beforeAll(async () => { + await beforeAllDb('UserItemModel'); + }); + + afterAll(async () => { + await afterAllTests(); + }); + + beforeEach(async () => { + await beforeEachDb(); + }); + + test('should throw error if item does not exist', async () => { + const mockUserId = 'not-a-real-user-id'; + const mockId = 'not-a-real-item-id'; + expect(async () => models().userItem().add(mockUserId, mockId)).rejects.toThrow('No such item: not-a-real-item-id'); + }); +}); +