2024-02-18 03:14:10 +00:00
|
|
|
import nock from 'nock'
|
|
|
|
import {writeAttestation} from '../src/store'
|
|
|
|
|
|
|
|
describe('writeAttestation', () => {
|
|
|
|
const originalEnv = process.env
|
|
|
|
const attestation = {foo: 'bar '}
|
|
|
|
const token = 'token'
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
process.env = {
|
|
|
|
...originalEnv,
|
|
|
|
GITHUB_REPOSITORY: 'foo/bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
process.env = originalEnv
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the api call is successful', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
nock('https://api.github.com')
|
|
|
|
.matchHeader('authorization', `token ${token}`)
|
|
|
|
.post('/repos/foo/bar/attestations', {bundle: attestation})
|
|
|
|
.reply(201, {id: '123'})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('persists the attestation', async () => {
|
|
|
|
await expect(writeAttestation(attestation, token)).resolves.toEqual('123')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the api call fails', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
nock('https://api.github.com')
|
|
|
|
.matchHeader('authorization', `token ${token}`)
|
|
|
|
.post('/repos/foo/bar/attestations', {bundle: attestation})
|
|
|
|
.reply(500, 'oops')
|
|
|
|
})
|
|
|
|
|
2024-03-22 02:25:36 +00:00
|
|
|
it('throws an error', async () => {
|
2024-02-18 03:14:10 +00:00
|
|
|
await expect(writeAttestation(attestation, token)).rejects.toThrow(/oops/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|