-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |