test(pcs): add prefix test (#5867)
This commit is contained in:
@@ -3,7 +3,7 @@ import type { CollectionConfig } from 'payload/types'
|
||||
export const Media: CollectionConfig = {
|
||||
slug: 'media',
|
||||
upload: {
|
||||
disableLocalStorage: true, // don't save me
|
||||
disableLocalStorage: true,
|
||||
resizeOptions: {
|
||||
position: 'center',
|
||||
width: 200,
|
||||
|
||||
9
test/plugin-cloud-storage/collections/MediaWithPrefix.ts
Normal file
9
test/plugin-cloud-storage/collections/MediaWithPrefix.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { CollectionConfig } from 'payload/types'
|
||||
|
||||
export const MediaWithPrefix: CollectionConfig = {
|
||||
slug: 'media-with-prefix',
|
||||
upload: {
|
||||
disableLocalStorage: true,
|
||||
},
|
||||
fields: [],
|
||||
}
|
||||
@@ -1,20 +1,23 @@
|
||||
import type { Adapter } from '@payloadcms/plugin-cloud-storage/types'
|
||||
|
||||
import { cloudStorage } from '@payloadcms/plugin-cloud-storage'
|
||||
import { azureBlobStorageAdapter } from '@payloadcms/plugin-cloud-storage/azure'
|
||||
import { gcsAdapter } from '@payloadcms/plugin-cloud-storage/gcs'
|
||||
import { s3Adapter } from '@payloadcms/plugin-cloud-storage/s3'
|
||||
import dotenv from 'dotenv'
|
||||
import path from 'path'
|
||||
|
||||
import { azureBlobStorageAdapter } from '../../packages/plugin-cloud-storage/src/adapters/azure/index.js'
|
||||
import { gcsAdapter } from '../../packages/plugin-cloud-storage/src/adapters/gcs/index.js'
|
||||
import { s3Adapter } from '../../packages/plugin-cloud-storage/src/adapters/s3/index.js'
|
||||
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
|
||||
import { devUser } from '../credentials.js'
|
||||
import { Media } from './collections/Media.js'
|
||||
import { MediaWithPrefix } from './collections/MediaWithPrefix.js'
|
||||
import { Users } from './collections/Users.js'
|
||||
import { mediaSlug, mediaWithPrefixSlug, prefix } from './shared.js'
|
||||
|
||||
let adapter: Adapter
|
||||
let uploadOptions
|
||||
|
||||
// Load config to work with emulated services
|
||||
dotenv.config({
|
||||
path: path.resolve(process.cwd(), './test/plugin-cloud-storage/.env.emulated'),
|
||||
})
|
||||
@@ -84,7 +87,7 @@ if (process.env.PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER === 'r2') {
|
||||
}
|
||||
|
||||
export default buildConfigWithDefaults({
|
||||
collections: [Media, Users],
|
||||
collections: [Media, MediaWithPrefix, Users],
|
||||
onInit: async (payload) => {
|
||||
/*const client = new AWS.S3({
|
||||
endpoint: process.env.S3_ENDPOINT,
|
||||
@@ -115,9 +118,13 @@ export default buildConfigWithDefaults({
|
||||
plugins: [
|
||||
cloudStorage({
|
||||
collections: {
|
||||
media: {
|
||||
[mediaSlug]: {
|
||||
adapter,
|
||||
},
|
||||
[mediaWithPrefixSlug]: {
|
||||
adapter,
|
||||
prefix,
|
||||
},
|
||||
},
|
||||
}),
|
||||
],
|
||||
|
||||
@@ -7,6 +7,7 @@ import { fileURLToPath } from 'url'
|
||||
import { describeIfInCIOrHasLocalstack } from '../helpers.js'
|
||||
import { initPayloadInt } from '../helpers/initPayloadInt.js'
|
||||
import configPromise from './config.js'
|
||||
import { mediaSlug, mediaWithPrefixSlug, prefix } from './shared.js'
|
||||
|
||||
const filename = fileURLToPath(import.meta.url)
|
||||
const dirname = path.dirname(filename)
|
||||
@@ -41,6 +42,7 @@ describe('@payloadcms/plugin-cloud-storage', () => {
|
||||
})
|
||||
|
||||
await createTestBucket()
|
||||
await clearTestBucket()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -49,14 +51,36 @@ describe('@payloadcms/plugin-cloud-storage', () => {
|
||||
|
||||
it('can upload', async () => {
|
||||
const upload = await payload.create({
|
||||
collection: 'media',
|
||||
collection: mediaSlug,
|
||||
data: {},
|
||||
filePath: path.resolve(dirname, '../uploads/image.png'),
|
||||
})
|
||||
|
||||
expect(upload.id).toBeTruthy()
|
||||
|
||||
await verifyUploads(upload.id)
|
||||
await verifyUploads({
|
||||
collectionSlug: mediaSlug,
|
||||
uploadId: upload.id,
|
||||
})
|
||||
|
||||
expect(upload.url).toEqual(`/api/${mediaSlug}/file/${upload.filename as string}`)
|
||||
})
|
||||
|
||||
it('can upload with prefix', async () => {
|
||||
const upload = await payload.create({
|
||||
collection: mediaWithPrefixSlug,
|
||||
data: {},
|
||||
filePath: path.resolve(dirname, '../uploads/image.png'),
|
||||
})
|
||||
|
||||
expect(upload.id).toBeTruthy()
|
||||
|
||||
await verifyUploads({
|
||||
collectionSlug: mediaWithPrefixSlug,
|
||||
uploadId: upload.id,
|
||||
prefix,
|
||||
})
|
||||
expect(upload.url).toEqual(`/api/${mediaWithPrefixSlug}/file/${String(upload.filename)}`)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -105,26 +129,42 @@ describe('@payloadcms/plugin-cloud-storage', () => {
|
||||
}
|
||||
}
|
||||
|
||||
async function verifyUploads(uploadId: number | string) {
|
||||
try {
|
||||
async function verifyUploads({
|
||||
collectionSlug,
|
||||
uploadId,
|
||||
prefix = '',
|
||||
}: {
|
||||
collectionSlug: string
|
||||
prefix?: string
|
||||
uploadId: number | string
|
||||
}) {
|
||||
const uploadData = (await payload.findByID({
|
||||
collection: 'media',
|
||||
collection: collectionSlug,
|
||||
id: uploadId,
|
||||
})) as unknown as { filename: string; sizes: Record<string, { filename: string }> }
|
||||
|
||||
const fileKeys = Object.keys(uploadData.sizes).map((key) => uploadData.sizes[key].filename)
|
||||
fileKeys.push(uploadData.filename)
|
||||
const fileKeys = Object.keys(uploadData.sizes || {}).map((key) => {
|
||||
const rawFilename = uploadData.sizes[key].filename
|
||||
return prefix ? `${prefix}/${rawFilename}` : rawFilename
|
||||
})
|
||||
|
||||
fileKeys.push(`${prefix ? `${prefix}/` : ''}${uploadData.filename}`)
|
||||
try {
|
||||
for (const key of fileKeys) {
|
||||
const { $metadata } = await client.send(
|
||||
new AWS.HeadObjectCommand({ Bucket: TEST_BUCKET, Key: key }),
|
||||
)
|
||||
|
||||
if ($metadata.httpStatusCode !== 200) {
|
||||
console.error('Error verifying uploads', key, $metadata)
|
||||
throw new Error(`Error verifying uploads: ${key}, ${$metadata.httpStatusCode}`)
|
||||
}
|
||||
|
||||
// Verify each size was properly uploaded
|
||||
expect($metadata.httpStatusCode).toBe(200)
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
console.error('Error verifying uploads:', error)
|
||||
console.error('Error verifying uploads:', fileKeys, error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
3
test/plugin-cloud-storage/shared.ts
Normal file
3
test/plugin-cloud-storage/shared.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export const mediaSlug = 'media'
|
||||
export const mediaWithPrefixSlug = 'media-with-prefix'
|
||||
export const prefix = 'test-prefix'
|
||||
Reference in New Issue
Block a user