74 lines
8.0 KiB
JavaScript
74 lines
8.0 KiB
JavaScript
// Actual Razor view in a loopback, database-free pending-auth fixture. Only
|
||
// synthetic codes are used. Clipboard cases use the browser clipboard + Ctrl+V;
|
||
// native insertion uses trusted CDP input (Android/IME-style), not value assignment.
|
||
const {chromium}=require('playwright');
|
||
const assert=require('node:assert/strict');
|
||
const fs=require('node:fs');
|
||
const base=process.env.OTP_VISUAL_URL||'http://127.0.0.1:5282';
|
||
assert(/^http:\/\/127\.0\.0\.1:\d+$/.test(base),'Loopback fixture required');
|
||
const output=process.env.OTP_SCREENSHOTS;
|
||
(async()=>{const browser=await chromium.launch();const results=[];try{
|
||
for(const width of [1440,390,320]){
|
||
const context=await browser.newContext({viewport:{width,height:950},permissions:['clipboard-read','clipboard-write']});
|
||
const page=await context.newPage();let submits=[],posts=[],pastes=[],insertions=[];
|
||
await page.exposeFunction('observeOtp',x=>{if(x.type==='submit')submits.push(x);else if(x.type==='paste')pastes.push(x);else insertions.push(x);});
|
||
await page.addInitScript(()=>{
|
||
document.addEventListener('paste',e=>{if(e.target.matches('[data-two-factor-digit]'))window.observeOtp({type:'paste',trusted:e.isTrusted});},true);
|
||
document.addEventListener('beforeinput',e=>{if(e.target.matches('[data-two-factor-digit]'))window.observeOtp({type:'beforeinput',trusted:e.isTrusted,inputType:e.inputType,data:e.data});},true);
|
||
document.addEventListener('submit',e=>{if(e.target.matches('[data-two-factor-form]'))window.observeOtp({type:'submit',values:Array.from(e.target.querySelectorAll('[data-two-factor-digit]'),x=>x.value),code:e.target.querySelector('[data-two-factor-code]').value});},true);
|
||
});
|
||
page.on('request',r=>{if(r.method()==='POST'&&new URL(r.url()).pathname==='/Account/TwoFactor')posts.push(new URLSearchParams(r.postData()).get('Code'));});
|
||
const digits=page.locator('[data-two-factor-digit]');
|
||
const fresh=async()=>{await page.goto(base+'/Account/TwoFactor');await digits.first().waitFor();submits=[];posts=[];pastes=[];insertions=[];};
|
||
const clipboard=async text=>{await digits.first().focus();await page.evaluate(text=>navigator.clipboard.writeText(text),text);await page.keyboard.press('Control+V');};
|
||
const rejected=async()=>{await page.getByText('Synthetic authenticator rejection. Try again.',{exact:true}).waitFor();await page.waitForTimeout(30);};
|
||
const assertSubmission=code=>{assert.deepEqual(posts,[code]);assert.equal(submits.length,1);assert.equal(submits[0].code,code);assert.deepEqual(submits[0].values,[...code]);};
|
||
await fresh();
|
||
const layout=await page.evaluate(()=>{
|
||
const box=x=>{const r=x.getBoundingClientRect();return{x:r.x,y:r.y,w:r.width,h:r.height,cx:r.x+r.width/2}};
|
||
const xs=Array.from(document.querySelectorAll('[data-two-factor-digit]'),box),label=document.querySelector('.two-factor-label'),help=document.querySelector('.two-factor-help');
|
||
return{xs,label:box(label),labelAlign:getComputedStyle(label).textAlign,buttons:Array.from(document.querySelector('.two-factor-actions').children,box),helpAlign:getComputedStyle(help).textAlign,overflow:document.documentElement.scrollWidth>innerWidth,card:box(document.querySelector('.two-factor-card'))};
|
||
});
|
||
assert.equal(layout.overflow,false);assert.equal(layout.labelAlign,'center');assert.equal(layout.helpAlign,'center');
|
||
assert(layout.xs.every(x=>Math.abs(x.y-layout.xs[0].y)<.1&&Math.abs(x.w-layout.xs[0].w)<.1&&Math.abs(x.w-x.h)<.1));
|
||
const rowCenter=(layout.xs[0].x+layout.xs[5].x+layout.xs[5].w)/2;
|
||
assert(Math.abs(rowCenter-layout.label.cx)<1);assert(Math.abs((layout.buttons[0].x+layout.buttons[1].x+layout.buttons[1].w)/2-rowCenter)<1);
|
||
assert(Math.abs(layout.buttons[0].y-layout.buttons[1].y)<1);assert(layout.buttons[0].y-layout.xs[0].y-layout.xs[0].h>=20);
|
||
for(let i=0;i<6;i++){
|
||
assert.equal(await digits.nth(i).getAttribute('inputmode'),'numeric');assert.equal(await digits.nth(i).getAttribute('maxlength'),'1');
|
||
assert.equal(await digits.nth(i).getAttribute('aria-label'),`Digit ${i+1} of 6`);assert.equal(await digits.nth(i).getAttribute('autocomplete'),'one-time-code');
|
||
}
|
||
if(output)await page.screenshot({path:`${output}/centred-${width}.png`,fullPage:true});
|
||
for(const [text,expected] of [['123456','123456'],[' \t123456\r\n','123456'],['1 2 3 4 5 6','123456'],['012345','012345'],['\u00a0012\u202f345\u00a0','012345']]){
|
||
await fresh();await clipboard(text);await rejected();assertSubmission(expected);assert(pastes.some(x=>x.trusted));
|
||
results.push({width,case:'real-clipboard',synthetic_value:JSON.stringify(text),submitted:expected,posts:posts.length,all_six_populated_before_submit:true,trusted_paste:true});
|
||
}
|
||
for(const text of ['12345','1234567','abc123456','12-34-56','123\n456','123456']){
|
||
await fresh();await clipboard(text);await page.waitForTimeout(70);assert.equal(posts.length,0);assert.equal(submits.length,0);
|
||
assert.deepEqual(await digits.evaluateAll(xs=>xs.map(x=>x.value)),['','','','','','']);
|
||
}
|
||
// Browser-native insertion exposes the full value BEFORE maxlength truncation.
|
||
await fresh();await digits.first().focus();await page.keyboard.insertText('012345');await rejected();assertSubmission('012345');
|
||
assert(insertions.some(x=>x.trusted&&x.data==='012345'));results.push({width,case:'trusted-native-insertion',submitted:'012345',posts:posts.length});
|
||
// A real rejected response renders a new attempt; no cancellation/mock response.
|
||
submits=[];posts=[];pastes=[];await clipboard('654321');await rejected();assertSubmission('654321');
|
||
await fresh();for(let i=0;i<6;i++)await digits.nth(i).press(String(i+1));await rejected();assertSubmission('123456');
|
||
await fresh();await digits.first().press('1');await digits.nth(1).press('2');await digits.nth(2).press('3');await digits.nth(3).press('Backspace');
|
||
assert(await digits.nth(2).evaluate(x=>x===document.activeElement));assert.equal(await page.locator('[data-two-factor-code]').inputValue(),'12');
|
||
await digits.nth(2).press('4');await digits.first().focus();await digits.first().press('0');assert.equal(await page.locator('[data-two-factor-code]').inputValue(),'024');
|
||
await digits.first().press('Tab');assert(await digits.nth(1).evaluate(x=>x===document.activeElement));await digits.nth(1).press('Shift+Tab');assert(await digits.first().evaluate(x=>x===document.activeElement));
|
||
await digits.first().press('ArrowRight');await digits.first().press('ArrowLeft');assert.equal(await page.locator('[data-two-factor-code]').inputValue(),'024');
|
||
await page.getByRole('button',{name:'Continue',exact:true}).click();await rejected();assert.deepEqual(posts,['024']);
|
||
// A cancelled client validation must not permanently latch automatic submission.
|
||
await fresh();await page.evaluate(()=>document.querySelector('[data-two-factor-form]').addEventListener('submit',e=>e.preventDefault(),{once:true}));
|
||
await clipboard('123456');await page.waitForTimeout(50);assert.equal(posts.length,0);assert.equal(submits.length,1);
|
||
submits=[];await digits.first().focus();await digits.first().press('9');await rejected();assertSubmission('923456');
|
||
await fresh();await page.getByRole('button',{name:'Continue',exact:true}).click();await page.getByText('Enter the code from your authenticator app.',{exact:true}).waitFor();assert.deepEqual(posts,['']);
|
||
await page.goto(base+'/fixture/setup');assert.equal(await page.getByLabel('Verification code').count(),1);assert.equal(await page.locator('[data-two-factor-digit]').count(),0);
|
||
assert((await page.getByLabel('Verification code').boundingBox()).width>100);
|
||
await context.close();
|
||
}
|
||
if(output)fs.writeFileSync(`${output}/genuine-clipboard-results.json`,JSON.stringify({status:'passed',physical_android_device_tested:false,results},null,2));
|
||
console.log('PASS actual Razor/HTTP rejection: genuine Ctrl+V, whitespace/grouping/leading zero, malformed rejection, trusted native insertion, exactly one POST, typing/backspace/correction/navigation/manual submit, cancelled-submit retry, rejected-code retry; centred desktop/390/320 layout and separate setup field.');
|
||
}finally{await browser.close();}})().catch(e=>{console.error(e);process.exit(1)});
|