onboarding.mjs 1.4 KB

123456789101112131415161718192021
  1. /** Enter through the native local-admin flow, then set isolated fixture credentials. */
  2. export async function enterLocal(page, { username = 'desktop-fixture', password = 'desktop-fixture-password' } = {}) {
  3. await page.waitForURL(url => url.protocol === 'http:' || url.protocol === 'https:', { timeout: 90000 })
  4. // Rotate fixture credentials outside the SPA: its in-flight requests still
  5. // carry the previous session and can otherwise trigger native login recovery.
  6. const origin = new URL(page.url()).origin
  7. const fixtureURL = `${origin}/__desktop_fixture_credentials__`
  8. await page.route(fixtureURL, route => route.fulfill({ contentType: 'text/html', body: '<title>Fixture account setup</title>' }))
  9. try {
  10. await page.goto(fixtureURL)
  11. await page.evaluate(async ({username,password}) => {
  12. const bootstrap = await (await fetch('/api/app/bootstrap')).json()
  13. if (!bootstrap.user?.localDesktop) throw new Error('Native administrator session was not established')
  14. const response = await fetch('/api/app/account/credentials', { method: 'PUT',
  15. headers: {'Content-Type':'application/json','X-CSRF-Token':bootstrap.csrfToken},
  16. body: JSON.stringify({username,currentPassword:'',newPassword:password}) })
  17. if (!response.ok) throw new Error('Fixture account configuration failed')
  18. }, {username,password})
  19. } finally { await page.unroute(fixtureURL) }
  20. await page.goto(`${origin}/conversations`)
  21. }