|
| 1 | +import type { Request, Response } from 'express' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +import type { AkritesExternalContactDetailRow } from '@crowd/data-access-layer' |
| 5 | + |
| 6 | +import { refreshAkritesExternalContactDetail } from './refreshAkritesExternalContactDetail' |
| 7 | + |
| 8 | +const { execute, getContactDetailsByPurls } = vi.hoisted(() => ({ |
| 9 | + execute: vi.fn(), |
| 10 | + getContactDetailsByPurls: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@/db/packagesTemporal', () => ({ |
| 14 | + getPackagesTemporalClient: vi.fn().mockResolvedValue({ workflow: { execute } }), |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@/db/packagesDb', () => ({ |
| 18 | + getPackagesQx: vi.fn().mockResolvedValue({}), |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@crowd/data-access-layer', () => ({ |
| 22 | + getContactDetailsByPurls, |
| 23 | +})) |
| 24 | + |
| 25 | +function baseRow( |
| 26 | + overrides: Partial<AkritesExternalContactDetailRow> = {}, |
| 27 | +): AkritesExternalContactDetailRow { |
| 28 | + return { |
| 29 | + purl: 'pkg:npm/lodash', |
| 30 | + name: 'lodash', |
| 31 | + ecosystem: 'npm', |
| 32 | + securityPolicyUrl: null, |
| 33 | + vulnerabilityReportingUrl: null, |
| 34 | + bugBountyUrl: null, |
| 35 | + pvrEnabled: null, |
| 36 | + declaredRepositoryUrl: null, |
| 37 | + resolvedRepositoryUrl: null, |
| 38 | + repoMappingConfidence: null, |
| 39 | + securityContacts: [], |
| 40 | + ...overrides, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +function mockReqRes(body: unknown) { |
| 45 | + execute.mockClear() |
| 46 | + getContactDetailsByPurls.mockClear() |
| 47 | + |
| 48 | + const req = { body } as unknown as Request |
| 49 | + |
| 50 | + const json = vi.fn() |
| 51 | + const status = vi.fn().mockReturnValue({ json }) |
| 52 | + const res = { status, json } as unknown as Response |
| 53 | + |
| 54 | + return { req, res, status, json } |
| 55 | +} |
| 56 | + |
| 57 | +describe('refreshAkritesExternalContactDetail', () => { |
| 58 | + it('executes ingestSecurityContactsForPurlWorkflow and returns the re-read contact detail', async () => { |
| 59 | + execute.mockResolvedValue({ found: true, repoId: 'repo-1' }) |
| 60 | + getContactDetailsByPurls.mockResolvedValue([baseRow()]) |
| 61 | + |
| 62 | + const { req, res, json } = mockReqRes({ purl: 'pkg:npm/lodash' }) |
| 63 | + |
| 64 | + await refreshAkritesExternalContactDetail(req, res) |
| 65 | + |
| 66 | + expect(execute).toHaveBeenCalledTimes(1) |
| 67 | + const [workflowType, options] = execute.mock.calls[0] |
| 68 | + expect(workflowType).toBe('ingestSecurityContactsForPurlWorkflow') |
| 69 | + expect(options.taskQueue).toBe('security-contacts-worker') |
| 70 | + expect(options.workflowId).toMatch(/^security-contacts-ondemand:[0-9a-f]{64}$/) |
| 71 | + expect(options.args).toEqual(['pkg:npm/lodash']) |
| 72 | + |
| 73 | + expect(getContactDetailsByPurls).toHaveBeenCalledWith(expect.anything(), ['pkg:npm/lodash']) |
| 74 | + expect(json).toHaveBeenCalledWith(expect.objectContaining({ purl: 'pkg:npm/lodash' })) |
| 75 | + }) |
| 76 | + |
| 77 | + it('derives the same deterministic workflowId for the same purl', async () => { |
| 78 | + execute.mockResolvedValue({ found: true }) |
| 79 | + getContactDetailsByPurls.mockResolvedValue([baseRow()]) |
| 80 | + |
| 81 | + const { req: req1, res: res1 } = mockReqRes({ purl: 'pkg:npm/lodash' }) |
| 82 | + await refreshAkritesExternalContactDetail(req1, res1) |
| 83 | + const id1 = execute.mock.calls[0][1].workflowId |
| 84 | + |
| 85 | + const { req: req2, res: res2 } = mockReqRes({ purl: 'pkg:npm/lodash' }) |
| 86 | + await refreshAkritesExternalContactDetail(req2, res2) |
| 87 | + const id2 = execute.mock.calls[0][1].workflowId |
| 88 | + |
| 89 | + expect(id1).toBe(id2) |
| 90 | + }) |
| 91 | + |
| 92 | + it('throws NotFoundError when the workflow reports no linked repo', async () => { |
| 93 | + execute.mockResolvedValue({ found: false }) |
| 94 | + |
| 95 | + const { req, res } = mockReqRes({ purl: 'pkg:npm/left-pad' }) |
| 96 | + |
| 97 | + await expect(refreshAkritesExternalContactDetail(req, res)).rejects.toThrow() |
| 98 | + expect(getContactDetailsByPurls).not.toHaveBeenCalled() |
| 99 | + }) |
| 100 | + |
| 101 | + it('throws NotFoundError when the re-read finds no row', async () => { |
| 102 | + execute.mockResolvedValue({ found: true }) |
| 103 | + getContactDetailsByPurls.mockResolvedValue([]) |
| 104 | + |
| 105 | + const { req, res } = mockReqRes({ purl: 'pkg:npm/lodash' }) |
| 106 | + |
| 107 | + await expect(refreshAkritesExternalContactDetail(req, res)).rejects.toThrow() |
| 108 | + }) |
| 109 | + |
| 110 | + it('rejects a request missing purl without executing a workflow', async () => { |
| 111 | + const { req, res } = mockReqRes({}) |
| 112 | + |
| 113 | + await expect(refreshAkritesExternalContactDetail(req, res)).rejects.toThrow() |
| 114 | + expect(execute).not.toHaveBeenCalled() |
| 115 | + }) |
| 116 | +}) |
0 commit comments