|
| 1 | +import type { ContainerClient } from '@azure/storage-blob' |
| 2 | +import type { CollectionSlug, Payload } from 'payload' |
| 3 | + |
| 4 | +import { BlobServiceClient } from '@azure/storage-blob' |
| 5 | +import path from 'path' |
| 6 | +import { fileURLToPath } from 'url' |
| 7 | + |
| 8 | +import type { NextRESTClient } from '../helpers/NextRESTClient.js' |
| 9 | + |
| 10 | +import { initPayloadInt } from '../helpers/initPayloadInt.js' |
| 11 | +import { mediaSlug, mediaWithPrefixSlug, prefix } from './shared.js' |
| 12 | + |
| 13 | +const filename = fileURLToPath(import.meta.url) |
| 14 | +const dirname = path.dirname(filename) |
| 15 | + |
| 16 | +let restClient: NextRESTClient |
| 17 | +let payload: Payload |
| 18 | + |
| 19 | +describe('@payloadcms/storage-azure', () => { |
| 20 | + let TEST_CONTAINER: string |
| 21 | + let client: ContainerClient |
| 22 | + |
| 23 | + beforeAll(async () => { |
| 24 | + ;({ payload, restClient } = await initPayloadInt(dirname)) |
| 25 | + TEST_CONTAINER = process.env.AZURE_STORAGE_CONTAINER_NAME! |
| 26 | + |
| 27 | + const blobServiceClient = BlobServiceClient.fromConnectionString( |
| 28 | + process.env.AZURE_STORAGE_CONNECTION_STRING!, |
| 29 | + ) |
| 30 | + client = blobServiceClient.getContainerClient(TEST_CONTAINER) |
| 31 | + |
| 32 | + await client.createIfNotExists() |
| 33 | + await clearContainer() |
| 34 | + }) |
| 35 | + |
| 36 | + afterAll(async () => { |
| 37 | + await payload.destroy() |
| 38 | + }) |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + await clearContainer() |
| 42 | + }) |
| 43 | + |
| 44 | + it('can upload', async () => { |
| 45 | + const upload = await payload.create({ |
| 46 | + collection: mediaSlug, |
| 47 | + data: {}, |
| 48 | + filePath: path.resolve(dirname, '../uploads/image.png'), |
| 49 | + }) |
| 50 | + |
| 51 | + expect(upload.id).toBeTruthy() |
| 52 | + await verifyUploads({ collectionSlug: mediaSlug, uploadId: upload.id }) |
| 53 | + expect(upload.url).toEqual(`/api/${mediaSlug}/file/${String(upload.filename)}`) |
| 54 | + }) |
| 55 | + |
| 56 | + it('can upload with prefix', async () => { |
| 57 | + const upload = await payload.create({ |
| 58 | + collection: mediaWithPrefixSlug, |
| 59 | + data: {}, |
| 60 | + filePath: path.resolve(dirname, '../uploads/image.png'), |
| 61 | + }) |
| 62 | + |
| 63 | + expect(upload.id).toBeTruthy() |
| 64 | + await verifyUploads({ |
| 65 | + collectionSlug: mediaWithPrefixSlug, |
| 66 | + uploadId: upload.id, |
| 67 | + prefix, |
| 68 | + }) |
| 69 | + expect(upload.url).toEqual(`/api/${mediaWithPrefixSlug}/file/${String(upload.filename)}`) |
| 70 | + }) |
| 71 | + |
| 72 | + it('returns 404 for non-existing file', async () => { |
| 73 | + const response = await restClient.GET(`/${mediaSlug}/file/nonexistent.png`) |
| 74 | + expect(response.status).toBe(404) |
| 75 | + }) |
| 76 | + |
| 77 | + async function clearContainer() { |
| 78 | + for await (const blob of client.listBlobsFlat()) { |
| 79 | + await client.deleteBlob(blob.name) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + async function verifyUploads({ |
| 84 | + collectionSlug, |
| 85 | + uploadId, |
| 86 | + prefix = '', |
| 87 | + }: { |
| 88 | + collectionSlug: CollectionSlug |
| 89 | + prefix?: string |
| 90 | + uploadId: number | string |
| 91 | + }) { |
| 92 | + const uploadData = (await payload.findByID({ |
| 93 | + collection: collectionSlug, |
| 94 | + id: uploadId, |
| 95 | + })) as unknown as { filename: string; sizes: Record<string, { filename: string }> } |
| 96 | + |
| 97 | + const fileKeys = Object.keys(uploadData.sizes || {}).map((key) => { |
| 98 | + const rawFilename = uploadData.sizes[key].filename |
| 99 | + return prefix ? `${prefix}/${rawFilename}` : rawFilename |
| 100 | + }) |
| 101 | + |
| 102 | + fileKeys.push(`${prefix ? `${prefix}/` : ''}${uploadData.filename}`) |
| 103 | + |
| 104 | + for (const key of fileKeys) { |
| 105 | + const blobClient = client.getBlobClient(key) |
| 106 | + try { |
| 107 | + const props = await blobClient.getProperties() |
| 108 | + expect(props).toBeDefined() |
| 109 | + expect(props.contentLength).toBeGreaterThan(0) |
| 110 | + } catch (error) { |
| 111 | + console.error('Error verifying uploads:', key, error) |
| 112 | + throw error |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +}) |
0 commit comments