|
| 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 | +import { WorkflowIdConflictPolicy, WorkflowIdReusePolicy } from '@crowd/temporal' |
| 6 | + |
| 7 | +import { ingestAkritesExternalContactDetail } from './ingestAkritesExternalContactDetail' |
| 8 | + |
| 9 | +const { execute, getContactDetailsByPurls } = vi.hoisted(() => ({ |
| 10 | + execute: vi.fn(), |
| 11 | + getContactDetailsByPurls: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/db/packagesTemporal', () => ({ |
| 15 | + getPackagesTemporalClient: vi.fn().mockResolvedValue({ workflow: { execute } }), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@/db/packagesDb', () => ({ |
| 19 | + getPackagesQx: vi.fn().mockResolvedValue({}), |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('@crowd/data-access-layer', () => ({ |
| 23 | + getContactDetailsByPurls, |
| 24 | +})) |
| 25 | + |
| 26 | +function baseRow( |
| 27 | + overrides: Partial<AkritesExternalContactDetailRow> = {}, |
| 28 | +): AkritesExternalContactDetailRow { |
| 29 | + return { |
| 30 | + purl: 'pkg:npm/lodash', |
| 31 | + name: 'lodash', |
| 32 | + ecosystem: 'npm', |
| 33 | + securityPolicyUrl: null, |
| 34 | + vulnerabilityReportingUrl: null, |
| 35 | + bugBountyUrl: null, |
| 36 | + pvrEnabled: null, |
| 37 | + declaredRepositoryUrl: null, |
| 38 | + resolvedRepositoryUrl: null, |
| 39 | + repoMappingConfidence: null, |
| 40 | + contactsLastRefreshed: null, |
| 41 | + securityContacts: [], |
| 42 | + ...overrides, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function mockReqRes(body: unknown) { |
| 47 | + execute.mockClear() |
| 48 | + getContactDetailsByPurls.mockClear() |
| 49 | + |
| 50 | + const req = { body } as unknown as Request |
| 51 | + |
| 52 | + const json = vi.fn() |
| 53 | + const status = vi.fn().mockReturnValue({ json }) |
| 54 | + const res = { status, json } as unknown as Response |
| 55 | + |
| 56 | + return { req, res, status, json } |
| 57 | +} |
| 58 | + |
| 59 | +describe('ingestAkritesExternalContactDetail', () => { |
| 60 | + it('returns the existing contact detail without triggering the workflow when already ingested', async () => { |
| 61 | + getContactDetailsByPurls.mockResolvedValue([ |
| 62 | + baseRow({ contactsLastRefreshed: '2024-01-01T00:00:00.000Z' }), |
| 63 | + ]) |
| 64 | + |
| 65 | + const { req, res, json } = mockReqRes({ purl: 'pkg:npm/lodash' }) |
| 66 | + |
| 67 | + await ingestAkritesExternalContactDetail(req, res) |
| 68 | + |
| 69 | + expect(execute).not.toHaveBeenCalled() |
| 70 | + expect(getContactDetailsByPurls).toHaveBeenCalledTimes(1) |
| 71 | + expect(json).toHaveBeenCalledWith(expect.objectContaining({ purl: 'pkg:npm/lodash' })) |
| 72 | + }) |
| 73 | + |
| 74 | + it('executes ingestSecurityContactsForPurlWorkflow and returns the re-read contact detail when never ingested', async () => { |
| 75 | + execute.mockResolvedValue({ found: true, repoId: 'repo-1' }) |
| 76 | + getContactDetailsByPurls |
| 77 | + .mockResolvedValueOnce([ |
| 78 | + baseRow({ |
| 79 | + resolvedRepositoryUrl: 'https://github.com/lodash/lodash', |
| 80 | + contactsLastRefreshed: null, |
| 81 | + }), |
| 82 | + ]) |
| 83 | + .mockResolvedValueOnce([baseRow({ contactsLastRefreshed: '2024-01-01T00:00:00.000Z' })]) |
| 84 | + |
| 85 | + const { req, res, json } = mockReqRes({ purl: 'pkg:npm/lodash' }) |
| 86 | + |
| 87 | + await ingestAkritesExternalContactDetail(req, res) |
| 88 | + |
| 89 | + expect(execute).toHaveBeenCalledTimes(1) |
| 90 | + const [workflowType, options] = execute.mock.calls[0] |
| 91 | + expect(workflowType).toBe('ingestSecurityContactsForPurlWorkflow') |
| 92 | + expect(options.taskQueue).toBe('security-contacts-worker') |
| 93 | + expect(options.workflowId).toMatch(/^security-contacts-ondemand:[0-9a-f]{64}$/) |
| 94 | + expect(options.workflowIdConflictPolicy).toBe(WorkflowIdConflictPolicy.USE_EXISTING) |
| 95 | + expect(options.workflowIdReusePolicy).toBe(WorkflowIdReusePolicy.ALLOW_DUPLICATE) |
| 96 | + expect(options.args).toEqual(['pkg:npm/lodash']) |
| 97 | + |
| 98 | + expect(getContactDetailsByPurls).toHaveBeenCalledTimes(2) |
| 99 | + expect(getContactDetailsByPurls).toHaveBeenCalledWith(expect.anything(), ['pkg:npm/lodash']) |
| 100 | + expect(json).toHaveBeenCalledWith(expect.objectContaining({ purl: 'pkg:npm/lodash' })) |
| 101 | + }) |
| 102 | + |
| 103 | + it('derives the same deterministic workflowId for the same purl', async () => { |
| 104 | + execute.mockResolvedValue({ found: true }) |
| 105 | + getContactDetailsByPurls.mockResolvedValue([ |
| 106 | + baseRow({ |
| 107 | + resolvedRepositoryUrl: 'https://github.com/lodash/lodash', |
| 108 | + contactsLastRefreshed: null, |
| 109 | + }), |
| 110 | + ]) |
| 111 | + |
| 112 | + const { req: req1, res: res1 } = mockReqRes({ purl: 'pkg:npm/lodash' }) |
| 113 | + await ingestAkritesExternalContactDetail(req1, res1) |
| 114 | + const id1 = execute.mock.calls[0][1].workflowId |
| 115 | + |
| 116 | + const { req: req2, res: res2 } = mockReqRes({ purl: 'pkg:npm/lodash' }) |
| 117 | + await ingestAkritesExternalContactDetail(req2, res2) |
| 118 | + const id2 = execute.mock.calls[0][1].workflowId |
| 119 | + |
| 120 | + expect(id1).toBe(id2) |
| 121 | + }) |
| 122 | + |
| 123 | + it('throws NotFoundError without executing the workflow when the purl is unknown', async () => { |
| 124 | + getContactDetailsByPurls.mockResolvedValue([]) |
| 125 | + |
| 126 | + const { req, res } = mockReqRes({ purl: 'pkg:npm/left-pad' }) |
| 127 | + |
| 128 | + await expect(ingestAkritesExternalContactDetail(req, res)).rejects.toThrow() |
| 129 | + expect(execute).not.toHaveBeenCalled() |
| 130 | + expect(getContactDetailsByPurls).toHaveBeenCalledTimes(1) |
| 131 | + }) |
| 132 | + |
| 133 | + it('throws NotFoundError without executing the workflow when the package has no linked repo', async () => { |
| 134 | + getContactDetailsByPurls.mockResolvedValue([ |
| 135 | + baseRow({ resolvedRepositoryUrl: null, contactsLastRefreshed: null }), |
| 136 | + ]) |
| 137 | + |
| 138 | + const { req, res } = mockReqRes({ purl: 'pkg:npm/left-pad' }) |
| 139 | + |
| 140 | + await expect(ingestAkritesExternalContactDetail(req, res)).rejects.toThrow() |
| 141 | + expect(execute).not.toHaveBeenCalled() |
| 142 | + expect(getContactDetailsByPurls).toHaveBeenCalledTimes(1) |
| 143 | + }) |
| 144 | + |
| 145 | + it('throws NotFoundError when the workflow reports no linked repo', async () => { |
| 146 | + execute.mockResolvedValue({ found: false }) |
| 147 | + getContactDetailsByPurls.mockResolvedValue([ |
| 148 | + baseRow({ |
| 149 | + resolvedRepositoryUrl: 'https://github.com/example/left-pad', |
| 150 | + contactsLastRefreshed: null, |
| 151 | + }), |
| 152 | + ]) |
| 153 | + |
| 154 | + const { req, res } = mockReqRes({ purl: 'pkg:npm/left-pad' }) |
| 155 | + |
| 156 | + await expect(ingestAkritesExternalContactDetail(req, res)).rejects.toThrow() |
| 157 | + expect(getContactDetailsByPurls).toHaveBeenCalledTimes(1) |
| 158 | + }) |
| 159 | + |
| 160 | + it('throws NotFoundError when the re-read finds no row', async () => { |
| 161 | + execute.mockResolvedValue({ found: true }) |
| 162 | + getContactDetailsByPurls |
| 163 | + .mockResolvedValueOnce([ |
| 164 | + baseRow({ |
| 165 | + resolvedRepositoryUrl: 'https://github.com/lodash/lodash', |
| 166 | + contactsLastRefreshed: null, |
| 167 | + }), |
| 168 | + ]) |
| 169 | + .mockResolvedValueOnce([]) |
| 170 | + |
| 171 | + const { req, res } = mockReqRes({ purl: 'pkg:npm/lodash' }) |
| 172 | + |
| 173 | + await expect(ingestAkritesExternalContactDetail(req, res)).rejects.toThrow() |
| 174 | + }) |
| 175 | + |
| 176 | + it('rejects a request missing purl without executing a workflow', async () => { |
| 177 | + const { req, res } = mockReqRes({}) |
| 178 | + |
| 179 | + await expect(ingestAkritesExternalContactDetail(req, res)).rejects.toThrow() |
| 180 | + expect(execute).not.toHaveBeenCalled() |
| 181 | + }) |
| 182 | +}) |
0 commit comments