Skip to content

Commit

Permalink
test: add test cases for the provenance marshall (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal authored Sep 27, 2023
1 parent b33500b commit fb9823e
Showing 1 changed file with 155 additions and 0 deletions.
155 changes: 155 additions & 0 deletions __tests__/marshalls.provenance.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
jest.mock('pacote', () => {
return {
// manifest method should be a promise that resolves to a value:
manifest: jest.fn().mockResolvedValue({
name: 'packageName',
version: '1.0.0'
// Add other relevant properties
})
}
})

jest.mock('node-fetch')

const fetch = require('node-fetch')

const ProvenanceMarshall = require('../lib/marshalls/provenance.marshall')
const pacote = require('pacote')

describe('Provenance test suites', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.resetAllMocks()
})

test('has the right title', async () => {
const testMarshall = new ProvenanceMarshall({
packageRepoUtils: {
getPackageInfo: (pkgInfo) => {
return new Promise((resolve) => {
resolve(pkgInfo)
})
}
}
})

expect(testMarshall.title()).toEqual('Verifying package provenance')
})

test('should successfully validate a package with correct signature', async () => {
// Mock the response from fetch
const mockResponse = {
json: jest.fn().mockResolvedValue({
keys: [
{
key: 'publicKey1'
},
{
key: 'publicKey2'
}
]
})
}
fetch.mockImplementationOnce(() => Promise.resolve(mockResponse))

// Call the validate method with a package object
const pkg = {
packageName: 'packageName',
packageVersion: '1.0.0'
}

const testMarshall = new ProvenanceMarshall({
packageRepoUtils: {
getPackageInfo: (pkgInfo) => {
return new Promise((resolve) => {
resolve({
name: pkg.packageName,
version: pkg.packageVersion
})
})
},
parsePackageVersion: (pkgVersion) => {
return {
version: pkgVersion
}
}
}
})

// We assert that the validate method didn't throw an error,
// because the keys match the signature
await testMarshall.validate(pkg)

// Assert that the fetch method is called with the correct URL
// eslint-disable-next-line no-undef
expect(fetch).toHaveBeenCalledWith('https://registry.npmjs.org/-/npm/v1/keys')

// Assert that the pacote.manifest method is called with the correct arguments
expect(pacote.manifest).toHaveBeenCalledWith('[email protected]', {
verifyAttestations: true,
registry: 'https://registry.npmjs.org',
'//registry.npmjs.org/:_keys': [
{
key: 'publicKey1',
pemkey: '-----BEGIN PUBLIC KEY-----\npublicKey1\n-----END PUBLIC KEY-----'
},
{
key: 'publicKey2',
pemkey: '-----BEGIN PUBLIC KEY-----\npublicKey2\n-----END PUBLIC KEY-----'
}
]
})
})

test('should throw an error if keys dont match and manifest() throws an error', async () => {
// Mock the response from fetch
const mockResponse = {
json: jest.fn().mockResolvedValue({
keys: [
{
key: 'publicKey1'
},
{
key: 'publicKey2'
}
]
})
}
fetch.mockImplementationOnce(() => Promise.resolve(mockResponse))

const pkg = {
packageName: 'packageName',
packageVersion: '1.0.0'
}

// the manifest() method should throw an error
// in this jest mock to simulate a problem:
pacote.manifest = jest.fn().mockRejectedValue(new Error('mocked manifest error'))

const testMarshall = new ProvenanceMarshall({
packageRepoUtils: {
getPackageInfo: (pkgInfo) => {
return new Promise((resolve) => {
resolve({
name: pkg.packageName,
version: pkg.packageVersion
})
})
},
parsePackageVersion: (pkgVersion) => {
return {
version: pkgVersion
}
}
}
})

// We assert that the validate method didn't throw an error,
// because the keys match the signature
await expect(testMarshall.validate(pkg)).rejects.toThrow('mocked manifest error')

// Assert that the fetch method is called with the correct URL
// eslint-disable-next-line no-undef
expect(fetch).toHaveBeenCalledWith('https://registry.npmjs.org/-/npm/v1/keys')
})
})

0 comments on commit fb9823e

Please sign in to comment.