2024-04-10 23:53:17 +00:00
|
|
|
import {MockAgent, setGlobalDispatcher} from 'undici'
|
2024-02-18 03:14:10 +00:00
|
|
|
import {writeAttestation} from '../src/store'
|
|
|
|
|
|
|
|
describe('writeAttestation', () => {
|
|
|
|
const originalEnv = process.env
|
|
|
|
const attestation = {foo: 'bar '}
|
|
|
|
const token = 'token'
|
2024-08-14 18:21:15 +00:00
|
|
|
const headers = {'X-GitHub-Foo': 'true'}
|
2024-02-18 03:14:10 +00:00
|
|
|
|
2024-04-10 23:53:17 +00:00
|
|
|
const mockAgent = new MockAgent()
|
|
|
|
setGlobalDispatcher(mockAgent)
|
|
|
|
|
2024-02-18 03:14:10 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
process.env = {
|
|
|
|
...originalEnv,
|
|
|
|
GITHUB_REPOSITORY: 'foo/bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
process.env = originalEnv
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the api call is successful', () => {
|
|
|
|
beforeEach(() => {
|
2024-04-10 23:53:17 +00:00
|
|
|
mockAgent
|
|
|
|
.get('https://api.github.com')
|
|
|
|
.intercept({
|
|
|
|
path: '/repos/foo/bar/attestations',
|
|
|
|
method: 'POST',
|
2024-08-14 18:21:15 +00:00
|
|
|
headers: {authorization: `token ${token}`, ...headers},
|
2024-04-10 23:53:17 +00:00
|
|
|
body: JSON.stringify({bundle: attestation})
|
|
|
|
})
|
2024-02-18 03:14:10 +00:00
|
|
|
.reply(201, {id: '123'})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('persists the attestation', async () => {
|
2024-08-14 18:21:15 +00:00
|
|
|
await expect(
|
|
|
|
writeAttestation(attestation, token, {headers})
|
|
|
|
).resolves.toEqual('123')
|
2024-02-18 03:14:10 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the api call fails', () => {
|
|
|
|
beforeEach(() => {
|
2024-04-10 23:53:17 +00:00
|
|
|
mockAgent
|
|
|
|
.get('https://api.github.com')
|
|
|
|
.intercept({
|
|
|
|
path: '/repos/foo/bar/attestations',
|
|
|
|
method: 'POST',
|
|
|
|
headers: {authorization: `token ${token}`},
|
|
|
|
body: JSON.stringify({bundle: attestation})
|
|
|
|
})
|
2024-02-18 03:14:10 +00:00
|
|
|
.reply(500, 'oops')
|
|
|
|
})
|
|
|
|
|
2024-03-22 02:25:36 +00:00
|
|
|
it('throws an error', async () => {
|
2024-04-24 18:31:11 +00:00
|
|
|
await expect(
|
|
|
|
writeAttestation(attestation, token, {retry: 0})
|
|
|
|
).rejects.toThrow(/oops/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the api call fails but succeeds on retry', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const pool = mockAgent.get('https://api.github.com')
|
|
|
|
|
|
|
|
pool
|
|
|
|
.intercept({
|
|
|
|
path: '/repos/foo/bar/attestations',
|
|
|
|
method: 'POST',
|
|
|
|
headers: {authorization: `token ${token}`},
|
|
|
|
body: JSON.stringify({bundle: attestation})
|
|
|
|
})
|
|
|
|
.reply(500, 'oops')
|
|
|
|
.times(1)
|
|
|
|
|
|
|
|
pool
|
|
|
|
.intercept({
|
|
|
|
path: '/repos/foo/bar/attestations',
|
|
|
|
method: 'POST',
|
|
|
|
headers: {authorization: `token ${token}`},
|
|
|
|
body: JSON.stringify({bundle: attestation})
|
|
|
|
})
|
|
|
|
.reply(201, {id: '123'})
|
|
|
|
.times(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('persists the attestation', async () => {
|
|
|
|
await expect(writeAttestation(attestation, token)).resolves.toEqual('123')
|
2024-02-18 03:14:10 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|