Skip to content
Snippets Groups Projects
Commit 6bc3dfc4 authored by anne.matthes's avatar anne.matthes
Browse files

Fix unit tests and add new ones

parent ed6f1a26
No related branches found
No related tags found
No related merge requests found
const { describe, it, expect } = require('@jest/globals')
const { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach } = require('@jest/globals')
const mock = require('mock-fs')
const fetchMock = require('node-fetch')
const request = require('supertest')
const createApp = require('../src/createApp')
jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox())
describe('Manifest service', () => {
let app
beforeAll(() => {
mock({
'./config/manifests': {
'urls.yaml': `manifests:
- https://some-fake-url.k3s.de/api/manifest.json`
}
})
app = createApp()
})
afterAll(() => {
mock.restore()
})
beforeEach(() => {
fetchMock.get('https://some-fake-url.k3s.de/api/manifest.json', JSON.parse('{"some":"thing"}'))
})
afterEach(() => {
fetchMock.restore()
app.timeout = 30000
})
it('is healthy', async () => {
const app = createApp()
return await request(app)
.get('/healthy')
.then(response => {
expect(response.statusCode).toBe(200)
})
})
it('fetches manifest data', async () => {
return await request(app)
.get('/api/manifest.json')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([JSON.parse('{"some":"thing"}')])
})
})
it('caches manifest data', async () => {
await request(app)
.get('/api/manifest.json')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([JSON.parse('{"some":"thing"}')])
})
fetchMock.restore()
fetchMock.get('https://some-fake-url.k3s.de/api/manifest.json', JSON.parse('{"some":"different"}'))
await new Promise((resolve, reject) => setTimeout(resolve, 150))
await request(app)
.get('/api/manifest.json')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([JSON.parse('{"some":"thing"}')])
})
})
it('refreshes manifest data after caching timeout', async () => {
app.timeout = 100
await request(app)
.get('/api/manifest.json')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([JSON.parse('{"some":"thing"}')])
})
fetchMock.restore()
fetchMock.get('https://some-fake-url.k3s.de/api/manifest.json', JSON.parse('{"some":"different"}'))
await new Promise((resolve, reject) => setTimeout(resolve, 150))
await request(app)
.get('/api/manifest.json')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([JSON.parse('{"some":"different"}')])
})
})
it.skip('can load multiple configurations', async () => {
mock({
'./config/manifests': {
'urls.yaml': `manifests:
- https://some-fake-url.k3s.de/api/manifest.json
- https://some-other-fake-url.k3s.de/api/manifest.json`
}
})
fetchMock.get('https://some-other-fake-url.k3s.de/api/manifest.json', JSON.parse('{"some":"other"}'))
const app = createApp()
await request(app)
.get('/api/manifest.json')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([JSON.parse('{"some":"thing"}'), JSON.parse('{"some":"other"}')])
})
})
})
......@@ -57,9 +57,10 @@ module.exports = () => {
let manifestCache = []
let lastCached
app.timeout = 30000
const fetchManifest = async () => {
if (+new Date() < lastCached + 30000) return
if (+new Date() < lastCached + (app.timeout || 30000)) return
const results = urls.map(url => fetch(url).then(result => {
if (!result.ok) throw new Error(`Failed to load manifest for url ${result.url} (Status: ${result.status}: ${result.statusText})`)
return result.json().catch(err => { throw new Error(`Failed to load manifest for url ${result.url}: ${err}`) })
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment