Files
payload/test/config/int.spec.ts
Hulpoi George-Valentin 9c72ab97b0 feat: configure cors allowed headers (#6837)
## Description

Currently, the Payload doesn't support to extend the Allowed Headers in
CORS context. With this PR, `cors` property can be an object with
`origins` and `headers`.

- [x] I have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

## Type of change

- [ ] Chore (non-breaking change which does not add functionality)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Change to the
[templates](https://github.com/payloadcms/payload/tree/main/templates)
directory (does not affect core functionality)
- [ ] Change to the
[examples](https://github.com/payloadcms/payload/tree/main/examples)
directory (does not affect core functionality)
- [x] This change requires a documentation update

## Checklist:

- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] Existing test suite passes locally with my changes
- [x] I have made corresponding changes to the documentation

Co-authored-by: Alessio Gravili <alessio@gravili.de>
2024-07-15 13:26:29 -04:00

105 lines
3.0 KiB
TypeScript

import type { BlockField, Payload } from 'payload'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
import configPromise from './config.js'
let restClient: NextRESTClient
let payload: Payload
describe('Config', () => {
beforeAll(async () => {
;({ payload, restClient } = await initPayloadInt(configPromise))
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('payload config', () => {
it('allows a custom field at the config root', () => {
const { config } = payload
expect(config.custom).toEqual({
name: 'Customer portal',
})
})
it('allows a custom field in the root endpoints', () => {
const [endpoint] = payload.config.endpoints
expect(endpoint.custom).toEqual({
description: 'Get the sanitized payload config',
})
})
})
describe('collection config', () => {
it('allows a custom field in collections', () => {
const [collection] = payload.config.collections
expect(collection.custom).toEqual({
externalLink: 'https://foo.bar',
})
})
it('allows a custom field in collection endpoints', () => {
const [collection] = payload.config.collections
const [endpoint] = collection.endpoints || []
expect(endpoint.custom).toEqual({
examples: [{ type: 'response', value: { message: 'hi' } }],
})
})
it('allows a custom field in collection fields', () => {
const [collection] = payload.config.collections
const [field] = collection.fields
expect(field.custom).toEqual({
description: 'The title of this page',
})
})
it('allows a custom field in blocks in collection fields', () => {
const [collection] = payload.config.collections
const [, blocksField] = collection.fields
expect((blocksField as BlockField).blocks[0].custom).toEqual({
description: 'The blockOne of this page',
})
})
})
describe('global config', () => {
it('allows a custom field in globals', () => {
const [global] = payload.config.globals
expect(global.custom).toEqual({ foo: 'bar' })
})
it('allows a custom field in global endpoints', () => {
const [global] = payload.config.globals
const [endpoint] = global.endpoints || []
expect(endpoint.custom).toEqual({ params: [{ name: 'name', type: 'string', in: 'query' }] })
})
it('allows a custom field in global fields', () => {
const [global] = payload.config.globals
const [field] = global.fields
expect(field.custom).toEqual({
description: 'The title of my global',
})
})
})
describe('cors config', () => {
it('includes a custom header in Access-Control-Allow-Headers', async () => {
const response = await restClient.GET(`/pages`)
expect(response.headers.get('Access-Control-Allow-Headers')).toContain('x-custom-header')
})
})
})