Branchwell

Inkspring / website / tests / connect-http.test.mjs

1import {test} from 'node:test';
2import assert from 'node:assert/strict';
3import handler from '../api/connect.js';
4import { hash, sealTicket } from '../server/connect-crypto.mjs';
5function response(){return{headers:{},code:0,body:null,setHeader(name,value){this.headers[name]=value},status(value){this.code=value;return this},json(value){this.body=value;return this},send(value){this.body=value;return this},redirect(code,value){this.code=code;this.headers.Location=value;return this}};}
6test('HTTP config is public, uncached and exposes no server environment values',async()=>{
7 const res=response();await handler({method:'GET',url:'/api/desktop/connect/config',query:{action:'config'},headers:{}},res);
8 assert.equal(res.code,200);assert.equal(res.headers['Cache-Control'],'no-store');assert.equal(res.headers['Referrer-Policy'],'no-referrer');assert.equal(res.body.version,1);assert.equal('CONNECT_SECRET' in res.body,false);
9});
10test('HTTP endpoints reject malformed actions, parameters and refresh bodies without reflecting input',async()=>{
11 for(const req of [
12 {method:'POST',query:{action:'config'},headers:{}},
13 {method:'GET',query:{action:'start',provider:['x','instagram']},headers:{}},
14 {method:'POST',query:{action:'refresh'},headers:{'content-type':'text/plain'},body:'untrusted'},
15 {method:'POST',query:{action:'refresh'},headers:{'content-type':'application/json','content-length':'24001'},body:{}},
16 {method:'POST',query:{action:'refresh'},headers:{'content-type':'application/json'},body:'bad json containing a secret'}
17 ]){const res=response();await handler({url:'/api/desktop/connect/refresh',...req},res);assert.ok([400,405].includes(res.code));assert.ok(!JSON.stringify(res.body).includes('secret'));}
18});
19
20test('refreshing a ticket from before a signing key change tells the user to reconnect', async () => {
21 const refreshToken = 'fixture-refresh-token';
22 const clientId = 'fixture-client-id';
23 const ticket = await sealTicket({
24 provider: 'linkedin',
25 clientId,
26 tokenHash: hash(refreshToken),
27 expiresAt: Date.now() + 60_000,
28 }, 'a'.repeat(64), 'refresh');
29 const res = response();
30 await handler({
31 method: 'POST',
32 url: '/api/desktop/connect/refresh',
33 query: { action: 'refresh' },
34 headers: { 'content-type': 'application/json' },
35 body: { provider: 'linkedin', refreshToken, ticket },
36 }, res, { env: { CONNECT_SECRET: 'b'.repeat(64), LINKEDIN_CLIENT_ID: clientId, LINKEDIN_CLIENT_SECRET: 'fixture-client-secret' } });
37 assert.equal(res.code, 401);
38 assert.match(res.body.error, /Open Connections in InkSpring and reconnect/);
39 assert.ok(!res.body.error.includes(refreshToken));
40 assert.ok(!res.body.error.includes(ticket));
41});
42
43test('deprecated OAuth actions return a stable 410 without reflecting token values', async () => {
44 for (const provider of ['reddit', 'tiktok', 'substack', 'youtube']) {
45 for (const action of ['start', 'callback', 'refresh']) {
46 const req={url:'/api/connect',query:{action,provider},method:action==='refresh'?'POST':'GET',headers:{'content-type':'application/json'},body:{provider,refreshToken:'fixture-old-secret'}};
47 const res=response();await handler(req,res);
48 assert.equal(res.code,410);assert.equal(res.headers['Cache-Control'],'no-store');
49 assert.doesNotMatch(JSON.stringify(res.body),/fixture-old-secret/);
50 }
51 }
52});