Skip to content

Commit

Permalink
Adds tests for KeyStoreWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
2624789 committed Apr 17, 2024
1 parent 3973d10 commit 34a7988
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
32 changes: 32 additions & 0 deletions __mocks__/react-native-secure-key-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ACCESSIBLE } from "react-native-secure-key-store"

export { ACCESSIBLE }

class RNSecureKeyStoreMock {
store

constructor() {
this.store = new Map()
}

get(k) {
const result = this.store.get(k)
return Promise.resolve(result)
}

remove(k) {
this.store.delete(k)
return Promise.resolve(true)
}

set(k, value) {
console.log("set", k, value)

this.store.set(k, value)
return Promise.resolve(true)
}
}

const RNSecureKeyStore = new RNSecureKeyStoreMock()

export default RNSecureKeyStore
114 changes: 114 additions & 0 deletions __tests__/utils/KeyStoreWraper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import RNSecureKeyStore from "react-native-secure-key-store"

import { defaultSecureStorageState } from "@app/store/persistent-state/state-migrations"
import KeyStoreWrapper from "@app/utils/storage/secureStorage"

const options = { accessible: "AccessibleAlwaysThisDeviceOnly" }

describe("KeyStoreWrapper", () => {
beforeEach(() => {
jest.clearAllMocks()
})

describe("getSecureStorageState", () => {
let spy: jest.SpyInstance

beforeEach(() => {
spy = jest.spyOn(RNSecureKeyStore, "get")
})

afterEach(() => {
spy.mockRestore()
})

it("should use the right key", async () => {
await KeyStoreWrapper.getSecureStorageState()
expect(spy).toHaveBeenCalledWith("secureState")
})

it("should parse and return secure stored state on success", async () => {
const secureStorageState = { galoyAuthToken: "token" }
spy.mockResolvedValue(JSON.stringify(secureStorageState))

const state = await KeyStoreWrapper.getSecureStorageState()
expect(spy).toHaveBeenCalledWith("secureState")
expect(state).toEqual(secureStorageState)
})

it("should return default secure storage state on error", async () => {
spy.mockRejectedValue(new Error("Simulated error"))

const state = await KeyStoreWrapper.getSecureStorageState()
expect(spy).toHaveBeenCalledWith("secureState")
expect(state).toEqual(defaultSecureStorageState)
})
})

describe("setSecureStorageState", () => {
let spy: jest.SpyInstance

beforeEach(() => {
spy = jest.spyOn(RNSecureKeyStore, "set")
})

afterEach(() => {
spy.mockRestore()
})

it("should use the right arguments", async () => {
const secureStorageState = { galoyAuthToken: "token" }
await KeyStoreWrapper.setSecureStorageState(secureStorageState)
expect(spy).toHaveBeenCalledWith(
"secureState",
JSON.stringify(secureStorageState),
options,
)
})

it("should return true on success", async () => {
const secureStorageState = { galoyAuthToken: "token" }

const response = await KeyStoreWrapper.setSecureStorageState(secureStorageState)
expect(response).toBe(true)
})

it("should return false on error", async () => {
const secureStorageState = { galoyAuthToken: "token" }
spy.mockRejectedValue(new Error("Simulated error"))

const response = await KeyStoreWrapper.setSecureStorageState(secureStorageState)
expect(response).toBe(false)
spy.mockRestore()
})
})

describe("removeSecureStorageState", () => {
let spy: jest.SpyInstance

beforeEach(() => {
spy = jest.spyOn(RNSecureKeyStore, "remove")
})

afterEach(() => {
spy.mockRestore()
})

it("should use the right key", async () => {
await KeyStoreWrapper.removeSecureStorageState()
expect(spy).toHaveBeenCalledWith("secureState")
})

it("should return true on success", async () => {
const response = await KeyStoreWrapper.removeSecureStorageState()
expect(response).toBe(true)
})

it("should return false on error", async () => {
spy.mockRejectedValue(new Error("Simulated error"))

const response = await KeyStoreWrapper.removeSecureStorageState()
expect(response).toBe(false)
spy.mockRestore()
})
})
})

0 comments on commit 34a7988

Please sign in to comment.