fix(scraper): harden Bloomberg login state (#40)
This commit is contained in:
52
scraper-service/tests/login-state-ticket40.test.js
Normal file
52
scraper-service/tests/login-state-ticket40.test.js
Normal file
@ -0,0 +1,52 @@
|
||||
const { test } = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const vm = require('node:vm')
|
||||
const cheerio = require('cheerio')
|
||||
const { inspectLoginState, classifyLoginState, authenticatedDestination, clearLoginInput, typeVisibleInput, submitInputForm } = require('../bloomberg-login')
|
||||
|
||||
// Synthetic DOM fixtures only; no browser navigation, input capture or publisher HTML.
|
||||
function fixture (html) {
|
||||
const $ = cheerio.load(html); const clicked = []; const values = new Map()
|
||||
const wrap = node => ({
|
||||
hidden: $(node).is('[hidden]'), disabled: $(node).is('[disabled]'),
|
||||
getAttribute: name => $(node).attr(name) ?? null,
|
||||
getClientRects: () => $(node).parents().addBack().is('[hidden], [aria-hidden="true"], [style="display:none"]') ? [] : [{}],
|
||||
get form () { const form = $(node).closest('form')[0];return form ? wrap(form) : null },
|
||||
querySelectorAll: selector => $(node).find(selector).toArray().map(wrap),
|
||||
click () { clicked.push($(node).attr('id')) },
|
||||
get value () { return values.has(node) ? values.get(node) : $(node).attr('value') },
|
||||
set value (value) { values.set(node,value) }, dispatchEvent () {}
|
||||
})
|
||||
const document = {querySelectorAll: selector => $(selector).toArray().map(wrap), querySelector: selector => {const n=$(selector)[0];return n ? wrap(n) : null}}
|
||||
const run = (fn,arg) => vm.runInNewContext(`(${fn.toString()})(${arg === undefined ? '' : JSON.stringify(arg)})`, {document, Event:class {}})
|
||||
return {document,run,clicked}
|
||||
}
|
||||
test('only explicit visible session evidence without login form confirms authentication', () => {
|
||||
for (const [html,want] of [
|
||||
['<main>Public page</main>','authentication_unconfirmed'],
|
||||
['<a href="/account/logout">Account control</a>','authenticated'],
|
||||
['<a hidden href="/account/logout">Hidden</a>','authentication_unconfirmed'],
|
||||
['<a href="/account/logout">Account control</a><input type="password">','authentication_unconfirmed'],
|
||||
['<a href="/account/logout">Account control</a><input type="email" name="newsletter">','authenticated'],
|
||||
['<div id="px-captcha"></div>','challenge'],
|
||||
['<input autocomplete="one-time-code">','two_factor_required'],
|
||||
['<div data-testid="login-error"></div>','auth_rejected']
|
||||
]) {const f=fixture(html);assert.equal(classifyLoginState(f.run(inspectLoginState)),want)}
|
||||
assert.equal(authenticatedDestination('https://www.bloomberg.com/account/signin'),false)
|
||||
assert.equal(authenticatedDestination('https://other.invalid/'),false)
|
||||
assert.equal(authenticatedDestination('https://www.bloomberg.com/markets'),true)
|
||||
})
|
||||
test('submission targets the entered field form rather than another page form', () => {
|
||||
const f=fixture('<form><button type="submit" id="unrelated">Other form</button></form><form><input id="entered"><button type="submit" id="correct">Submit</button></form>')
|
||||
assert.equal(f.run(submitInputForm,'#entered'),true)
|
||||
assert.deepEqual(f.clicked,['correct'])
|
||||
assert.equal(f.run(submitInputForm,'#missing'),false)
|
||||
})
|
||||
test('visible input is cleared before typing and hidden fields cannot receive credentials', async () => {
|
||||
const f=fixture('<form><input hidden id="hidden"><input id="entered" value="synthetic-prefill"></form>')
|
||||
assert.equal(f.run(clearLoginInput,'#hidden'),false)
|
||||
const typed=[]
|
||||
const page={async waitForSelector(selector,options){assert.equal(options.visible,true)},async evaluate(fn,selector){return f.run(fn,selector)},async type(selector){assert.equal(f.document.querySelector(selector).value,'');typed.push(selector)}}
|
||||
assert.equal(await typeVisibleInput(page,['#hidden','#entered'],'synthetic-test-input'),'#entered')
|
||||
assert.deepEqual(typed,['#entered'])
|
||||
})
|
||||
Reference in New Issue
Block a user