Skip to content
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
44 changes: 44 additions & 0 deletions src/app/api/label-mapping/label-mapping.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const mockLabelFindFirst = jest.fn()
const mockLabelDelete = jest.fn()

jest.mock('@/lib/db', () => ({
__esModule: true,
default: {
getInstance: () => ({
label: { findFirst: mockLabelFindFirst, delete: mockLabelDelete },
}),
},
}))

jest.mock('@/utils/CopilotAPI', () => ({ CopilotAPI: jest.fn() }))

import { LabelMappingService } from '@api/label-mapping/label-mapping.service'
import User from '@api/core/models/User.model'
import { UserRole } from '@api/core/types/user'

const user = {
workspaceId: 'ws-1',
role: UserRole.IU,
internalUserId: 'iu-1',
token: 'token',
} as unknown as User

describe('LabelMappingService#deleteLabel', () => {
beforeEach(() => jest.clearAllMocks())

it('deletes the matching label row', async () => {
mockLabelFindFirst.mockResolvedValue({ id: 'label-1' })

await new LabelMappingService(user).deleteLabel('ASS10-009')

expect(mockLabelDelete).toHaveBeenCalledWith({ where: { id: 'label-1' } })
})

it('no-ops when the label row is already gone', async () => {
mockLabelFindFirst.mockResolvedValue(null)

await new LabelMappingService(user).deleteLabel('ASS10-009')

expect(mockLabelDelete).not.toHaveBeenCalled()
})
})
3 changes: 2 additions & 1 deletion src/app/api/label-mapping/label-mapping.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ export class LabelMappingService extends BaseService {
label,
},
})
if (!currentLabel) return
await this.db.label.delete({
where: {
Comment on lines +178 to 180

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Non-idempotent label deletion race

If two task updates or deletions concurrently remove the same label mapping, both calls can pass findFirst, after which the second delete fails because the selected ID is gone, aborting its enclosing task transaction and returning the same class of 500 this change intends to prevent.

Knowledge Base Used: Workspace Settings & Configuration APIs

id: currentLabel?.id,
id: currentLabel.id,
},
})
}
Expand Down
Loading