Skip to content
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
16 changes: 11 additions & 5 deletions services/offline/src/lib/__tests__/clear-sensitive-caches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const deleteMockDefault = makeCachesDeleteMock([])
const cachesDefault = {
keys: keysMockDefault,
delete: deleteMockDefault,
// the following to satisfy types:
has: () => Promise.resolve(true),
open: () => Promise.resolve(new Cache()),
match: () => Promise.resolve(new Response()),
}
window.caches = cachesDefault

Expand Down Expand Up @@ -53,6 +57,7 @@ it('returns false if caches.keys throws', async () => {
throw new Error('Security Error')
})
window.caches = {
...cachesDefault,
keys: spy,
}

Expand All @@ -63,20 +68,21 @@ it('returns false if caches.keys throws', async () => {
})

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

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

expect(deleteMock).toHaveBeenCalledTimes(3)
expect(deleteMock).toHaveBeenCalledTimes(4)
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')
expect(deleteMock.mock.calls[3][0]).toBe('other-assets')
})

it('preserves keepable caches', async () => {
Expand All @@ -93,11 +99,11 @@ it('preserves keepable caches', async () => {

await clearSensitiveCaches()

expect(deleteMockDefault).toHaveBeenCalledTimes(3)
expect(deleteMockDefault).toHaveBeenCalledTimes(4)
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(deleteMockDefault).not.toHaveBeenCalledWith('other-assets')
expect(deleteMockDefault.mock.calls[3][0]).toBe('other-assets')
expect(deleteMockDefault).not.toHaveBeenCalledWith(
'workbox-precache-v2-https://hey.howareya.now/'
)
Expand Down
1 change: 0 additions & 1 deletion services/offline/src/lib/clear-sensitive-caches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const SECTIONS_STORE = 'sections-store'
// Non-sensitive caches that can be kept:
const KEEPABLE_CACHES = [
/^workbox-precache/, // precached static assets
/^other-assets/, // static assets cached at runtime - shouldn't be sensitive
Copy link
Contributor Author

@KaiVandivier KaiVandivier Oct 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is the main change. The rest are to update tests and satisfy typescript

]

declare global {
Expand Down