|
| 1 | +/** |
| 2 | + * Tests for the plane-2 usage-auth report path: |
| 3 | + * - Allow2Api.reportAuthEvent -> POST /api/authEvent with the pairToken seam + method/childId |
| 4 | + * - DeviceDaemon.reportAuthEvent -> pulls creds, defaults childId to the selected child, emits event |
| 5 | + * |
| 6 | + * The HTTP layer is mocked by stubbing global.fetch, so no live endpoint is touched. |
| 7 | + */ |
| 8 | + |
| 9 | +import { test } from 'node:test'; |
| 10 | +import assert from 'node:assert/strict'; |
| 11 | + |
| 12 | +import { Allow2Api } from '../src/api.js'; |
| 13 | +import { DeviceDaemon } from '../src/daemon.js'; |
| 14 | + |
| 15 | +/** Install a fake fetch that records the call and returns a JSON 200. Returns { calls, restore }. */ |
| 16 | +function stubFetch(responseBody = { status: 'success' }, status = 200) { |
| 17 | + const original = global.fetch; |
| 18 | + const calls = []; |
| 19 | + global.fetch = async function (url, options) { |
| 20 | + calls.push({ url, options }); |
| 21 | + return { |
| 22 | + ok: status >= 200 && status < 300, |
| 23 | + status, |
| 24 | + async json() { return responseBody; }, |
| 25 | + }; |
| 26 | + }; |
| 27 | + return { calls, restore() { global.fetch = original; } }; |
| 28 | +} |
| 29 | + |
| 30 | +test('Allow2Api.reportAuthEvent POSTs the exact /api/authEvent contract', async () => { |
| 31 | + const { calls, restore } = stubFetch({ status: 'success' }); |
| 32 | + try { |
| 33 | + const api = new Allow2Api({ apiUrl: 'https://example.test', vid: 42, token: 'devtok' }); |
| 34 | + const res = await api.reportAuthEvent({ |
| 35 | + userId: 'owner-uuid', |
| 36 | + pairId: 7, |
| 37 | + pairToken: 'pair-secret', |
| 38 | + childId: 'child-uuid', |
| 39 | + method: 'pin', |
| 40 | + }); |
| 41 | + |
| 42 | + assert.equal(res.status, 'success'); |
| 43 | + assert.equal(calls.length, 1); |
| 44 | + |
| 45 | + const { url, options } = calls[0]; |
| 46 | + assert.equal(url, 'https://example.test/api/authEvent'); |
| 47 | + assert.equal(options.method, 'POST'); |
| 48 | + assert.equal(options.headers['Content-Type'], 'application/json'); |
| 49 | + |
| 50 | + const body = JSON.parse(options.body); |
| 51 | + assert.deepEqual(body, { |
| 52 | + userId: 'owner-uuid', |
| 53 | + pairId: 7, |
| 54 | + pairToken: 'pair-secret', |
| 55 | + deviceToken: 'devtok', // pulled from the client's version token (like logUsage) |
| 56 | + childId: 'child-uuid', |
| 57 | + method: 'pin', |
| 58 | + }); |
| 59 | + } finally { |
| 60 | + restore(); |
| 61 | + } |
| 62 | +}); |
| 63 | + |
| 64 | +test('Allow2Api.reportAuthEvent surfaces a 401 as an error (best-effort, no swallow)', async () => { |
| 65 | + const { restore } = stubFetch({ status: 'error', message: 'Invalid request.' }, 401); |
| 66 | + try { |
| 67 | + const api = new Allow2Api({ apiUrl: 'https://example.test', vid: 42, token: 'devtok' }); |
| 68 | + await assert.rejects( |
| 69 | + () => api.reportAuthEvent({ userId: 'o', pairId: 1, pairToken: 'p', method: 'pin' }), |
| 70 | + (err) => err.status === 401, |
| 71 | + ); |
| 72 | + } finally { |
| 73 | + restore(); |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +test('DeviceDaemon.reportAuthEvent uses stored creds, defaults childId, and emits', async () => { |
| 78 | + const daemon = new DeviceDaemon({ |
| 79 | + activities: [{ id: 1 }], |
| 80 | + credentialBackend: { async load() { return null; }, async store() {}, async clear() {} }, |
| 81 | + childResolver: { resolve() { return null; } }, |
| 82 | + }); |
| 83 | + |
| 84 | + // Simulate a paired + child-selected device without touching the network. |
| 85 | + daemon._credentials = { userId: 'owner-uuid', pairId: 7, pairToken: 'pair-secret', children: [] }; |
| 86 | + daemon._childId = 'selected-child'; |
| 87 | + |
| 88 | + let apiParams = null; |
| 89 | + daemon._api.reportAuthEvent = async (params) => { apiParams = params; return { status: 'success' }; }; |
| 90 | + |
| 91 | + let emitted = null; |
| 92 | + daemon.on('auth-event-reported', (e) => { emitted = e; }); |
| 93 | + |
| 94 | + const res = await daemon.reportAuthEvent({ method: 'offline_code' }); |
| 95 | + |
| 96 | + assert.equal(res.status, 'success'); |
| 97 | + assert.deepEqual(apiParams, { |
| 98 | + userId: 'owner-uuid', |
| 99 | + pairId: 7, |
| 100 | + pairToken: 'pair-secret', |
| 101 | + childId: 'selected-child', // defaulted from the selected child |
| 102 | + method: 'offline_code', |
| 103 | + }); |
| 104 | + assert.deepEqual(emitted, { childId: 'selected-child', method: 'offline_code' }); |
| 105 | +}); |
| 106 | + |
| 107 | +test('DeviceDaemon.reportAuthEvent throws when the device is not paired', async () => { |
| 108 | + const daemon = new DeviceDaemon({ |
| 109 | + activities: [{ id: 1 }], |
| 110 | + credentialBackend: { async load() { return null; }, async store() {}, async clear() {} }, |
| 111 | + childResolver: { resolve() { return null; } }, |
| 112 | + }); |
| 113 | + daemon._credentials = null; |
| 114 | + |
| 115 | + await assert.rejects(() => daemon.reportAuthEvent({ method: 'pin' }), /not paired/i); |
| 116 | +}); |
0 commit comments