@terreno/admin-frontend 57.19.2 → 57.20.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.
Files changed (149) hide show
  1. package/CHANGELOG.md +393 -0
  2. package/dist/AdminFieldRenderer.d.ts +3 -2
  3. package/dist/AdminFieldRenderer.d.ts.map +1 -1
  4. package/dist/AdminFieldRenderer.js +12 -5
  5. package/dist/AdminFieldRenderer.js.map +1 -1
  6. package/dist/AdminFieldRendererCore.d.ts +6 -3
  7. package/dist/AdminFieldRendererCore.d.ts.map +1 -1
  8. package/dist/AdminFieldRendererCore.js +10 -7
  9. package/dist/AdminFieldRendererCore.js.map +1 -1
  10. package/dist/AdminFilterDrawer.d.ts.map +1 -1
  11. package/dist/AdminFilterDrawer.js +2 -3
  12. package/dist/AdminFilterDrawer.js.map +1 -1
  13. package/dist/AdminHome.d.ts.map +1 -1
  14. package/dist/AdminHome.js +5 -2
  15. package/dist/AdminHome.js.map +1 -1
  16. package/dist/AdminModelForm.d.ts.map +1 -1
  17. package/dist/AdminModelForm.js +4 -0
  18. package/dist/AdminModelForm.js.map +1 -1
  19. package/dist/AdminNestedArrayField.d.ts +2 -0
  20. package/dist/AdminNestedArrayField.d.ts.map +1 -1
  21. package/dist/AdminNestedArrayField.js +51 -25
  22. package/dist/AdminNestedArrayField.js.map +1 -1
  23. package/dist/AdminNestedObjectField.d.ts +43 -0
  24. package/dist/AdminNestedObjectField.d.ts.map +1 -0
  25. package/dist/AdminNestedObjectField.js +72 -0
  26. package/dist/AdminNestedObjectField.js.map +1 -0
  27. package/dist/AdminRolesField.d.ts +3 -1
  28. package/dist/AdminRolesField.d.ts.map +1 -1
  29. package/dist/AdminRolesField.js +6 -2
  30. package/dist/AdminRolesField.js.map +1 -1
  31. package/dist/AdminShell.d.ts +3 -3
  32. package/dist/AdminShell.d.ts.map +1 -1
  33. package/dist/AdminShell.js +4 -6
  34. package/dist/AdminShell.js.map +1 -1
  35. package/dist/ConfigurationScreen.d.ts.map +1 -1
  36. package/dist/ConfigurationScreen.js +3 -3
  37. package/dist/ConfigurationScreen.js.map +1 -1
  38. package/dist/ConsentHistory.d.ts.map +1 -1
  39. package/dist/ConsentHistory.js +17 -2
  40. package/dist/ConsentHistory.js.map +1 -1
  41. package/dist/ConsentResponseViewer.d.ts.map +1 -1
  42. package/dist/ConsentResponseViewer.js +20 -2
  43. package/dist/ConsentResponseViewer.js.map +1 -1
  44. package/dist/Constants.d.ts +5 -2
  45. package/dist/Constants.d.ts.map +1 -1
  46. package/dist/Constants.js +5 -2
  47. package/dist/Constants.js.map +1 -1
  48. package/dist/UserSecurityScreen.d.ts +45 -0
  49. package/dist/UserSecurityScreen.d.ts.map +1 -0
  50. package/dist/UserSecurityScreen.js +66 -0
  51. package/dist/UserSecurityScreen.js.map +1 -0
  52. package/dist/consentPdfTemplates.d.ts +64 -0
  53. package/dist/consentPdfTemplates.d.ts.map +1 -0
  54. package/dist/consentPdfTemplates.js +160 -0
  55. package/dist/consentPdfTemplates.js.map +1 -0
  56. package/dist/generateConsentHistoryPdf.d.ts.map +1 -1
  57. package/dist/generateConsentHistoryPdf.js +7 -55
  58. package/dist/generateConsentHistoryPdf.js.map +1 -1
  59. package/dist/generateConsentPdf.d.ts +1 -26
  60. package/dist/generateConsentPdf.d.ts.map +1 -1
  61. package/dist/generateConsentPdf.js +7 -69
  62. package/dist/generateConsentPdf.js.map +1 -1
  63. package/dist/index.d.ts +3 -0
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +3 -0
  66. package/dist/index.js.map +1 -1
  67. package/dist/orgs/MemberSecurityPanel.d.ts +34 -0
  68. package/dist/orgs/MemberSecurityPanel.d.ts.map +1 -0
  69. package/dist/orgs/MemberSecurityPanel.js +204 -0
  70. package/dist/orgs/MemberSecurityPanel.js.map +1 -0
  71. package/dist/orgs/OrgMembersScreen.d.ts.map +1 -1
  72. package/dist/orgs/OrgMembersScreen.js +33 -6
  73. package/dist/orgs/OrgMembersScreen.js.map +1 -1
  74. package/dist/orgs/adminModelEndpoints.d.ts +13 -1
  75. package/dist/orgs/adminModelEndpoints.d.ts.map +1 -1
  76. package/dist/orgs/adminModelEndpoints.js +30 -13
  77. package/dist/orgs/adminModelEndpoints.js.map +1 -1
  78. package/dist/orgs/useMemberSecurityApi.d.ts +97 -0
  79. package/dist/orgs/useMemberSecurityApi.d.ts.map +1 -0
  80. package/dist/orgs/useMemberSecurityApi.js +98 -0
  81. package/dist/orgs/useMemberSecurityApi.js.map +1 -0
  82. package/dist/pdfUtils.d.ts +15 -0
  83. package/dist/pdfUtils.d.ts.map +1 -1
  84. package/dist/pdfUtils.js +70 -0
  85. package/dist/pdfUtils.js.map +1 -1
  86. package/dist/types.d.ts +5 -1
  87. package/dist/types.d.ts.map +1 -1
  88. package/dist/types.js.map +1 -1
  89. package/dist/useAdminApi.d.ts +8 -2
  90. package/dist/useAdminApi.d.ts.map +1 -1
  91. package/dist/useAdminApi.js +11 -46
  92. package/dist/useAdminApi.js.map +1 -1
  93. package/dist/useAdminRoles.d.ts +5 -1
  94. package/dist/useAdminRoles.d.ts.map +1 -1
  95. package/dist/useAdminRoles.js +3 -3
  96. package/dist/useAdminRoles.js.map +1 -1
  97. package/dist/useAdminRpc.d.ts.map +1 -1
  98. package/dist/useAdminRpc.js +18 -3
  99. package/dist/useAdminRpc.js.map +1 -1
  100. package/dist/useConsentHistory.d.ts +7 -0
  101. package/dist/useConsentHistory.d.ts.map +1 -1
  102. package/dist/useConsentHistory.js.map +1 -1
  103. package/package.json +2 -2
  104. package/src/AdminFieldRenderer.tsx +48 -3
  105. package/src/AdminFieldRendererCore.tsx +12 -5
  106. package/src/AdminFilterDrawer.tsx +2 -2
  107. package/src/AdminHome.test.tsx +64 -1
  108. package/src/AdminHome.tsx +14 -6
  109. package/src/AdminModelForm.tsx +4 -0
  110. package/src/AdminNestedArrayField.tsx +116 -44
  111. package/src/AdminNestedObjectField.test.tsx +239 -0
  112. package/src/AdminNestedObjectField.tsx +145 -0
  113. package/src/AdminRolesField.tsx +6 -2
  114. package/src/AdminRolesList.test.tsx +25 -1
  115. package/src/AdminShell.test.tsx +15 -1
  116. package/src/AdminShell.tsx +5 -8
  117. package/src/ConfigurationScreen.tsx +6 -2
  118. package/src/ConsentHistory.test.tsx +69 -1
  119. package/src/ConsentHistory.tsx +39 -1
  120. package/src/ConsentResponseViewer.test.tsx +113 -1
  121. package/src/ConsentResponseViewer.tsx +106 -8
  122. package/src/Constants.ts +5 -2
  123. package/src/UserSecurityScreen.tsx +184 -0
  124. package/src/consentPdfTemplates.test.ts +180 -0
  125. package/src/consentPdfTemplates.ts +228 -0
  126. package/src/generateConsentHistoryPdf.ts +9 -63
  127. package/src/generateConsentPdf.ts +14 -107
  128. package/src/index.tsx +18 -0
  129. package/src/isolated/AdminNestedArrayField.isolated.tsx +93 -1
  130. package/src/isolated/UserSecurityScreen.isolated.tsx +365 -0
  131. package/src/isolated/generateConsentHistoryPdf.isolated.ts +33 -0
  132. package/src/isolated/generateConsentPdf.isolated.ts +47 -0
  133. package/src/isolated/generateConsentPdfMobile.isolated.ts +28 -0
  134. package/src/isolated/generateConsentPdfsMobile.isolated.ts +36 -0
  135. package/src/isolated/hooks.isolated.tsx +97 -0
  136. package/src/orgs/MemberSecurityPanel.test.tsx +584 -0
  137. package/src/orgs/MemberSecurityPanel.tsx +520 -0
  138. package/src/orgs/OrgManagementScreens.test.tsx +120 -1
  139. package/src/orgs/OrgMembersScreen.tsx +71 -4
  140. package/src/orgs/adminModelEndpoints.ts +56 -15
  141. package/src/orgs/useMemberSecurityApi.ts +198 -0
  142. package/src/pdfUtils.test.ts +24 -1
  143. package/src/pdfUtils.ts +93 -0
  144. package/src/types.ts +5 -1
  145. package/src/useAdminApi.ts +23 -3
  146. package/src/useAdminRoles.ts +11 -3
  147. package/src/useAdminRpc.test.tsx +115 -1
  148. package/src/useAdminRpc.ts +19 -3
  149. package/src/useConsentHistory.ts +5 -0
