jazz-react-native 0.8.51 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,19 +1,31 @@
1
1
  import { AgentSecret } from "cojson";
2
2
  import { Account, ID } from "jazz-tools";
3
3
  import { beforeEach, describe, expect, it, vi } from "vitest";
4
- import { RNDemoAuth } from "../auth/DemoAuthMethod";
4
+ import { RNDemoAuth, encodeUsername } from "../auth/DemoAuthMethod";
5
5
  import { KvStore, KvStoreContext } from "../storage/kv-store-context";
6
6
 
7
7
  // Initialize mock storage
8
8
  const mockStorage: { [key: string]: string } = {};
9
9
 
10
+ function validateKey(key: string) {
11
+ if (key.includes("+") || key.includes("/") || key.includes("=")) {
12
+ throw new Error("Invalid key");
13
+ }
14
+ }
15
+
10
16
  // Mock KvStore implementation
11
17
  const mockKvStore: KvStore = {
12
- get: vi.fn(async (key: string) => mockStorage[key] || null),
18
+ get: vi.fn(async (key: string) => {
19
+ validateKey(key);
20
+ return mockStorage[key] || null;
21
+ }),
13
22
  set: vi.fn(async (key: string, value: string) => {
23
+ validateKey(key);
24
+
14
25
  mockStorage[key] = value;
15
26
  }),
16
27
  delete: vi.fn(async (key: string) => {
28
+ validateKey(key);
17
29
  delete mockStorage[key];
18
30
  }),
19
31
  clearAll: vi.fn(async () => {
@@ -61,7 +73,7 @@ describe("RNDemoAuth", () => {
61
73
  "testUser",
62
74
  );
63
75
  expect(mockKvStore.set).toHaveBeenCalledWith(
64
- "demo-auth-existing-users-" + btoa("testUser"),
76
+ "demo-auth-existing-users-" + encodeUsername("testUser"),
65
77
  expect.any(String),
66
78
  );
67
79
  });
@@ -83,6 +95,21 @@ describe("RNDemoAuth", () => {
83
95
  expect(result.saveCredentials).toBeDefined();
84
96
  });
85
97
 
98
+ it("should convert unsupported chars from base64 to a valid key", async () => {
99
+ const { mockDriver } = setup();
100
+
101
+ mockDriver.onReady = vi.fn(({ signUp }) => {
102
+ signUp(atob("+/=="));
103
+ });
104
+
105
+ const auth = await RNDemoAuth.init(mockDriver);
106
+ const result = await auth.start();
107
+
108
+ expect(mockDriver.onReady).toHaveBeenCalled();
109
+ expect(result.type).toBe("new");
110
+ expect(result.saveCredentials).toBeDefined();
111
+ });
112
+
86
113
  it("should handle existing user login", async () => {
87
114
  const { mockStorage, mockDriver } = setup();
88
115
 
@@ -145,7 +172,7 @@ describe("RNDemoAuth", () => {
145
172
  });
146
173
 
147
174
  expect(mockKvStore.set).toHaveBeenCalledWith(
148
- "demo-auth-existing-users-" + btoa("testUser"),
175
+ "demo-auth-existing-users-" + encodeUsername("testUser"),
149
176
  expect.any(String),
150
177
  );
151
178
 
@@ -168,7 +195,7 @@ describe("RNDemoAuth", () => {
168
195
  accountSecret: "test-secret" as AgentSecret,
169
196
  };
170
197
 
171
- mockStorage["demo-auth-existing-users-" + btoa("testUser")] =
198
+ mockStorage["demo-auth-existing-users-" + encodeUsername("testUser")] =
172
199
  JSON.stringify(credentials);
173
200
 
174
201
  mockDriver.onReady = vi.fn(({ logInAs }) => {
@@ -214,7 +241,7 @@ describe("RNDemoAuth", () => {
214
241
  });
215
242
 
216
243
  expect(mockKvStore.set).toHaveBeenCalledWith(
217
- "demo-auth-existing-users-" + btoa("testUser-2"),
244
+ "demo-auth-existing-users-" + encodeUsername("testUser-2"),
218
245
  expect.any(String),
219
246
  );
220
247
 
@@ -265,7 +292,7 @@ describe("RNDemoAuth", () => {
265
292
  await RNDemoAuth.init(mockDriver);
266
293
 
267
294
  expect(mockKvStore.set).toHaveBeenCalledWith(
268
- "demo-auth-existing-users-" + btoa("testUser"),
295
+ "demo-auth-existing-users-" + encodeUsername("testUser"),
269
296
  value,
270
297
  );
271
298