Files
payloadcms/test/query-presets/seed.ts
Alessio Gravili 545d870650 chore: fix various e2e test setup issues (#12670)
I noticed a few issues when running e2e tests that will be resolved by
this PR:

- Most important: for some test suites (fields, fields-relationship,
versions, queues, lexical), the database was cleared and seeded
**twice** in between each test run. This is because the onInit function
was running the clear and seed script, when it should only have been
running the seed script. Clearing the database / the snapshot workflow
is being done by the reInit endpoint, which then calls onInit to seed
the actual data.
- The slowest part of `clearAndSeedEverything` is recreating indexes on
mongodb. This PR slightly improves performance here by:
- Skipping this process for the built-in `['payload-migrations',
'payload-preferences', 'payload-locked-documents']` collections
- Previously we were calling both `createIndexes` and `ensureIndexes`.
This was unnecessary - `ensureIndexes` is a deprecated alias of
`createIndexes`. This PR changes it to only call `createIndexes`
- Makes the reinit endpoint accept GET requests instead of POST requests
- this makes it easier to debug right in the browser
- Some typescript fixes
- Adds a `dev:memorydb` script to the package.json. For some reason,
`dev` is super unreliable on mongodb locally when running e2e tests - it
frequently fails during index creation. Using the memorydb fixes this
issue, with the bonus of more closely resembling the CI environment
- Previously, you were unable to run test suites using turbopack +
postgres. This fixes it, by explicitly installing `pg` as devDependency
in our monorepo
- Fixes jest open handles warning
2025-06-04 17:34:37 -03:00

189 lines
4.1 KiB
TypeScript

import type { Payload, QueryPreset } from 'payload'
import { devUser as devCredentials, regularUser as regularCredentials } from '../credentials.js'
import { executePromises } from '../helpers/executePromises.js'
import { pagesSlug, usersSlug } from './slugs.js'
type SeededQueryPreset = {
relatedCollection: 'pages'
} & Omit<QueryPreset, 'id' | 'relatedCollection'>
export const seedData: {
everyone: () => SeededQueryPreset
onlyMe: () => SeededQueryPreset
specificUsers: (args: { adminUserID: string }) => SeededQueryPreset
} = {
onlyMe: () => ({
relatedCollection: pagesSlug,
isShared: false,
title: 'Only Me',
columns: [
{
accessor: 'text',
active: true,
},
],
access: {
delete: {
constraint: 'onlyMe',
},
update: {
constraint: 'onlyMe',
},
read: {
constraint: 'onlyMe',
},
},
where: {
text: {
equals: 'example page',
},
},
}),
everyone: () => ({
relatedCollection: pagesSlug,
isShared: true,
title: 'Everyone',
access: {
delete: {
constraint: 'everyone',
},
update: {
constraint: 'everyone',
},
read: {
constraint: 'everyone',
},
},
columns: [
{
accessor: 'text',
active: true,
},
],
where: {
text: {
equals: 'example page',
},
},
}),
specificUsers: ({ adminUserID }: { adminUserID: string }) => ({
title: 'Specific Users',
isShared: true,
where: {
text: {
equals: 'example page',
},
},
access: {
read: {
constraint: 'specificUsers',
users: [adminUserID],
},
update: {
constraint: 'specificUsers',
users: [adminUserID],
},
delete: {
constraint: 'specificUsers',
users: [adminUserID],
},
},
columns: [
{
accessor: 'text',
active: true,
},
],
relatedCollection: pagesSlug,
}),
}
export const seed = async (_payload: Payload) => {
const [adminUser] = await executePromises(
[
() =>
_payload.create({
collection: usersSlug,
data: {
email: devCredentials.email,
password: devCredentials.password,
name: 'Admin',
roles: ['admin'],
},
}),
() =>
_payload.create({
collection: usersSlug,
data: {
email: regularCredentials.email,
password: regularCredentials.password,
name: 'Editor',
roles: ['editor'],
},
}),
() =>
_payload.create({
collection: usersSlug,
data: {
email: 'public@email.com',
password: regularCredentials.password,
name: 'Public User',
roles: ['user'],
},
}),
],
false,
)
await executePromises(
[
() =>
_payload.create({
collection: pagesSlug,
data: {
text: 'example page',
},
}),
() =>
_payload.create({
collection: 'payload-query-presets',
user: adminUser,
overrideAccess: false,
data: seedData.specificUsers({
adminUserID: adminUser?.id || '',
}),
}),
() =>
_payload.create({
collection: 'payload-query-presets',
user: adminUser,
overrideAccess: false,
data: seedData.everyone(),
}),
() =>
_payload.create({
collection: 'payload-query-presets',
user: adminUser,
overrideAccess: false,
data: seedData.onlyMe(),
}),
() =>
_payload.create({
collection: 'payload-query-presets',
user: adminUser,
data: {
relatedCollection: 'pages',
title: 'Noone',
access: {
read: {
constraint: 'noone',
},
},
},
}),
],
false,
)
}