Skip to content

Commit

Permalink
fix(offline): return correct value from clearSensitiveCaches (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiVandivier authored Sep 16, 2021
1 parent a711a34 commit 02c2c6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
28 changes: 19 additions & 9 deletions services/offline/src/lib/__tests__/clear-sensitive-caches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import {

// Mocks for CacheStorage API

// Returns true if an existing cache is deleted
const makeCachesDeleteMock = (keys: string[]) => {
return jest
.fn()
.mockImplementation(key => Promise.resolve(keys.includes(key)))
}

const keysMockDefault = jest.fn().mockImplementation(async () => [])
const deleteMockDefault = jest.fn().mockImplementation(async () => null)
const deleteMockDefault = makeCachesDeleteMock([])
const cachesDefault = {
keys: keysMockDefault,
delete: deleteMockDefault,
Expand Down Expand Up @@ -38,21 +45,24 @@ afterAll(() => {
})

it('does not fail if there are no caches or no sections-db', () => {
return expect(clearSensitiveCaches()).resolves.toBeDefined()
return expect(clearSensitiveCaches()).resolves.toBe(false)
})

it('clears potentially sensitive caches', async () => {
const testKeys = ['cache1', 'cache2', 'app-shell']
const keysMock = jest
.fn()
.mockImplementation(async () => ['cache1', 'cache2', 'app-shell'])
window.caches = { ...cachesDefault, keys: keysMock }
.mockImplementation(() => Promise.resolve(testKeys))
const deleteMock = makeCachesDeleteMock(testKeys)
window.caches = { keys: keysMock, delete: deleteMock }

await clearSensitiveCaches()
const cachesDeleted = await clearSensitiveCaches()
expect(cachesDeleted).toBe(true)

expect(deleteMockDefault).toHaveBeenCalledTimes(3)
expect(deleteMockDefault.mock.calls[0][0]).toBe('cache1')
expect(deleteMockDefault.mock.calls[1][0]).toBe('cache2')
expect(deleteMockDefault.mock.calls[2][0]).toBe('app-shell')
expect(deleteMock).toHaveBeenCalledTimes(3)
expect(deleteMock.mock.calls[0][0]).toBe('cache1')
expect(deleteMock.mock.calls[1][0]).toBe('cache2')
expect(deleteMock.mock.calls[2][0]).toBe('app-shell')
})

it('preserves keepable caches', async () => {
Expand Down
9 changes: 5 additions & 4 deletions services/offline/src/lib/clear-sensitive-caches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ export async function clearSensitiveCaches(

const cacheKeys = await caches.keys()
return Promise.all([
clearDB(dbName),
// remove caches if not in keepable list
// (Resolves to 'false' because this can't detect if anything was deleted):
clearDB(dbName).then(() => false),
// Remove caches if not in keepable list
...cacheKeys.map(key => {
if (!KEEPABLE_CACHES.some(pattern => pattern.test(key))) {
// .then() satisfies typescript
return caches.delete(key).then(() => undefined)
return caches.delete(key)
}
return false
}),
]).then(responses => {
// Return true if any caches have been cleared
Expand Down

0 comments on commit 02c2c6c

Please sign in to comment.