Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(offline): return correct value from clearSensitiveCaches #1008

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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