@@ -0,0 +1,365 @@
1
+ import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
2
+ import {Page} from "@terreno/ui";
3
+ import {act, fireEvent, waitFor} from "@testing-library/react-native";
4
+ import React from "react";
5
+ import {renderWithTheme} from "../../../ui/src/test-utils";
6
+ import {AdminProvider} from "../AdminProvider";
7
+ import type {MemberSecuritySummary} from "../orgs/useMemberSecurityApi";
8
+ import type {AdminApi} from "../types";
9
+ import {
10
+ UserSecurityLink,
11
+ type UserSecurityLinkProps,
12
+ UserSecurityScreen,
13
+ type UserSecurityScreenProps,
14
+ userSecurityRoute,
15
+ } from "../UserSecurityScreen";
16
+
17
+ const pushMock = mock((_href: string) => {});
18
+
19
+ // Own process (see scripts/run-tests.sh), so this replacement cannot leak into shared suites.
20
+ mock.module("expo-router", () => ({
21
+ router: {back: mock(() => {}), push: pushMock},
22
+ }));
23
+
24
+ const api = {} as unknown as AdminApi;
25
+ const ORIGIN = "http://api.test";
26
+ const SECURITY_URL = `${ORIGIN}/users/user-1/security`;
27
+ const PANEL = "user-security-screen-panel";
28
+
29
+ interface RecordedCall {
30
+ method: string;
31
+ url: string;
32
+ }
33
+
34
+ interface StubResponse {
35
+ body: unknown;
36
+ status?: number;
37
+ }
38
+
39
+ const summaryFor = (disabled: boolean): MemberSecuritySummary => ({
40
+ actions: [
41
+ {destructive: true, id: "revoke-sessions", label: "Sign out everywhere"},
42
+ {destructive: false, id: "send-password-reset", label: "Send password reset"},
43
+ disabled
44
+ ? {destructive: false, id: "enable", label: "Enable account"}
45
+ : {destructive: true, id: "disable", label: "Disable account"},
46
+ ],
47
+ betterAuthLinked: true,
48
+ disabled,
49
+ emailVerified: true,
50
+ lastSignIn: {at: null, method: "email"},
51
+ legacyPassword: false,
52
+ passkeys: {count: 0, items: []},
53
+ password: true,
54
+ providers: [],
55
+ sessions: {active: disabled ? 0 : 1},
56
+ twoFactor: {backupCodesRemaining: null, enabled: false},
57
+ userId: "user-1",
58
+ });
59
+
60
+ const adminConfig = {
61
+ models: [
62
+ {
63
+ defaultSort: "-created",
64
+ displayName: "Users",
65
+ fields: {},
66
+ listFields: ["email"],
67
+ name: "User",
68
+ routePath: "/admin/users",
69
+ },
70
+ ],
71
+ };
72
+
73
+ const jsonResponse = ({body, status = 200}: StubResponse): Response =>
74
+ new Response(JSON.stringify(body), {headers: {"Content-Type": "application/json"}, status});
75
+
76
+ describe("UserSecurityScreen", () => {
77
+ const originalFetch = globalThis.fetch;
78
+ let calls: RecordedCall[];
79
+ let isDisabled: boolean;
80
+ let configResponse: StubResponse;
81
+ let userResponse: StubResponse;
82
+ let securityResponse: StubResponse | undefined;
83
+
84
+ beforeEach(() => {
85
+ pushMock.mockClear();
86
+ calls = [];
87
+ isDisabled = false;
88
+ securityResponse = undefined;
89
+ configResponse = {body: adminConfig};
90
+ userResponse = {body: {data: {_id: "user-1", email: "ada@example.com", name: "Ada Lovelace"}}};
91
+ globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
92
+ const url = String(input);
93
+ const method = init?.method ?? "GET";
94
+ calls.push({method, url});
95
+ if (url === `${ORIGIN}/admin/config`) {
96
+ return jsonResponse(configResponse);
97
+ }
98
+ if (url === `${ORIGIN}/admin/users/user-1`) {
99
+ return jsonResponse(userResponse);
100
+ }
101
+ if (method === "GET" && url === SECURITY_URL) {
102
+ return jsonResponse(securityResponse ?? {body: {data: summaryFor(isDisabled)}});
103
+ }
104
+ if (method === "POST" && url === `${SECURITY_URL}/disable`) {
105
+ isDisabled = true;
106
+ return jsonResponse({body: {data: {action: "disable", changed: true}}});
107
+ }
108
+ return jsonResponse({body: {title: "Not found"}, status: 404});
109
+ }) as typeof fetch;
110
+ });
111
+
112
+ afterEach(() => {
113
+ globalThis.fetch = originalFetch;
114
+ });
115
+
116
+ const renderScreen = (props: Partial<UserSecurityScreenProps> = {}) =>
117
+ renderWithTheme(
118
+ <AdminProvider
119
+ api={api}
120
+ apiBase="/admin"
121
+ apiOrigin={ORIGIN}
122
+ getAuthHeaders={async () => ({authorization: "Bearer test"})}
123
+ routeBase="/admin"
124
+ >
125
+ <UserSecurityScreen userId="user-1" {...props} />
126
+ </AdminProvider>
127
+ );
128
+
129
+ const renderLoaded = async (props: Partial<UserSecurityScreenProps> = {}) => {
130
+ const screen = renderScreen(props);
131
+ await waitFor(() => {
132
+ expect(screen.getByTestId(`${PANEL}-sessions`)).toBeTruthy();
133
+ });
134
+ return screen;
135
+ };
136
+
137
+ it("shows the user from the admin record and the panel against the platform routes", async () => {
138
+ const screen = await renderLoaded();
139
+
140
+ await waitFor(() => {
141
+ expect(screen.getByTestId("user-security-screen-user-name")).toBeTruthy();
142
+ });
143
+ expect(screen.getByText("Ada Lovelace")).toBeTruthy();
144
+ expect(screen.getAllByTestId("user-security-screen-user-email").length).toBeGreaterThan(0);
145
+ expect(screen.getByText("ada@example.com")).toBeTruthy();
146
+ expect(calls.some((call) => call.method === "GET" && call.url === SECURITY_URL)).toBe(true);
147
+ expect(calls.some((call) => call.url.includes("/orgs/"))).toBe(false);
148
+ expect(screen.getByTestId(`${PANEL}-action-disable`)).toBeTruthy();
149
+ expect(screen.queryByTestId("user-security-screen-user-error")).toBeNull();
150
+ });
151
+
152
+ it("clears the previous name while the next user's record is loading", async () => {
153
+ const screen = await renderLoaded();
154
+ expect(screen.getByText("Ada Lovelace")).toBeTruthy();
155
+
156
+ let releaseBea: ((response: Response) => void) | undefined;
157
+ globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
158
+ const url = String(input);
159
+ const method = init?.method ?? "GET";
160
+ calls.push({method, url});
161
+ if (url === `${ORIGIN}/admin/config`) {
162
+ return jsonResponse(configResponse);
163
+ }
164
+ if (url === `${ORIGIN}/admin/users/user-2`) {
165
+ return await new Promise<Response>((resolve) => {
166
+ releaseBea = resolve;
167
+ });
168
+ }
169
+ if (method === "GET" && url.endsWith("/security")) {
170
+ return jsonResponse({body: {data: summaryFor(false)}});
171
+ }
172
+ return jsonResponse({body: {title: "Not found"}, status: 404});
173
+ }) as typeof fetch;
174
+
175
+ screen.rerender(
176
+ <AdminProvider
177
+ api={api}
178
+ apiBase="/admin"
179
+ apiOrigin={ORIGIN}
180
+ getAuthHeaders={async () => ({authorization: "Bearer test"})}
181
+ routeBase="/admin"
182
+ >
183
+ <UserSecurityScreen userId="user-2" />
184
+ </AdminProvider>
185
+ );
186
+
187
+ await waitFor(() => {
188
+ expect(screen.queryByText("Ada Lovelace")).toBeNull();
189
+ expect(screen.getByTestId("user-security-screen-user-loading")).toBeTruthy();
190
+ });
191
+
192
+ await act(async () => {
193
+ releaseBea?.(
194
+ jsonResponse({
195
+ body: {data: {_id: "user-2", email: "bea@example.com", name: "Bea"}},
196
+ })
197
+ );
198
+ });
199
+ await waitFor(() => {
200
+ expect(screen.getByText("Bea")).toBeTruthy();
201
+ });
202
+ expect(screen.queryByText("Ada Lovelace")).toBeNull();
203
+ });
204
+
205
+ it("posts disable to /users/:id/security/disable after confirming and shows the new state", async () => {
206
+ const screen = await renderLoaded();
207
+
208
+ await act(async () => {
209
+ fireEvent.press(screen.getByTestId(`${PANEL}-action-disable`));
210
+ });
211
+ expect(
212
+ screen.getByText("The account is disabled and signed out on every device.")
213
+ ).toBeTruthy();
214
+ expect(calls.filter((call) => call.method === "POST")).toHaveLength(0);
215
+
216
+ await act(async () => {
217
+ fireEvent.press(screen.getByTestId(`${PANEL}-confirm.primary`));
218
+ });
219
+
220
+ await waitFor(() => {
221
+ expect(screen.getByText("Account disabled")).toBeTruthy();
222
+ });
223
+ expect(calls.filter((call) => call.method === "POST")).toEqual([
224
+ {method: "POST", url: `${SECURITY_URL}/disable`},
225
+ ]);
226
+ await waitFor(() => {
227
+ expect(screen.getByTestId(`${PANEL}-disabled`)).toBeTruthy();
228
+ });
229
+ expect(screen.getByTestId(`${PANEL}-action-enable`)).toBeTruthy();
230
+ });
231
+
232
+ it("goes back to the admin user record by default and to backHref when set", async () => {
233
+ const screen = await renderLoaded();
234
+ act(() => {
235
+ screen.UNSAFE_root.findByType(Page).props.onBack();
236
+ });
237
+ expect(pushMock).toHaveBeenLastCalledWith("/admin/User/user-1");
238
+
239
+ screen.unmount();
240
+ const custom = await renderLoaded({backHref: "/admin/people"});
241
+ act(() => {
242
+ custom.UNSAFE_root.findByType(Page).props.onBack();
243
+ });
244
+ expect(pushMock).toHaveBeenLastCalledWith("/admin/people");
245
+ });
246
+
247
+ it("falls back to the email when the record has no name", async () => {
248
+ userResponse = {body: {email: "grace@example.com"}};
249
+ const screen = await renderLoaded();
250
+
251
+ await waitFor(() => {
252
+ expect(screen.getByText("grace@example.com")).toBeTruthy();
253
+ });
254
+ expect(screen.queryByTestId("user-security-screen-user-email")).toBeNull();
255
+ });
256
+
257
+ it("falls back to the id when the record has neither name nor email", async () => {
258
+ userResponse = {body: {data: {_id: "user-1", email: " "}}};
259
+ const screen = await renderLoaded();
260
+
261
+ await waitFor(() => {
262
+ expect(calls.some((call) => call.url === `${ORIGIN}/admin/users/user-1`)).toBe(true);
263
+ });
264
+ await waitFor(() => {
265
+ expect(screen.getByText("user-1")).toBeTruthy();
266
+ });
267
+ expect(screen.queryByTestId("user-security-screen-user-email")).toBeNull();
268
+ expect(screen.queryByTestId("user-security-screen-user-error")).toBeNull();
269
+ });
270
+
271
+ it("shows the server's refusal in the panel and keeps the header when the target is out of reach", async () => {
272
+ securityResponse = {body: {title: "Target holds permissions you lack"}, status: 403};
273
+ const screen = renderScreen();
274
+
275
+ await waitFor(() => {
276
+ expect(screen.getByTestId(`${PANEL}-error`)).toBeTruthy();
277
+ });
278
+ expect(screen.getByText("Target holds permissions you lack")).toBeTruthy();
279
+ await waitFor(() => {
280
+ expect(screen.getByText("Ada Lovelace")).toBeTruthy();
281
+ });
282
+ expect(screen.queryByTestId(`${PANEL}-action-disable`)).toBeNull();
283
+ expect(screen.queryByTestId("user-security-screen-user-error")).toBeNull();
284
+ });
285
+
286
+ it("names the user by id when the user model is not in the admin config", async () => {
287
+ configResponse = {body: {models: []}};
288
+ const screen = await renderLoaded();
289
+
290
+ await waitFor(() => {
291
+ expect(screen.getByTestId("user-security-screen-user-name")).toBeTruthy();
292
+ });
293
+ expect(screen.getByText("user-1")).toBeTruthy();
294
+ expect(calls.some((call) => call.url.includes("/admin/users/"))).toBe(false);
295
+ });
296
+
297
+ it("reads the record from a custom user model and shows an error when it fails to load", async () => {
298
+ configResponse = {
299
+ body: {models: [{...adminConfig.models[0], name: "Account", routePath: "/admin/users"}]},
300
+ };
301
+ userResponse = {body: {title: "Forbidden"}, status: 403};
302
+ const screen = await renderLoaded({userModelName: "Account"});
303
+
304
+ await waitFor(() => {
305
+ expect(screen.getByTestId("user-security-screen-user-error")).toBeTruthy();
306
+ });
307
+ expect(screen.getByText("Could not load the user record.")).toBeTruthy();
308
+ expect(screen.getByText("user-1")).toBeTruthy();
309
+ // The panel still loads: the security routes do not depend on the admin record.
310
+ expect(screen.getByTestId(`${PANEL}-action-disable`)).toBeTruthy();
311
+ });
312
+
313
+ it("shows a loading state while the user record loads", () => {
314
+ const screen = renderScreen();
315
+ expect(screen.getByTestId("user-security-screen-user-loading")).toBeTruthy();
316
+ });
317
+ });
318
+
319
+ describe("UserSecurityLink", () => {
320
+ beforeEach(() => {
321
+ pushMock.mockClear();
322
+ });
323
+
324
+ const renderLink = (props: Partial<UserSecurityLinkProps> = {}, routeBase?: string) => {
325
+ const link = <UserSecurityLink userId="user 1" {...props} />;
326
+ if (!routeBase) {
327
+ return renderWithTheme(link);
328
+ }
329
+ return renderWithTheme(
330
+ <AdminProvider api={api} apiBase="/admin" routeBase={routeBase}>
331
+ {link}
332
+ </AdminProvider>
333
+ );
334
+ };
335
+
336
+ it("builds the security route under the admin route base", () => {
337
+ expect(userSecurityRoute({userId: "abc"})).toBe("/admin/users/abc/security");
338
+ expect(userSecurityRoute({routeBase: "/staff", userId: "a/b"})).toBe(
339
+ "/staff/users/a%2Fb/security"
340
+ );
341
+ });
342
+
343
+ it("opens the security screen under the provider's route base", async () => {
344
+ const screen = renderLink({}, "/staff");
345
+ await act(async () => {
346
+ fireEvent.press(screen.getByTestId("admin-user-security-link"));
347
+ });
348
+ expect(pushMock).toHaveBeenLastCalledWith("/staff/users/user%201/security");
349
+ });
350
+
351
+ it("defaults to /admin without a provider and honors href", async () => {
352
+ const screen = renderLink();
353
+ await act(async () => {
354
+ fireEvent.press(screen.getByTestId("admin-user-security-link"));
355
+ });
356
+ expect(pushMock).toHaveBeenLastCalledWith("/admin/users/user%201/security");
357
+
358
+ screen.unmount();
359
+ const custom = renderLink({href: "/elsewhere", testID: "custom-link"});
360
+ await act(async () => {
361
+ fireEvent.press(custom.getByTestId("custom-link"));
362
+ });
363
+ expect(pushMock).toHaveBeenLastCalledWith("/elsewhere");
364
+ });
365
+ });
@@ -73,6 +73,7 @@ mock.module("@terreno/ui", () => ({
73
73
  Platform.OS = "web";
74
74
 
75
75
  import {generateConsentHistoryPdf} from "../generateConsentHistoryPdf";
76
+ import {formatDate} from "../pdfUtils";
76
77
  import type {ConsentHistoryEntry} from "../useConsentHistory";
77
78
 
78
79
  const getRenderedText = (): string[] =>
@@ -248,4 +249,36 @@ describe("generateConsentHistoryPdf", () => {
248
249
  expect(buildConsentPdfHtml).toHaveBeenCalled();
249
250
  expect(sharePdfFromHtml).toHaveBeenCalled();
250
251
  });
252
+ it("draws the provisional signer and date for the user, without a note", async () => {
253
+ await generateConsentHistoryPdf({
254
+ ...baseEntry(),
255
+ agreedAt: "2026-10-20T15:30:00Z",
256
+ provisional: true,
257
+ provisionalSignedBy: {name: "Dana Rivera"},
258
+ });
259
+
260
+ const text = getRenderedText();
261
+ expect(text).toContain("Provisional signature");
262
+ expect(text).toContain("Dana Rivera");
263
+ expect(text).toContain(formatDate("2026-10-20T15:30:00Z"));
264
+ expect(text).not.toContain("Note:");
265
+ expect(text).not.toContain("Signer IP:");
266
+ });
267
+
268
+ it("marks a voided provisional signature", async () => {
269
+ await generateConsentHistoryPdf({
270
+ ...baseEntry(),
271
+ provisional: true,
272
+ provisionalSignedBy: null,
273
+ voidedAt: "2026-10-21T09:00:00Z",
274
+ });
275
+ const text = getRenderedText();
276
+ expect(text).toContain("Staff");
277
+ expect(text).toContain(`Voided ${formatDate("2026-10-21T09:00:00Z")}`);
278
+ });
279
+
280
+ it("draws no provisional section for the user's own entry", async () => {
281
+ await generateConsentHistoryPdf({...baseEntry(), provisional: false});
282
+ expect(getRenderedText()).not.toContain("Provisional signature");
283
+ });
251
284
  });
@@ -63,6 +63,7 @@ Platform.OS = "web";
63
63
 
64
64
  // Single import of the module-under-test so all tests share coverage counters.
65
65
  import {generateConsentPdf} from "../generateConsentPdf";
66
+ import {formatDate} from "../pdfUtils";
66
67
 
67
68
  describe("generateConsentPdf", () => {
68
69
  beforeEach(() => {
@@ -297,4 +298,50 @@ describe("generateConsentPdf", () => {
297
298
  const pageBreaks = calls.filter((c) => c.method === "addPage");
298
299
  expect(pageBreaks.length).toBeGreaterThan(0);
299
300
  });
301
+ it("draws a provisional signature with the signer, date, note, IP, and void date", async () => {
302
+ await generateConsentPdf({
303
+ _id: "response-9",
304
+ agreed: true,
305
+ agreedAt: "2026-10-20T15:30:00Z",
306
+ consentFormId: {slug: "intake", title: "Intake", type: "agreement", version: 1},
307
+ provisional: true,
308
+ provisionalNote: "Guardian agreed by phone",
309
+ provisionalSignedBy: {_id: "staff-1", email: "dana@example.com", name: "Dana Rivera"},
310
+ provisionalSignerIp: "10.0.0.7",
311
+ userId: {_id: "member-1", name: "Sam"},
312
+ voidedAt: "2026-10-21T09:00:00Z",
313
+ });
314
+
315
+ const textArgs = calls.filter((c) => c.method === "text").map((c) => String(c.args[0]));
316
+ expect(textArgs).toContain("Provisional signature");
317
+ expect(textArgs).toContain("Dana Rivera");
318
+ expect(textArgs).toContain(formatDate("2026-10-20T15:30:00Z"));
319
+ expect(textArgs).toContain("Guardian agreed by phone");
320
+ expect(textArgs).toContain("10.0.0.7");
321
+ expect(textArgs).toContain(`Voided ${formatDate("2026-10-21T09:00:00Z")}`);
322
+ });
323
+
324
+ it("falls back to Staff when the provisional signer is not populated", async () => {
325
+ await generateConsentPdf({
326
+ agreed: true,
327
+ provisional: true,
328
+ provisionalSignedBy: "staff-1",
329
+ userId: "member-1",
330
+ });
331
+ const textArgs = calls.filter((c) => c.method === "text").map((c) => String(c.args[0]));
332
+ expect(textArgs).toContain("Provisional signature");
333
+ expect(textArgs).toContain("Staff");
334
+ expect(textArgs).not.toContain("Signed at:");
335
+ });
336
+
337
+ it("draws no provisional section for the user's own response", async () => {
338
+ await generateConsentPdf({
339
+ agreed: true,
340
+ agreedAt: "2026-10-20T15:30:00Z",
341
+ provisional: false,
342
+ userId: "member-1",
343
+ });
344
+ const textArgs = calls.filter((c) => c.method === "text").map((c) => String(c.args[0]));
345
+ expect(textArgs).not.toContain("Provisional signature");
346
+ });
300
347
  });
@@ -84,4 +84,32 @@ describe("generateConsentPdf mobile", () => {
84
84
  assert.deepEqual(template.fields, [{label: "Decision:", value: "Declined"}]);
85
85
  assert.deepEqual(template.userInfo, {email: undefined, name: undefined, userId: "legacy-user"});
86
86
  });
87
+ it("passes the full provisional signature to the native template", async () => {
88
+ await generateConsentPdf({
89
+ agreed: true,
90
+ agreedAt: "2026-10-20T15:30:00Z",
91
+ provisional: true,
92
+ provisionalNote: "Guardian agreed by phone",
93
+ provisionalSignedBy: {_id: "staff-1", name: "Dana Rivera"},
94
+ provisionalSignerIp: "10.0.0.7",
95
+ userId: "member-1",
96
+ });
97
+
98
+ const template = buildConsentPdfHtml.mock.calls[0]?.[0] as {
99
+ provisionalSignature?: Record<string, unknown>;
100
+ };
101
+ assert.deepEqual(template.provisionalSignature, {
102
+ note: "Guardian agreed by phone",
103
+ signedAt: "2026-10-20T15:30:00Z",
104
+ signedByName: "Dana Rivera",
105
+ signerIp: "10.0.0.7",
106
+ voidedAt: undefined,
107
+ });
108
+ });
109
+
110
+ it("leaves provisionalSignature unset for the user's own response", async () => {
111
+ await generateConsentPdf({agreed: true, userId: "member-1"});
112
+ const template = buildConsentPdfHtml.mock.calls[0]?.[0] as {provisionalSignature?: unknown};
113
+ assert.isUndefined(template.provisionalSignature);
114
+ });
87
115
  });
@@ -126,4 +126,40 @@ describe("mobile consent PDF generation", () => {
126
126
  });
127
127
  expect(shareCalls[0].filename).toMatch(/^consent-response-\d{4}-\d{2}-\d{2}\.pdf$/);
128
128
  });
