Files
ipal-kit/reference/e2e/unused-media.spec.ts
T

47 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { expect, test } from '@playwright/test'
import { editorTestAccount, totp } from './totp'
/**
* `iapl({ unusedMedia: true })` (1.18.0; docs/media.md): above the Media list, how many files nothing uses, with a
* link to Payload's own list filtered to them. An image uploaded here and used nowhere must be listed; it is deleted
* by this test afterwards, never by the panel. Signs in as the test editor (the admins' 2FA attempts stay for the
* other specs).
*/
test('Media: the unused-files notice lists a file nothing uses and links to the filtered list', async ({ baseURL, page }) => {
const { E2E_ADMIN_EMAIL: email, E2E_ADMIN_PASSWORD: password, E2E_TOTP_SECRET: secret } = process.env
test.skip(!email || !password || !secret, 'E2E_* test admin not configured')
await page.goto('/admin/login')
await page.fill('#field-email', editorTestAccount(email!))
await page.fill('#field-password', password!)
await page.click('.form-submit button')
await page.waitForURL(/verify-totp|\/admin(\?|$)/, { waitUntil: 'commit' })
if (page.url().includes('verify-totp')) {
await page.getByRole('textbox').first().click()
await page.keyboard.type(totp(secret!))
}
await expect(page).toHaveTitle(/Dashboard|Pulpit/, { timeout: 20_000 })
// a 1×1 PNG nobody points at
const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64')
// the session cookie counts only with the site's own Origin (Payload's CSRF rule)
const origin = { origin: new URL(baseURL!).origin }
const created = await page.request.post('/api/media', {
headers: origin,
multipart: { _payload: JSON.stringify({ alt: 'unused-media e2e' }), file: { buffer: png, mimeType: 'image/png', name: 'unused-media-e2e.png' } },
})
expect(created.status(), await created.text()).toBe(201)
const id = ((await created.json()) as { doc: { id: string } }).doc.id
try {
await page.goto('/admin/collections/media')
const notice = page.locator('[data-iapl="unused-media"]')
await expect(notice).toBeVisible()
const href = await notice.locator('a').getAttribute('href')
expect(href).toContain(`=${id}`)
await notice.locator('a').click()
await expect(page.getByText('unused-media-e2e', { exact: false }).first()).toBeVisible()
} finally {
await page.request.delete(`/api/media/${id}`, { headers: origin })
}
})