Files
MikrotikManager/tests/e2e/auth-return-path.spec.ts
T

29 lines
2.2 KiB
TypeScript

import {test,expect} from '@playwright/test'
const token=(permissions:string[])=>[Buffer.from('{}').toString('base64url'),Buffer.from(JSON.stringify({sub:'own199',name:'Own199',email:'[email protected]',apps:['mm'],permissions,exp:Math.floor(Date.now()/1000)+600})).toString('base64url'),'fixture'].join('.')
for(const width of [1440,320]){
for(const [name,next,permissions,expected]of [
['query','/servers?server=42&tab=details',['mm:servers:read'],'/servers?server=42&tab=details'],
['denied','/settings',['mm:servers:read'],'/servers'],
['external','//outside.invalid',['mm:servers:read'],'/servers'],
['loop','/auth/callback?next=/servers',['mm:servers:read'],'/servers'],
] as const)test(`SSO return ${name} ${width}`,async({page})=>{
await page.setViewportSize({width,height:1000})
await page.route('**/api/**',r=>{const path=new URL(r.request().url()).pathname;return r.fulfill({json:path==='/api/auth/config'?{required:true,portal_url:'https://portal.example.test'}:path==='/api/servers'?[]:path==='/api/v1/app-switcher'?{menuLabel:'Приложения',apps:[]}:{}})})
await page.goto('/auth/callback?next='+encodeURIComponent(next)+'#access_token='+token([...permissions]))
await expect.poll(()=>new URL(page.url()).pathname+new URL(page.url()).search).toBe(expected)
expect(new URL(page.url()).hash).toBe('')
await expect(page.getByRole('button',{name:'Добавить сервер',exact:true})).toBeVisible()
})
test(`SSO handoff captures query ${width}`,async({page})=>{
await page.setViewportSize({width,height:1000})
await page.route('**/api/auth/config',r=>r.fulfill({json:{required:true,portal_url:'https://portal.example.test/'}}))
await page.route('https://portal.example.test/**',r=>r.fulfill({contentType:'text/html',body:'<p>Own Portal fixture</p>'}))
await page.goto('/servers?server=42&tab=details')
await expect.poll(()=>new URL(page.url()).hostname).toBe('portal.example.test')
const callback=new URL(new URL(page.url()).searchParams.get('return_to')!)
expect(callback.origin).toBe('http://127.0.0.1:3100')
expect(callback.pathname).toBe('/auth/callback')
expect(callback.searchParams.get('next')).toBe('/servers?server=42&tab=details')
})
}