-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathsecure-storage.test.ts
More file actions
111 lines (88 loc) · 3.44 KB
/
secure-storage.test.ts
File metadata and controls
111 lines (88 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Secure Storage Tests
*
* Tests for the secure storage migration function.
*/
import { migratePartnerApiKey } from "@/utils/secure-storage";
import { storage } from "@/utils/storage";
// Get the mocked secure store
const SecureStore = require("expo-secure-store");
describe("migratePartnerApiKey", () => {
beforeEach(() => {
// Clear secure storage mock between tests
if (SecureStore.__clearMockStorage) {
SecureStore.__clearMockStorage();
}
// Clear the migration completed flag
storage.removeItem("migration_partner_api_key_completed");
});
it("should migrate from old key to new key when old key exists", async () => {
// Set up old key
await SecureStore.setItemAsync("merchant_api_key", "test-api-key-123");
const migrated = await migratePartnerApiKey();
expect(migrated).toBe(true);
expect(SecureStore.setItemAsync).toHaveBeenCalledWith(
"partner_api_key",
"test-api-key-123",
);
expect(SecureStore.deleteItemAsync).toHaveBeenCalledWith(
"merchant_api_key",
);
});
it("should not overwrite existing new key", async () => {
// Set up both old and new keys
await SecureStore.setItemAsync("merchant_api_key", "old-api-key");
await SecureStore.setItemAsync("partner_api_key", "existing-new-key");
// Clear mock calls from setup
jest.clearAllMocks();
const migrated = await migratePartnerApiKey();
// Should still delete old key but not set new key (existing value preserved)
expect(migrated).toBe(false);
expect(SecureStore.setItemAsync).not.toHaveBeenCalledWith(
"partner_api_key",
expect.anything(),
);
expect(SecureStore.deleteItemAsync).toHaveBeenCalledWith(
"merchant_api_key",
);
});
it("should do nothing when old key does not exist", async () => {
// No keys set up
const migrated = await migratePartnerApiKey();
expect(migrated).toBe(false);
expect(SecureStore.setItemAsync).not.toHaveBeenCalledWith(
"partner_api_key",
expect.anything(),
);
expect(SecureStore.deleteItemAsync).not.toHaveBeenCalled();
});
it("should track migration completion and skip on subsequent calls", async () => {
// Set up old key
await SecureStore.setItemAsync("merchant_api_key", "test-api-key");
// First call should perform migration
const firstResult = await migratePartnerApiKey();
expect(firstResult).toBe(true);
// Clear mocks but keep storage state
jest.clearAllMocks();
// Set up old key again to simulate what would happen if migration ran again
await SecureStore.setItemAsync("merchant_api_key", "another-key");
// Second call should skip due to completion flag
const secondResult = await migratePartnerApiKey();
expect(secondResult).toBe(false);
// Should not have attempted to read/write secure storage for migration
// (only the setup call above)
expect(SecureStore.getItemAsync).not.toHaveBeenCalledWith(
"merchant_api_key",
);
});
it("should properly clean up old key after migration", async () => {
await SecureStore.setItemAsync("merchant_api_key", "api-key-to-migrate");
await migratePartnerApiKey();
// Verify old key was deleted
const oldValue = await SecureStore.getItemAsync("merchant_api_key");
expect(oldValue).toBeNull();
// Verify new key has the value
const newValue = await SecureStore.getItemAsync("partner_api_key");
expect(newValue).toBe("api-key-to-migrate");
});
});