chore(examples/testing): builds testing example (#3443)

This commit is contained in:
Jarrod Flesch
2023-10-05 13:55:36 -04:00
committed by GitHub
parent 9880880384
commit deb5be5a9b
14 changed files with 9058 additions and 1 deletions

View File

@@ -0,0 +1,35 @@
/* tslint:disable */
/* eslint-disable */
/**
* This file was automatically generated by Payload.
* 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 {
collections: {
posts: Post
users: User
}
globals: {}
}
export interface Post {
id: string
title?: string
author?: string | User
updatedAt: string
createdAt: string
}
export interface User {
id: string
updatedAt: string
createdAt: string
email: string
resetPasswordToken?: string
resetPasswordExpiration?: string
salt?: string
hash?: string
loginAttempts?: number
lockUntil?: string
password?: string
}

View File

@@ -0,0 +1,33 @@
import path from 'path'
import dotenv from 'dotenv'
import { buildConfig } from 'payload/config'
dotenv.config({
path: path.resolve(__dirname, '../.env'),
})
export default buildConfig({
serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL,
typescript: {
outputFile: path.resolve(__dirname, 'payload-types.ts'),
},
collections: [
{
slug: 'posts',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
},
{
name: 'author',
type: 'relationship',
relationTo: 'users',
},
],
},
],
})

View File

@@ -0,0 +1,24 @@
import path from 'path'
import express from 'express'
import payload from 'payload'
// Use `dotenv` to import your `.env` file automatically
require('dotenv').config({
path: path.resolve(__dirname, '../.env'),
})
const app = express()
async function start() {
await payload.init({
secret: process.env.PAYLOAD_SECRET_KEY,
mongoURL: process.env.MONGO_URL,
express: app,
})
app.listen(process.env.PORT, async () => {
console.log(`Express is now listening for incoming connections on port ${process.env.PORT}.`)
})
}
start()

View File

@@ -0,0 +1,4 @@
export default {
email: 'test@test.com',
password: 'test',
}

View File

@@ -0,0 +1,44 @@
import { resolve } from 'path'
import payload from 'payload'
import express from 'express'
import testCredentials from './credentials'
require('dotenv').config({
path: resolve(__dirname, '../../.env'),
})
const app = express()
const globalSetup = async () => {
await payload.init({
secret: process.env.PAYLOAD_SECRET_KEY,
mongoURL: process.env.MONGO_URL,
express: app,
})
app.listen(process.env.PORT, async () => {
console.log(`Express is now listening for incoming connections on port ${process.env.PORT}.`)
})
const response = await fetch(
`${process.env.PAYLOAD_PUBLIC_SERVER_URL}/api/users/first-register`,
{
body: JSON.stringify({
email: testCredentials.email,
password: testCredentials.password,
}),
headers: {
'Content-Type': 'application/json',
},
method: 'post',
},
)
const data = await response.json()
if (!data.user || !data.user.token) {
throw new Error('Failed to register first user')
}
}
export default globalSetup

View File

@@ -0,0 +1,22 @@
import { User } from '../payload-types'
import testCredentials from './credentials'
describe('Users', () => {
it('should allow a user to log in', async () => {
const result: {
token: string
user: User
} = await fetch(`${process.env.PAYLOAD_PUBLIC_SERVER_URL}/api/users/login`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: testCredentials.email,
password: testCredentials.password,
}),
}).then((res) => res.json())
expect(result.token).toBeDefined()
})
})