Skip to content

Commit

Permalink
Added unit tests to cover exception handling in secure-storage; Fixed…
Browse files Browse the repository at this point in the history
… a minor issue (#276)
  • Loading branch information
radomird authored Dec 8, 2021
1 parent cbd90e9 commit 68a0536
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/lib/secure-storage/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { mocked } from 'ts-jest/utils'

import { setItem, getItem } from './storage'
const { saveObject, getObject } = jest.requireActual('./index')

jest.mock('./storage', () => ({
setItem: jest.fn(),
getItem: jest.fn(),
}))

const getItemMocked = mocked(getItem)
const setItemMocked = mocked(setItem)

describe('storage', () => {
afterEach(() => {
getItemMocked.mockClear()
setItemMocked.mockClear()
})

it('saveObject to reject when exception occurs in setItem', async () => {
setItemMocked.mockRejectedValueOnce('setItem error')
await expect(saveObject('foo', 'bar')).rejects.toEqual('setItem error')
})

it('getObject to return null when exception occurs in getItem', async () => {
getItemMocked.mockRejectedValueOnce('getItem error')
await expect(getObject('foo')).resolves.toBeNull()
})

it('getObject to return null when exception occurs in JSON.parse', async () => {
getItemMocked.mockResolvedValueOnce('bad json')
await expect(getObject('foo')).resolves.toBeNull()
})
})
72 changes: 72 additions & 0 deletions src/lib/secure-storage/storage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { writeAsStringAsync, readAsStringAsync } from 'expo-file-system'
import { getItemAsync, setItemAsync } from 'expo-secure-store'
import { mocked } from 'ts-jest/utils'
const { setItem, getItem } = jest.requireActual('./storage')

jest.mock('expo-file-system', () => ({
writeAsStringAsync: jest.fn(),
getInfoAsync: jest.fn().mockReturnValue({ exists: true }),
readAsStringAsync: jest.fn(),
}))

jest.mock('expo-secure-store', () => ({
getItemAsync: jest.fn(),
setItemAsync: jest.fn(),
}))

global.crypto = {
getRandomValues: jest.fn().mockReturnValue(new Uint8Array(32)),
}

const writeAsStringAsyncMocked = mocked(writeAsStringAsync)
const readAsStringAsyncMocked = mocked(readAsStringAsync)
const getItemAsyncMocked = mocked(getItemAsync)
const setItemAsyncMocked = mocked(setItemAsync)

describe('storage', () => {
afterEach(() => {
writeAsStringAsyncMocked.mockClear()
readAsStringAsyncMocked.mockClear()
getItemAsyncMocked.mockClear()
setItemAsyncMocked.mockClear()
})

it('setItem to reject when exception occurs in readAsStringAsync', async () => {
readAsStringAsyncMocked.mockRejectedValueOnce('readAsStringAsync error')
await expect(setItem('foo', 'bar')).rejects.toEqual(
'readAsStringAsync error'
)
})

it('setItem to reject when exception occurs in setItemAsync', async () => {
setItemAsyncMocked.mockRejectedValueOnce('setItemAsync error')
readAsStringAsyncMocked.mockReturnValueOnce(null)
await expect(setItem('foo', 'bar')).rejects.toEqual('setItemAsync error')
})

it('setItem to reject when exception occurs in writeAsStringAsync', async () => {
setItemAsyncMocked.mockResolvedValueOnce()
readAsStringAsyncMocked.mockReturnValueOnce(null)
writeAsStringAsyncMocked.mockRejectedValueOnce('writeAsStringAsync error')
await expect(setItem('foo', 'bar')).rejects.toEqual(
'writeAsStringAsync error'
)
})

it('getItem to reject when exception occurs in readAsStringAsync', async () => {
readAsStringAsyncMocked.mockRejectedValueOnce('readAsStringAsync error')
await expect(getItem('foo')).rejects.toEqual('readAsStringAsync error')
})

it('getItem to reject when getItemAsync returns null', async () => {
readAsStringAsyncMocked.mockResolvedValueOnce('encrypted_storage_key')
getItemAsyncMocked.mockResolvedValueOnce(null)
await expect(getItem('foo')).rejects.toEqual('Storage key not found')
})

it('getItem to reject when exception occurs in getItemAsyncMocked', async () => {
readAsStringAsyncMocked.mockResolvedValueOnce('encrypted_storage_key')
getItemAsyncMocked.mockRejectedValueOnce('getItemAsync error')
await expect(getItem('foo')).rejects.toEqual('getItemAsync error')
})
})
2 changes: 1 addition & 1 deletion src/lib/secure-storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const getDecryptedStorage = async () => {
const encryptedStorage = await FileSystem.readAsStringAsync(
storageFileNameURI
)
if (encryptedStorage.length === 0) {
if (!encryptedStorage || encryptedStorage.length === 0) {
return {}
}

Expand Down

0 comments on commit 68a0536

Please sign in to comment.