mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
A guardian acting on a managed profile can now connect and disconnect its Withings, WHOOP, Oura, Polar, Strava and Google Health from the profile's integration cards, and each card says whose record it connects for. The connect starts with a fenced fetch that mints a state naming the profile; the provider's callback lands tokens and data there only after checking that the browser belongs to a guardian with a live MANAGE grant on it. Without a provider app of its own, the profile borrows the guardian's, with an audit row. Withings and Google Health callbacks read the session directly, since a provider redirect cannot carry the record-session header.
115 lines
4.3 KiB
TypeScript
115 lines
4.3 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
const fromRoot = (...segments: string[]) => resolve(process.cwd(), ...segments);
|
|
|
|
function expectMatchingMessageKeys(expected: unknown, actual: unknown): void {
|
|
if (
|
|
expected === null ||
|
|
typeof expected !== "object" ||
|
|
actual === null ||
|
|
typeof actual !== "object"
|
|
) {
|
|
expect(typeof actual).toBe(typeof expected);
|
|
return;
|
|
}
|
|
|
|
const expectedRecord = expected as Record<string, unknown>;
|
|
const actualRecord = actual as Record<string, unknown>;
|
|
expect(Object.keys(actualRecord).sort()).toEqual(
|
|
Object.keys(expectedRecord).sort(),
|
|
);
|
|
for (const key of Object.keys(expectedRecord)) {
|
|
expectMatchingMessageKeys(expectedRecord[key], actualRecord[key]);
|
|
}
|
|
}
|
|
|
|
describe("managed record settings UI", () => {
|
|
it("gates shared deep links and controls integrations only through the managed-record routes", async () => {
|
|
const [
|
|
statusView,
|
|
managedSettingsView,
|
|
settingsPage,
|
|
layoutModulePage,
|
|
sectionGate,
|
|
...localeBundles
|
|
] = await Promise.all([
|
|
readFile(
|
|
fromRoot("src/components/settings/managed-integration-status.tsx"),
|
|
"utf8",
|
|
),
|
|
readFile(
|
|
fromRoot("src/components/settings/managed-record-settings-section.tsx"),
|
|
"utf8",
|
|
),
|
|
readFile(fromRoot("src/app/settings/[section]/page.tsx"), "utf8"),
|
|
readFile(fromRoot("src/app/settings/layout/[module]/page.tsx"), "utf8"),
|
|
readFile(
|
|
fromRoot("src/components/settings/record-settings-section-gate.tsx"),
|
|
"utf8",
|
|
),
|
|
...["de", "en", "es", "fr", "it", "pl", "ko", "nl"].map((locale) =>
|
|
readFile(fromRoot(`messages/${locale}.json`), "utf8"),
|
|
),
|
|
]);
|
|
|
|
expect(statusView).toContain("recordSettingsIntegrations");
|
|
expect(statusView).toContain("/api/record-settings/integrations");
|
|
expect(statusView).toContain("assertRecordSettingsResponseForRecord");
|
|
// A guardian's controls go through the fenced managed-record routes, never
|
|
// the owner's provider routes and never a navigation to the API.
|
|
expect(statusView).toContain(
|
|
"/api/record-settings/integrations/${provider}/connect",
|
|
);
|
|
expect(statusView).toContain(
|
|
"/api/record-settings/integrations/${provider}/disconnect",
|
|
);
|
|
expect(statusView).not.toMatch(/["`]\/api\/\$\{provider\}\//);
|
|
expect(statusView).not.toMatch(/loadDocument\(\s*["`]\/api/);
|
|
expect(statusView).not.toMatch(/<Link[\s>]/);
|
|
expect(settingsPage).toContain("RecordSettingsSectionGate");
|
|
expect(sectionGate).toContain("if (!inSharedRecord) return children");
|
|
expect(sectionGate).toContain("ManagedIntegrationStatus");
|
|
expect(sectionGate).toContain("ManagedRecordSettingsSection");
|
|
expect(sectionGate).toContain("if (isLoading)");
|
|
expect(layoutModulePage).toContain(
|
|
'RecordSettingsSectionGate section="layout"',
|
|
);
|
|
expect(managedSettingsView).toContain(
|
|
"safeParseManagedRecordSettingsPatch",
|
|
);
|
|
expect(managedSettingsView).toContain("/api/record-settings/${family}");
|
|
expect(managedSettingsView).not.toContain("/api/auth/");
|
|
expect(managedSettingsView).not.toContain("/api/user/");
|
|
expect(managedSettingsView).not.toContain("<textarea");
|
|
expect(managedSettingsView).not.toContain("Display name");
|
|
expect(managedSettingsView).not.toContain("Enable mood reminder");
|
|
const referenceMessages = JSON.parse(localeBundles[0]).settings.sharedRecord
|
|
.managedSettings;
|
|
for (const bundle of localeBundles) {
|
|
const sharedRecord = JSON.parse(bundle).settings.sharedRecord;
|
|
expect(sharedRecord).toMatchObject({
|
|
unavailableTitle: expect.any(String),
|
|
integrationStatusDescription: expect.any(String),
|
|
managedSettings: {
|
|
profile: {
|
|
title: expect.any(String),
|
|
displayName: expect.any(String),
|
|
},
|
|
modules: { title: expect.any(String) },
|
|
notifications: { title: expect.any(String) },
|
|
thresholds: { title: expect.any(String) },
|
|
coach: { title: expect.any(String) },
|
|
insights: { title: expect.any(String) },
|
|
},
|
|
});
|
|
expectMatchingMessageKeys(
|
|
referenceMessages,
|
|
sharedRecord.managedSettings,
|
|
);
|
|
}
|
|
});
|
|
});
|