2024-03-25 17:44:45 +00:00
|
|
|
import {
|
|
|
|
|
getRepositoryMetadata,
|
|
|
|
|
getContainerRegistryURL
|
|
|
|
|
} from '../src/api-client'
|
|
|
|
|
|
|
|
|
|
const url = 'https://registry.example.com'
|
2024-08-09 14:31:15 +01:00
|
|
|
const test_token = 'test_token'
|
2024-03-25 17:44:45 +00:00
|
|
|
|
|
|
|
|
let fetchMock: jest.SpyInstance
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
fetchMock = jest.spyOn(global, 'fetch')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
fetchMock.mockRestore()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('getRepositoryMetadata', () => {
|
|
|
|
|
it('returns repository metadata when the fetch response is ok', async () => {
|
|
|
|
|
fetchMock.mockResolvedValueOnce(
|
2024-04-15 12:03:02 +01:00
|
|
|
new Response(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
id: '123',
|
|
|
|
|
owner: { id: '456' },
|
|
|
|
|
visibility: 'public'
|
|
|
|
|
})
|
|
|
|
|
)
|
2024-03-25 17:44:45 +00:00
|
|
|
)
|
2024-08-09 14:31:15 +01:00
|
|
|
const result = await getRepositoryMetadata(url, 'repository', test_token)
|
2024-04-15 12:03:02 +01:00
|
|
|
expect(result).toEqual({
|
|
|
|
|
repoId: '123',
|
|
|
|
|
ownerId: '456',
|
|
|
|
|
visibility: 'public'
|
|
|
|
|
})
|
2024-08-09 14:31:15 +01:00
|
|
|
|
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
|
|
|
'https://registry.example.com/repos/repository',
|
|
|
|
|
{
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${test_token}`,
|
|
|
|
|
Accept: 'application/vnd.github.v3+json'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-03-25 17:44:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('throws an error when the fetch errors', async () => {
|
|
|
|
|
fetchMock.mockRejectedValueOnce(new Error('API is down'))
|
|
|
|
|
await expect(
|
|
|
|
|
getRepositoryMetadata(url, 'repository', 'token')
|
|
|
|
|
).rejects.toThrow('API is down')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('throws an error when the response status is not ok', async () => {
|
|
|
|
|
fetchMock.mockResolvedValueOnce(new Response(null, { status: 500 }))
|
|
|
|
|
await expect(
|
|
|
|
|
getRepositoryMetadata(url, 'repository', 'token')
|
|
|
|
|
).rejects.toThrow(
|
|
|
|
|
'Failed to fetch repository metadata due to bad status code: 500'
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('throws an error when the response data is in the wrong format', async () => {
|
|
|
|
|
fetchMock.mockResolvedValueOnce(
|
|
|
|
|
new Response(JSON.stringify({ wrong: 'format' }))
|
|
|
|
|
)
|
|
|
|
|
await expect(
|
|
|
|
|
getRepositoryMetadata(url, 'repository', 'token')
|
|
|
|
|
).rejects.toThrow(
|
|
|
|
|
'Failed to fetch repository metadata: unexpected response format'
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('getContainerRegistryURL', () => {
|
|
|
|
|
it('returns container registry URL when the fetch response is ok', async () => {
|
|
|
|
|
fetchMock.mockResolvedValueOnce(
|
|
|
|
|
new Response(JSON.stringify({ url: 'https://registry.example.com' }))
|
|
|
|
|
)
|
2024-08-09 14:31:15 +01:00
|
|
|
const result = await getContainerRegistryURL(url, test_token)
|
|
|
|
|
|
2024-03-25 17:44:45 +00:00
|
|
|
expect(result).toEqual(new URL('https://registry.example.com'))
|
2024-08-09 14:31:15 +01:00
|
|
|
|
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
|
|
|
'https://registry.example.com/packages/container-registry-url',
|
|
|
|
|
{
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${test_token}`,
|
|
|
|
|
Accept: 'application/vnd.github.v3+json'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-03-25 17:44:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('throws an error when the fetch errors', async () => {
|
|
|
|
|
fetchMock.mockRejectedValueOnce(new Error('API is down'))
|
2024-08-09 14:31:15 +01:00
|
|
|
await expect(getContainerRegistryURL(url, test_token)).rejects.toThrow(
|
|
|
|
|
'API is down'
|
|
|
|
|
)
|
2024-03-25 17:44:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('throws an error when the response status is not ok', async () => {
|
|
|
|
|
fetchMock.mockResolvedValueOnce(new Response(null, { status: 500 }))
|
2024-08-09 14:31:15 +01:00
|
|
|
await expect(getContainerRegistryURL(url, test_token)).rejects.toThrow(
|
2024-03-25 17:44:45 +00:00
|
|
|
'Failed to fetch container registry url due to bad status code: 500'
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('throws an error when the response data is in the wrong format', async () => {
|
|
|
|
|
fetchMock.mockResolvedValueOnce(
|
|
|
|
|
new Response(JSON.stringify({ wrong: 'format' }))
|
|
|
|
|
)
|
2024-08-09 14:31:15 +01:00
|
|
|
await expect(getContainerRegistryURL(url, test_token)).rejects.toThrow(
|
2024-03-25 17:44:45 +00:00
|
|
|
'Failed to fetch repository metadata: unexpected response format'
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
})
|