|
| 1 | +const process = require('process') |
| 2 | + |
| 3 | +const test = require('ava') |
| 4 | +const semver = require('semver') |
| 5 | + |
| 6 | +const { purgeCache } = require('../../dist/lib/purge_cache') |
| 7 | +const { invokeLambda } = require('../helpers/main') |
| 8 | +const MockFetch = require('../helpers/mock_fetch') |
| 9 | + |
| 10 | +const globalFetch = globalThis.fetch |
| 11 | +const hasFetchAPI = semver.gte(process.version, '18.0.0') |
| 12 | + |
| 13 | +test.beforeEach(() => { |
| 14 | + delete process.env.NETLIFY_PURGE_API_TOKEN |
| 15 | + delete process.env.SITE_ID |
| 16 | +}) |
| 17 | + |
| 18 | +test.afterEach(() => { |
| 19 | + globalThis.fetch = globalFetch |
| 20 | +}) |
| 21 | + |
| 22 | +test.serial('Calls the purge API endpoint and returns `undefined` if the operation was successful', async (t) => { |
| 23 | + if (!hasFetchAPI) { |
| 24 | + console.warn('Skipping test requires the fetch API') |
| 25 | + |
| 26 | + return t.pass() |
| 27 | + } |
| 28 | + |
| 29 | + const mockSiteID = '123456789' |
| 30 | + const mockToken = '1q2w3e4r5t6y7u8i9o0p' |
| 31 | + |
| 32 | + process.env.NETLIFY_PURGE_API_TOKEN = mockToken |
| 33 | + process.env.SITE_ID = mockSiteID |
| 34 | + |
| 35 | + const mockAPI = new MockFetch().post({ |
| 36 | + body: (payload) => { |
| 37 | + const data = JSON.parse(payload) |
| 38 | + |
| 39 | + t.is(data.site_id, mockSiteID) |
| 40 | + }, |
| 41 | + headers: { Authorization: `Bearer ${mockToken}` }, |
| 42 | + method: 'post', |
| 43 | + response: new Response(null, { status: 202 }), |
| 44 | + url: `https://api.netlify.com/api/v1/purge`, |
| 45 | + }) |
| 46 | + const myFunction = async () => { |
| 47 | + await purgeCache() |
| 48 | + } |
| 49 | + |
| 50 | + globalThis.fetch = mockAPI.fetcher |
| 51 | + |
| 52 | + const response = await invokeLambda(myFunction) |
| 53 | + |
| 54 | + t.is(response, undefined) |
| 55 | + t.true(mockAPI.fulfilled) |
| 56 | +}) |
| 57 | + |
| 58 | +test.serial('Throws if the API response does not have a successful status code', async (t) => { |
| 59 | + if (!hasFetchAPI) { |
| 60 | + console.warn('Skipping test requires the fetch API') |
| 61 | + |
| 62 | + return t.pass() |
| 63 | + } |
| 64 | + |
| 65 | + const mockSiteID = '123456789' |
| 66 | + const mockToken = '1q2w3e4r5t6y7u8i9o0p' |
| 67 | + |
| 68 | + process.env.NETLIFY_PURGE_API_TOKEN = mockToken |
| 69 | + process.env.SITE_ID = mockSiteID |
| 70 | + |
| 71 | + const mockAPI = new MockFetch().post({ |
| 72 | + body: (payload) => { |
| 73 | + const data = JSON.parse(payload) |
| 74 | + |
| 75 | + t.is(data.site_id, mockSiteID) |
| 76 | + }, |
| 77 | + headers: { Authorization: `Bearer ${mockToken}` }, |
| 78 | + method: 'post', |
| 79 | + response: new Response(null, { status: 500 }), |
| 80 | + url: `https://api.netlify.com/api/v1/purge`, |
| 81 | + }) |
| 82 | + const myFunction = async () => { |
| 83 | + await purgeCache() |
| 84 | + } |
| 85 | + |
| 86 | + globalThis.fetch = mockAPI.fetcher |
| 87 | + |
| 88 | + await t.throwsAsync( |
| 89 | + async () => await invokeLambda(myFunction), |
| 90 | + 'Cache purge API call returned an unexpected status code: 500', |
| 91 | + ) |
| 92 | +}) |
0 commit comments