test: move endpoints tests to new suite
This commit is contained in:
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,
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
48
test/endpoints/int.spec.ts
Normal file
48
test/endpoints/int.spec.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { initPayloadTest } from '../helpers/configHelpers';
|
||||
import { RESTClient } from '../helpers/rest';
|
||||
import { collectionSlug, globalEndpoint, globalSlug } from './config';
|
||||
|
||||
require('isomorphic-fetch');
|
||||
|
||||
let client: RESTClient;
|
||||
|
||||
describe('Endpoints', () => {
|
||||
beforeAll(async () => {
|
||||
const config = await initPayloadTest({ __dirname, init: { local: false } });
|
||||
const { serverURL } = config;
|
||||
client = new RESTClient(config, { serverURL, defaultSlug: collectionSlug });
|
||||
});
|
||||
|
||||
describe('Collections', () => {
|
||||
it('should GET a static endpoint', async () => {
|
||||
const { status, data } = await client.endpoint(`/${collectionSlug}/say-hello/joe-bloggs`);
|
||||
expect(status).toBe(200);
|
||||
expect(data.message).toStrictEqual('Hey Joey!');
|
||||
});
|
||||
|
||||
it('should GET an endpoint with a parameter', async () => {
|
||||
const name = 'George';
|
||||
const { status, data } = await client.endpoint(`/${collectionSlug}/say-hello/${name}`);
|
||||
expect(status).toBe(200);
|
||||
expect(data.message).toStrictEqual(`Hello ${name}!`);
|
||||
});
|
||||
|
||||
it('should POST an endpoint with data', async () => {
|
||||
const params = { name: 'George', age: 29 };
|
||||
const { status, data } = await client.endpoint(`/${collectionSlug}/whoami`, 'post', params);
|
||||
expect(status).toBe(200);
|
||||
expect(data.name).toStrictEqual(params.name);
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user