Files
payloadcms/test/access-control/e2e.spec.ts
Said Akhrarov 1d6ffcb80e feat(ui): adds support for copy pasting complex fields (#11513)
<!--

Thank you for the PR! Please go through the checklist below and make
sure you've completed all the steps.

Please review the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository if you haven't already.

The following items will ensure that your PR is handled as smoothly as
possible:

- PR Title must follow conventional commits format. For example, `feat:
my new feature`, `fix(plugin-seo): my fix`.
- Minimal description explained as if explained to someone not
immediately familiar with the code.
- Provide before/after screenshots or code diffs if applicable.
- Link any related issues/discussions from GitHub or Discord.
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Fixes #

-->
### What?
This PR introduces support for copy + pasting complex fields such as
Arrays and Blocks. These changes introduce a new `ClipboardAction`
component that houses logic for copy + pasting to and from the clipboard
to supported fields. I've scoped this PR to include only Blocks &
Arrays, however the structure of the components introduced lend
themselves to be easily extended to other field types. I've limited the
scope because there may be design & functional blockers that make it
unclear how to add actions to particular fields.

Supported fields:
- Arrays
([Demo](https://github.com/user-attachments/assets/523916f6-77d0-43e2-9a11-a6a9d8c1b71c))
- Array Rows
([Demo](https://github.com/user-attachments/assets/0cd01a1f-3e5e-4fea-ac83-8c0bba8d1aac))
- Blocks
([Demo](https://github.com/user-attachments/assets/4c55ac2b-55f4-4793-9b53-309b2e090dd9))
- Block Rows
([Demo](https://github.com/user-attachments/assets/1b4d2bea-981a-485b-a6c4-c59a77a50567))

Fields that may be supported in the future with minimal effort by
adopting the changes introduced here:
- Tabs
- Groups
- Collapsible
- Relationships

This PR also encompasses e2e tests that check both field and row-level
copy/pasting.

### Why?
To make it simpler and faster to copy complex fields over between
documents and rows within those docs.

### How?
Introduces a new `ClipboardAction` component with helper utilities to
aid in copy/pasting and validating field data.

Addresses #2977 & #10703

Notes:
- There seems to be an issue with Blocks & Arrays that contain RichText
fields where the RichText field dissappears from the dom upon replacing
form state. These fields are resurfaced after either saving the data or
dragging/dropping the row containing them.
- Copying a Row and then pasting it at the field-level will overwrite
the field to include only that one row. This is intended however can be
changed if requested.
- Clipboard permissions are required to use this feature. [See Clipboard
API caniuse](https://caniuse.com/async-clipboard).

#### TODO
- [x] ~~I forgot BlockReferences~~
- [x] ~~Fix tests failing due to new buttons causing locator conflicts~~
- [x] ~~Ensure deeply nested structures work~~
- [x] ~~Add missing translations~~
- [x] ~~Implement local storage instead of clipboard api~~
- [x] ~~Improve tests~~

---------

Co-authored-by: Germán Jabloñski <43938777+GermanJablo@users.noreply.github.com>
2025-07-09 13:59:22 +00:00

779 lines
27 KiB
TypeScript

import type { BrowserContext, Page } from '@playwright/test'
import type { TypeWithID } from 'payload'
import { expect, test } from '@playwright/test'
import { devUser } from 'credentials.js'
import { openDocControls } from 'helpers/e2e/openDocControls.js'
import { openNav } from 'helpers/e2e/toggleNav.js'
import path from 'path'
import { email, wait } from 'payload/shared'
import { fileURLToPath } from 'url'
import type { PayloadTestSDK } from '../helpers/sdk/index.js'
import type { Config, ReadOnlyCollection, RestrictedVersion } from './payload-types.js'
import {
closeNav,
ensureCompilationIsDone,
exactText,
getRoutes,
initPageConsoleErrorCatch,
login,
saveDocAndAssert,
} from '../helpers.js'
import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js'
import { POLL_TOPASS_TIMEOUT, TEST_TIMEOUT_LONG } from '../playwright.config.js'
import {
authSlug,
createNotUpdateCollectionSlug,
disabledSlug,
docLevelAccessSlug,
fullyRestrictedSlug,
nonAdminEmail,
publicUserEmail,
publicUsersSlug,
readNotUpdateGlobalSlug,
readOnlyGlobalSlug,
readOnlySlug,
restrictedVersionsAdminPanelSlug,
restrictedVersionsSlug,
slug,
unrestrictedSlug,
userRestrictedCollectionSlug,
userRestrictedGlobalSlug,
} from './shared.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
/**
* TODO: Access Control
*
* FSK: 'should properly prevent / allow public users from reading a restricted field'
*
* Repeat all above for globals
*/
const { beforeAll, beforeEach, describe } = test
let payload: PayloadTestSDK<Config>
describe('Access Control', () => {
let page: Page
let url: AdminUrlUtil
let restrictedUrl: AdminUrlUtil
let unrestrictedURL: AdminUrlUtil
let readOnlyCollectionUrl: AdminUrlUtil
let richTextUrl: AdminUrlUtil
let readOnlyGlobalUrl: AdminUrlUtil
let restrictedVersionsUrl: AdminUrlUtil
let restrictedVersionsAdminPanelUrl: AdminUrlUtil
let userRestrictedCollectionURL: AdminUrlUtil
let userRestrictedGlobalURL: AdminUrlUtil
let disabledFields: AdminUrlUtil
let serverURL: string
let context: BrowserContext
let logoutURL: string
let authFields: AdminUrlUtil
beforeAll(async ({ browser }, testInfo) => {
testInfo.setTimeout(TEST_TIMEOUT_LONG)
;({ payload, serverURL } = await initPayloadE2ENoConfig<Config>({ dirname }))
url = new AdminUrlUtil(serverURL, slug)
restrictedUrl = new AdminUrlUtil(serverURL, fullyRestrictedSlug)
richTextUrl = new AdminUrlUtil(serverURL, 'rich-text')
unrestrictedURL = new AdminUrlUtil(serverURL, unrestrictedSlug)
readOnlyCollectionUrl = new AdminUrlUtil(serverURL, readOnlySlug)
readOnlyGlobalUrl = new AdminUrlUtil(serverURL, readOnlySlug)
restrictedVersionsUrl = new AdminUrlUtil(serverURL, restrictedVersionsSlug)
restrictedVersionsAdminPanelUrl = new AdminUrlUtil(serverURL, restrictedVersionsAdminPanelSlug)
userRestrictedCollectionURL = new AdminUrlUtil(serverURL, userRestrictedCollectionSlug)
userRestrictedGlobalURL = new AdminUrlUtil(serverURL, userRestrictedGlobalSlug)
disabledFields = new AdminUrlUtil(serverURL, disabledSlug)
authFields = new AdminUrlUtil(serverURL, authSlug)
context = await browser.newContext()
page = await context.newPage()
initPageConsoleErrorCatch(page)
await ensureCompilationIsDone({ page, serverURL, noAutoLogin: true })
await login({ page, serverURL })
await ensureCompilationIsDone({ page, serverURL })
const {
admin: {
routes: { logout: logoutRoute },
},
routes: { admin: adminRoute },
} = getRoutes({})
logoutURL = `${serverURL}${adminRoute}${logoutRoute}`
})
describe('fields', () => {
test('field without read access should not show', async () => {
const { id } = await createDoc({ restrictedField: 'restricted' })
await page.goto(url.edit(id))
await expect(page.locator('#field-restrictedField')).toHaveCount(0)
})
test('field without read access inside a group should not show', async () => {
const { id } = await createDoc({ restrictedField: 'restricted' })
await page.goto(url.edit(id))
await expect(page.locator('#field-group__restrictedGroupText')).toHaveCount(0)
})
test('field without read access inside a collapsible should not show', async () => {
const { id } = await createDoc({ restrictedField: 'restricted' })
await page.goto(url.edit(id))
await expect(page.locator('#field-restrictedRowText')).toHaveCount(0)
})
test('field without read access inside a row should not show', async () => {
const { id } = await createDoc({ restrictedField: 'restricted' })
await page.goto(url.edit(id))
await expect(page.locator('#field-restrictedCollapsibleText')).toHaveCount(0)
})
test('should not show field without permission', async () => {
await page.goto(url.account)
await expect(page.locator('#field-roles')).toBeHidden()
})
})
describe('rich text', () => {
test('rich text within block should render as editable', async () => {
await page.goto(richTextUrl.create)
await page.locator('.blocks-field__drawer-toggler').click()
await page.locator('.thumbnail-card').click()
const richTextField = page.locator('.rich-text-lexical')
const contentEditable = richTextField.locator('.ContentEditable__root').first()
await expect(contentEditable).toBeVisible()
await contentEditable.click()
const typedText = 'Hello, this field is editable!'
await page.keyboard.type(typedText)
await expect(
page.locator('[data-lexical-text="true"]', {
hasText: exactText(typedText),
}),
).toHaveCount(1)
})
const ensureRegression1FieldsHaveCorrectAccess = async () => {
await expect(
page.locator('#field-group1 .rich-text-lexical .ContentEditable__root'),
).toBeVisible()
// Wait until the contenteditable is editable
await expect(
page.locator('#field-group1 .rich-text-lexical .ContentEditable__root'),
).toBeEditable()
await expect(async () => {
const isAttached = page.locator('#field-group1 .rich-text-lexical--read-only')
await expect(isAttached).toBeHidden()
}).toPass({ timeout: 10000, intervals: [100] })
await expect(page.locator('#field-group1 #field-group1__text')).toBeEnabled()
// Click on button with text Tab1
await page.locator('.tabs-field__tab-button').getByText('Tab1').click()
await expect(
page.locator('.tabs-field__tab .rich-text-lexical .ContentEditable__root').first(),
).toBeVisible()
await expect(
page.locator('.tabs-field__tab .rich-text-lexical--read-only').first(),
).not.toBeAttached()
await expect(
page.locator(
'.tabs-field__tab #field-tab1__blocks2 .rich-text-lexical .ContentEditable__root',
),
).toBeVisible()
await expect(
page.locator('.tabs-field__tab #field-tab1__blocks2 .rich-text-lexical--read-only'),
).not.toBeAttached()
await expect(
page.locator('#field-array #array-row-0 .rich-text-lexical .ContentEditable__root'),
).toBeVisible()
await expect(
page.locator('#field-array #array-row-0 .rich-text-lexical--read-only'),
).not.toBeAttached()
await expect(
page.locator(
'#field-arrayWithAccessFalse #arrayWithAccessFalse-row-0 .rich-text-lexical .ContentEditable__root',
),
).toBeVisible()
await expect(
page.locator(
'#field-arrayWithAccessFalse #arrayWithAccessFalse-row-0 .rich-text-lexical--read-only',
),
).toBeVisible()
await expect(
page.locator('#field-blocks .rich-text-lexical .ContentEditable__root'),
).toBeVisible()
await expect(page.locator('#field-blocks.rich-text-lexical--read-only')).not.toBeAttached()
}
/**
* This reproduces a bug where certain fields were incorrectly marked as read-only
*/
test('ensure complex collection config fields show up in correct read-only state', async () => {
const regression1URL = new AdminUrlUtil(serverURL, 'regression1')
await page.goto(regression1URL.list)
await page.locator('.cell-id a').first().click()
await page.waitForURL(`**/collections/regression1/**`)
await ensureRegression1FieldsHaveCorrectAccess()
// Edit any field
await page.locator('#field-group1__text').fill('test!')
await saveDocAndAssert(page)
await wait(1000)
// Ensure fields still have the correct readOnly state. When saving the document, permissions are re-evaluated
await ensureRegression1FieldsHaveCorrectAccess()
})
const ensureRegression2FieldsHaveCorrectAccess = async () => {
await expect(
page.locator('#field-group .rich-text-lexical .ContentEditable__root'),
).toBeVisible()
// Wait until the contenteditable is editable
await expect(
page.locator('#field-group .rich-text-lexical .ContentEditable__root'),
).toBeEditable()
await expect(async () => {
const isAttached = page.locator('#field-group .rich-text-lexical--read-only')
await expect(isAttached).toBeHidden()
}).toPass({ timeout: 10000, intervals: [100] })
await expect(page.locator('#field-group #field-group__text')).toBeEnabled()
await expect(
page.locator('#field-array #array-row-0 .rich-text-lexical .ContentEditable__root'),
).toBeVisible()
await expect(
page.locator('#field-array #array-row-0 .rich-text-lexical--read-only'),
).toBeVisible() // => is read-only
}
/**
* This reproduces a bug where certain fields were incorrectly marked as read-only
*/
test('ensure complex collection config fields show up in correct read-only state 2', async () => {
const regression2URL = new AdminUrlUtil(serverURL, 'regression2')
await page.goto(regression2URL.list)
await page.locator('.cell-id a').first().click()
await page.waitForURL(`**/collections/regression2/**`)
await ensureRegression2FieldsHaveCorrectAccess()
// Edit any field
await page.locator('#field-group__text').fill('test!')
await saveDocAndAssert(page)
await wait(1000)
// Ensure fields still have the correct readOnly state. When saving the document, permissions are re-evaluated
await ensureRegression2FieldsHaveCorrectAccess()
})
})
describe('collection — fully restricted', () => {
let existingDoc: ReadOnlyCollection
beforeAll(async () => {
existingDoc = await payload.create({
collection: fullyRestrictedSlug,
data: {
name: 'name',
},
})
})
test('should not show in card list', async () => {
await page.goto(url.admin)
await expect(page.locator(`#card-${fullyRestrictedSlug}`)).toHaveCount(0)
})
test('should not show in nav', async () => {
await page.goto(url.admin)
await openNav(page)
await expect(
page.locator('.nav a', {
hasText: exactText('Restricteds'),
}),
).toHaveCount(0)
})
test('should not have list url', async () => {
const errors = []
page.on('console', (exception) => {
errors.push(exception)
})
await page.goto(restrictedUrl.list)
// eslint-disable-next-line payload/no-flaky-assertions
expect(errors).not.toHaveLength(0)
})
test('should not have create url', async () => {
await page.goto(restrictedUrl.create)
await expect(page.locator('.not-found')).toBeVisible()
})
test('should not have access to existing doc', async () => {
await page.goto(restrictedUrl.edit(existingDoc.id))
await expect(page.locator('.not-found')).toBeVisible()
})
})
describe('collection — read-only', () => {
let existingDoc: ReadOnlyCollection
beforeAll(async () => {
existingDoc = await payload.create({
collection: readOnlySlug,
data: {
name: 'name',
},
})
})
test('should show in card list', async () => {
await page.goto(url.admin)
await expect(page.locator(`#card-${readOnlySlug}`)).toHaveCount(1)
})
test('should show in nav', async () => {
await page.goto(url.admin)
await expect(page.locator(`.nav a[href="/admin/collections/${readOnlySlug}"]`)).toHaveCount(1)
})
test('should have collection url', async () => {
await page.goto(readOnlyCollectionUrl.list)
await expect(page).toHaveURL(new RegExp(`${readOnlyCollectionUrl.list}.*`)) // will redirect to ?limit=10 at the end, so we have to use a wildcard at the end
})
test('should not have "Create New" button', async () => {
await page.goto(readOnlyCollectionUrl.create)
await expect(page.locator('.collection-list__header a')).toHaveCount(0)
})
test('should not have quick create button', async () => {
await page.goto(url.admin)
await expect(page.locator(`#card-${readOnlySlug}`)).not.toHaveClass('card__actions')
})
test('should not display actions on edit view', async () => {
await page.goto(readOnlyCollectionUrl.edit(existingDoc.id))
await expect(page.locator('.collection-edit__collection-actions li')).toHaveCount(0)
})
test('fields should be read-only', async () => {
await page.goto(readOnlyCollectionUrl.edit(existingDoc.id))
await expect(page.locator('#field-name')).toBeDisabled()
await page.goto(readOnlyGlobalUrl.global(readOnlyGlobalSlug))
await expect(page.locator('#field-name')).toBeDisabled()
})
test('should not render dot menu popup when `create` and `delete` access control is set to false', async () => {
await page.goto(readOnlyCollectionUrl.edit(existingDoc.id))
await expect(page.locator('.collection-edit .doc-controls .doc-controls__popup')).toBeHidden()
})
})
describe('collection — create but not edit', () => {
test('should not show edit button', async () => {
const createNotUpdateURL = new AdminUrlUtil(serverURL, createNotUpdateCollectionSlug)
await page.goto(createNotUpdateURL.create)
await expect(page.locator('#field-name')).toBeVisible()
await page.locator('#field-name').fill('name')
await expect(page.locator('#field-name')).toHaveValue('name')
await expect(page.locator('#action-save')).toBeVisible()
await page.locator('#action-save').click()
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
await expect(page.locator('#action-save')).toBeHidden()
await expect(page.locator('#field-name')).toBeDisabled()
})
test('should maintain access control in document drawer', async () => {
const unrestrictedDoc = await payload.create({
collection: unrestrictedSlug,
data: {
name: 'unrestricted-123',
},
})
await page.goto(unrestrictedURL.edit(unrestrictedDoc.id.toString()))
const addDocButton = page.locator(
'#createNotUpdateDocs-add-new button.relationship-add-new__add-button.doc-drawer__toggler',
)
await expect(addDocButton).toBeVisible()
await addDocButton.click()
const documentDrawer = page.locator(`[id^=doc-drawer_${createNotUpdateCollectionSlug}_1_]`)
await expect(documentDrawer).toBeVisible()
await expect(documentDrawer.locator('#action-save')).toBeVisible()
await documentDrawer.locator('#field-name').fill('name')
await expect(documentDrawer.locator('#field-name')).toHaveValue('name')
await saveDocAndAssert(
page,
`[id^=doc-drawer_${createNotUpdateCollectionSlug}_1_] #action-save`,
)
await expect(documentDrawer.locator('#action-save')).toBeHidden()
await expect(documentDrawer.locator('#field-name')).toBeDisabled()
})
})
describe('global — read but not update', () => {
test('should not show edit button', async () => {
const createNotUpdateURL = new AdminUrlUtil(serverURL, readNotUpdateGlobalSlug)
await page.goto(createNotUpdateURL.global(readNotUpdateGlobalSlug))
await expect(page.locator('#field-name')).toBeVisible()
await expect(page.locator('#field-name')).toBeDisabled()
await expect(page.locator('#action-save')).toBeHidden()
})
})
describe('dynamic update access', () => {
describe('collection', () => {
test('should restrict update access based on document field', async () => {
await page.goto(userRestrictedCollectionURL.create)
await expect(page.locator('#field-name')).toBeVisible()
await page.locator('#field-name').fill('anonymous@email.com')
await page.locator('#action-save').click()
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
await expect(page.locator('#field-name')).toBeDisabled()
await expect(page.locator('#action-save')).toBeHidden()
await page.goto(userRestrictedCollectionURL.create)
await expect(page.locator('#field-name')).toBeVisible()
await page.locator('#field-name').fill(devUser.email)
await page.locator('#action-save').click()
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
await expect(page.locator('#field-name')).toBeEnabled()
await expect(page.locator('#action-save')).toBeVisible()
})
test('maintain access control in document drawer', async () => {
const unrestrictedDoc = await payload.create({
collection: unrestrictedSlug,
data: {
name: 'unrestricted-123',
},
})
await page.goto(unrestrictedURL.edit(unrestrictedDoc.id.toString()))
const field = page.locator('#field-userRestrictedDocs')
await expect(field.locator('input')).toBeEnabled()
const addDocButton = page.locator(
'#userRestrictedDocs-add-new button.relationship-add-new__add-button.doc-drawer__toggler',
)
await addDocButton.click()
const documentDrawer = page.locator('[id^=doc-drawer_user-restricted-collection_1_]')
await expect(documentDrawer).toBeVisible()
await documentDrawer.locator('#field-name').fill('anonymous@email.com')
await wait(500)
await documentDrawer.locator('#action-save').click()
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
await expect(documentDrawer.locator('#field-name')).toBeDisabled()
await documentDrawer.locator('button.doc-drawer__header-close').click()
await expect(documentDrawer).toBeHidden()
await addDocButton.click()
const documentDrawer2 = page.locator('[id^=doc-drawer_user-restricted-collection_1_]')
await expect(documentDrawer2).toBeVisible()
await documentDrawer2.locator('#field-name').fill('dev@payloadcms.com')
await documentDrawer2.locator('#action-save').click()
await expect(page.locator('.payload-toast-container')).toContainText('successfully')
await expect(documentDrawer2.locator('#field-name')).toBeEnabled()
})
})
describe('global', () => {
test('should restrict update access based on document field', async () => {
await page.goto(userRestrictedGlobalURL.global(userRestrictedGlobalSlug))
await expect(page.locator('#field-name')).toBeVisible()
await expect(page.locator('#field-name')).toHaveValue(devUser.email)
await expect(page.locator('#field-name')).toBeEnabled()
await page.locator('#field-name').fill('anonymous@email.com')
await page.locator('#action-save').click()
await expect(page.locator('.payload-toast-container')).toContainText(
'You are not allowed to perform this action',
)
await payload.updateGlobal({
slug: userRestrictedGlobalSlug,
data: {
name: 'anonymous@payloadcms.com',
},
})
await page.goto(userRestrictedGlobalURL.global(userRestrictedGlobalSlug))
await expect(page.locator('#field-name')).toBeDisabled()
await expect(page.locator('#action-save')).toBeHidden()
})
test('should restrict access based on user settings', async () => {
const url = `${serverURL}/admin/globals/settings`
await page.goto(url)
await openNav(page)
await expect(page.locator('#nav-global-settings')).toBeVisible()
await expect(page.locator('#nav-global-test')).toBeHidden()
await closeNav(page)
await page.locator('.checkbox-input:has(#field-test) input').check()
await saveDocAndAssert(page)
await openNav(page)
const globalTest = page.locator('#nav-global-test')
await expect(async () => await globalTest.isVisible()).toPass({
timeout: POLL_TOPASS_TIMEOUT,
})
})
})
})
describe('collection — restricted versions', () => {
let existingDoc: RestrictedVersion
beforeAll(async () => {
existingDoc = await payload.create({
collection: restrictedVersionsAdminPanelSlug,
data: {
name: 'name',
},
})
await payload.update({
collection: restrictedVersionsAdminPanelSlug,
id: existingDoc.id,
data: {
hidden: true,
},
})
})
test('versions tab should not show', async () => {
await page.goto(restrictedVersionsAdminPanelUrl.edit(existingDoc.id))
await page.locator('.doc-tabs__tabs').getByLabel('Versions').click()
const rows = page.locator('.versions table tbody tr')
await expect(rows).toHaveCount(1)
})
})
describe('doc level access', () => {
let existingDoc: ReadOnlyCollection
let docLevelAccessURL: AdminUrlUtil
beforeAll(async () => {
docLevelAccessURL = new AdminUrlUtil(serverURL, docLevelAccessSlug)
existingDoc = await payload.create({
collection: docLevelAccessSlug,
data: {
approvedForRemoval: false,
approvedTitle: 'Title',
lockTitle: true,
},
})
})
test('should disable field based on document data', async () => {
await page.goto(docLevelAccessURL.edit(existingDoc.id))
const isDisabled = page.locator('#field-approvedTitle')
await expect(isDisabled).toBeDisabled()
})
test('should disable operation based on document data', async () => {
await page.goto(docLevelAccessURL.edit(existingDoc.id))
await openDocControls(page)
await expect(page.locator('#action-delete')).toBeHidden()
await page.locator('#field-approvedForRemoval').check()
await saveDocAndAssert(page)
await openDocControls(page)
await expect(page.locator('#action-delete')).toBeVisible()
})
})
describe('admin access', () => {
test('unauthenticated users should not have access to the admin panel', async () => {
await page.goto(url.logout)
await expect(page.locator('.payload-toast-container')).toContainText(
'You have been logged out successfully.',
)
await expect(page.locator('form.login__form')).toBeVisible()
await page.goto(url.admin)
// wait for redirect to login
await page.waitForURL(url.login)
expect(page.url()).toEqual(url.login)
})
test('non-admin users should not have access to the admin panel', async () => {
await page.goto(url.logout)
await login({
data: {
email: nonAdminEmail,
password: 'test',
},
page,
serverURL,
})
await expect(page.locator('.unauthorized .form-header h1')).toHaveText(
'Unauthorized, this user does not have access to the admin panel.',
)
await page.goto(url.logout)
await expect(page.locator('.payload-toast-container')).toContainText(
'You have been logged out successfully.',
)
await expect(page.locator('form.login__form')).toBeVisible()
})
test('public users should not have access to access admin', async () => {
await page.goto(url.logout)
const user = await payload.login({
collection: publicUsersSlug,
data: {
email: publicUserEmail,
password: devUser.password,
},
})
await context.addCookies([
{
name: 'payload-token',
value: user.token,
domain: 'localhost',
path: '/',
httpOnly: true,
secure: true,
},
])
await page.reload()
await page.goto(url.admin)
// await for redirect to unauthorized
await page.waitForURL(/unauthorized$/)
await expect(page.locator('.unauthorized .form-header h1')).toHaveText(
'Unauthorized, this user does not have access to the admin panel.',
)
await page.goto(url.logout)
await expect(page.locator('.payload-toast-container')).toContainText(
'You have been logged out successfully.',
)
await expect(page.locator('form.login__form')).toBeVisible()
})
})
describe('read-only from access control', () => {
beforeAll(async () => {
await login({
data: {
email: devUser.email,
password: devUser.password,
},
page,
serverURL,
})
})
test('should be read-only when update returns false', async () => {
await page.goto(disabledFields.create)
// group field
await page.locator('#field-group__text').fill('group')
// named tab
await page.locator('#field-namedTab__text').fill('named tab')
// unnamed tab
await page.locator('.tabs-field__tab-button').nth(1).click()
await page.locator('#field-unnamedTab').fill('unnamed tab')
// array field
await page.locator('#field-array > button').click()
await page.locator('#field-array__0__text').fill('array row 0')
await saveDocAndAssert(page)
await expect(page.locator('#field-group__text')).toBeDisabled()
await expect(page.locator('#field-namedTab__text')).toBeDisabled()
await page.locator('.tabs-field__tab-button').nth(1).click()
await expect(page.locator('#field-unnamedTab')).toBeDisabled()
await expect(page.locator('#field-array__0__text')).toBeDisabled()
})
})
describe('restricting update access to auth fields', () => {
let existingDoc: ReadOnlyCollection
beforeAll(async () => {
existingDoc = await payload.create({
collection: authSlug,
data: {
email: 'test@payloadcms.com',
password: 'test',
},
})
})
test('should show email as readonly when user does not have update permission', async () => {
await page.goto(authFields.edit(existingDoc.id))
const emailField = page.locator('#field-email')
await expect(emailField).toBeVisible()
await expect(emailField).toBeDisabled()
})
test('should hide Change Password button when user does not have update permission', async () => {
await page.goto(authFields.edit(existingDoc.id))
const passwordField = page.locator('#field-password')
await expect(passwordField).toBeHidden()
const changePasswordButton = page.locator('#change-password')
await expect(changePasswordButton).toBeHidden()
})
})
})
async function createDoc(data: any): Promise<Record<string, unknown> & TypeWithID> {
return payload.create({
collection: slug,
data,
}) as any as Promise<Record<string, unknown> & TypeWithID>
}