test: move endpoints tests to new suite
This commit is contained in:
@@ -139,6 +139,7 @@ export type Config = {
|
|||||||
webpack?: (config: Configuration) => Configuration;
|
webpack?: (config: Configuration) => Configuration;
|
||||||
};
|
};
|
||||||
collections?: CollectionConfig[];
|
collections?: CollectionConfig[];
|
||||||
|
endpoints?: Endpoint[];
|
||||||
globals?: GlobalConfig[];
|
globals?: GlobalConfig[];
|
||||||
serverURL?: string;
|
serverURL?: string;
|
||||||
cookiePrefix?: string;
|
cookiePrefix?: string;
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
import { Response } from 'express';
|
|
||||||
import { CollectionConfig } from '../../../src/collections/config/types';
|
|
||||||
import { openAccess } from '../../helpers/configHelpers';
|
|
||||||
import { PayloadRequest } from '../../../src/express/types';
|
|
||||||
|
|
||||||
export const endpointsSlug = 'endpoints';
|
|
||||||
|
|
||||||
const Endpoints: CollectionConfig = {
|
|
||||||
slug: endpointsSlug,
|
|
||||||
access: openAccess,
|
|
||||||
endpoints: [
|
|
||||||
{
|
|
||||||
path: '/say-hello/joe-bloggs',
|
|
||||||
method: 'get',
|
|
||||||
handler: (req: PayloadRequest, res: Response): void => {
|
|
||||||
res.json({ message: 'Hey Joey!' });
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: '/say-hello/:group/:name',
|
|
||||||
method: 'get',
|
|
||||||
handler: (req: PayloadRequest, res: Response): void => {
|
|
||||||
res.json({ message: `Hello ${req.params.name} @ ${req.params.group}` });
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: '/say-hello/:name',
|
|
||||||
method: 'get',
|
|
||||||
handler: (req: PayloadRequest, res: Response): void => {
|
|
||||||
res.json({ message: `Hello ${req.params.name}!` });
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: '/whoami',
|
|
||||||
method: 'post',
|
|
||||||
handler: (req: PayloadRequest, res: Response): void => {
|
|
||||||
res.json({
|
|
||||||
name: req.body.name,
|
|
||||||
age: req.body.age,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
fields: [
|
|
||||||
{
|
|
||||||
name: 'title',
|
|
||||||
type: 'text',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Endpoints;
|
|
||||||
@@ -2,7 +2,6 @@ import type { CollectionConfig } from '../../src/collections/config/types';
|
|||||||
import { devUser } from '../credentials';
|
import { devUser } from '../credentials';
|
||||||
import { buildConfig } from '../buildConfig';
|
import { buildConfig } from '../buildConfig';
|
||||||
import type { Post } from './payload-types';
|
import type { Post } from './payload-types';
|
||||||
import Endpoints from './Endpoints';
|
|
||||||
|
|
||||||
export interface Relation {
|
export interface Relation {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -121,7 +120,6 @@ export default buildConfig({
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
Endpoints,
|
|
||||||
],
|
],
|
||||||
onInit: async (payload) => {
|
onInit: async (payload) => {
|
||||||
await payload.create({
|
await payload.create({
|
||||||
|
|||||||
@@ -93,16 +93,6 @@ export interface CustomIdNumber {
|
|||||||
createdAt: string;
|
createdAt: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* This interface was referenced by `Config`'s JSON-Schema
|
|
||||||
* via the `definition` "endpoints".
|
|
||||||
*/
|
|
||||||
export interface Endpoint {
|
|
||||||
id: string;
|
|
||||||
title?: string;
|
|
||||||
createdAt: string;
|
|
||||||
updatedAt: string;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* This interface was referenced by `Config`'s JSON-Schema
|
* This interface was referenced by `Config`'s JSON-Schema
|
||||||
* via the `definition` "users".
|
* via the `definition` "users".
|
||||||
|
|||||||
33
test/dataloader/payload-types.ts
Normal file
33
test/dataloader/payload-types.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/* tslint:disable */
|
||||||
|
/**
|
||||||
|
* This file was automatically generated by Payload CMS.
|
||||||
|
* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,
|
||||||
|
* and re-run `payload generate:types` to regenerate this file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface Config {}
|
||||||
|
/**
|
||||||
|
* This interface was referenced by `Config`'s JSON-Schema
|
||||||
|
* via the `definition` "posts".
|
||||||
|
*/
|
||||||
|
export interface Post {
|
||||||
|
id: string;
|
||||||
|
title: string;
|
||||||
|
owner?: string | User;
|
||||||
|
createdAt: string;
|
||||||
|
updatedAt: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* This interface was referenced by `Config`'s JSON-Schema
|
||||||
|
* via the `definition` "users".
|
||||||
|
*/
|
||||||
|
export interface User {
|
||||||
|
id: string;
|
||||||
|
email?: string;
|
||||||
|
resetPasswordToken?: string;
|
||||||
|
resetPasswordExpiration?: string;
|
||||||
|
loginAttempts?: number;
|
||||||
|
lockUntil?: string;
|
||||||
|
createdAt: string;
|
||||||
|
updatedAt: string;
|
||||||
|
}
|
||||||
80
test/endpoints/config.ts
Normal file
80
test/endpoints/config.ts
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
import { Response } from 'express';
|
||||||
|
import { devUser } from '../credentials';
|
||||||
|
import { buildConfig } from '../buildConfig';
|
||||||
|
import { openAccess } from '../helpers/configHelpers';
|
||||||
|
import { PayloadRequest } from '../../src/express/types';
|
||||||
|
|
||||||
|
export const collectionSlug = 'endpoints';
|
||||||
|
export const globalSlug = 'global-endpoints';
|
||||||
|
|
||||||
|
export const globalEndpoint = 'global';
|
||||||
|
|
||||||
|
export default buildConfig({
|
||||||
|
collections: [
|
||||||
|
{
|
||||||
|
slug: collectionSlug,
|
||||||
|
access: openAccess,
|
||||||
|
endpoints: [
|
||||||
|
{
|
||||||
|
path: '/say-hello/joe-bloggs',
|
||||||
|
method: 'get',
|
||||||
|
handler: (req: PayloadRequest, res: Response): void => {
|
||||||
|
res.json({ message: 'Hey Joey!' });
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/say-hello/:group/:name',
|
||||||
|
method: 'get',
|
||||||
|
handler: (req: PayloadRequest, res: Response): void => {
|
||||||
|
res.json({ message: `Hello ${req.params.name} @ ${req.params.group}` });
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/say-hello/:name',
|
||||||
|
method: 'get',
|
||||||
|
handler: (req: PayloadRequest, res: Response): void => {
|
||||||
|
res.json({ message: `Hello ${req.params.name}!` });
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/whoami',
|
||||||
|
method: 'post',
|
||||||
|
handler: (req: PayloadRequest, res: Response): void => {
|
||||||
|
res.json({
|
||||||
|
name: req.body.name,
|
||||||
|
age: req.body.age,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
type: 'text',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
globals: [
|
||||||
|
{
|
||||||
|
slug: globalSlug,
|
||||||
|
endpoints: [{
|
||||||
|
path: `/${globalEndpoint}`,
|
||||||
|
method: 'post',
|
||||||
|
handler: (req: PayloadRequest, res: Response): void => {
|
||||||
|
res.json(req.body);
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
fields: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
onInit: async (payload) => {
|
||||||
|
await payload.create({
|
||||||
|
collection: 'users',
|
||||||
|
data: {
|
||||||
|
email: devUser.email,
|
||||||
|
password: devUser.password,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -1,38 +1,48 @@
|
|||||||
import { initPayloadTest } from '../helpers/configHelpers';
|
import { initPayloadTest } from '../helpers/configHelpers';
|
||||||
import { endpointsSlug } from './Endpoints';
|
|
||||||
import { RESTClient } from '../helpers/rest';
|
import { RESTClient } from '../helpers/rest';
|
||||||
import { slug } from '../globals/config';
|
import { collectionSlug, globalEndpoint, globalSlug } from './config';
|
||||||
|
|
||||||
require('isomorphic-fetch');
|
require('isomorphic-fetch');
|
||||||
|
|
||||||
let client: RESTClient;
|
let client: RESTClient;
|
||||||
|
|
||||||
describe('Collections - Endpoints', () => {
|
describe('Endpoints', () => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const config = await initPayloadTest({ __dirname, init: { local: false } });
|
const config = await initPayloadTest({ __dirname, init: { local: false } });
|
||||||
const { serverURL } = config;
|
const { serverURL } = config;
|
||||||
client = new RESTClient(config, { serverURL, defaultSlug: slug });
|
client = new RESTClient(config, { serverURL, defaultSlug: collectionSlug });
|
||||||
});
|
});
|
||||||
describe('Endpoints', () => {
|
|
||||||
|
describe('Collections', () => {
|
||||||
it('should GET a static endpoint', async () => {
|
it('should GET a static endpoint', async () => {
|
||||||
const { status, data } = await client.endpoint(`/${endpointsSlug}/say-hello/joe-bloggs`);
|
const { status, data } = await client.endpoint(`/${collectionSlug}/say-hello/joe-bloggs`);
|
||||||
expect(status).toBe(200);
|
expect(status).toBe(200);
|
||||||
expect(data.message).toStrictEqual('Hey Joey!');
|
expect(data.message).toStrictEqual('Hey Joey!');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should GET an endpoint with a parameter', async () => {
|
it('should GET an endpoint with a parameter', async () => {
|
||||||
const name = 'George';
|
const name = 'George';
|
||||||
const { status, data } = await client.endpoint(`/${endpointsSlug}/say-hello/${name}`);
|
const { status, data } = await client.endpoint(`/${collectionSlug}/say-hello/${name}`);
|
||||||
expect(status).toBe(200);
|
expect(status).toBe(200);
|
||||||
expect(data.message).toStrictEqual(`Hello ${name}!`);
|
expect(data.message).toStrictEqual(`Hello ${name}!`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should POST an endpoint with data', async () => {
|
it('should POST an endpoint with data', async () => {
|
||||||
const params = { name: 'George', age: 29 };
|
const params = { name: 'George', age: 29 };
|
||||||
const { status, data } = await client.endpoint(`/${endpointsSlug}/whoami`, 'post', params);
|
const { status, data } = await client.endpoint(`/${collectionSlug}/whoami`, 'post', params);
|
||||||
expect(status).toBe(200);
|
expect(status).toBe(200);
|
||||||
expect(data.name).toStrictEqual(params.name);
|
expect(data.name).toStrictEqual(params.name);
|
||||||
expect(data.age).toStrictEqual(params.age);
|
expect(data.age).toStrictEqual(params.age);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Globals', () => {
|
||||||
|
it('should call custom endpoint', async () => {
|
||||||
|
const params = { globals: 'response' };
|
||||||
|
const { status, data } = await client.endpoint(`/globals/${globalSlug}/${globalEndpoint}`, 'post', params);
|
||||||
|
|
||||||
|
expect(status).toBe(200);
|
||||||
|
expect(params).toMatchObject(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
39
test/endpoints/payload-types.ts
Normal file
39
test/endpoints/payload-types.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/* tslint:disable */
|
||||||
|
/**
|
||||||
|
* This file was automatically generated by Payload CMS.
|
||||||
|
* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,
|
||||||
|
* and re-run `payload generate:types` to regenerate this file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface Config {}
|
||||||
|
/**
|
||||||
|
* This interface was referenced by `Config`'s JSON-Schema
|
||||||
|
* via the `definition` "global-endpoints".
|
||||||
|
*/
|
||||||
|
export interface GlobalEndpoints {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* This interface was referenced by `Config`'s JSON-Schema
|
||||||
|
* via the `definition` "endpoints".
|
||||||
|
*/
|
||||||
|
export interface Endpoint {
|
||||||
|
id: string;
|
||||||
|
title?: string;
|
||||||
|
createdAt: string;
|
||||||
|
updatedAt: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* This interface was referenced by `Config`'s JSON-Schema
|
||||||
|
* via the `definition` "users".
|
||||||
|
*/
|
||||||
|
export interface User {
|
||||||
|
id: string;
|
||||||
|
email?: string;
|
||||||
|
resetPasswordToken?: string;
|
||||||
|
resetPasswordExpiration?: string;
|
||||||
|
loginAttempts?: number;
|
||||||
|
lockUntil?: string;
|
||||||
|
createdAt: string;
|
||||||
|
updatedAt: string;
|
||||||
|
}
|
||||||
@@ -40,6 +40,45 @@ export interface BlockField {
|
|||||||
blocks: (
|
blocks: (
|
||||||
| {
|
| {
|
||||||
text: string;
|
text: string;
|
||||||
|
richText?: {
|
||||||
|
[k: string]: unknown;
|
||||||
|
}[];
|
||||||
|
id?: string;
|
||||||
|
blockName?: string;
|
||||||
|
blockType: 'text';
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
number: number;
|
||||||
|
id?: string;
|
||||||
|
blockName?: string;
|
||||||
|
blockType: 'number';
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
subBlocks: (
|
||||||
|
| {
|
||||||
|
text: string;
|
||||||
|
id?: string;
|
||||||
|
blockName?: string;
|
||||||
|
blockType: 'text';
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
number: number;
|
||||||
|
id?: string;
|
||||||
|
blockName?: string;
|
||||||
|
blockType: 'number';
|
||||||
|
}
|
||||||
|
)[];
|
||||||
|
id?: string;
|
||||||
|
blockName?: string;
|
||||||
|
blockType: 'subBlocks';
|
||||||
|
}
|
||||||
|
)[];
|
||||||
|
localizedBlocks: (
|
||||||
|
| {
|
||||||
|
text: string;
|
||||||
|
richText?: {
|
||||||
|
[k: string]: unknown;
|
||||||
|
}[];
|
||||||
id?: string;
|
id?: string;
|
||||||
blockName?: string;
|
blockName?: string;
|
||||||
blockType: 'text';
|
blockType: 'text';
|
||||||
@@ -188,6 +227,9 @@ export interface TabsField {
|
|||||||
blocks: (
|
blocks: (
|
||||||
| {
|
| {
|
||||||
text: string;
|
text: string;
|
||||||
|
richText?: {
|
||||||
|
[k: string]: unknown;
|
||||||
|
}[];
|
||||||
id?: string;
|
id?: string;
|
||||||
blockName?: string;
|
blockName?: string;
|
||||||
blockType: 'text';
|
blockType: 'text';
|
||||||
@@ -235,6 +277,7 @@ export interface TabsField {
|
|||||||
export interface TextField {
|
export interface TextField {
|
||||||
id: string;
|
id: string;
|
||||||
text: string;
|
text: string;
|
||||||
|
localizedText?: string;
|
||||||
defaultFunction?: string;
|
defaultFunction?: string;
|
||||||
defaultAsync?: string;
|
defaultAsync?: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
@@ -292,6 +335,8 @@ export interface IndexedField {
|
|||||||
*/
|
*/
|
||||||
point?: [number, number];
|
point?: [number, number];
|
||||||
};
|
};
|
||||||
|
collapsibleLocalizedUnique?: string;
|
||||||
|
collapsibleTextUnique?: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import { Response } from 'express';
|
|
||||||
import { devUser } from '../credentials';
|
import { devUser } from '../credentials';
|
||||||
import { buildConfig } from '../buildConfig';
|
import { buildConfig } from '../buildConfig';
|
||||||
import { PayloadRequest } from '../../src/express/types';
|
|
||||||
|
|
||||||
export const slug = 'global';
|
export const slug = 'global';
|
||||||
export const arraySlug = 'array';
|
export const arraySlug = 'array';
|
||||||
@@ -31,13 +29,6 @@ export default buildConfig({
|
|||||||
type: 'text',
|
type: 'text',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
endpoints: [{
|
|
||||||
path: `/${globalsEndpoint}`,
|
|
||||||
method: 'post',
|
|
||||||
handler: (req: PayloadRequest, res: Response): void => {
|
|
||||||
res.json(req.body);
|
|
||||||
},
|
|
||||||
}],
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
slug: arraySlug,
|
slug: arraySlug,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { GraphQLClient } from 'graphql-request';
|
import { GraphQLClient } from 'graphql-request';
|
||||||
import { initPayloadTest } from '../helpers/configHelpers';
|
import { initPayloadTest } from '../helpers/configHelpers';
|
||||||
import config, { arraySlug, englishLocale, globalsEndpoint, slug, spanishLocale } from './config';
|
import config, { arraySlug, englishLocale, slug, spanishLocale } from './config';
|
||||||
import payload from '../../src';
|
import payload from '../../src';
|
||||||
import { RESTClient } from '../helpers/rest';
|
import { RESTClient } from '../helpers/rest';
|
||||||
|
|
||||||
@@ -56,16 +56,6 @@ describe('globals', () => {
|
|||||||
expect(doc.array).toMatchObject(array);
|
expect(doc.array).toMatchObject(array);
|
||||||
expect(doc.id).toBeDefined();
|
expect(doc.id).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Endpoints', () => {
|
|
||||||
it('should call custom endpoint', async () => {
|
|
||||||
const params = { globals: 'response' };
|
|
||||||
const { status, data } = await client.endpoint(`/globals/${slug}/${globalsEndpoint}`, 'post', params);
|
|
||||||
|
|
||||||
expect(status).toBe(200);
|
|
||||||
expect(params).toMatchObject(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('local', () => {
|
describe('local', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user