Files
order-notification-automation/tests/bpm/native-auth-classification.test.cjs
T

54 lines
4.6 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict');
const {loadForm,loadData,historyURL}=require('../../src/modules/bpm/order-form.cjs');
const {BpmHistoryClient}=require('../../src/modules/bpm/history-client.cjs');
const formURL='https://bpm.skyworthdigital.com/YZSoft/Forms/XForm/FixtureV1.aspx?tid=21';
const response=({status,location,body=''})=>({status:()=>status,headers:()=>location?{location}:{},body:async()=>Buffer.from(body)});
const failures=[
{name:'HTTP401',status:401,want:401},
{name:'HTTP403',status:403,want:403},
{name:'HTTP502',status:502,want:502},
{name:'known same-origin gateway',status:302,location:'/skyworth/sso/open_sso_url',want:401},
{name:'known company SSO',status:302,location:'https://sso.skyworthdigital.com/realms/skyworthdigital/protocol/openid-connect/auth',want:401},
{name:'unknown same-origin redirect',status:302,location:'/maintenance',want:502},
{name:'unrelated login host',status:302,location:'https://sso.skyworthdigital.com.evil.invalid/realms/login',want:502},
{name:'redirect without location',status:302,want:502}
];
for(const fixture of failures){
test('native form HTML preserves '+fixture.name+' classification without following rejected redirects',async()=>{
const calls=[],request={get:async(url,options)=>{calls.push({url,options});return response(fixture);}};
await assert.rejects(loadForm(request,formURL,historyURL,{history:true}),cause=>cause.status===fixture.want);
assert.deepEqual(calls,[{url:formURL,options:{maxRedirects:0,timeout:20000}}]);
});
test('native readonly data preserves '+fixture.name+' classification and its request policy',async()=>{
const calls=[],request={post:async(url,options)=>{calls.push({url,options});return response(fixture);}};
await assert.rejects(loadData(request,new URL(formURL),{history:true}),cause=>cause.status===fixture.want);
assert.deepEqual(calls,[{url:'https://bpm.skyworthdigital.com/YZSoft/BPM/XMLService/DataProvider.ashx',options:{data:'<?xml version="1.0"?><Requests><Params><Method>GetFormReadData</Method><TID>21</TID></Params></Requests>',headers:{'Content-Type':'text/xml'},maxRedirects:0,timeout:20000}}]);
});
for(const phase of ['portal','business'])test('native history '+phase+' preserves '+fixture.name+' classification without a second request',async()=>{
const calls=[];let disposed=false;
const client=new BpmHistoryClient({requestFactory:async()=>({get:async(route,options)=>{calls.push({route,options});return response(phase==='business'&&route==='/'?{status:200,body:'Yiez.clientId="fixture-token";'}:fixture);},dispose:async()=>{disposed=true;}})});
await assert.rejects(client.scan({cookies:[],origins:[]},['Fixture']),cause=>cause.status===fixture.want);
assert.equal(calls.length,phase==='portal'?1:2);assert.equal(calls.every(call=>call.options.maxRedirects===0),true);assert.equal(disposed,true);
});
}
test('native history treats only an explicit login form as expired when a 200 portal has no token',async()=>{
for(const [body,want]of [['<form id="kc-form-login"></form>',401],['<h1>Maintenance</h1>',502]]){
let calls=0,disposed=false;const client=new BpmHistoryClient({requestFactory:async()=>({get:async()=>{calls++;return response({status:200,body});},dispose:async()=>{disposed=true;}})});
await assert.rejects(client.scan({},['Fixture']),cause=>cause.status===want);assert.equal(calls,1);assert.equal(disposed,true);
}
});
test('native history resolves relative redirects against the actual business request path',async()=>{
const client=new BpmHistoryClient({requestFactory:async()=>({get:async route=>response(route==='/'?{status:200,body:'Yiez.clientId="fixture-token";'}:{status:302,location:'login'}),dispose:async()=>{}})});
await assert.rejects(client.scan({},['Fixture']),cause=>cause.status===502);
});
test('native form follows only its already validated temporary SSO to history form chain',async()=>{
const entry='https://bpm.skyworthdigital.com/sso/ssologin.aspx?'+new URLSearchParams({ReturnUrl:'/YZSoft/Forms/Read.aspx?tid=21',todo_token:'FIXTURE',todo_account:'fixture'}),calls=[];
const request={get:async(url,options)=>{calls.push(url);assert.equal(options.maxRedirects,0);return response(url===entry?{status:302,location:'/YZSoft/Forms/Read.aspx?tid=21'}:url.endsWith('/Read.aspx?tid=21')?{status:302,location:formURL}:{status:200,body:'<form>fixture</form>'});}};
const loaded=await loadForm(request,entry,historyURL,{history:true});assert.equal(loaded.url.href,formURL);assert.equal(loaded.bytes.toString(),'<form>fixture</form>');assert.deepEqual(calls,[entry,'https://bpm.skyworthdigital.com/YZSoft/Forms/Read.aspx?tid=21',formURL]);
});