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

Added unit tests to cover exception handling in secure-storage #276

Merged
merged 1 commit into from
Dec 8, 2021
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
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