LifeMate — Personal, Family & Learning Companion

feat/weekly-planner-android
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
js 56 lines 3.36 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import express from 'express';
4 import jwt from 'jsonwebtoken';
5 import { createSessionAuth } from '../src/session_auth.js';
6
7 // Unit boundary tests: the pool is a stub. PostgreSQL integration is separate.
8 const secret = 'synthetic-managed-scope-unit-fixture-not-live';
9 async function probe({ path = '/v1/today', method = 'GET', scope = 'child_education', managed = true,
10 active = true, env = { NODE_ENV: 'test', ENABLE_CHILD_IDENTITY_SANDBOX: 'true' } } = {}) {
11 const app = express();
12 const pool = { async query() { return { rowCount: active ? 1 : 0,
13 rows: active ? [{ access_scope: scope, is_managed_child: managed, child_issuer_user_id: '00000000-0000-4000-8000-000000000004' }] : [] }; } };
14 app.use(createSessionAuth({ pool, jwtSecret: secret, env }));
15 app.use((_req, res) => res.json({ reached: true }));
16 const server = app.listen(0, '127.0.0.1');
17 await new Promise(resolve => server.once('listening', resolve));
18 const token = jwt.sign({ sub: '00000000-0000-4000-8000-000000000001', sid: '00000000-0000-4000-8000-000000000002' },
19 secret, { issuer: 'lifemate', audience: 'lifemate-api', expiresIn: '1h' });
20 try {
21 const response = await fetch('http://127.0.0.1:' + server.address().port + path,
22 { method, headers: { authorization: 'Bearer ' + token } });
23 return { status: response.status, body: await response.json() };
24 } finally { await new Promise(resolve => server.close(resolve)); }
25 }
26
27 test('managed child is fail-closed in production and without the sandbox flag', async () => {
28 assert.equal((await probe({ env: { NODE_ENV: 'production', ENABLE_CHILD_IDENTITY_SANDBOX: 'true' } })).status, 503);
29 assert.equal((await probe({ env: { NODE_ENV: 'test' } })).status, 503);
30 });
31 test('managed marker and stored restricted scope must agree', async () => {
32 assert.equal((await probe({ scope: 'standard' })).status, 401);
33 assert.equal((await probe({ managed: false })).status, 401);
34 });
35 test('restricted device cannot read legacy private records or change identity or membership', async () => {
36 for (const path of ['/v1/today', '/v1/plan-items', '/v1/study/sessions', '/v1/me/learning-attempts',
37 '/v1/education/library', '/v1/education/courses/00000000-0000-4000-8000-000000000003',
38 '/v1/learning/goals', '/v1/learning/checkins', '/v1/me/iran-profile', '/v1/me/menstrual-cycles',
39 '/v1/ai/sessions', '/v1/content-review', '/v1/profile/', '/V1/profile', '/v1/%70rofile']) {
40 assert.equal((await probe({ path })).status, 403, path);
41 }
42 for (const path of ['/v1/profile', '/v1/families', '/v1/sync/mutations', '/v1/auth/change-password']) {
43 assert.equal((await probe({ path, method: 'POST' })).status, 403, path);
44 }
45 });
46 test('restricted device only reaches exact safe catalogue and projected identity reads', async () => {
47 for (const path of ['/v1/profile', '/v1/families', '/v1/child-visibility', '/v1/education/catalog',
48 '/v1/calendar/iran', '/v1/education/units/00000000-0000-4000-8000-000000000003',
49 '/v1/education/units/00000000-0000-4000-8000-000000000003/assessment']) {
50 assert.equal((await probe({ path })).status, 200, path);
51 }
52 });
53 test('ordinary account retains legacy routes; stale managed access is rejected', async () => {
54 assert.equal((await probe({ managed: false, scope: 'standard' })).status, 200);
55 assert.equal((await probe({ active: false })).status, 401);
56 });