129
+ it("passes the provisional signer and date, without the note, for a user's history entry", async () => {
130
+ await generateConsentHistoryPdf({
131
+ _id: "history-3",
132
+ agreed: true,
133
+ agreedAt: "2026-10-20T15:30:00.000Z",
134
+ provisional: true,
135
+ provisionalSignedBy: {name: "Dana Rivera"},
136
+ });
137
+
138
+ expect((templateCalls[0] as {provisionalSignature?: unknown}).provisionalSignature).toEqual({
139
+ signedAt: "2026-10-20T15:30:00.000Z",
140
+ signedByName: "Dana Rivera",
141
+ voidedAt: undefined,
142
+ });
143
+ });
144
+
145
+ it("passes the provisional signature with note and IP for an admin response", async () => {
146
+ await generateConsentPdf({
147
+ agreed: true,
148
+ agreedAt: "2026-10-20T15:30:00.000Z",
149
+ provisional: true,
150
+ provisionalNote: "Phone",
151
+ provisionalSignedBy: {email: "dana@example.com"},
152
+ provisionalSignerIp: "10.0.0.7",
153
+ userId: "member-1",
154
+ voidedAt: "2026-10-21T09:00:00.000Z",
155
+ });
156
+
157
+ expect((templateCalls[0] as {provisionalSignature?: unknown}).provisionalSignature).toEqual({
158
+ note: "Phone",
159
+ signedAt: "2026-10-20T15:30:00.000Z",
160
+ signedByName: "dana@example.com",
161
+ signerIp: "10.0.0.7",
162
+ voidedAt: "2026-10-21T09:00:00.000Z",
163
+ });
164
+ });
129
165
  });
