test: plugin cloud storage suite (#3884)
* chore: proper admin mock for adapters * test: add plugin-cloud-storage dev
This commit is contained in:
10
.vscode/launch.json
vendored
10
.vscode/launch.json
vendored
@@ -9,6 +9,16 @@
|
||||
"request": "launch",
|
||||
"type": "node-terminal"
|
||||
},
|
||||
{
|
||||
"command": "pnpm run dev plugin-cloud-storage",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"name": "Run Dev - plugin-cloud-storage",
|
||||
"request": "launch",
|
||||
"type": "node-terminal",
|
||||
"env": {
|
||||
"PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER": "s3"
|
||||
}
|
||||
},
|
||||
{
|
||||
"command": "pnpm run dev fields",
|
||||
"cwd": "${workspaceFolder}",
|
||||
|
||||
@@ -1 +1 @@
|
||||
export const azureBlobStorageAdapter = () => ({})
|
||||
export const azureBlobStorageAdapter = () => {}
|
||||
|
||||
@@ -1 +1 @@
|
||||
export const gcsAdapter = () => ({})
|
||||
export const gcsAdapter = () => {}
|
||||
|
||||
@@ -1 +1 @@
|
||||
export const s3Adapter = () => ({})
|
||||
export const s3Adapter = () => {}
|
||||
|
||||
23
test/plugin-cloud-storage/.env.example
Normal file
23
test/plugin-cloud-storage/.env.example
Normal file
@@ -0,0 +1,23 @@
|
||||
# Sample creds for working locally with docker-compose
|
||||
|
||||
MONGODB_URI=mongodb://localhost/payload-plugin-cloud-storage
|
||||
PAYLOAD_PUBLIC_SERVER_URL=http://localhost:3000
|
||||
PAYLOAD_SECRET=45ligj345ligj4wl5igj4lw5igj45ligj45wlijl
|
||||
PAYLOAD_CONFIG_PATH=src/payload.config.ts
|
||||
|
||||
AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://localhost:10000/devstoreaccount1;QueueEndpoint=http://localhost:10001/devstoreaccount1;
|
||||
AZURE_STORAGE_CONTAINER_NAME=az-media
|
||||
AZURE_STORAGE_ALLOW_CONTAINER_CREATE=true
|
||||
AZURE_STORAGE_ACCOUNT_BASEURL=http://localhost:10000/devstoreaccount1
|
||||
|
||||
S3_ENDPOINT=http://localhost:4566
|
||||
S3_ACCESS_KEY_ID=payloadAccessKey
|
||||
S3_SECRET_ACCESS_KEY=alwiejglaiwhewlihgawe
|
||||
S3_BUCKET=payload-bucket
|
||||
S3_FORCE_PATH_STYLE=true
|
||||
|
||||
GCS_ENDPOINT=http://localhost:4443
|
||||
GCS_PROJECT_ID=test
|
||||
GCS_BUCKET=payload-bucket
|
||||
|
||||
PAYLOAD_DROP_DATABASE=true
|
||||
34
test/plugin-cloud-storage/collections/Media.ts
Normal file
34
test/plugin-cloud-storage/collections/Media.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { CollectionConfig } from '../../../packages/payload/src/collections/config/types'
|
||||
|
||||
export const Media: CollectionConfig = {
|
||||
slug: 'media',
|
||||
upload: {
|
||||
disableLocalStorage: false, // don't save me
|
||||
resizeOptions: {
|
||||
position: 'center',
|
||||
width: 200,
|
||||
height: 200,
|
||||
},
|
||||
imageSizes: [
|
||||
{
|
||||
height: 400,
|
||||
width: 400,
|
||||
crop: 'center',
|
||||
name: 'square',
|
||||
},
|
||||
{
|
||||
width: 900,
|
||||
height: 450,
|
||||
crop: 'center',
|
||||
name: 'sixteenByNineMedium',
|
||||
},
|
||||
],
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'alt',
|
||||
label: 'Alt Text',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
}
|
||||
16
test/plugin-cloud-storage/collections/Users.ts
Normal file
16
test/plugin-cloud-storage/collections/Users.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import type { CollectionConfig } from '../../../packages/payload/src/collections/config/types'
|
||||
|
||||
export const Users: CollectionConfig = {
|
||||
slug: 'users',
|
||||
auth: true,
|
||||
admin: {
|
||||
useAsTitle: 'email',
|
||||
},
|
||||
access: {
|
||||
read: () => true,
|
||||
},
|
||||
fields: [
|
||||
// Email added by default
|
||||
// Add more fields as needed
|
||||
],
|
||||
}
|
||||
110
test/plugin-cloud-storage/config.ts
Normal file
110
test/plugin-cloud-storage/config.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import dotenv from 'dotenv'
|
||||
import path from 'path'
|
||||
|
||||
import type { Adapter } from '../../packages/plugin-cloud-storage/src/types'
|
||||
|
||||
import { cloudStorage } from '../../packages/plugin-cloud-storage/src'
|
||||
import { azureBlobStorageAdapter } from '../../packages/plugin-cloud-storage/src/adapters/azure'
|
||||
import { gcsAdapter } from '../../packages/plugin-cloud-storage/src/adapters/gcs'
|
||||
import { s3Adapter } from '../../packages/plugin-cloud-storage/src/adapters/s3'
|
||||
import { buildConfigWithDefaults } from '../buildConfigWithDefaults'
|
||||
import { devUser } from '../credentials'
|
||||
import { Media } from './collections/Media'
|
||||
import { Users } from './collections/Users'
|
||||
|
||||
let adapter: Adapter
|
||||
let uploadOptions
|
||||
|
||||
dotenv.config({
|
||||
path: path.resolve(__dirname, '.env'),
|
||||
})
|
||||
|
||||
if (process.env.PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER === 'azure') {
|
||||
adapter = azureBlobStorageAdapter({
|
||||
connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING,
|
||||
containerName: process.env.AZURE_STORAGE_CONTAINER_NAME,
|
||||
allowContainerCreate: process.env.AZURE_STORAGE_ALLOW_CONTAINER_CREATE === 'true',
|
||||
baseURL: process.env.AZURE_STORAGE_ACCOUNT_BASEURL,
|
||||
})
|
||||
// uploadOptions = {
|
||||
// useTempFiles: true,
|
||||
// }
|
||||
}
|
||||
|
||||
if (process.env.PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER === 's3') {
|
||||
// The s3 adapter supports using temp files for uploads
|
||||
uploadOptions = {
|
||||
useTempFiles: true,
|
||||
}
|
||||
|
||||
adapter = s3Adapter({
|
||||
config: {
|
||||
endpoint: process.env.S3_ENDPOINT,
|
||||
forcePathStyle: process.env.S3_FORCE_PATH_STYLE === 'true',
|
||||
region: process.env.S3_REGION,
|
||||
credentials: {
|
||||
accessKeyId: process.env.S3_ACCESS_KEY_ID,
|
||||
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
|
||||
},
|
||||
},
|
||||
bucket: process.env.S3_BUCKET,
|
||||
})
|
||||
}
|
||||
|
||||
if (process.env.PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER === 'gcs') {
|
||||
adapter = gcsAdapter({
|
||||
options: {
|
||||
apiEndpoint: process.env.GCS_ENDPOINT,
|
||||
projectId: process.env.GCS_PROJECT_ID,
|
||||
},
|
||||
bucket: process.env.GCS_BUCKET,
|
||||
})
|
||||
}
|
||||
|
||||
export default buildConfigWithDefaults({
|
||||
collections: [Media, Users],
|
||||
upload: uploadOptions,
|
||||
admin: {
|
||||
webpack: (config) => ({
|
||||
...config,
|
||||
resolve: {
|
||||
...config.resolve,
|
||||
alias: {
|
||||
...config?.resolve?.alias,
|
||||
[path.resolve(__dirname, '../../packages/plugin-cloud-storage/src/index')]: path.resolve(
|
||||
__dirname,
|
||||
'../../packages/plugin-cloud-storage/src/admin/mock.js',
|
||||
),
|
||||
[path.resolve(__dirname, '../../packages/plugin-cloud-storage/src/adapters/s3/index')]:
|
||||
path.resolve(__dirname, '../../packages/plugin-cloud-storage/src/adapters/s3/mock.js'),
|
||||
[path.resolve(__dirname, '../../packages/plugin-cloud-storage/src/adapters/gcs/index')]:
|
||||
path.resolve(__dirname, '../../packages/plugin-cloud-storage/src/adapters/gcs/mock.js'),
|
||||
[path.resolve(__dirname, '../../packages/plugin-cloud-storage/src/adapters/azure/index')]:
|
||||
path.resolve(
|
||||
__dirname,
|
||||
'../../packages/plugin-cloud-storage/src/adapters/azure/mock.js',
|
||||
),
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
plugins: [
|
||||
cloudStorage({
|
||||
collections: {
|
||||
media: {
|
||||
adapter,
|
||||
},
|
||||
},
|
||||
}),
|
||||
],
|
||||
onInit: async (payload) => {
|
||||
await payload.create({
|
||||
collection: 'users',
|
||||
data: {
|
||||
email: devUser.email,
|
||||
password: devUser.password,
|
||||
},
|
||||
})
|
||||
console.log(process.env.S3_ENDPOINT)
|
||||
},
|
||||
})
|
||||
11
test/plugin-cloud-storage/int.spec.ts
Normal file
11
test/plugin-cloud-storage/int.spec.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { initPayloadTest } from '../helpers/configHelpers'
|
||||
|
||||
describe('plugin-cloud-storage', () => {
|
||||
beforeAll(async () => {
|
||||
await initPayloadTest({ __dirname, init: { local: true } })
|
||||
})
|
||||
|
||||
describe('tests', () => {
|
||||
it.todo('plugin-cloud-storage tests')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user