On by default with whiteLabel. The login screen and the 2FA screens get the look through AdminLogo and SafeTotpView, which every site already has in its import map: an upgrade needs no edit. allScreens adds one provider (SignInLook) for forgot / reset password, first user and logout; a production start leaves it out when the import map on disk lacks it. Logo and name come from Site settings: no new field, no migration. Language links through GET /api/ipal/admin-language (Payload's own cookie, back to an admin path only). Options: accent, logoMadeFor, languages, svgLogo; whiteLabel.brandAccess 'admin'. The 2FA code boxes get names for screen readers. Reference site and starter: allScreens on. Docs, agent guide, skills, tests. Co-Authored-By: Claude Fable 5.2 <[email protected]>
147 lines
8.2 KiB
TypeScript
147 lines
8.2 KiB
TypeScript
import { expect, type Page, test } from '@playwright/test'
|
|
|
|
/**
|
|
* The sign-in look (whiteLabel.signIn, white-label.md): the dark panel with the site's name or logo beside the
|
|
* form, on every sign-in screen (this site sets `allScreens`), in the page's first HTML, on a phone above the
|
|
* form, with working language links; and the 2FA code screen behind it, unchanged in what it does.
|
|
* No 2FA code is entered here: the account's verify attempts are left for the other specs.
|
|
* E2E_BASE_URL=http://localhost:3310 pnpm exec playwright test e2e/sign-in-look.spec.ts
|
|
*/
|
|
const { E2E_ADMIN_EMAIL: email, E2E_ADMIN_PASSWORD: password } = process.env
|
|
|
|
const PHONE = { height: 844, width: 390 }
|
|
const panel = (page: Page) => page.locator('.iapl-signin')
|
|
const box = async (page: Page, selector: string) => {
|
|
const b = await page.locator(selector).first().boundingBox()
|
|
if (!b) {throw new Error(`${selector} is not on the page`)}
|
|
return b
|
|
}
|
|
/** Nothing is wider than the window: no sideways scroll. */
|
|
const noSidewaysScroll = async (page: Page) => expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth)).toBe(true)
|
|
|
|
test('sign-in look — login: the look comes with the form (no flash of the plain screen), one panel, the form beside it', async ({ page, request }) => {
|
|
// the stylesheet and the panel are in the page's own response: no second request brings the look
|
|
const html = await (await request.get('/admin/login')).text()
|
|
expect(html).toContain('iapl-sign-in')
|
|
expect(html).toContain('iapl-signin__label')
|
|
|
|
const errors: string[] = []
|
|
page.on('console', (m) => m.type() === 'error' && errors.push(m.text()))
|
|
// Payload draws its screens in the browser. Whenever the form's template is in the page, the panel and its
|
|
// stylesheet must be there too — checked after every change of the page, before the browser paints it.
|
|
await page.addInitScript(() => {
|
|
const seen = { plain: 0, styled: 0 }
|
|
;(window as unknown as { iaplLook: typeof seen }).iaplLook = seen
|
|
new MutationObserver(() => {
|
|
if (!document.querySelector('.template-minimal')) {return}
|
|
const styled = document.querySelector('.iapl-signin') && document.querySelector('style[data-href="iapl-sign-in"]')
|
|
seen[styled ? 'styled' : 'plain'] += 1
|
|
}).observe(document, { childList: true, subtree: true })
|
|
})
|
|
await page.goto('/admin/login')
|
|
await page.locator('#field-email').waitFor()
|
|
const seen = await page.evaluate(() => (window as unknown as { iaplLook: { plain: number; styled: number } }).iaplLook)
|
|
expect(seen.plain).toBe(0)
|
|
expect(seen.styled).toBeGreaterThan(0)
|
|
await expect(panel(page)).toHaveCount(1)
|
|
await expect(panel(page)).toBeVisible()
|
|
await expect(panel(page).locator('.iapl-signin__label')).toHaveText(/Administration panel|Panel administracyjny/)
|
|
// the site's name (no logo in the seed) is a link to the site; nothing else on the panel leaves the admin
|
|
await expect(panel(page).locator('.iapl-signin__brand')).toHaveText('Example Studio')
|
|
await expect(panel(page).locator('.iapl-signin__brand')).toHaveAttribute('href', '/')
|
|
const p = await box(page, '.iapl-signin')
|
|
const form = await box(page, '#field-email')
|
|
expect(p.x + p.width).toBeLessThanOrEqual(form.x)
|
|
expect(p.height).toBeGreaterThan(600)
|
|
await noSidewaysScroll(page)
|
|
// the button carries the accent, the fields their labels
|
|
await expect(page.locator('.form-submit button')).toHaveCSS('background-color', 'rgb(0, 112, 192)')
|
|
await expect(page.getByLabel(/^(Email|E-mail)/)).toBeVisible()
|
|
expect(errors).toEqual([])
|
|
})
|
|
|
|
test('sign-in look — login on a phone: the panel is a band above the form, in both themes', async ({ baseURL, browser }) => {
|
|
for (const theme of ['light', 'dark'] as const) {
|
|
const context = await browser.newContext({ baseURL, viewport: PHONE })
|
|
await context.addCookies([{ name: 'payload-theme', url: baseURL!, value: theme }])
|
|
const page = await context.newPage()
|
|
await page.goto('/admin/login')
|
|
const p = await box(page, '.iapl-signin')
|
|
const form = await box(page, '#field-email')
|
|
expect(p.y).toBe(0)
|
|
expect(Math.round(p.width)).toBe(PHONE.width)
|
|
expect(p.y + p.height).toBeLessThanOrEqual(form.y)
|
|
await noSidewaysScroll(page)
|
|
await expect(page.locator('html')).toHaveAttribute('data-theme', theme)
|
|
// the page behind the form follows the theme; the panel is dark in both
|
|
const bg = await page.locator('.template-minimal').evaluate((el) => getComputedStyle(el).backgroundColor)
|
|
expect(bg === 'rgb(255, 255, 255)').toBe(theme === 'light')
|
|
await expect(panel(page)).toHaveCSS('background-color', 'rgb(6, 6, 6)')
|
|
await context.close()
|
|
}
|
|
})
|
|
|
|
test('sign-in look — the other sign-in screens carry the same panel; the rest of the admin does not show it', async ({ page }) => {
|
|
for (const path of ['/admin/forgot', '/admin/reset/not-a-real-token', '/admin/logout']) {
|
|
await page.goto(path)
|
|
await page.waitForLoadState('networkidle')
|
|
await expect(panel(page), path).toHaveCount(1)
|
|
await expect(panel(page), path).toBeVisible()
|
|
await noSidewaysScroll(page)
|
|
}
|
|
await page.goto('/admin/forgot')
|
|
const p = await box(page, '.iapl-signin')
|
|
const form = await box(page, '#field-email')
|
|
expect(p.x + p.width).toBeLessThanOrEqual(form.x)
|
|
})
|
|
|
|
test('sign-in look — language links: one click changes the panel\'s language and comes back to the same screen', async ({ baseURL, browser, request }) => {
|
|
const context = await browser.newContext({ baseURL, locale: 'en-US' })
|
|
const page = await context.newPage()
|
|
await page.goto('/admin/forgot')
|
|
await expect(panel(page).locator('.iapl-signin__label')).toHaveText('Administration panel')
|
|
await expect(panel(page).locator('[aria-current="true"]')).toHaveText('English')
|
|
// a plain link: the endpoint sets the cookie and redirects, no script of the page takes part
|
|
await expect(panel(page).getByRole('link', { name: 'Polski' })).toHaveAttribute('href', '/api/ipal/admin-language?lng=pl')
|
|
await panel(page).getByRole('link', { name: 'Polski' }).click()
|
|
await expect(page).toHaveURL(/\/admin\/forgot$/)
|
|
await expect(panel(page).locator('.iapl-signin__label')).toHaveText('Panel administracyjny')
|
|
await expect(panel(page).locator('[aria-current="true"]')).toHaveText('Polski')
|
|
await expect(page.locator('html')).toHaveAttribute('lang', 'pl')
|
|
await context.close()
|
|
|
|
// the endpoint writes a supported language only, and never sends the browser off the admin
|
|
const unknown = await request.get('/api/ipal/admin-language?lng=xx', { maxRedirects: 0 })
|
|
expect(unknown.status()).toBe(400)
|
|
expect(unknown.headers()['set-cookie']).toBeUndefined()
|
|
const away = await request.get('/api/ipal/admin-language?lng=pl', { headers: { referer: 'https://evil.example/phish' }, maxRedirects: 0 })
|
|
expect(away.status()).toBe(303)
|
|
expect(away.headers().location).toBe('/admin')
|
|
expect(away.headers()['set-cookie']).toMatch(/^payload-lng=pl; Path=\/; Max-Age=31536000; SameSite=Lax/)
|
|
})
|
|
|
|
test('sign-in look — the 2FA code screen: the panel beside it, the code boxes named, the brand without a media request', async ({ page }) => {
|
|
test.skip(!email || !password, 'E2E_* test admin not configured')
|
|
const media: string[] = []
|
|
page.on('request', (r) => r.url().includes('/api/media/') && media.push(r.url()))
|
|
await page.goto('/admin/login')
|
|
await page.fill('#field-email', email!)
|
|
await page.fill('#field-password', password!)
|
|
await page.click('.form-submit button')
|
|
await page.waitForURL(/verify-totp/)
|
|
await expect(panel(page)).toHaveCount(1)
|
|
await expect(panel(page)).toBeVisible()
|
|
const p = await box(page, '.iapl-signin')
|
|
const first = await box(page, '.template-minimal form input[type="text"]')
|
|
expect(p.x + p.width).toBeLessThanOrEqual(first.x)
|
|
// six boxes, each with a name a screen reader can say, the first one focused as before
|
|
const boxes = page.getByRole('textbox')
|
|
await expect(boxes).toHaveCount(6)
|
|
await expect(boxes.first()).toHaveAttribute('aria-label', /^(Digit 1 of 6|Cyfra 1 z 6)$/)
|
|
await expect(boxes.nth(5)).toHaveAttribute('aria-label', /^(Digit 6 of 6|Cyfra 6 z 6)$/)
|
|
await expect(boxes.first()).toBeFocused()
|
|
// a session that owes its code is refused `media`: the page asks for none
|
|
expect(media).toEqual([])
|
|
await noSidewaysScroll(page)
|
|
})
|