|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Unit tests for providers/session.js (pure) — run: node test/session.test.js |
| 3 | + * - jwtExpiresAt: reads `exp` off a JWT payload without verifying; never throws |
| 4 | + * - accessNeedsRefresh: the 5-minute margin, expired, unreadable |
| 5 | + * - classifyRefresh: ONLY a 401 ends the session; offline/5xx keep the tokens; a 2xx is a |
| 6 | + * renewal only when what it carries can be stored |
| 7 | + * - isSessionExpiredError: the shapes a dead session arrives in |
| 8 | + *--------------------------------------------------------------------------------------------*/ |
| 9 | +// @ts-check |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +const assert = require('assert'); |
| 13 | +const S = require('../providers/session'); |
| 14 | + |
| 15 | +let n = 0; |
| 16 | +function test(name, fn) { fn(); n++; console.log(' ok - ' + name); } |
| 17 | + |
| 18 | +/** An unsigned JWT with the given payload — the shape is all jwtExpiresAt reads. */ |
| 19 | +function jwt(payload) { |
| 20 | + const b64 = (o) => Buffer.from(JSON.stringify(o)).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); |
| 21 | + return b64({ alg: 'HS256', typ: 'JWT' }) + '.' + b64(payload) + '.sig'; |
| 22 | +} |
| 23 | + |
| 24 | +const NOW = 1_800_000_000_000; // fixed "now" so the margin arithmetic is exact |
| 25 | + |
| 26 | +test('jwtExpiresAt: reads exp (seconds) as epoch milliseconds', () => { |
| 27 | + assert.strictEqual(S.jwtExpiresAt(jwt({ sub: 1, exp: 1_800_000_123 })), 1_800_000_123_000); |
| 28 | +}); |
| 29 | +test('jwtExpiresAt: survives base64url padding edge cases (payload lengths mod 4)', () => { |
| 30 | + for (const pad of ['', 'x', 'xy', 'xyz']) { |
| 31 | + assert.strictEqual(S.jwtExpiresAt(jwt({ p: pad, exp: 7 })), 7000, 'pad=' + JSON.stringify(pad)); |
| 32 | + } |
| 33 | +}); |
| 34 | +test('jwtExpiresAt: null for a non-JWT, a JWT without exp, garbage, and nothing — never throws', () => { |
| 35 | + assert.strictEqual(S.jwtExpiresAt('opaque-token'), null); |
| 36 | + assert.strictEqual(S.jwtExpiresAt(jwt({ sub: 1 })), null); |
| 37 | + assert.strictEqual(S.jwtExpiresAt('a.!!!.c'), null); |
| 38 | + assert.strictEqual(S.jwtExpiresAt(''), null); |
| 39 | + assert.strictEqual(S.jwtExpiresAt(null), null); |
| 40 | + assert.strictEqual(S.jwtExpiresAt(undefined), null); |
| 41 | +}); |
| 42 | + |
| 43 | +test('accessNeedsRefresh: a token with hours left does not', () => { |
| 44 | + assert.strictEqual(S.accessNeedsRefresh(jwt({ exp: NOW / 1000 + 3600 }), NOW), false); |
| 45 | +}); |
| 46 | +test('accessNeedsRefresh: inside the 5-minute margin, at the margin, and already expired all do', () => { |
| 47 | + assert.strictEqual(S.accessNeedsRefresh(jwt({ exp: NOW / 1000 + 299 }), NOW), true); |
| 48 | + assert.strictEqual(S.accessNeedsRefresh(jwt({ exp: NOW / 1000 + 300 }), NOW), true); |
| 49 | + assert.strictEqual(S.accessNeedsRefresh(jwt({ exp: NOW / 1000 - 1 }), NOW), true); |
| 50 | + assert.strictEqual(S.accessNeedsRefresh(jwt({ exp: NOW / 1000 + 301 }), NOW), false); |
| 51 | +}); |
| 52 | +test('accessNeedsRefresh: an unreadable token is treated as expired (ask the server, do not guess)', () => { |
| 53 | + assert.strictEqual(S.accessNeedsRefresh('opaque', NOW), true); |
| 54 | + assert.strictEqual(S.accessNeedsRefresh('', NOW), true); |
| 55 | +}); |
| 56 | + |
| 57 | +test('classifyRefresh: 200 with an access token → ok', () => { |
| 58 | + assert.strictEqual(S.classifyRefresh({ status: 200, body: { access: 'a' } }), 'ok'); |
| 59 | + assert.strictEqual(S.classifyRefresh({ status: 200, body: { token: 'a' } }), 'ok'); // legacy field name |
| 60 | +}); |
| 61 | +test('classifyRefresh: ONLY an explicit 401 ends the session', () => { |
| 62 | + assert.strictEqual(S.classifyRefresh({ status: 401, body: { error: { code: 'refresh_expired' } } }), 'expired'); |
| 63 | + assert.strictEqual(S.classifyRefresh({ status: 401, body: null }), 'expired'); |
| 64 | +}); |
| 65 | +test('classifyRefresh: offline, 5xx, 403, a 200 with no token, nothing at all → retry (tokens kept)', () => { |
| 66 | + assert.strictEqual(S.classifyRefresh(null), 'retry'); |
| 67 | + assert.strictEqual(S.classifyRefresh({ status: 503, body: null }), 'retry'); |
| 68 | + assert.strictEqual(S.classifyRefresh({ status: 500, body: {} }), 'retry'); |
| 69 | + assert.strictEqual(S.classifyRefresh({ status: 403, body: {} }), 'retry'); |
| 70 | + assert.strictEqual(S.classifyRefresh({ status: 200, body: {} }), 'retry'); |
| 71 | + assert.strictEqual(S.classifyRefresh({ status: 200, body: null }), 'retry'); |
| 72 | +}); |
| 73 | + |
| 74 | +test('classifyRefresh: a 2xx whose access token is not a usable string is NOT a renewal', () => { |
| 75 | + for (const access of [{}, [], ['a'], 123, true]) { |
| 76 | + assert.strictEqual(S.classifyRefresh({ status: 200, body: { access } }), 'retry', 'access=' + JSON.stringify(access)); |
| 77 | + } |
| 78 | + assert.strictEqual(S.classifyRefresh({ status: 200, body: { token: {} } }), 'retry', 'the legacy field too'); |
| 79 | + // The host stores `access || token`, so that is the one judged: a broken `access` is not rescued by a |
| 80 | + // good `token` beside it, and an EMPTY `access` falls through to `token` exactly as the store would. |
| 81 | + assert.strictEqual(S.classifyRefresh({ status: 200, body: { access: {}, token: 'legacy' } }), 'retry'); |
| 82 | + assert.strictEqual(S.classifyRefresh({ status: 200, body: { access: '', token: 'legacy' } }), 'ok'); |
| 83 | +}); |
| 84 | +test('classifyRefresh: a refresh token, when one is sent, has to be a usable string too', () => { |
| 85 | + for (const refresh of [{}, [], ['r'], 123, true]) { |
| 86 | + assert.strictEqual(S.classifyRefresh({ status: 200, body: { access: 'a', refresh } }), 'retry', 'refresh=' + JSON.stringify(refresh)); |
| 87 | + } |
| 88 | +}); |
| 89 | +test('classifyRefresh: no refresh token is still ok — a server that does not rotate sends none', () => { |
| 90 | + for (const body of [{ access: 'a' }, { access: 'a', refresh: null }, { access: 'a', refresh: '' }, { access: 'a', refresh: 'r' }, { token: 'a', refresh: 'r' }]) { |
| 91 | + assert.strictEqual(S.classifyRefresh({ status: 200, body }), 'ok', JSON.stringify(body)); |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +test('isSessionExpiredError: the adapter\'s "<label> API 401: …" shape, with and without e.status', () => { |
| 96 | + const e = new Error('LevelCode Cloud API 401: Your LevelCode Cloud session has expired. Sign in again to continue.'); |
| 97 | + assert.strictEqual(S.isSessionExpiredError(e), true); |
| 98 | + // @ts-ignore |
| 99 | + e.status = 401; assert.strictEqual(S.isSessionExpiredError(e), true); |
| 100 | + assert.strictEqual(S.isSessionExpiredError({ status: 401 }), true); |
| 101 | +}); |
| 102 | +test('isSessionExpiredError: the server codes and the old raw JWT text, as bare strings', () => { |
| 103 | + assert.strictEqual(S.isSessionExpiredError('token_expired'), true); |
| 104 | + assert.strictEqual(S.isSessionExpiredError('refresh_expired'), true); |
| 105 | + assert.strictEqual(S.isSessionExpiredError('Signature has expired'), true); |
| 106 | +}); |
| 107 | +test('isSessionExpiredError: a 402 cap hit, a 500, a 4010-byte message, nothing → not a dead session', () => { |
| 108 | + assert.strictEqual(S.isSessionExpiredError(new Error('LevelCode Cloud API 402: {"error":{"code":"cap_reached"}}')), false); |
| 109 | + assert.strictEqual(S.isSessionExpiredError(new Error('LevelCode Cloud API 500: upstream')), false); |
| 110 | + assert.strictEqual(S.isSessionExpiredError(new Error('read 4010 bytes')), false); |
| 111 | + assert.strictEqual(S.isSessionExpiredError(null), false); |
| 112 | + assert.strictEqual(S.isSessionExpiredError(undefined), false); |
| 113 | +}); |
| 114 | + |
| 115 | +console.log('\nsession: ' + n + ' tests passed.'); |
0 commit comments