oauth.test.ts36 lines · main
1import { test } from 'node:test';
2import { strict as assert } from 'node:assert';
3import { handleCallback, generateState, isLoopback } from './oauth.js';
4
5test('generateState yields 64-hex chars', () => {
6 const s = generateState();
7 assert.match(s, /^[a-f0-9]{64}$/);
8});
9
10test('isLoopback accepts 127.0.0.1 + localhost only', () => {
11 assert.equal(isLoopback('http://127.0.0.1:8080/cb'), true);
12 assert.equal(isLoopback('http://localhost:8080/cb'), true);
13 assert.equal(isLoopback('http://example.com/cb'), false);
14 assert.equal(isLoopback('https://127.0.0.1:8080/cb'), false);
15});
16
17test('handleCallback rejects state mismatch', () => {
18 const req = new Request('http://127.0.0.1/cb?token=t&state=B');
19 const r = handleCallback(req, 'A');
20 assert.equal(r.ok, false);
21 if (!r.ok) assert.match(r.reason, /state/);
22});
23
24test('handleCallback accepts matching state', () => {
25 const req = new Request('http://127.0.0.1/cb?token=tk&state=AA');
26 const r = handleCallback(req, 'AA');
27 assert.equal(r.ok, true);
28 if (r.ok) assert.equal(r.token, 'tk');
29});
30
31test('handleCallback honors denied=1', () => {
32 const req = new Request('http://127.0.0.1/cb?denied=1&state=AA');
33 const r = handleCallback(req, 'AA');
34 assert.equal(r.ok, false);
35 if (!r.ok) assert.match(r.reason, /denied/);
36});