Skip to content

Commit

Permalink
Server: Fixes #10118: Missing record validation before trying to add …
Browse files Browse the repository at this point in the history
…item to user (#10471)
  • Loading branch information
pedr authored Jun 4, 2024
1 parent ac71654 commit 19f0b66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/server/src/models/UserItemModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export default class UserItemModel extends BaseModel<UserItem> {

public async add(userId: Uuid, itemId: Uuid, options: SaveOptions = {}): Promise<void> {
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);
}

Expand Down
23 changes: 23 additions & 0 deletions packages/server/src/models/UserItemModels.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit 19f0b66

Please sign in to comment.