@@ -10,8 +10,14 @@
10
10
  * - the returned hook functions correspond to the generated hook names
11
11
  */
12
12
  import {describe, expect, it, mock} from "bun:test";
13
+ import {configureStore} from "@reduxjs/toolkit";
14
+ import {createApi} from "@reduxjs/toolkit/query/react";
15
+ import {act, waitFor} from "@testing-library/react-native";
13
16
  import React from "react";
17
+ import {Provider} from "react-redux";
14
18
  import {renderWithTheme} from "../../../ui/src/test-utils";
19
+ import {adminOrgScopedArg} from "../orgs/adminModelEndpoints";
20
+ import {OrgContextProvider, useOrgContext} from "../orgs/useOrgContext";
15
21
  import {useAdminApi} from "../useAdminApi";
16
22
  import {useAdminBackgroundTaskMutation} from "../useAdminBackgroundTask";
17
23
  import {useAdminConfig} from "../useAdminConfig";
@@ -234,6 +240,97 @@ describe("useAdminApi", () => {
234
240
  });
235
241
  });
236
242
 
243
+ describe("useAdminApi organization switches", () => {
244
+ // A real RTK Query store whose server answers with the organization header it was sent.
245
+ const buildStore = () => {
246
+ const requests: {orgId?: string; url: string}[] = [];
247
+ const api = createApi({
248
+ baseQuery: async (args: {headers?: Record<string, string>; url: string}) => {
249
+ const orgId = args.headers?.["X-Organization-Id"];
250
+ requests.push({orgId, url: args.url});
251
+ return {data: {_id: "user-1", org: orgId ?? "none"}};
252
+ },
253
+ endpoints: () => ({}),
254
+ reducerPath: "terreno-rtk",
255
+ });
256
+ const store = configureStore({
257
+ middleware: (getDefault) => getDefault({serializableCheck: false}).concat(api.middleware),
258
+ reducer: {[api.reducerPath]: api.reducer},
259
+ });
260
+ return {api, requests, store};
261
+ };
262
+
263
+ it("reads the record again for the organization chosen after the first read started", async () => {
264
+ const {api, requests, store} = buildStore();
265
+ let selectOrganization: (organization: {_id: string; name: string}) => void = () => {};
266
+ let latest: {data?: {org?: string}; isLoading?: boolean} = {};
267
+ const Probe: React.FC = () => {
268
+ selectOrganization = useOrgContext().selectOrganization;
269
+ const {useReadQuery} = useAdminApi(api as never, "/admin/users", "User");
270
+ latest = useReadQuery("user-1");
271
+ return null;
272
+ };
273
+ renderWithTheme(
274
+ <Provider store={store}>
275
+ <OrgContextProvider>
276
+ <Probe />
277
+ </OrgContextProvider>
278
+ </Provider>
279
+ );
280
+ await waitFor(() => expect(latest.data?.org).toBe("none"));
281
+
282
+ // The org switcher settles on a default organization after the screen's first read.
283
+ act(() => {
284
+ selectOrganization({_id: "org-a", name: "Alpha"});
285
+ });
286
+
287
+ await waitFor(() => expect(latest.data?.org).toBe("org-a"));
288
+ expect(latest.isLoading).toBe(false);
289
+ expect(requests).toEqual([
290
+ {orgId: undefined, url: "/admin/users/user-1"},
291
+ {orgId: "org-a", url: "/admin/users/user-1"},
292
+ ]);
293
+ });
294
+
295
+ it("keys list and read entries by the organization carried in the argument", () => {
296
+ const api = makeMockApi();
297
+ runHook(() => useAdminApi(api, "/admin/users", "User"));
298
+ const injected = (api as Record<string, unknown>).__injected as CapturedEndpoints & {
299
+ [key: string]: {
300
+ serializeQueryArgs: (args: {endpointName: string; queryArgs: unknown}) => string;
301
+ };
302
+ };
303
+ const read = injected.adminRead_User;
304
+ const list = injected.adminList_User;
305
+
306
+ expect(read.query(adminOrgScopedArg("abc", "org-b"))).toEqual({
307
+ headers: {"X-Organization-Id": "org-b"},
308
+ method: "GET",
309
+ url: "/admin/users/abc",
310
+ });
311
+ expect(read.providesTags(null, null, adminOrgScopedArg("abc", "org-b"))).toEqual([
312
+ {id: "abc", type: "admin_User"},
313
+ ]);
314
+ expect(
315
+ read.serializeQueryArgs({
316
+ endpointName: "adminRead_User",
317
+ queryArgs: adminOrgScopedArg("abc", "org-b"),
318
+ })
319
+ ).toBe("adminRead_User:org-b:abc");
320
+ expect(list.query(adminOrgScopedArg({limit: 1}, undefined))).toEqual({
321
+ headers: undefined,
322
+ method: "GET",
323
+ url: "/admin/users?limit=1",
324
+ });
325
+ expect(
326
+ list.serializeQueryArgs({
327
+ endpointName: "adminList_User",
328
+ queryArgs: adminOrgScopedArg({limit: 1}, "org-b"),
329
+ })
330
+ ).toBe('adminList_User:org-b:{"limit":1}');
331
+ });
332
+ });
333
+
237
334
  describe("useAdminConfig", () => {
238
335
  it("injects adminConfig query and exposes config hook data", () => {
239
336
  const api = makeMockApi();