Skip to content
Closed
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
16 changes: 8 additions & 8 deletions test/repo/unit/api-public.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const requestCases = [
{
name: 'authenticated',
apiKey: 'sktsec_test_token',
origin: 'https://api.socket.dev',
path: '/v0/purl',
origin: proxyOrigin,
path: '/batch',
query: { alerts: 'true', compact: 'false' },
},
]
Expand Down Expand Up @@ -257,8 +257,8 @@ describe('public package response bounds', () => {

describe('authenticated package response bounds', () => {
test('rejects identity responses beyond the SDK wire-byte limit', async () => {
const scope = nock('https://api.socket.dev')
.post('/v0/purl')
const scope = nock(proxyOrigin)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(200, Buffer.alloc(10 * 1024 * 1024 + 1, 0x20), {
'content-encoding': 'identity',
Expand All @@ -281,14 +281,14 @@ describe('authenticated package response bounds', () => {
purls.push(`pkg:npm/example-authenticated-${index}`)
}
const artifacts = [publicArtifact(purls[0]!), publicArtifact(purls[1024]!)]
const first = nock('https://api.socket.dev')
.post('/v0/purl', {
const first = nock(proxyOrigin)
.post('/batch', {
components: purls.slice(0, 1024).map(purl => ({ purl })),
})
.query({ alerts: 'true', compact: 'false' })
.reply(200, JSON.stringify(artifacts[0]))
const second = nock('https://api.socket.dev')
.post('/v0/purl', { components: [{ purl: purls[1024] }] })
const second = nock(proxyOrigin)
.post('/batch', { components: [{ purl: purls[1024] }] })
.query({ alerts: 'true', compact: 'false' })
.reply(200, JSON.stringify(artifacts[1]))
const seen: PackageScoreAndAlerts[] = []
Expand Down
51 changes: 26 additions & 25 deletions test/repo/unit/api.test.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file Unit tests for the Socket API layer (src/api.mts), which wraps
* `@socketsecurity/sdk`. Every request is exercised against a nock-mocked
* api.socket.dev — no real network (the fleet setup fails net-connect
* purl-api.socket.dev — no real network (the test setup fails net-connect
* closed), and no owned-infrastructure mocking. We assert both the request
* the SDK sends (path, query, body) and how api.ts reshapes the response.
*/
Expand All @@ -19,10 +19,11 @@ import type { PackageScoreAndAlerts } from '../../../src/api.mts'
import type { SimPURL } from '../../../src/ui/externals/parse-externals.mts'

const API_ORIGIN = 'https://api.socket.dev'
const PURL_ORIGIN = 'https://purl-api.socket.dev'
const TOKEN = 'sktsec_test_token'
const nullPayloadValue: unknown = JSON.parse('null')

// Build a full artifact line as the /v0/purl NDJSON stream emits it.
// Build a full artifact line as the PURL batch NDJSON stream emits it.
function artifactLine(purl: SimPURL, overall: number): string {
const artifact: PackageScoreAndAlerts = {
alerts: [],
Expand Down Expand Up @@ -128,8 +129,8 @@ describe('api getOrganizations', () => {
describe('api streamPackageScores', () => {
test('accepts the cache timeout within the SDK transport limit', async () => {
const purl = 'pkg:npm/example-cache-timeout' as SimPURL
const scope = nock(API_ORIGIN)
.post('/v0/purl')
const scope = nock(PURL_ORIGIN)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(200, `${artifactLine(purl, 0.5)}\n`)

Expand Down Expand Up @@ -159,7 +160,7 @@ describe('api streamPackageScores', () => {
cause: new ResponseError(
response,
TOKEN,
`${API_ORIGIN}/?token=${TOKEN}`,
`${PURL_ORIGIN}/?token=${TOKEN}`,
),
}),
)
Expand Down Expand Up @@ -217,8 +218,8 @@ describe('api streamPackageScores', () => {
{ version: 42 },
]
for (let index = 0, { length } = malformed; index < length; index += 1) {
const scope = nock(API_ORIGIN)
.post('/v0/purl')
const scope = nock(PURL_ORIGIN)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(
200,
Expand All @@ -239,8 +240,8 @@ describe('api streamPackageScores', () => {
async identity => {
const purl = 'pkg:npm/example-invalid-identity' as SimPURL
const invalid = { ...JSON.parse(artifactLine(purl, 0.1)), ...identity }
const scope = nock(API_ORIGIN)
.post('/v0/purl')
const scope = nock(PURL_ORIGIN)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(200, `${JSON.stringify(invalid)}\n${artifactLine(purl, 0.9)}\n`)

Expand Down Expand Up @@ -276,8 +277,8 @@ describe('api streamPackageScores', () => {
},
],
}
const scope = nock(API_ORIGIN)
.post('/v0/purl')
const scope = nock(PURL_ORIGIN)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(200, `${JSON.stringify(artifact)}\n`)

Expand All @@ -295,7 +296,7 @@ describe('api streamPackageScores', () => {
status,
error: TOKEN,
cause: `Authorization: Bearer ${TOKEN}`,
url: `${API_ORIGIN}/v0/purl?token=${TOKEN}`,
url: `${PURL_ORIGIN}/batch?token=${TOKEN}`,
})

expect(error).toMatchObject({
Expand All @@ -311,8 +312,8 @@ describe('api streamPackageScores', () => {
'rejects an SDK HTTP %i failure without response details',
async status => {
const purl = 'pkg:npm/example-denied@1.0.0' as SimPURL
const scope = nock(API_ORIGIN)
.post('/v0/purl')
const scope = nock(PURL_ORIGIN)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(status, { error: { message: `Authorization: Bearer ${TOKEN}` } })

Expand All @@ -339,8 +340,8 @@ describe('api streamPackageScores', () => {
type: 'npm',
alerts: [{ action: 'warn', type, severity: 'medium' }],
})
const scope = nock(API_ORIGIN)
.post('/v0/purl')
const scope = nock(PURL_ORIGIN)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(
200,
Expand Down Expand Up @@ -371,8 +372,8 @@ describe('api streamPackageScores', () => {
`{"inputPurl":"${purl}","name":"example-invalid-score","type":"npm","alerts":[],"score":${score}}`,
)
.join('\n')
const scope = nock(API_ORIGIN)
.post('/v0/purl')
const scope = nock(PURL_ORIGIN)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(200, `${lines}\n${artifactLine(purl, 0)}\n`)

Expand All @@ -386,8 +387,8 @@ describe('api streamPackageScores', () => {
test('yields one score+alerts object per artifact line', async () => {
const purlA = 'pkg:npm/left-pad@1.0.0' as SimPURL
const purlB = 'pkg:npm/right-pad@2.0.0' as SimPURL
nock(API_ORIGIN)
.post('/v0/purl')
nock(PURL_ORIGIN)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(200, `${artifactLine(purlA, 42)}\n${artifactLine(purlB, 99)}\n`)

Expand All @@ -400,11 +401,11 @@ describe('api streamPackageScores', () => {
expect(seen.map(s => s.score.overall)).toEqual([42, 99])
})

test('requests /v0/purl with the pending purls in the body', async () => {
test('requests /batch with the pending purls in the body', async () => {
const purl = 'pkg:npm/express@4.0.0' as SimPURL
let sentBody: unknown
nock(API_ORIGIN)
.post('/v0/purl', body => {
nock(PURL_ORIGIN)
.post('/batch', body => {
sentBody = body
return true
})
Expand Down Expand Up @@ -438,8 +439,8 @@ describe('api streamPackageScores', () => {
_type: 'purlError',
value: { error: 'Package not found', inputPurl: purl },
})
nock(API_ORIGIN)
.post('/v0/purl')
nock(PURL_ORIGIN)
.post('/batch')
.query({ alerts: 'true', compact: 'false' })
.reply(200, `${artifactLine(purl, 77)}\n${summaryLine}\n${errorLine}\n`)

Expand Down
Loading