test: plugin-sentry suite (#4040)

This commit is contained in:
Elliot DeNolf
2023-11-07 13:51:40 -05:00
committed by GitHub
parent 2abb46f4f1
commit 55c38a8934
15 changed files with 54 additions and 241 deletions

View File

@@ -1,2 +0,0 @@
MONGODB_URI=mongodb://localhost/plugin-sentry
PAYLOAD_SECRET=mwfkfksosksseanllcosjshdncm

View File

@@ -1,6 +0,0 @@
node_modules
.env
dist
build
.DS_Store
package-lock.json

View File

@@ -1,7 +0,0 @@
# Sentry Plugin Demo
This project was created using create-payload-app using the blank template.
## How to Use
`yarn dev` will start up your application and reload on any changes.

View File

@@ -1,4 +0,0 @@
{
"exec": "ts-node src/server.ts",
"ext": "ts"
}

View File

@@ -1,32 +0,0 @@
{
"name": "plugin-sentry-demo",
"description": "Payload project to demonstrate how to use the sentry plugin",
"version": "1.0.0",
"main": "dist/server.js",
"license": "MIT",
"scripts": {
"dev": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts nodemon",
"build:payload": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload build",
"build:server": "tsc",
"build": "yarn copyfiles && yarn build:payload && yarn build:server",
"serve": "cross-env PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production node dist/server.js",
"copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png}\" dist/",
"generate:types": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:types",
"generate:graphQLSchema": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:graphQLSchema"
},
"dependencies": {
"@sentry/react": "^7.56.0",
"dotenv": "^8.2.0",
"eslint": "^8.42.0",
"express": "^4.17.1",
"payload": "^1.9.2"
},
"devDependencies": {
"@types/express": "^4.17.9",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"nodemon": "^2.0.6",
"ts-node": "^9.1.1",
"typescript": "^4.8.4"
}
}

View File

@@ -1,17 +0,0 @@
import type { CollectionConfig } from 'payload/types'
const Posts: CollectionConfig = {
slug: 'posts',
access: {
create: () => false,
read: () => true,
},
fields: [
{
name: 'text',
type: 'text',
},
],
}
export default Posts

View File

@@ -1,19 +0,0 @@
import type { CollectionConfig } from 'payload/types'
const Users: CollectionConfig = {
slug: 'users',
auth: true,
admin: {
useAsTitle: 'email',
},
access: {
read: () => true,
delete: () => false,
},
fields: [
// Email added by default
// Add more fields as needed
],
}
export default Users

View File

@@ -1,39 +0,0 @@
/* eslint-disable import/no-relative-packages */
import path from 'path'
import { buildConfig } from 'payload/config'
import { sentry } from '../../src/index'
import Posts from './collections/Posts'
import Users from './collections/Users'
import { testErrors } from './test/component'
export default buildConfig({
serverURL: 'http://localhost:3000',
admin: {
user: Users.slug,
components: {
beforeDashboard: [testErrors],
},
},
collections: [Posts, Users],
typescript: {
outputFile: path.resolve(__dirname, 'payload-types.ts'),
},
graphQL: {
schemaOutputFile: path.resolve(__dirname, 'generated-schema.graphql'),
},
plugins: [
sentry({
dsn: 'https://61edebe5ee6d4d38a9d6459c7323d777@o4505289711681536.ingest.sentry.io/4505357688242176',
options: {
init: {
debug: true,
},
requestHandler: {
serverName: false,
},
captureErrors: [400, 403, 404],
},
}),
],
})

View File

@@ -1,27 +0,0 @@
import dotenv from 'dotenv'
import express from 'express'
import payload from 'payload'
dotenv.config()
const app = express()
// Redirect root to Admin panel
app.get('/', (_, res) => {
res.redirect('/admin')
})
// Initialize Payload
const start = async (): Promise<void> => {
await payload.init({
secret: process.env.PAYLOAD_SECRET,
mongoURL: process.env.MONGODB_URI,
express: app,
onInit: () => {
payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
},
})
app.listen(3000)
}
start()

View File

@@ -1,92 +0,0 @@
import React from 'react'
import * as Sentry from '@sentry/react'
export const testErrors = () => {
const notFound = async () => {
const req = await fetch('http://localhost:3000/api/users/notFound', {
method: 'GET',
})
}
const cannotCreate = async () => {
const req = await fetch('http://localhost:3000/api/posts', {
method: 'POST',
body: JSON.stringify({
text: 'New post',
}),
})
}
const badLogin = async () => {
const req = await fetch('http://localhost:3000/api/users/login', {
method: 'POST',
body: JSON.stringify({
email: 'sorry@whoareyou.com',
password: '123456',
}),
})
}
const badReq = async () => {
const req = await fetch('http://localhost:3000/api/users/forgot-password', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
})
}
const badReset = async () => {
const req = await fetch('http://localhost:3000/api/users/reset-password', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
token: '7eac3830ffcfc7f9f66c00315dabeb11575dba91',
password: 'newPassword',
}),
})
}
const badVerify = async () => {
const req = await fetch('http://localhost:3000/api/users/unlock', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})
}
return (
<Sentry.ErrorBoundary>
<h4>Test Errors</h4>
<div style={{ display: 'flex', gap: '10px' }}>
<button style={{ marginBottom: '20px' }} onClick={() => notFound()} type="button">
Not Found
</button>
<button style={{ marginBottom: '20px' }} onClick={() => cannotCreate()} type="button">
Forbidden
</button>
<button style={{ marginBottom: '20px' }} onClick={() => badLogin()} type="button">
Bad login
</button>
<button style={{ marginBottom: '20px' }} onClick={() => badReq()} type="button">
TypeError
</button>
<button style={{ marginBottom: '20px' }} onClick={() => badReset()} type="button">
Bad Reset
</button>
<button style={{ marginBottom: '20px' }} onClick={() => badVerify()} type="button">
Bad Verify
</button>
</div>
</Sentry.ErrorBoundary>
)
}

View File

@@ -1,16 +0,0 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"strict": false,
"esModuleInterop": true,
"skipLibCheck": true,
"outDir": "./dist",
"rootDir": "../",
"jsx": "react",
"sourceMap": true
},
"ts-node": {
"transpileOnly": true
}
}

View File

@@ -27,7 +27,7 @@
"dist"
],
"peerDependencies": {
"payload": "^1.10.1",
"payload": "^1.10.1 || ^2.0.0",
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
"dependencies": {
@@ -36,7 +36,7 @@
"express": "^4.18.2"
},
"devDependencies": {
"@payloadcms/eslint-config": "^1.0.0",
"@payloadcms/eslint-config": "workspace:*",
"@types/express": "^4.17.9",
"@types/jest": "^29.5.2",
"@types/node": "18.11.3",
@@ -46,7 +46,7 @@
"dotenv": "^8.2.0",
"jest": "^29.5.0",
"nodemon": "^2.0.6",
"payload": "^1.10.1",
"payload": "workspace:*",
"ts-jest": "^29.1.0",
"webpack": "^5.78.0"
}