Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@danj/audible-auth

Locked Audible gateway using one owner-provisioned Kody login.

FollowFork

Files

disconnect-target-app.test.ts

60 lines · 3.0 KB · TypeScript
import assert from 'node:assert/strict'
import { beforeEach, test } from 'node:test'
import { fixture as f, reset } from './helpers/runtime.ts'
const app = (await import('../app.ts')).default
const { readBackoff } = await import('../backoff.ts')
Object.assign(f.context, { appBasePath:'', hostedUrl:'https://audible-auth--danj.kody.run/' })
const base = 'https://audible-auth--danj.kody.run/'
let direct: string[] = []
const post = (action: string, fields: Record<string,string> = {}) => app.fetch(new Request(base + action, { method:'POST', body:new URLSearchParams(fields) }))
const get = (location = base) => app.fetch(new Request(new URL(location, base)))
beforeEach(() => {
 reset(); direct = []
 globalThis.fetch = async url => {
  direct.push(String(url))
  return String(url).endsWith('/auth/token') ? Response.json({ access_token:crypto.randomUUID() }) : Response.json({})
 }
})
test('a leftover device-v1 record is ignored: Disconnect uses only the login', async () => {
 f.store.set('device-v1', { locale:'ca', withUsername:false, serial:'TEST', deviceType:'TEST', connectedAt:'2026-10-30T00:00:00Z' })
 const response = await post('disconnect', { target:'login', confirm:'yes' })
 assert.equal(response.status, 303)
 assert.deepEqual(direct, []); assert.equal(f.fetches, 1)
 assert.equal((await readBackoff()).disconnected, true)
})
test('Verify redirects to a safe GET, preserving the approved return link', async () => {
 const back = 'https://some-app--danj.kody.run/'
 const response = await post('verify', { return:back })
 assert.equal(response.status, 303)
 const location = response.headers.get('location')!
 assert.equal(new URL(location, base).searchParams.get('return'), back)
 assert.match(await (await get(location)).text(), /Library verification succeeded/)
 await get(location)
 assert.equal(f.fetches, 1, 'reload is GET and does not re-verify')
})
test('successful Disconnect redirects to GET with a fixed outcome message', async () => {
 assert.match(await (await get()).text(), /name="target" value="login"/)
 const current = await post('disconnect', { target:'login', confirm:'yes' })
 assert.equal(current.status, 303)
 const location = current.headers.get('location')!
 assert.match(await (await get(location)).text(), /you can delete <code>audible-login<\/code>/)
 await get(location); assert.equal(f.fetches, 1)
})
test('missing or invalid Disconnect targets are refused without provider calls', async () => {
 for (const fields of [{}, { target:'other' }, { target:'legacy', confirm:'yes' }]) {
  const response = await post('disconnect', fields)
  assert.equal(response.status, 400)
 }
 assert.equal(f.fetches, 0); assert.deepEqual(direct, [])
})

test('disconnect asks for confirmation before removing a device', async () => {
 const page = await post('disconnect', { target:'login' })
 assert.equal(page.status, 200)
 const html = await page.text()
 assert.match(html, /<html lang="en">/)
 assert.match(html, /<meta charset="utf-8">/)
 assert.match(html, /<h1>Disconnect Audible\?<\/h1>/)
 assert.ok(html.indexOf('>Cancel</a>') < html.indexOf('<form'))
 assert.equal(f.fetches, 0)
})