Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server: Fixes #10118: Missing record validation before trying to add item to user #10471

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});

Loading